Add test for IniMarginAccount (#18)

* split out srm tests. add init margin account test

Co-authored-by: Kevin Lu <kevin@bkcm.co>
This commit is contained in:
kevlubkcm 2021-05-03 02:54:33 -04:00 committed by GitHub
parent b4e360f037
commit 971a8df0b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 133 additions and 33 deletions

View File

@ -77,5 +77,5 @@ Regression and integration tests are in progress. To run them
```
cd program
cargo test # run non-solana VM tests (none at the moment but would include simple unit tests in the future)
cargo test-bpf --features test-bpf # run tests that use the solana VM (ie the smart contract tests)
```
cargo test-bpf # run tests that use the solana VM (ie the smart contract tests)
```

View File

@ -1,3 +1,5 @@
#![cfg(feature="test-bpf")]
use std::mem::size_of;
use std::convert::TryInto;
use safe_transmute::{self, to_bytes::transmute_one_to_bytes};
@ -311,6 +313,7 @@ pub fn add_mango_group_prodlike(test: &mut ProgramTest, program_id: Pubkey) -> T
}
}
#[allow(dead_code)] // Compiler complains about this even tho it is used
pub async fn get_token_balance(banks_client: &mut BanksClient, pubkey: Pubkey) -> u64 {
let token: Account = banks_client.get_account(pubkey).await.unwrap().unwrap();

View File

@ -1,3 +1,4 @@
// Tests related to initializing MangoGroups and MarginAccounts
#![cfg(feature="test-bpf")]
mod helpers;
@ -15,8 +16,8 @@ use solana_program::account_info::AccountInfo;
use mango::{
entrypoint::process_instruction,
instruction::deposit_srm,
state::MangoSrmAccount,
instruction::init_margin_account,
state::MarginAccount,
};
#[tokio::test]
@ -52,10 +53,10 @@ async fn test_init_mango_group() {
assert!(banks_client.process_transaction(transaction).await.is_ok());
}
#[tokio::test]
async fn test_deposit_srm() {
// Test that the DepositSrm instruction succeeds in the simple case
async fn test_init_margin_account() {
// Test that we can create a MarginAccount
// Also make sure that new accounts start with 0 balance
let program_id = Pubkey::new_unique();
let mut test = ProgramTest::new(
@ -67,30 +68,22 @@ async fn test_deposit_srm() {
// limit to track compute unit increase
test.set_bpf_compute_max_units(20_000);
let initial_amount = 500;
let deposit_amount = 100;
let user = Keypair::new();
let user_pk = user.pubkey();
let mango_group = add_mango_group_prodlike(&mut test, program_id);
let mango_srm_account_pk = Pubkey::new_unique();
test.add_account(mango_srm_account_pk, Account::new(u32::MAX as u64, size_of::<MangoSrmAccount>(), &program_id));
let user_srm_account = add_token_account(&mut test, user_pk, mango_group.srm_mint.pubkey, initial_amount);
let margin_account_pk = Pubkey::new_unique();
test.add_account(margin_account_pk, Account::new(u32::MAX as u64, size_of::<MarginAccount>(), &program_id));
let user = Keypair::new();
test.add_account(user.pubkey(), Account::new(u32::MAX as u64, 0, &user.pubkey()));
let (mut banks_client, payer, recent_blockhash) = test.start().await;
let mut transaction = Transaction::new_with_payer(
&[
mango_group.init_mango_group(&payer.pubkey()),
deposit_srm(
init_margin_account(
&program_id,
&mango_group.mango_group_pk,
&mango_srm_account_pk,
&user_pk,
&user_srm_account.pubkey,
&mango_group.srm_vault.pubkey,
deposit_amount,
&margin_account_pk,
&user.pubkey(),
).unwrap(),
],
Some(&payer.pubkey()),
@ -102,18 +95,17 @@ async fn test_deposit_srm() {
);
assert!(banks_client.process_transaction(transaction).await.is_ok());
let final_user_balance = get_token_balance(&mut banks_client, user_srm_account.pubkey).await;
assert_eq!(final_user_balance, initial_amount - deposit_amount);
let mango_vault_srm_balance = get_token_balance(&mut banks_client, mango_group.srm_vault.pubkey).await;
assert_eq!(mango_vault_srm_balance, deposit_amount);
let mut mango_srm_account = banks_client.get_account(mango_srm_account_pk).await.unwrap().unwrap();
let account_info: AccountInfo = (&mango_srm_account_pk, &mut mango_srm_account).into();
let mango_srm_account = MangoSrmAccount::load_mut_checked(
let mut account = banks_client.get_account(margin_account_pk).await.unwrap().unwrap();
let account_info: AccountInfo = (&margin_account_pk, &mut account).into();
let margin_account = MarginAccount::load_mut_checked(
&program_id,
&account_info,
&mango_group.mango_group_pk,
).unwrap();
assert_eq!(mango_srm_account.amount, deposit_amount);
}
for dep in &margin_account.deposits {
assert_eq!(dep.to_bits(), 0);
}
for borrow in &margin_account.borrows {
assert_eq!(borrow.to_bits(), 0);
}
}

105
program/tests/test_srm.rs Normal file
View File

@ -0,0 +1,105 @@
// Tests related to the SRM vault of a MangoGroup
#![cfg(feature="test-bpf")]
mod helpers;
use std::mem::size_of;
use helpers::*;
use solana_program_test::*;
use solana_sdk::{
pubkey::Pubkey,
signature::{Signer, Keypair},
transaction::Transaction,
account::Account,
};
use solana_program::account_info::AccountInfo;
use mango::{
entrypoint::process_instruction,
instruction::{deposit_srm, withdraw_srm},
state::MangoSrmAccount,
};
#[tokio::test]
async fn test_deposit_srm() {
// Test that the DepositSrm instruction succeeds in the simple case
let program_id = Pubkey::new_unique();
let mut test = ProgramTest::new(
"mango",
program_id,
processor!(process_instruction),
);
// limit to track compute unit increase
test.set_bpf_compute_max_units(20_000);
let initial_amount = 500;
let deposit_amount = 100;
let user = Keypair::new();
let user_pk = user.pubkey();
let mango_group = add_mango_group_prodlike(&mut test, program_id);
let mango_srm_account_pk = Pubkey::new_unique();
test.add_account(mango_srm_account_pk, Account::new(u32::MAX as u64, size_of::<MangoSrmAccount>(), &program_id));
let user_srm_account = add_token_account(&mut test, user_pk, mango_group.srm_mint.pubkey, initial_amount);
let (mut banks_client, payer, recent_blockhash) = test.start().await;
let mut transaction = Transaction::new_with_payer(
&[
mango_group.init_mango_group(&payer.pubkey()),
deposit_srm(
&program_id,
&mango_group.mango_group_pk,
&mango_srm_account_pk,
&user_pk,
&user_srm_account.pubkey,
&mango_group.srm_vault.pubkey,
deposit_amount,
).unwrap(),
],
Some(&payer.pubkey()),
);
transaction.sign(
&[&payer, &user],
recent_blockhash,
);
assert!(banks_client.process_transaction(transaction).await.is_ok());
let final_user_balance = get_token_balance(&mut banks_client, user_srm_account.pubkey).await;
assert_eq!(final_user_balance, initial_amount - deposit_amount);
let mango_vault_srm_balance = get_token_balance(&mut banks_client, mango_group.srm_vault.pubkey).await;
assert_eq!(mango_vault_srm_balance, deposit_amount);
let mut mango_srm_account = banks_client.get_account(mango_srm_account_pk).await.unwrap().unwrap();
let account_info: AccountInfo = (&mango_srm_account_pk, &mut mango_srm_account).into();
let mango_srm_account = MangoSrmAccount::load_mut_checked(
&program_id,
&account_info,
&mango_group.mango_group_pk,
).unwrap();
assert_eq!(mango_srm_account.amount, deposit_amount);
// Seems like the solana_program_test harness hangs if you try to process a second transaction batch?
// let mut transaction = Transaction::new_with_payer(
// &[
// withdraw_srm(
// &program_id,
// &mango_group.mango_group_pk,
// &mango_srm_account_pk,
// &user_pk,
// &user_srm_account.pubkey,
// &mango_group.srm_vault.pubkey,
// &user_pk,
// 50,
// ).unwrap(),
// ],
// Some(&user_pk),
// );
// transaction.sign(&[&user], banks_client.get_recent_blockhash().await.unwrap());
// assert!(banks_client.process_transaction(transaction).await.is_ok());
}