Serum index reservation: use a real account
Makes it easier and more future-proof
This commit is contained in:
parent
c31f11013f
commit
868c55fef1
|
@ -20,13 +20,13 @@ pub struct Serum3DeregisterMarket<'info> {
|
||||||
)]
|
)]
|
||||||
pub serum_market: AccountLoader<'info, Serum3Market>,
|
pub serum_market: AccountLoader<'info, Serum3Market>,
|
||||||
|
|
||||||
/// CHECK: Unused account
|
|
||||||
#[account(
|
#[account(
|
||||||
mut,
|
mut,
|
||||||
seeds = [b"Serum3Index".as_ref(), group.key().as_ref(), &serum_market.load()?.market_index.to_le_bytes()],
|
has_one = group,
|
||||||
bump
|
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)]
|
#[account(mut)]
|
||||||
/// CHECK: target for account rent needs no checks
|
/// CHECK: target for account rent needs no checks
|
||||||
|
@ -35,20 +35,6 @@ pub struct Serum3DeregisterMarket<'info> {
|
||||||
pub token_program: Program<'info, Token>,
|
pub token_program: Program<'info, Token>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn serum3_deregister_market(ctx: Context<Serum3DeregisterMarket>) -> Result<()> {
|
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(),
|
|
||||||
);
|
|
||||||
Ok(())
|
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
|
|
||||||
}
|
|
||||||
|
|
|
@ -31,16 +31,15 @@ pub struct Serum3RegisterMarket<'info> {
|
||||||
)]
|
)]
|
||||||
pub serum_market: AccountLoader<'info, Serum3Market>,
|
pub serum_market: AccountLoader<'info, Serum3Market>,
|
||||||
|
|
||||||
/// CHECK: Unused account
|
|
||||||
#[account(
|
#[account(
|
||||||
init,
|
init,
|
||||||
// block using the same market index twice
|
// block using the same market index twice
|
||||||
seeds = [b"Serum3Index".as_ref(), group.key().as_ref(), &market_index.to_le_bytes()],
|
seeds = [b"Serum3Index".as_ref(), group.key().as_ref(), &market_index.to_le_bytes()],
|
||||||
bump,
|
bump,
|
||||||
payer = payer,
|
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)]
|
#[account(has_one = group)]
|
||||||
pub quote_bank: AccountLoader<'info, Bank>,
|
pub quote_bank: AccountLoader<'info, Bank>,
|
||||||
|
@ -91,5 +90,12 @@ pub fn serum3_register_market(
|
||||||
reserved: [0; 128],
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -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_export]
|
||||||
macro_rules! serum_market_seeds {
|
macro_rules! serum_market_seeds {
|
||||||
( $acc:expr ) => {
|
( $acc:expr ) => {
|
||||||
|
|
|
@ -1397,26 +1397,7 @@ export type MangoV4 = {
|
||||||
{
|
{
|
||||||
"name": "indexReservation",
|
"name": "indexReservation",
|
||||||
"isMut": true,
|
"isMut": true,
|
||||||
"isSigner": false,
|
"isSigner": false
|
||||||
"pda": {
|
|
||||||
"seeds": [
|
|
||||||
{
|
|
||||||
"kind": "const",
|
|
||||||
"type": "string",
|
|
||||||
"value": "Serum3Index"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"kind": "account",
|
|
||||||
"type": "publicKey",
|
|
||||||
"path": "group"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"kind": "account",
|
|
||||||
"type": "publicKey",
|
|
||||||
"path": "serum_market"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "solDestination",
|
"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": [
|
"types": [
|
||||||
|
@ -6705,26 +6711,7 @@ export const IDL: MangoV4 = {
|
||||||
{
|
{
|
||||||
"name": "indexReservation",
|
"name": "indexReservation",
|
||||||
"isMut": true,
|
"isMut": true,
|
||||||
"isSigner": false,
|
"isSigner": false
|
||||||
"pda": {
|
|
||||||
"seeds": [
|
|
||||||
{
|
|
||||||
"kind": "const",
|
|
||||||
"type": "string",
|
|
||||||
"value": "Serum3Index"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"kind": "account",
|
|
||||||
"type": "publicKey",
|
|
||||||
"path": "group"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"kind": "account",
|
|
||||||
"type": "publicKey",
|
|
||||||
"path": "serum_market"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "solDestination",
|
"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": [
|
"types": [
|
||||||
|
|
Loading…
Reference in New Issue