2021-11-30 06:10:49 -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
|
|
|
voting_mint: &VotingMintConfigCookie,
|
2021-11-30 06:10:49 -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 = voting_mint.vault_balance(&context.solana, &voter).await;
|
2021-11-30 06:10:49 -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 06:10:49 -08:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
Balances {
|
|
|
|
token,
|
|
|
|
vault,
|
|
|
|
deposit,
|
|
|
|
voter_weight: vwr.voter_weight,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(unaligned_references)]
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_deposit_cliff() -> 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,
|
|
|
|
1.0,
|
|
|
|
2 * 24 * 60 * 60,
|
2021-12-05 07:59:22 -08:00
|
|
|
None,
|
|
|
|
)
|
2021-11-30 06:10:49 -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 06:10:49 -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 06:10:49 -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 06:10:49 -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 06:10:49 -08:00
|
|
|
®istrar,
|
|
|
|
&voter,
|
2021-12-03 11:36:42 -08:00
|
|
|
&mngo_voting_mint,
|
2021-11-30 06:10:49 -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 06:10:49 -08:00
|
|
|
|
|
|
|
addin
|
2021-12-02 02:01:14 -08:00
|
|
|
.create_deposit_entry(
|
2021-11-30 06:10:49 -08:00
|
|
|
®istrar,
|
|
|
|
&voter,
|
2021-12-01 01:20:39 -08:00
|
|
|
&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::Cliff,
|
2021-12-17 00:57:22 -08:00
|
|
|
None,
|
2021-11-30 06:10:49 -08:00
|
|
|
3, // days
|
2021-12-01 01:20:39 -08:00
|
|
|
false,
|
2021-11-30 06:10:49 -08:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2021-12-02 02:06:15 -08:00
|
|
|
deposit(9000).await.unwrap();
|
2021-11-30 06:10:49 -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-12-09 02:58:15 -08:00
|
|
|
assert_eq!(after_deposit.voter_weight, 2 * after_deposit.vault); // saturated locking bonus
|
2021-11-30 06:10:49 -08:00
|
|
|
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");
|
|
|
|
|
2021-12-09 02:58:15 -08:00
|
|
|
// advance a day
|
|
|
|
addin
|
|
|
|
.set_time_offset(®istrar, &realm_authority, 24 * 60 * 60)
|
|
|
|
.await;
|
|
|
|
let after_day1 = get_balances(0).await;
|
|
|
|
assert_eq!(after_day1.voter_weight, 2 * after_day1.vault); // still saturated
|
|
|
|
|
|
|
|
// advance a second day
|
|
|
|
addin
|
|
|
|
.set_time_offset(®istrar, &realm_authority, 48 * 60 * 60)
|
|
|
|
.await;
|
|
|
|
let after_day2 = get_balances(0).await;
|
|
|
|
assert_eq!(after_day2.voter_weight, 3 * after_day2.vault / 2); // locking half done
|
|
|
|
|
|
|
|
// advance to almost three days
|
2021-11-30 06:10:49 -08:00
|
|
|
addin
|
|
|
|
.set_time_offset(®istrar, &realm_authority, 71 * 60 * 60)
|
|
|
|
.await;
|
|
|
|
context.solana.advance_clock_by_slots(2).await;
|
|
|
|
|
|
|
|
withdraw(1).await.expect_err("nothing vested yet");
|
|
|
|
|
|
|
|
// deposit some more
|
2021-12-02 02:06:15 -08:00
|
|
|
deposit(1000).await.unwrap();
|
2021-11-30 06:10:49 -08:00
|
|
|
|
|
|
|
// advance more than three days
|
|
|
|
addin
|
|
|
|
.set_time_offset(®istrar, &realm_authority, 73 * 60 * 60)
|
|
|
|
.await;
|
|
|
|
context.solana.advance_clock_by_slots(2).await;
|
|
|
|
|
|
|
|
let after_cliff = get_balances(0).await;
|
2021-12-16 23:02:14 -08:00
|
|
|
assert_eq!(token, after_cliff.token + after_cliff.vault);
|
2021-11-30 06:10:49 -08:00
|
|
|
assert_eq!(after_cliff.voter_weight, after_cliff.vault);
|
|
|
|
assert_eq!(after_cliff.vault, 10000);
|
|
|
|
assert_eq!(after_cliff.deposit, 10000);
|
|
|
|
|
|
|
|
// can withdraw everything now
|
|
|
|
withdraw(10001).await.expect_err("withdrew too much");
|
|
|
|
withdraw(10000).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 06:10:49 -08:00
|
|
|
assert_eq!(after_withdraw.voter_weight, after_withdraw.vault);
|
|
|
|
assert_eq!(after_withdraw.vault, 0);
|
|
|
|
assert_eq!(after_withdraw.deposit, 0);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|