Serum deregister: Close index reservation account

Only safe because this is a testing-only instruction
This commit is contained in:
Christian Kamm 2022-09-01 12:11:43 +02:00
parent 1f0cd62846
commit c31f11013f
3 changed files with 84 additions and 2 deletions

View File

@ -20,6 +20,14 @@ 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
)]
pub index_reservation: UncheckedAccount<'info>,
#[account(mut)]
/// CHECK: target for account rent needs no checks
pub sol_destination: UncheckedAccount<'info>,
@ -27,6 +35,20 @@ pub struct Serum3DeregisterMarket<'info> {
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(())
}
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

@ -1387,7 +1387,7 @@ impl<'keypair> ClientInstruction for Serum3DeregisterMarketInstruction<'keypair>
type Instruction = mango_v4::instruction::Serum3DeregisterMarket;
async fn to_instruction(
&self,
_account_loader: impl ClientAccountLoader + 'async_trait,
account_loader: impl ClientAccountLoader + 'async_trait,
) -> (Self::Accounts, instruction::Instruction) {
let program_id = mango_v4::id();
let instruction = Self::Instruction {};
@ -1401,11 +1401,23 @@ impl<'keypair> ClientInstruction for Serum3DeregisterMarketInstruction<'keypair>
&program_id,
)
.0;
let serum_market_data: Serum3Market = account_loader.load(&serum_market).await.unwrap();
let index_reservation = Pubkey::find_program_address(
&[
b"Serum3Index".as_ref(),
self.group.as_ref(),
&serum_market_data.market_index.to_le_bytes(),
],
&program_id,
)
.0;
let accounts = Self::Accounts {
group: self.group,
admin: self.admin.pubkey(),
serum_market,
index_reservation,
sol_destination: self.sol_destination,
token_program: Token::id(),
};

View File

@ -1394,6 +1394,30 @@ export type MangoV4 = {
"isMut": true,
"isSigner": false
},
{
"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"
}
]
}
},
{
"name": "solDestination",
"isMut": true,
@ -6678,6 +6702,30 @@ export const IDL: MangoV4 = {
"isMut": true,
"isSigner": false
},
{
"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"
}
]
}
},
{
"name": "solDestination",
"isMut": true,