serum3_register_market: better validation
Previously we just trusted the base and quote token index to be correct, now the appropriate banks are validated.
This commit is contained in:
parent
babbff6bec
commit
afbfd3b7f9
|
@ -5,7 +5,6 @@ use crate::state::*;
|
|||
|
||||
#[derive(Accounts)]
|
||||
pub struct Serum3CreateOpenOrders<'info> {
|
||||
// TODO: do we even need the group?
|
||||
pub group: AccountLoader<'info, Group>,
|
||||
|
||||
#[account(
|
||||
|
@ -21,8 +20,6 @@ pub struct Serum3CreateOpenOrders<'info> {
|
|||
has_one = serum_market_external,
|
||||
)]
|
||||
pub serum_market: AccountLoader<'info, Serum3Market>,
|
||||
|
||||
// TODO: limit?
|
||||
pub serum_program: UncheckedAccount<'info>,
|
||||
pub serum_market_external: UncheckedAccount<'info>,
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use anchor_lang::prelude::*;
|
||||
|
||||
use crate::error::MangoError;
|
||||
use crate::serum3_cpi::{load_market_state, pubkey_from_u64_array};
|
||||
use crate::state::*;
|
||||
|
||||
#[derive(Accounts)]
|
||||
|
@ -26,6 +27,11 @@ pub struct Serum3RegisterMarket<'info> {
|
|||
)]
|
||||
pub serum_market: AccountLoader<'info, Serum3Market>,
|
||||
|
||||
#[account(has_one = group)]
|
||||
pub quote_bank: AccountLoader<'info, Bank>,
|
||||
#[account(has_one = group)]
|
||||
pub base_bank: AccountLoader<'info, Bank>,
|
||||
|
||||
#[account(mut)]
|
||||
pub payer: Signer<'info>,
|
||||
|
||||
|
@ -36,11 +42,23 @@ pub struct Serum3RegisterMarket<'info> {
|
|||
pub fn serum3_register_market(
|
||||
ctx: Context<Serum3RegisterMarket>,
|
||||
market_index: Serum3MarketIndex,
|
||||
base_token_index: TokenIndex,
|
||||
quote_token_index: TokenIndex,
|
||||
) -> Result<()> {
|
||||
// TODO: must guard against accidentally using the same market_index twice!
|
||||
// TODO: verify that base_token_index and quote_token_index are correct!
|
||||
|
||||
let base_bank = ctx.accounts.base_bank.load()?;
|
||||
let quote_bank = ctx.accounts.quote_bank.load()?;
|
||||
let market_external = load_market_state(
|
||||
&ctx.accounts.serum_market_external,
|
||||
&ctx.accounts.serum_program.key(),
|
||||
)?;
|
||||
require!(
|
||||
pubkey_from_u64_array(market_external.pc_mint) == quote_bank.mint,
|
||||
MangoError::SomeError
|
||||
);
|
||||
require!(
|
||||
pubkey_from_u64_array(market_external.coin_mint) == base_bank.mint,
|
||||
MangoError::SomeError
|
||||
);
|
||||
|
||||
let mut serum_market = ctx.accounts.serum_market.load_init()?;
|
||||
*serum_market = Serum3Market {
|
||||
|
@ -48,8 +66,8 @@ pub fn serum3_register_market(
|
|||
serum_program: ctx.accounts.serum_program.key(),
|
||||
serum_market_external: ctx.accounts.serum_market_external.key(),
|
||||
market_index,
|
||||
base_token_index,
|
||||
quote_token_index,
|
||||
base_token_index: base_bank.token_index,
|
||||
quote_token_index: quote_bank.token_index,
|
||||
bump: *ctx.bumps.get("serum_market").ok_or(MangoError::SomeError)?,
|
||||
};
|
||||
|
||||
|
|
|
@ -92,10 +92,8 @@ pub mod mango_v4 {
|
|||
pub fn serum3_register_market(
|
||||
ctx: Context<Serum3RegisterMarket>,
|
||||
market_index: Serum3MarketIndex,
|
||||
base_token_index: TokenIndex,
|
||||
quote_token_index: TokenIndex,
|
||||
) -> Result<()> {
|
||||
instructions::serum3_register_market(ctx, market_index, base_token_index, quote_token_index)
|
||||
instructions::serum3_register_market(ctx, market_index)
|
||||
}
|
||||
|
||||
pub fn serum3_create_open_orders(ctx: Context<Serum3CreateOpenOrders>) -> Result<()> {
|
||||
|
|
|
@ -109,6 +109,10 @@ pub fn load_open_orders<'a>(acc: &'a AccountInfo) -> Result<Ref<'a, serum_dex::s
|
|||
Ok(Ref::map(strip_dex_padding(acc)?, bytemuck::from_bytes))
|
||||
}
|
||||
|
||||
pub fn pubkey_from_u64_array(d: [u64; 4]) -> Pubkey {
|
||||
Pubkey::new(bytemuck::cast_slice(&d as &[_]))
|
||||
}
|
||||
|
||||
pub struct InitOpenOrders<'info> {
|
||||
pub program: AccountInfo<'info>,
|
||||
pub market: AccountInfo<'info>,
|
||||
|
|
|
@ -697,9 +697,10 @@ pub struct Serum3RegisterMarketInstruction<'keypair> {
|
|||
pub serum_program: Pubkey,
|
||||
pub serum_market_external: Pubkey,
|
||||
|
||||
pub base_bank: Pubkey,
|
||||
pub quote_bank: Pubkey,
|
||||
|
||||
pub market_index: Serum3MarketIndex,
|
||||
pub base_token_index: TokenIndex,
|
||||
pub quote_token_index: TokenIndex,
|
||||
}
|
||||
#[async_trait::async_trait(?Send)]
|
||||
impl<'keypair> ClientInstruction for Serum3RegisterMarketInstruction<'keypair> {
|
||||
|
@ -712,8 +713,6 @@ impl<'keypair> ClientInstruction for Serum3RegisterMarketInstruction<'keypair> {
|
|||
let program_id = mango_v4::id();
|
||||
let instruction = Self::Instruction {
|
||||
market_index: self.market_index,
|
||||
base_token_index: self.base_token_index,
|
||||
quote_token_index: self.quote_token_index,
|
||||
};
|
||||
|
||||
let serum_market = Pubkey::find_program_address(
|
||||
|
@ -732,6 +731,8 @@ impl<'keypair> ClientInstruction for Serum3RegisterMarketInstruction<'keypair> {
|
|||
serum_program: self.serum_program,
|
||||
serum_market_external: self.serum_market_external,
|
||||
serum_market,
|
||||
base_bank: self.base_bank,
|
||||
quote_bank: self.quote_bank,
|
||||
payer: self.payer.pubkey(),
|
||||
system_program: System::id(),
|
||||
};
|
||||
|
|
|
@ -86,7 +86,7 @@ async fn test_health_compute_serum() -> Result<(), TransportError> {
|
|||
// SETUP: Create a group and an account
|
||||
//
|
||||
|
||||
let mango_setup::GroupWithTokens { group, .. } = mango_setup::GroupWithTokensConfig {
|
||||
let mango_setup::GroupWithTokens { group, tokens } = mango_setup::GroupWithTokensConfig {
|
||||
admin,
|
||||
payer,
|
||||
mints,
|
||||
|
@ -110,14 +110,20 @@ async fn test_health_compute_serum() -> Result<(), TransportError> {
|
|||
//
|
||||
// SETUP: Create serum markets and register them
|
||||
//
|
||||
let quote_mint = &mints[0];
|
||||
let quote_token = &tokens[0];
|
||||
let mut serum_market_cookies = vec![];
|
||||
for mint in mints[1..].iter() {
|
||||
serum_market_cookies.push(context.serum.list_spot_market(mint, quote_mint).await);
|
||||
for token in tokens[1..].iter() {
|
||||
serum_market_cookies.push((
|
||||
token,
|
||||
context
|
||||
.serum
|
||||
.list_spot_market(&token.mint, "e_token.mint)
|
||||
.await,
|
||||
));
|
||||
}
|
||||
|
||||
let mut serum_markets = vec![];
|
||||
for s in serum_market_cookies {
|
||||
for (base_token, spot) in serum_market_cookies {
|
||||
serum_markets.push(
|
||||
send_tx(
|
||||
solana,
|
||||
|
@ -125,10 +131,10 @@ async fn test_health_compute_serum() -> Result<(), TransportError> {
|
|||
group,
|
||||
admin,
|
||||
serum_program: context.serum.program_id,
|
||||
serum_market_external: s.market,
|
||||
market_index: s.coin_mint.index as u16,
|
||||
base_token_index: s.coin_mint.index as u16,
|
||||
quote_token_index: s.pc_mint.index as u16,
|
||||
serum_market_external: spot.market,
|
||||
market_index: spot.coin_mint.index as u16,
|
||||
base_bank: base_token.bank,
|
||||
quote_bank: quote_token.bank,
|
||||
payer,
|
||||
},
|
||||
)
|
||||
|
|
|
@ -77,8 +77,8 @@ async fn test_liq_tokens_force_cancel() -> Result<(), TransportError> {
|
|||
serum_program: context.serum.program_id,
|
||||
serum_market_external: serum_market_cookie.market,
|
||||
market_index: 0,
|
||||
base_token_index: base_token.index,
|
||||
quote_token_index: quote_token.index,
|
||||
base_bank: base_token.bank,
|
||||
quote_bank: quote_token.bank,
|
||||
payer,
|
||||
},
|
||||
)
|
||||
|
|
|
@ -96,8 +96,8 @@ async fn test_serum() -> Result<(), TransportError> {
|
|||
serum_program: context.serum.program_id,
|
||||
serum_market_external: serum_market_cookie.market,
|
||||
market_index: 0,
|
||||
base_token_index: base_token.index,
|
||||
quote_token_index: quote_token.index,
|
||||
base_bank: base_token.bank,
|
||||
quote_bank: quote_token.bank,
|
||||
payer,
|
||||
},
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue