Serum index reservation: use a real account

Makes it easier and more future-proof
This commit is contained in:
Christian Kamm 2022-09-01 12:26:50 +02:00
parent c31f11013f
commit 868c55fef1
4 changed files with 76 additions and 62 deletions

View File

@ -20,13 +20,13 @@ pub struct Serum3DeregisterMarket<'info> {
)]
pub serum_market: AccountLoader<'info, Serum3Market>,
/// CHECK: Unused account
#[account(
mut,
seeds = [b"Serum3Index".as_ref(), group.key().as_ref(), &serum_market.load()?.market_index.to_le_bytes()],
bump
has_one = group,
constraint = serum_market.load()?.market_index == index_reservation.load()?.market_index,
close = sol_destination
)]
pub index_reservation: UncheckedAccount<'info>,
pub index_reservation: AccountLoader<'info, Serum3MarketIndexReservation>,
#[account(mut)]
/// CHECK: target for account rent needs no checks
@ -35,20 +35,6 @@ pub struct Serum3DeregisterMarket<'info> {
pub token_program: Program<'info, Token>,
}
pub fn serum3_deregister_market(ctx: Context<Serum3DeregisterMarket>) -> Result<()> {
close_unsafe(
ctx.accounts.index_reservation.to_account_info(),
ctx.accounts.sol_destination.to_account_info(),
);
pub fn serum3_deregister_market(_ctx: Context<Serum3DeregisterMarket>) -> Result<()> {
Ok(())
}
fn close_unsafe<'info>(info: AccountInfo<'info>, sol_destination: AccountInfo<'info>) {
// Transfer tokens from the account to the sol_destination.
let dest_starting_lamports = sol_destination.lamports();
**sol_destination.lamports.borrow_mut() =
dest_starting_lamports.checked_add(info.lamports()).unwrap();
**info.lamports.borrow_mut() = 0;
// Does NOT prevent reinit attacks in any way
}

View File

@ -31,16 +31,15 @@ pub struct Serum3RegisterMarket<'info> {
)]
pub serum_market: AccountLoader<'info, Serum3Market>,
/// CHECK: Unused account
#[account(
init,
// block using the same market index twice
seeds = [b"Serum3Index".as_ref(), group.key().as_ref(), &market_index.to_le_bytes()],
bump,
payer = payer,
space = 8, // like an anchor account with only a discriminator
space = 8 + std::mem::size_of::<Serum3MarketIndexReservation>(),
)]
pub index_reservation: UncheckedAccount<'info>,
pub index_reservation: AccountLoader<'info, Serum3MarketIndexReservation>,
#[account(has_one = group)]
pub quote_bank: AccountLoader<'info, Bank>,
@ -91,5 +90,12 @@ pub fn serum3_register_market(
reserved: [0; 128],
};
let mut serum_index_reservation = ctx.accounts.index_reservation.load_init()?;
*serum_index_reservation = Serum3MarketIndexReservation {
group: ctx.accounts.group.key(),
market_index,
reserved: [0; 38],
};
Ok(())
}

View File

@ -44,6 +44,16 @@ impl Serum3Market {
}
}
#[account(zero_copy)]
#[derive(Debug)]
pub struct Serum3MarketIndexReservation {
pub group: Pubkey,
pub market_index: Serum3MarketIndex,
pub reserved: [u8; 38],
}
const_assert_eq!(size_of::<Serum3MarketIndexReservation>(), 32 + 2 + 38);
const_assert_eq!(size_of::<Serum3MarketIndexReservation>() % 8, 0);
#[macro_export]
macro_rules! serum_market_seeds {
( $acc:expr ) => {

View File

@ -1397,26 +1397,7 @@ export type MangoV4 = {
{
"name": "indexReservation",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "const",
"type": "string",
"value": "Serum3Index"
},
{
"kind": "account",
"type": "publicKey",
"path": "group"
},
{
"kind": "account",
"type": "publicKey",
"path": "serum_market"
}
]
}
"isSigner": false
},
{
"name": "solDestination",
@ -3681,6 +3662,31 @@ export type MangoV4 = {
}
]
}
},
{
"name": "serum3MarketIndexReservation",
"type": {
"kind": "struct",
"fields": [
{
"name": "group",
"type": "publicKey"
},
{
"name": "marketIndex",
"type": "u16"
},
{
"name": "reserved",
"type": {
"array": [
"u8",
38
]
}
}
]
}
}
],
"types": [
@ -6705,26 +6711,7 @@ export const IDL: MangoV4 = {
{
"name": "indexReservation",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "const",
"type": "string",
"value": "Serum3Index"
},
{
"kind": "account",
"type": "publicKey",
"path": "group"
},
{
"kind": "account",
"type": "publicKey",
"path": "serum_market"
}
]
}
"isSigner": false
},
{
"name": "solDestination",
@ -8989,6 +8976,31 @@ export const IDL: MangoV4 = {
}
]
}
},
{
"name": "serum3MarketIndexReservation",
"type": {
"kind": "struct",
"fields": [
{
"name": "group",
"type": "publicKey"
},
{
"name": "marketIndex",
"type": "u16"
},
{
"name": "reserved",
"type": {
"array": [
"u8",
38
]
}
}
]
}
}
],
"types": [