Tests: Helper for create + fund

This commit is contained in:
Christian Kamm 2022-08-26 15:59:47 +02:00
parent 7d508e5df5
commit d6d66402f7
11 changed files with 175 additions and 506 deletions

View File

@ -5,7 +5,7 @@ use solana_sdk::signature::Keypair;
use super::mango_client::*; use super::mango_client::*;
use super::solana::SolanaCookie; use super::solana::SolanaCookie;
use super::{send_tx, MintCookie}; use super::{send_tx, ClonableKeypair, MintCookie, UserCookie};
pub struct GroupWithTokensConfig<'a> { pub struct GroupWithTokensConfig<'a> {
pub admin: &'a Keypair, pub admin: &'a Keypair,
@ -137,3 +137,48 @@ impl<'a> GroupWithTokensConfig<'a> {
} }
} }
} }
pub async fn create_funded_account(
solana: &SolanaCookie,
group: Pubkey,
owner: &Keypair,
account_num: u32,
payer: &UserCookie,
mints: &[MintCookie],
amounts: u64,
bank_index: usize,
) -> Pubkey {
let account = send_tx(
solana,
AccountCreateInstruction {
account_num,
token_count: 16,
serum3_count: 8,
perp_count: 8,
perp_oo_count: 8,
group,
owner,
payer: &payer.key,
},
)
.await
.unwrap()
.account;
for mint in mints {
send_tx(
solana,
TokenDepositInstruction {
amount: amounts,
account,
token_account: payer.token_accounts[mint.index],
token_authority: payer.key.clone(),
bank_index,
},
)
.await
.unwrap();
}
account
}

View File

@ -10,6 +10,8 @@ use solana_sdk::{
use mango_v4::state::*; use mango_v4::state::*;
use program_test::*; use program_test::*;
use mango_setup::*;
mod program_test; mod program_test;
#[tokio::test] #[tokio::test]
@ -27,7 +29,7 @@ async fn test_bankrupt_tokens_socialize_loss() -> Result<(), TransportError> {
// SETUP: Create a group and an account to fill the vaults // SETUP: Create a group and an account to fill the vaults
// //
let mango_setup::GroupWithTokens { group, tokens, .. } = mango_setup::GroupWithTokensConfig { let GroupWithTokens { group, tokens, .. } = GroupWithTokensConfig {
admin, admin,
payer, payer,
mints, mints,
@ -40,37 +42,18 @@ async fn test_bankrupt_tokens_socialize_loss() -> Result<(), TransportError> {
let collateral_token2 = &tokens[3]; let collateral_token2 = &tokens[3];
// deposit some funds, to the vaults aren't empty // deposit some funds, to the vaults aren't empty
let vault_account = send_tx(
solana,
AccountCreateInstruction {
account_num: 2,
token_count: 16,
serum3_count: 8,
perp_count: 8,
perp_oo_count: 8,
group,
owner,
payer,
},
)
.await
.unwrap()
.account;
let vault_amount = 100000; let vault_amount = 100000;
for &token_account in payer_mint_accounts { let vault_account = create_funded_account(
send_tx( &solana,
solana, group,
TokenDepositInstruction { owner,
amount: vault_amount, 2,
account: vault_account, &context.users[1],
token_account, mints,
token_authority: payer.clone(), vault_amount,
bank_index: 1, 1,
}, )
) .await;
.await
.unwrap();
}
// also add a tiny amount to bank0 for borrow_token1, so we can test multi-bank socialized loss // also add a tiny amount to bank0 for borrow_token1, so we can test multi-bank socialized loss
send_tx( send_tx(

View File

@ -6,6 +6,8 @@ use solana_sdk::{signature::Keypair, signature::Signer, transport::TransportErro
use mango_v4::state::*; use mango_v4::state::*;
use program_test::*; use program_test::*;
use mango_setup::*;
mod program_test; mod program_test;
#[tokio::test] #[tokio::test]
@ -24,7 +26,7 @@ async fn test_delegate() -> Result<(), TransportError> {
// SETUP: Create a group, register a token (mint0), create an account // SETUP: Create a group, register a token (mint0), create an account
// //
let mango_setup::GroupWithTokens { group, tokens, .. } = mango_setup::GroupWithTokensConfig { let GroupWithTokens { group, tokens, .. } = GroupWithTokensConfig {
admin, admin,
payer, payer,
mints, mints,
@ -33,36 +35,8 @@ async fn test_delegate() -> Result<(), TransportError> {
.await; .await;
let bank = tokens[0].bank; let bank = tokens[0].bank;
let account = send_tx( let account =
solana, create_funded_account(&solana, group, owner, 0, &context.users[1], mints, 100, 0).await;
AccountCreateInstruction {
account_num: 0,
token_count: 16,
serum3_count: 8,
perp_count: 8,
perp_oo_count: 8,
group,
owner,
payer,
},
)
.await
.unwrap()
.account;
// deposit
send_tx(
solana,
TokenDepositInstruction {
amount: 100,
account,
token_account: payer_mint0_account,
token_authority: payer.clone(),
bank_index: 0,
},
)
.await
.unwrap();
// //
// TEST: Edit account - Set delegate // TEST: Edit account - Set delegate

View File

@ -7,6 +7,8 @@ use solana_sdk::{signature::Keypair, transport::TransportError};
use program_test::*; use program_test::*;
use mango_setup::*;
mod program_test; mod program_test;
// Try to reach compute limits in health checks by having many different tokens in an account // Try to reach compute limits in health checks by having many different tokens in an account
@ -19,13 +21,12 @@ async fn test_health_compute_tokens() -> Result<(), TransportError> {
let owner = &context.users[0].key; let owner = &context.users[0].key;
let payer = &context.users[1].key; let payer = &context.users[1].key;
let mints = &context.mints[0..10]; let mints = &context.mints[0..10];
let payer_mint_accounts = &context.users[1].token_accounts[0..mints.len()];
// //
// SETUP: Create a group and an account // SETUP: Create a group and an account
// //
let mango_setup::GroupWithTokens { group, .. } = mango_setup::GroupWithTokensConfig { let GroupWithTokens { group, .. } = GroupWithTokensConfig {
admin, admin,
payer, payer,
mints, mints,
@ -33,43 +34,8 @@ async fn test_health_compute_tokens() -> Result<(), TransportError> {
.create(solana) .create(solana)
.await; .await;
let account = send_tx( // each deposit ends with a health check
solana, create_funded_account(&solana, group, owner, 0, &context.users[1], mints, 1000, 0).await;
AccountCreateInstruction {
account_num: 0,
token_count: 16,
serum3_count: 8,
perp_count: 8,
perp_oo_count: 8,
group,
owner,
payer,
},
)
.await
.unwrap()
.account;
//
// TEST: Deposit user funds for all the mints
// each deposit will end with a health check
//
for &token_account in payer_mint_accounts {
let deposit_amount = 1000;
send_tx(
solana,
TokenDepositInstruction {
amount: deposit_amount,
account,
token_account,
token_authority: payer.clone(),
bank_index: 0,
},
)
.await
.unwrap();
}
// TODO: actual explicit CU comparisons. // TODO: actual explicit CU comparisons.
// On 2022-5-25 the final deposit costs 36905 CU and each new token increases it by roughly 1600 CU // On 2022-5-25 the final deposit costs 36905 CU and each new token increases it by roughly 1600 CU
@ -216,36 +182,17 @@ async fn test_health_compute_perp() -> Result<(), TransportError> {
.create(solana) .create(solana)
.await; .await;
let account = send_tx( let account = create_funded_account(
solana, &solana,
AccountCreateInstruction { group,
account_num: 0, owner,
token_count: 16, 0,
serum3_count: 8, &context.users[1],
perp_count: 8, &mints[..1],
perp_oo_count: 8, 1000,
group, 0,
owner,
payer,
},
) )
.await .await;
.unwrap()
.account;
// Give the account some quote currency
send_tx(
solana,
TokenDepositInstruction {
amount: 1000,
account,
token_account: payer_mint_accounts[0],
token_authority: payer.clone(),
bank_index: 0,
},
)
.await
.unwrap();
// //
// SETUP: Create perp markets // SETUP: Create perp markets

View File

@ -7,6 +7,8 @@ use mango_v4::state::MangoAccount;
use program_test::*; use program_test::*;
use mango_setup::*;
mod program_test; mod program_test;
#[tokio::test] #[tokio::test]
@ -24,7 +26,7 @@ async fn test_health_wrap() -> Result<(), TransportError> {
// SETUP: Create a group, account, register a token (mint0) // SETUP: Create a group, account, register a token (mint0)
// //
let mango_setup::GroupWithTokens { group, tokens, .. } = mango_setup::GroupWithTokensConfig { let GroupWithTokens { group, tokens, .. } = GroupWithTokensConfig {
admin, admin,
payer, payer,
mints, mints,
@ -33,35 +35,7 @@ async fn test_health_wrap() -> Result<(), TransportError> {
.await; .await;
// SETUP: Create an account with deposits, so the second account can borrow more than it has // SETUP: Create an account with deposits, so the second account can borrow more than it has
let setup_account = send_tx( create_funded_account(&solana, group, owner, 0, &context.users[1], mints, 1000, 0).await;
solana,
AccountCreateInstruction {
account_num: 0,
token_count: 8,
serum3_count: 0,
perp_count: 0,
perp_oo_count: 0,
group,
owner,
payer,
},
)
.await
.unwrap()
.account;
send_tx(
solana,
TokenDepositInstruction {
amount: 1000,
account: setup_account,
token_account: payer_mint_accounts[0],
token_authority: payer.clone(),
bank_index: 0,
},
)
.await
.unwrap();
// SETUP: Make a second account // SETUP: Make a second account
let account = send_tx( let account = send_tx(

View File

@ -7,6 +7,8 @@ use solana_sdk::{signature::Keypair, transport::TransportError};
use mango_v4::instructions::{Serum3OrderType, Serum3SelfTradeBehavior, Serum3Side}; use mango_v4::instructions::{Serum3OrderType, Serum3SelfTradeBehavior, Serum3Side};
use program_test::*; use program_test::*;
use mango_setup::*;
mod program_test; mod program_test;
#[tokio::test] #[tokio::test]
@ -26,7 +28,7 @@ async fn test_liq_tokens_force_cancel() -> Result<(), TransportError> {
// SETUP: Create a group and an account to fill the vaults // SETUP: Create a group and an account to fill the vaults
// //
let mango_setup::GroupWithTokens { group, tokens, .. } = mango_setup::GroupWithTokensConfig { let GroupWithTokens { group, tokens, .. } = GroupWithTokensConfig {
admin, admin,
payer, payer,
mints, mints,
@ -37,36 +39,7 @@ async fn test_liq_tokens_force_cancel() -> Result<(), TransportError> {
let quote_token = &tokens[1]; let quote_token = &tokens[1];
// deposit some funds, to the vaults aren't empty // deposit some funds, to the vaults aren't empty
let vault_account = send_tx( create_funded_account(&solana, group, owner, 0, &context.users[1], mints, 10000, 0).await;
solana,
AccountCreateInstruction {
account_num: 2,
token_count: 16,
serum3_count: 8,
perp_count: 8,
perp_oo_count: 8,
group,
owner,
payer,
},
)
.await
.unwrap()
.account;
for &token_account in payer_mint_accounts {
send_tx(
solana,
TokenDepositInstruction {
amount: 10000,
account: vault_account,
token_account,
token_authority: payer.clone(),
bank_index: 0,
},
)
.await
.unwrap();
}
// //
// SETUP: Create serum market // SETUP: Create serum market
@ -96,36 +69,18 @@ async fn test_liq_tokens_force_cancel() -> Result<(), TransportError> {
// //
// SETUP: Make an account and deposit some quote // SETUP: Make an account and deposit some quote
// //
let account = send_tx(
solana,
AccountCreateInstruction {
account_num: 0,
token_count: 16,
serum3_count: 8,
perp_count: 8,
perp_oo_count: 8,
group,
owner,
payer,
},
)
.await
.unwrap()
.account;
let deposit_amount = 1000; let deposit_amount = 1000;
send_tx( let account = create_funded_account(
solana, &solana,
TokenDepositInstruction { group,
amount: deposit_amount, owner,
account, 1,
token_account: payer_mint_accounts[1], &context.users[1],
token_authority: payer.clone(), &mints[1..2],
bank_index: 0, deposit_amount,
}, 0,
) )
.await .await;
.unwrap();
// //
// SETUP: Create an open orders account and an order // SETUP: Create an open orders account and an order

View File

@ -6,6 +6,8 @@ use solana_sdk::signature::Signer;
use program_test::*; use program_test::*;
use mango_setup::*;
mod program_test; mod program_test;
// This is an unspecific happy-case test that just runs a few instructions to check // This is an unspecific happy-case test that just runs a few instructions to check
@ -21,7 +23,6 @@ async fn test_margin_trade() -> Result<(), BanksClientError> {
let payer = &context.users[1].key; let payer = &context.users[1].key;
let mints = &context.mints[0..2]; let mints = &context.mints[0..2];
let payer_mint0_account = context.users[1].token_accounts[0]; let payer_mint0_account = context.users[1].token_accounts[0];
let payer_mint1_account = context.users[1].token_accounts[1];
let loan_origination_fee = 0.0005; let loan_origination_fee = 0.0005;
// higher resolution that the loan_origination_fee for one token // higher resolution that the loan_origination_fee for one token
@ -31,7 +32,7 @@ async fn test_margin_trade() -> Result<(), BanksClientError> {
// SETUP: Create a group, account, register a token (mint0) // SETUP: Create a group, account, register a token (mint0)
// //
let mango_setup::GroupWithTokens { group, tokens, .. } = mango_setup::GroupWithTokensConfig { let GroupWithTokens { group, tokens, .. } = GroupWithTokensConfig {
admin, admin,
payer, payer,
mints, mints,
@ -45,48 +46,17 @@ async fn test_margin_trade() -> Result<(), BanksClientError> {
// provide some funds for tokens, so the test user can borrow // provide some funds for tokens, so the test user can borrow
// //
let provided_amount = 1000; let provided_amount = 1000;
create_funded_account(
let provider_account = send_tx( &solana,
solana, group,
AccountCreateInstruction { owner,
account_num: 1, 1,
token_count: 16, &context.users[1],
serum3_count: 8, mints,
perp_count: 8, provided_amount,
perp_oo_count: 8, 0,
group,
owner,
payer,
},
) )
.await .await;
.unwrap()
.account;
send_tx(
solana,
TokenDepositInstruction {
amount: provided_amount,
account: provider_account,
token_account: payer_mint0_account,
token_authority: payer.clone(),
bank_index: 0,
},
)
.await
.unwrap();
send_tx(
solana,
TokenDepositInstruction {
amount: provided_amount,
account: provider_account,
token_account: payer_mint1_account,
token_authority: payer.clone(),
bank_index: 0,
},
)
.await
.unwrap();
// //
// create thes test user account // create thes test user account

View File

@ -7,6 +7,8 @@ use program_test::*;
use solana_program_test::*; use solana_program_test::*;
use solana_sdk::{signature::Keypair, signer::Signer, transport::TransportError}; use solana_sdk::{signature::Keypair, signer::Signer, transport::TransportError};
use mango_setup::*;
mod program_test; mod program_test;
#[tokio::test] #[tokio::test]
@ -18,13 +20,12 @@ async fn test_perp() -> Result<(), TransportError> {
let owner = &context.users[0].key; let owner = &context.users[0].key;
let payer = &context.users[1].key; let payer = &context.users[1].key;
let mints = &context.mints[0..2]; let mints = &context.mints[0..2];
let payer_mint_accounts = &context.users[1].token_accounts[0..=2];
// //
// SETUP: Create a group and an account // SETUP: Create a group and an account
// //
let mango_setup::GroupWithTokens { group, tokens, .. } = mango_setup::GroupWithTokensConfig { let GroupWithTokens { group, tokens, .. } = GroupWithTokensConfig {
admin, admin,
payer, payer,
mints, mints,
@ -32,102 +33,29 @@ async fn test_perp() -> Result<(), TransportError> {
.create(solana) .create(solana)
.await; .await;
let account_0 = send_tx( let deposit_amount = 1000;
solana, let account_0 = create_funded_account(
AccountCreateInstruction { &solana,
account_num: 0, group,
token_count: 16, owner,
serum3_count: 8, 0,
perp_count: 8, &context.users[1],
perp_oo_count: 8, mints,
group, deposit_amount,
owner, 0,
payer,
},
) )
.await .await;
.unwrap() let account_1 = create_funded_account(
.account; &solana,
group,
let account_1 = send_tx( owner,
solana, 1,
AccountCreateInstruction { &context.users[1],
account_num: 1, mints,
token_count: 16, deposit_amount,
serum3_count: 8, 0,
perp_count: 8,
perp_oo_count: 8,
group,
owner,
payer,
},
) )
.await .await;
.unwrap()
.account;
//
// SETUP: Deposit user funds
//
{
let deposit_amount = 1000;
send_tx(
solana,
TokenDepositInstruction {
amount: deposit_amount,
account: account_0,
token_account: payer_mint_accounts[0],
token_authority: payer.clone(),
bank_index: 0,
},
)
.await
.unwrap();
send_tx(
solana,
TokenDepositInstruction {
amount: deposit_amount,
account: account_0,
token_account: payer_mint_accounts[1],
token_authority: payer.clone(),
bank_index: 0,
},
)
.await
.unwrap();
}
{
let deposit_amount = 1000;
send_tx(
solana,
TokenDepositInstruction {
amount: deposit_amount,
account: account_1,
token_account: payer_mint_accounts[0],
token_authority: payer.clone(),
bank_index: 0,
},
)
.await
.unwrap();
send_tx(
solana,
TokenDepositInstruction {
amount: deposit_amount,
account: account_1,
token_account: payer_mint_accounts[1],
token_authority: payer.clone(),
bank_index: 0,
},
)
.await
.unwrap();
}
// //
// TEST: Create a perp market // TEST: Create a perp market

View File

@ -6,6 +6,8 @@ use solana_sdk::signature::Keypair;
use program_test::*; use program_test::*;
use crate::mango_setup::*;
mod program_test; mod program_test;
// Check opening and closing positions // Check opening and closing positions
@ -50,43 +52,18 @@ async fn test_position_lifetime() -> Result<()> {
.unwrap() .unwrap()
.account; .account;
let funding_account = send_tx( let funding_amount = 1000000;
solana, create_funded_account(
AccountCreateInstruction { &solana,
account_num: 1, group,
token_count: 16, owner,
serum3_count: 8, 1,
perp_count: 8, &context.users[1],
perp_oo_count: 8, mints,
group, funding_amount,
owner, 0,
payer,
},
) )
.await .await;
.unwrap()
.account;
//
// SETUP: Put some tokens into the funding account to allow borrowing
//
{
let funding_amount = 1000000;
for &payer_token in payer_mint_accounts {
send_tx(
solana,
TokenDepositInstruction {
amount: funding_amount,
account: funding_account,
token_account: payer_token,
token_authority: payer.clone(),
bank_index: 0,
},
)
.await
.unwrap();
}
}
// //
// TEST: Deposit and withdraw tokens for all mints // TEST: Deposit and withdraw tokens for all mints

View File

@ -8,6 +8,8 @@ use program_test::*;
mod program_test; mod program_test;
use mango_setup::*;
#[tokio::test] #[tokio::test]
async fn test_serum() -> Result<(), TransportError> { async fn test_serum() -> Result<(), TransportError> {
let mut test_builder = TestContextBuilder::new(); let mut test_builder = TestContextBuilder::new();
@ -19,13 +21,12 @@ async fn test_serum() -> Result<(), TransportError> {
let owner = &context.users[0].key; let owner = &context.users[0].key;
let payer = &context.users[1].key; let payer = &context.users[1].key;
let mints = &context.mints[0..2]; let mints = &context.mints[0..2];
let payer_mint_accounts = &context.users[1].token_accounts[0..=2];
// //
// SETUP: Create a group and an account // SETUP: Create a group and an account
// //
let mango_setup::GroupWithTokens { group, tokens, .. } = mango_setup::GroupWithTokensConfig { let GroupWithTokens { group, tokens, .. } = GroupWithTokensConfig {
admin, admin,
payer, payer,
mints, mints,
@ -35,22 +36,18 @@ async fn test_serum() -> Result<(), TransportError> {
let base_token = &tokens[0]; let base_token = &tokens[0];
let quote_token = &tokens[1]; let quote_token = &tokens[1];
let account = send_tx( let deposit_amount = 1000;
solana, let account = create_funded_account(
AccountCreateInstruction { &solana,
account_num: 0, group,
token_count: 16, owner,
serum3_count: 8, 0,
perp_count: 8, &context.users[1],
perp_oo_count: 8, mints,
group, deposit_amount,
owner, 0,
payer,
},
) )
.await .await;
.unwrap()
.account;
// //
// SETUP: Create serum market // SETUP: Create serum market
@ -60,39 +57,6 @@ async fn test_serum() -> Result<(), TransportError> {
.list_spot_market(&base_token.mint, &quote_token.mint) .list_spot_market(&base_token.mint, &quote_token.mint)
.await; .await;
//
// SETUP: Deposit user funds
//
{
let deposit_amount = 1000;
send_tx(
solana,
TokenDepositInstruction {
amount: deposit_amount,
account,
token_account: payer_mint_accounts[0],
token_authority: payer.clone(),
bank_index: 0,
},
)
.await
.unwrap();
send_tx(
solana,
TokenDepositInstruction {
amount: deposit_amount,
account,
token_account: payer_mint_accounts[1],
token_authority: payer.clone(),
bank_index: 0,
},
)
.await
.unwrap();
}
// //
// TEST: Register a serum market // TEST: Register a serum market
// //

View File

@ -4,6 +4,7 @@ use mango_v4::state::*;
use solana_program_test::*; use solana_program_test::*;
use solana_sdk::{signature::Keypair, transport::TransportError}; use solana_sdk::{signature::Keypair, transport::TransportError};
use mango_setup::*;
use program_test::*; use program_test::*;
mod program_test; mod program_test;
@ -17,13 +18,12 @@ async fn test_token_update_index_and_rate() -> Result<(), TransportError> {
let owner = &context.users[0].key; let owner = &context.users[0].key;
let payer = &context.users[1].key; let payer = &context.users[1].key;
let mints = &context.mints[0..2]; let mints = &context.mints[0..2];
let payer_mint_accounts = &context.users[1].token_accounts[0..2];
// //
// SETUP: Create a group and an account to fill the vaults // SETUP: Create a group and an account to fill the vaults
// //
let mango_setup::GroupWithTokens { group, tokens, .. } = mango_setup::GroupWithTokensConfig { let GroupWithTokens { group, tokens, .. } = GroupWithTokensConfig {
admin, admin,
payer, payer,
mints, mints,
@ -32,66 +32,18 @@ async fn test_token_update_index_and_rate() -> Result<(), TransportError> {
.await; .await;
// deposit some funds, to the vaults aren't empty // deposit some funds, to the vaults aren't empty
let deposit_account = send_tx( create_funded_account(&solana, group, owner, 0, &context.users[1], mints, 10000, 0).await;
solana, let withdraw_account = create_funded_account(
AccountCreateInstruction { &solana,
account_num: 0, group,
token_count: 16, owner,
serum3_count: 8, 1,
perp_count: 8, &context.users[1],
perp_oo_count: 8, &mints[1..2],
group, 100000,
owner, 0,
payer,
},
) )
.await .await;
.unwrap()
.account;
for &token_account in payer_mint_accounts {
send_tx(
solana,
TokenDepositInstruction {
amount: 10000,
account: deposit_account,
token_account,
token_authority: payer.clone(),
bank_index: 0,
},
)
.await
.unwrap();
}
let withdraw_account = send_tx(
solana,
AccountCreateInstruction {
account_num: 1,
token_count: 16,
serum3_count: 8,
perp_count: 8,
perp_oo_count: 8,
group,
owner,
payer,
},
)
.await
.unwrap()
.account;
send_tx(
solana,
TokenDepositInstruction {
amount: 100000,
account: withdraw_account,
token_account: payer_mint_accounts[1],
token_authority: payer.clone(),
bank_index: 0,
},
)
.await
.unwrap();
send_tx( send_tx(
solana, solana,