Add CloseAccount instruction to make testing easier

This commit is contained in:
Christian Kamm 2022-03-31 17:22:56 +02:00
parent ad3ad37417
commit 8038f9e257
5 changed files with 80 additions and 1 deletions

View File

@ -0,0 +1,26 @@
use anchor_lang::prelude::*;
use anchor_spl::token::Token;
use crate::state::*;
#[derive(Accounts)]
pub struct CloseAccount<'info> {
#[account(
mut,
has_one = owner,
close = sol_destination
)]
pub account: AccountLoader<'info, MangoAccount>,
pub owner: Signer<'info>,
#[account(mut)]
pub sol_destination: UncheckedAccount<'info>,
pub token_program: Program<'info, Token>,
}
pub fn close_account(_ctx: Context<CloseAccount>) -> Result<()> {
// CRITICAL: currently can close any account, even one with bad health
// TODO: Implement
Ok(())
}

View File

@ -1,4 +1,5 @@
pub use self::margin_trade::*;
pub use close_account::*;
pub use consume_events::*;
pub use create_account::*;
pub use create_group::*;
@ -17,6 +18,7 @@ pub use serum3_settle_funds::*;
pub use set_stub_oracle::*;
pub use withdraw::*;
mod close_account;
mod consume_events;
mod create_account;
mod create_group;

View File

@ -57,6 +57,10 @@ pub mod mango_v4 {
instructions::create_account(ctx, account_num)
}
pub fn close_account(ctx: Context<CloseAccount>) -> Result<()> {
instructions::close_account(ctx)
}
// todo:
// ckamm: generally, using an I80F48 arg will make it harder to call
// because generic anchor clients won't know how to deal with it

View File

@ -697,6 +697,38 @@ impl<'keypair> ClientInstruction for CreateAccountInstruction<'keypair> {
}
}
pub struct CloseAccountInstruction<'keypair> {
pub account: Pubkey,
pub owner: &'keypair Keypair,
pub sol_destination: Pubkey,
}
#[async_trait::async_trait(?Send)]
impl<'keypair> ClientInstruction for CloseAccountInstruction<'keypair> {
type Accounts = mango_v4::accounts::CloseAccount;
type Instruction = mango_v4::instruction::CloseAccount;
async fn to_instruction(
&self,
_account_loader: impl ClientAccountLoader + 'async_trait,
) -> (Self::Accounts, instruction::Instruction) {
let program_id = mango_v4::id();
let instruction = Self::Instruction {};
let accounts = Self::Accounts {
owner: self.owner.pubkey(),
account: self.account,
sol_destination: self.sol_destination,
token_program: Token::id(),
};
let instruction = make_instruction(program_id, &accounts, instruction);
(accounts, instruction)
}
fn signers(&self) -> Vec<&Keypair> {
vec![self.owner]
}
}
pub struct Serum3RegisterMarketInstruction<'keypair> {
pub group: Pubkey,
pub admin: &'keypair Keypair,

View File

@ -2,7 +2,7 @@
use fixed::types::I80F48;
use solana_program_test::*;
use solana_sdk::{signature::Keypair, transport::TransportError};
use solana_sdk::{signature::Keypair, signature::Signer, transport::TransportError};
use mango_v4::state::*;
use program_test::*;
@ -121,5 +121,20 @@ async fn test_basic() -> Result<(), TransportError> {
);
}
//
// TEST: Close account
// TODO: This just checks execution, preconditions etc need to be tested!
//
send_tx(
solana,
CloseAccountInstruction {
account,
owner,
sol_destination: payer.pubkey(),
},
)
.await
.unwrap();
Ok(())
}