test: stubOracle is not a PDA (allow multiple with same mint)

This commit is contained in:
Lou-Kamades 2023-11-19 18:01:24 -06:00
parent 6288f2dd7d
commit c22db3be0c
6 changed files with 34 additions and 52 deletions

View File

@ -12,8 +12,6 @@ pub struct StubOracleCreate<'info> {
#[account(
init,
seeds = [b"StubOracle".as_ref(), group.key().as_ref(), mint.key().as_ref()],
bump,
payer = payer,
space = 8 + std::mem::size_of::<StubOracle>(),
)]

View File

@ -3,7 +3,10 @@ use fixed::types::I80F48;
use crate::accounts_ix::*;
pub fn stub_oracle_create(ctx: Context<StubOracleCreate>, price: I80F48) -> Result<()> {
pub fn stub_oracle_create(
ctx: Context<StubOracleCreate>,
price: I80F48
) -> Result<()> {
let mut oracle = ctx.accounts.oracle.load_init()?;
oracle.group = ctx.accounts.group.key();
oracle.mint = ctx.accounts.mint.key();

View File

@ -975,6 +975,7 @@ async fn test_perp_pnl_settle_limit() -> Result<(), TransportError> {
send_tx(
solana,
StubOracleSetInstruction {
oracle: tokens[0].oracle,
group,
admin,
mint: mints[1].pubkey,

View File

@ -17,7 +17,7 @@ async fn test_stale_oracle_deposit_withdraw() -> Result<(), TransportError> {
// SETUP: Create a group, account, register tokens
//
let mango_setup::GroupWithTokens { group, .. } = mango_setup::GroupWithTokensConfig {
let mango_setup::GroupWithTokens { group, tokens, .. } = mango_setup::GroupWithTokensConfig {
admin,
payer,
mints: mints.to_vec(),
@ -71,6 +71,7 @@ async fn test_stale_oracle_deposit_withdraw() -> Result<(), TransportError> {
send_tx(
solana,
StubOracleSetTestInstruction {
oracle: tokens[0].oracle,
group,
mint: mints[0].pubkey,
admin,
@ -84,6 +85,7 @@ async fn test_stale_oracle_deposit_withdraw() -> Result<(), TransportError> {
send_tx(
solana,
StubOracleSetTestInstruction {
oracle: tokens[1].oracle,
group,
mint: mints[1].pubkey,
admin,
@ -97,6 +99,7 @@ async fn test_stale_oracle_deposit_withdraw() -> Result<(), TransportError> {
send_tx(
solana,
StubOracleSetTestInstruction {
oracle: tokens[2].oracle,
group,
mint: mints[2].pubkey,
admin,
@ -171,4 +174,3 @@ async fn test_stale_oracle_deposit_withdraw() -> Result<(), TransportError> {
.unwrap();
Ok(())
}

View File

@ -48,7 +48,8 @@ pub async fn send_tx<CI: ClientInstruction>(
) -> std::result::Result<CI::Accounts, TransportError> {
let (accounts, instruction) = ix.to_instruction(solana).await;
let signers = ix.signers();
let instructions = vec![instruction];
let instructions = vec![instruction.clone()];
println!("IX IX: {:?}", instruction);
let result = solana
.process_transaction(&instructions, Some(&signers[..]))
.await?;
@ -428,6 +429,7 @@ pub async fn set_bank_stub_oracle_price(
send_tx(
solana,
StubOracleSetInstruction {
oracle: token.oracle,
group,
admin,
mint: token.mint.pubkey,
@ -959,6 +961,7 @@ pub struct TokenRegisterInstruction {
pub group: Pubkey,
pub admin: TestKeypair,
pub mint: Pubkey,
pub oracle: Pubkey,
pub payer: TestKeypair,
}
#[async_trait::async_trait(?Send)]
@ -1042,16 +1045,7 @@ impl ClientInstruction for TokenRegisterInstruction {
&program_id,
)
.0;
// TODO: remove copy pasta of pda derivation, use reference
let oracle = Pubkey::find_program_address(
&[
b"StubOracle".as_ref(),
self.group.as_ref(),
self.mint.as_ref(),
],
&program_id,
)
.0;
let fallback_oracle = Pubkey::default();
let accounts = Self::Accounts {
group: self.group,
@ -1060,7 +1054,8 @@ impl ClientInstruction for TokenRegisterInstruction {
bank,
vault,
mint_info,
oracle,
oracle: self.oracle,
fallback_oracle,
payer: self.payer.pubkey(),
token_program: Token::id(),
system_program: System::id(),
@ -1262,6 +1257,7 @@ pub fn token_edit_instruction_default() -> mango_v4::instruction::TokenEdit {
maint_weight_shift_asset_target_opt: None,
maint_weight_shift_liab_target_opt: None,
maint_weight_shift_abort: false,
fallback_oracle_opt: None,
}
}
@ -1297,6 +1293,7 @@ impl ClientInstruction for TokenEdit {
admin: self.admin.pubkey(),
mint_info: mint_info_key,
oracle: mint_info.oracle,
fallback_oracle: mint_info.fallback_oracle,
};
let mut instruction = make_instruction(program_id, &accounts, &self.options);
@ -1360,6 +1357,7 @@ impl ClientInstruction for TokenEditWeights {
admin: self.admin.pubkey(),
mint_info: mint_info_key,
oracle: mint_info.oracle,
fallback_oracle: mint_info.fallback_oracle,
};
let mut instruction = make_instruction(program_id, &accounts, &instruction);
@ -1416,6 +1414,7 @@ impl ClientInstruction for TokenResetStablePriceModel {
admin: self.admin.pubkey(),
mint_info: mint_info_key,
oracle: mint_info.oracle,
fallback_oracle: mint_info.fallback_oracle,
};
let mut instruction = make_instruction(program_id, &accounts, &instruction);
@ -1477,6 +1476,7 @@ impl ClientInstruction for TokenResetNetBorrows {
admin: self.admin.pubkey(),
mint_info: mint_info_key,
oracle: mint_info.oracle,
fallback_oracle: mint_info.fallback_oracle,
};
let mut instruction = make_instruction(program_id, &accounts, &instruction);
@ -1535,6 +1535,7 @@ impl ClientInstruction for TokenMakeReduceOnly {
admin: self.admin.pubkey(),
mint_info: mint_info_key,
oracle: mint_info.oracle,
fallback_oracle: mint_info.fallback_oracle,
};
let mut instruction = make_instruction(program_id, &accounts, &instruction);
@ -1558,6 +1559,7 @@ pub struct StubOracleSetInstruction {
pub group: Pubkey,
pub admin: TestKeypair,
pub price: f64,
pub oracle: Pubkey,
}
#[async_trait::async_trait(?Send)]
impl ClientInstruction for StubOracleSetInstruction {
@ -1572,19 +1574,9 @@ impl ClientInstruction for StubOracleSetInstruction {
let instruction = Self::Instruction {
price: I80F48::from_num(self.price),
};
// TODO: remove copy pasta of pda derivation, use reference
let oracle = Pubkey::find_program_address(
&[
b"StubOracle".as_ref(),
self.group.as_ref(),
self.mint.as_ref(),
],
&program_id,
)
.0;
let accounts = Self::Accounts {
oracle,
oracle: self.oracle,
group: self.group,
admin: self.admin.pubkey(),
};
@ -1599,6 +1591,7 @@ impl ClientInstruction for StubOracleSetInstruction {
}
pub struct StubOracleSetTestInstruction {
pub oracle: Pubkey,
pub mint: Pubkey,
pub group: Pubkey,
pub admin: TestKeypair,
@ -1621,18 +1614,9 @@ impl ClientInstruction for StubOracleSetTestInstruction {
last_update_slot: self.last_update_slot,
deviation: I80F48::from_num(self.deviation),
};
let oracle = Pubkey::find_program_address(
&[
b"StubOracle".as_ref(),
self.group.as_ref(),
self.mint.as_ref(),
],
&program_id,
)
.0;
let accounts = Self::Accounts {
oracle,
oracle: self.oracle,
group: self.group,
admin: self.admin.pubkey(),
};
@ -1647,10 +1631,11 @@ impl ClientInstruction for StubOracleSetTestInstruction {
}
pub struct StubOracleCreate {
pub group: Pubkey,
pub mint: Pubkey,
pub oracle: TestKeypair,
pub admin: TestKeypair,
pub payer: TestKeypair,
pub group: Pubkey,
pub mint: Pubkey,
}
#[async_trait::async_trait(?Send)]
impl ClientInstruction for StubOracleCreate {
@ -1666,19 +1651,9 @@ impl ClientInstruction for StubOracleCreate {
price: I80F48::from_num(1.0),
};
let oracle = Pubkey::find_program_address(
&[
b"StubOracle".as_ref(),
self.group.as_ref(),
self.mint.as_ref(),
],
&program_id,
)
.0;
let accounts = Self::Accounts {
group: self.group,
oracle,
oracle: self.oracle.pubkey(),
mint: self.mint,
admin: self.admin.pubkey(),
payer: self.payer.pubkey(),
@ -1690,7 +1665,7 @@ impl ClientInstruction for StubOracleCreate {
}
fn signers(&self) -> Vec<TestKeypair> {
vec![self.payer, self.admin]
vec![self.payer, self.admin, self.oracle]
}
}

View File

@ -58,6 +58,7 @@ impl<'a> GroupWithTokensConfig {
let create_stub_oracle_accounts = send_tx(
solana,
StubOracleCreate {
oracle: TestKeypair::new(),
group,
mint: mint.pubkey,
admin,
@ -74,6 +75,7 @@ impl<'a> GroupWithTokensConfig {
admin,
mint: mint.pubkey,
price: 1.0,
oracle,
},
)
.await
@ -104,6 +106,7 @@ impl<'a> GroupWithTokensConfig {
liquidation_fee: 0.02,
group,
admin,
oracle,
mint: mint.pubkey,
payer,
min_vault_to_deposits_ratio: 0.2,