2021-11-30 00:24:57 -08:00
|
|
|
use anchor_spl::token::TokenAccount;
|
|
|
|
use program_test::*;
|
|
|
|
use solana_program_test::*;
|
|
|
|
use solana_sdk::{pubkey::Pubkey, signature::Keypair, signer::Signer, transport::TransportError};
|
|
|
|
|
|
|
|
mod program_test;
|
|
|
|
|
|
|
|
struct Balances {
|
|
|
|
token: u64,
|
|
|
|
vault: u64,
|
|
|
|
deposit: u64,
|
|
|
|
voter_weight: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn balances(
|
|
|
|
context: &TestContext,
|
|
|
|
registrar: &RegistrarCookie,
|
|
|
|
address: Pubkey,
|
|
|
|
voter: &VoterCookie,
|
2021-12-03 11:36:42 -08:00
|
|
|
rate: &VotingMintConfigCookie,
|
2021-11-30 00:24:57 -08:00
|
|
|
deposit_id: u8,
|
|
|
|
) -> Balances {
|
|
|
|
// Advance slots to avoid caching of the UpdateVoterWeightRecord call
|
|
|
|
// TODO: Is this something that could be an issue on a live node?
|
|
|
|
context.solana.advance_clock_by_slots(2).await;
|
|
|
|
|
|
|
|
let token = context.solana.token_account_balance(address).await;
|
2021-12-16 23:02:14 -08:00
|
|
|
let vault = rate.vault_balance(&context.solana, &voter).await;
|
2021-11-30 00:24:57 -08:00
|
|
|
let deposit = voter.deposit_amount(&context.solana, deposit_id).await;
|
|
|
|
let vwr = context
|
|
|
|
.addin
|
2021-11-30 23:59:25 -08:00
|
|
|
.update_voter_weight_record(®istrar, &voter)
|
2021-11-30 00:24:57 -08:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
Balances {
|
|
|
|
token,
|
|
|
|
vault,
|
|
|
|
deposit,
|
|
|
|
voter_weight: vwr.voter_weight,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(unaligned_references)]
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_deposit_monthly_vesting() -> Result<(), TransportError> {
|
|
|
|
let context = TestContext::new().await;
|
|
|
|
let addin = &context.addin;
|
|
|
|
|
|
|
|
let payer = &context.users[0].key;
|
|
|
|
let realm_authority = Keypair::new();
|
|
|
|
let realm = context
|
|
|
|
.governance
|
|
|
|
.create_realm(
|
|
|
|
"testrealm",
|
|
|
|
realm_authority.pubkey(),
|
|
|
|
&context.mints[0],
|
|
|
|
&payer,
|
|
|
|
&context.addin.program_id,
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let voter_authority = &context.users[1].key;
|
|
|
|
let token_owner_record = realm
|
|
|
|
.create_token_owner_record(voter_authority.pubkey(), &payer)
|
|
|
|
.await;
|
|
|
|
|
2021-12-01 23:06:45 -08:00
|
|
|
let registrar = addin
|
|
|
|
.create_registrar(&realm, &realm_authority, payer)
|
|
|
|
.await;
|
2021-12-03 11:36:42 -08:00
|
|
|
let mngo_voting_mint = addin
|
2021-12-05 07:59:22 -08:00
|
|
|
.configure_voting_mint(
|
|
|
|
®istrar,
|
|
|
|
&realm_authority,
|
|
|
|
payer,
|
|
|
|
0,
|
|
|
|
&context.mints[0],
|
2021-12-09 02:58:15 -08:00
|
|
|
0,
|
|
|
|
1.0,
|
|
|
|
0.0,
|
|
|
|
5 * 365 * 24 * 60 * 60,
|
2021-12-05 07:59:22 -08:00
|
|
|
None,
|
2022-02-09 00:45:57 -08:00
|
|
|
None,
|
2021-12-05 07:59:22 -08:00
|
|
|
)
|
2021-11-30 00:24:57 -08:00
|
|
|
.await;
|
|
|
|
|
|
|
|
let voter = addin
|
2021-12-03 00:48:10 -08:00
|
|
|
.create_voter(®istrar, &token_owner_record, &voter_authority, &payer)
|
2021-11-30 00:24:57 -08:00
|
|
|
.await;
|
|
|
|
|
|
|
|
let reference_account = context.users[1].token_accounts[0];
|
|
|
|
let get_balances = |depot_id| {
|
|
|
|
balances(
|
|
|
|
&context,
|
|
|
|
®istrar,
|
|
|
|
reference_account,
|
|
|
|
&voter,
|
2021-12-03 11:36:42 -08:00
|
|
|
&mngo_voting_mint,
|
2021-11-30 00:24:57 -08:00
|
|
|
depot_id,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
let withdraw = |amount: u64| {
|
|
|
|
addin.withdraw(
|
|
|
|
®istrar,
|
|
|
|
&voter,
|
2021-12-03 11:36:42 -08:00
|
|
|
&mngo_voting_mint,
|
2021-11-30 00:24:57 -08:00
|
|
|
&voter_authority,
|
|
|
|
reference_account,
|
|
|
|
0,
|
|
|
|
amount,
|
|
|
|
)
|
|
|
|
};
|
2021-12-02 02:06:15 -08:00
|
|
|
let deposit = |amount: u64| {
|
|
|
|
addin.deposit(
|
2021-11-30 00:24:57 -08:00
|
|
|
®istrar,
|
|
|
|
&voter,
|
2021-12-03 11:36:42 -08:00
|
|
|
&mngo_voting_mint,
|
2021-11-30 00:24:57 -08:00
|
|
|
&voter_authority,
|
|
|
|
reference_account,
|
|
|
|
0,
|
|
|
|
amount,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
// test deposit and withdraw
|
2021-12-16 23:02:14 -08:00
|
|
|
let token = context
|
|
|
|
.solana
|
|
|
|
.token_account_balance(reference_account)
|
|
|
|
.await;
|
2021-11-30 00:24:57 -08:00
|
|
|
|
|
|
|
addin
|
2021-12-02 02:01:14 -08:00
|
|
|
.create_deposit_entry(
|
2021-11-30 00:24:57 -08:00
|
|
|
®istrar,
|
|
|
|
&voter,
|
|
|
|
&voter_authority,
|
2021-12-03 11:36:42 -08:00
|
|
|
&mngo_voting_mint,
|
2021-12-03 00:48:10 -08:00
|
|
|
0,
|
2021-12-03 00:52:48 -08:00
|
|
|
voter_stake_registry::state::LockupKind::Monthly,
|
2021-12-17 00:57:22 -08:00
|
|
|
None,
|
2021-11-30 00:24:57 -08:00
|
|
|
3,
|
2021-12-01 01:20:39 -08:00
|
|
|
false,
|
2021-11-30 00:24:57 -08:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2021-12-02 02:06:15 -08:00
|
|
|
deposit(9000).await.unwrap();
|
2021-11-30 00:24:57 -08:00
|
|
|
|
|
|
|
let after_deposit = get_balances(0).await;
|
2021-12-16 23:02:14 -08:00
|
|
|
assert_eq!(token, after_deposit.token + after_deposit.vault);
|
2021-11-30 00:24:57 -08:00
|
|
|
assert_eq!(after_deposit.voter_weight, after_deposit.vault);
|
|
|
|
assert_eq!(after_deposit.vault, 9000);
|
|
|
|
assert_eq!(after_deposit.deposit, 9000);
|
|
|
|
|
|
|
|
// cannot withdraw yet, nothing is vested
|
|
|
|
withdraw(1).await.expect_err("nothing vested yet");
|
|
|
|
|
|
|
|
// advance not quite a month
|
|
|
|
addin
|
|
|
|
.set_time_offset(®istrar, &realm_authority, 30 * 24 * 60 * 60)
|
|
|
|
.await;
|
|
|
|
context.solana.advance_clock_by_slots(2).await;
|
|
|
|
|
|
|
|
// cannot withdraw yet, nothing is vested
|
|
|
|
withdraw(1).await.expect_err("nothing vested yet");
|
|
|
|
|
|
|
|
// advance to the next month
|
|
|
|
addin
|
|
|
|
.set_time_offset(®istrar, &realm_authority, 32 * 24 * 60 * 60)
|
|
|
|
.await;
|
|
|
|
context.solana.advance_clock_by_slots(2).await;
|
|
|
|
|
|
|
|
withdraw(3001).await.expect_err("withdrew too much");
|
|
|
|
withdraw(3000).await.unwrap();
|
|
|
|
|
|
|
|
let after_withdraw = get_balances(0).await;
|
2021-12-16 23:02:14 -08:00
|
|
|
assert_eq!(token, after_withdraw.token + after_withdraw.vault);
|
2021-11-30 00:24:57 -08:00
|
|
|
assert_eq!(after_withdraw.voter_weight, after_withdraw.vault);
|
|
|
|
assert_eq!(after_withdraw.vault, 6000);
|
|
|
|
assert_eq!(after_withdraw.deposit, 6000);
|
|
|
|
|
|
|
|
// There are two vesting periods left, if we add 5000 to the deposit,
|
|
|
|
// half of that should vest each month.
|
2021-12-02 02:06:15 -08:00
|
|
|
deposit(5000).await.unwrap();
|
2021-11-30 00:24:57 -08:00
|
|
|
|
|
|
|
let after_deposit = get_balances(0).await;
|
2021-12-16 23:02:14 -08:00
|
|
|
assert_eq!(token, after_deposit.token + after_deposit.vault);
|
2021-11-30 00:24:57 -08:00
|
|
|
assert_eq!(after_deposit.voter_weight, after_deposit.vault);
|
|
|
|
assert_eq!(after_deposit.vault, 11000);
|
|
|
|
assert_eq!(after_deposit.deposit, 11000);
|
|
|
|
|
|
|
|
withdraw(1).await.expect_err("nothing vested yet");
|
|
|
|
|
|
|
|
// advance another month
|
|
|
|
addin
|
|
|
|
.set_time_offset(®istrar, &realm_authority, 64 * 24 * 60 * 60)
|
|
|
|
.await;
|
|
|
|
context.solana.advance_clock_by_slots(2).await;
|
|
|
|
|
|
|
|
// There is just one period left, should be fully withdrawable after
|
2021-12-02 02:06:15 -08:00
|
|
|
deposit(1000).await.unwrap();
|
2021-11-30 00:24:57 -08:00
|
|
|
|
|
|
|
context.solana.advance_clock_by_slots(2).await;
|
|
|
|
|
|
|
|
// can withdraw 3000 (original deposit) plus 2500 (second deposit)
|
|
|
|
// nothing from the third deposit is vested
|
|
|
|
withdraw(5501).await.expect_err("withdrew too much");
|
|
|
|
withdraw(5500).await.unwrap();
|
|
|
|
|
|
|
|
let after_withdraw = get_balances(0).await;
|
2021-12-16 23:02:14 -08:00
|
|
|
assert_eq!(token, after_withdraw.token + after_withdraw.vault);
|
2021-11-30 00:24:57 -08:00
|
|
|
assert_eq!(after_withdraw.voter_weight, after_withdraw.vault);
|
|
|
|
assert_eq!(after_withdraw.vault, 6500);
|
|
|
|
assert_eq!(after_withdraw.deposit, 6500);
|
|
|
|
|
|
|
|
// advance another month
|
|
|
|
addin
|
|
|
|
.set_time_offset(®istrar, &realm_authority, 94 * 24 * 60 * 60)
|
|
|
|
.await;
|
|
|
|
context.solana.advance_clock_by_slots(2).await;
|
|
|
|
|
|
|
|
// can withdraw the rest
|
|
|
|
withdraw(6500).await.unwrap();
|
|
|
|
|
|
|
|
let after_withdraw = get_balances(0).await;
|
2021-12-16 23:02:14 -08:00
|
|
|
assert_eq!(token, after_withdraw.token + after_withdraw.vault);
|
2021-11-30 00:24:57 -08:00
|
|
|
assert_eq!(after_withdraw.voter_weight, after_withdraw.vault);
|
|
|
|
assert_eq!(after_withdraw.vault, 0);
|
|
|
|
assert_eq!(after_withdraw.deposit, 0);
|
|
|
|
|
|
|
|
// if we deposit now, we can immediately withdraw
|
2021-12-02 02:06:15 -08:00
|
|
|
deposit(1000).await.unwrap();
|
2021-11-30 00:24:57 -08:00
|
|
|
|
|
|
|
let after_deposit = get_balances(0).await;
|
2021-12-16 23:02:14 -08:00
|
|
|
assert_eq!(token, after_deposit.token + after_deposit.vault);
|
2021-11-30 00:24:57 -08:00
|
|
|
assert_eq!(after_deposit.voter_weight, after_deposit.vault);
|
|
|
|
assert_eq!(after_deposit.vault, 1000);
|
|
|
|
assert_eq!(after_deposit.deposit, 1000);
|
|
|
|
|
2021-11-30 02:49:59 -08:00
|
|
|
// cannot close yet, has funds
|
2021-11-30 02:56:14 -08:00
|
|
|
addin
|
2021-12-02 02:08:18 -08:00
|
|
|
.close_deposit_entry(&voter, &voter_authority, 0)
|
2021-11-30 02:56:14 -08:00
|
|
|
.await
|
|
|
|
.expect_err("deposit not empty");
|
2021-11-30 02:49:59 -08:00
|
|
|
|
2021-11-30 00:24:57 -08:00
|
|
|
withdraw(1000).await.unwrap();
|
|
|
|
|
|
|
|
let after_withdraw = get_balances(0).await;
|
2021-12-16 23:02:14 -08:00
|
|
|
assert_eq!(token, after_withdraw.token + after_withdraw.vault);
|
2021-11-30 00:24:57 -08:00
|
|
|
assert_eq!(after_withdraw.voter_weight, after_withdraw.vault);
|
|
|
|
assert_eq!(after_withdraw.vault, 0);
|
|
|
|
assert_eq!(after_withdraw.deposit, 0);
|
|
|
|
|
2021-11-30 02:56:14 -08:00
|
|
|
addin
|
2021-12-02 02:08:18 -08:00
|
|
|
.close_deposit_entry(&voter, &voter_authority, 0)
|
2021-11-30 02:56:14 -08:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2021-11-30 02:49:59 -08:00
|
|
|
|
2021-11-30 00:24:57 -08:00
|
|
|
Ok(())
|
|
|
|
}
|