Client: add token_withdraw

This commit is contained in:
Christian Kamm 2022-08-10 09:30:43 +02:00
parent 7fcee1af96
commit f169bcdafe
1 changed files with 51 additions and 0 deletions

View File

@ -395,6 +395,57 @@ impl MangoClient {
.map_err(prettify_client_error)
}
pub fn token_withdraw(
&self,
mint: Pubkey,
amount: u64,
allow_borrow: bool,
) -> anyhow::Result<Signature> {
let token = self.context.token_by_mint(&mint)?;
let token_index = token.token_index;
let mint_info = token.mint_info;
let health_check_metas =
self.derive_health_check_remaining_account_metas(vec![token_index], false)?;
self.program()
.request()
.instruction(create_associated_token_account_idempotent(
&self.owner(),
&self.owner(),
&mint,
))
.instruction(Instruction {
program_id: mango_v4::id(),
accounts: {
let mut ams = anchor_lang::ToAccountMetas::to_account_metas(
&mango_v4::accounts::TokenWithdraw {
group: self.group(),
account: self.mango_account_address,
owner: self.owner(),
bank: mint_info.first_bank(),
vault: mint_info.first_vault(),
token_account: get_associated_token_address(
&self.owner(),
&mint_info.mint,
),
token_program: Token::id(),
},
None,
);
ams.extend(health_check_metas.into_iter());
ams
},
data: anchor_lang::InstructionData::data(&mango_v4::instruction::TokenWithdraw {
amount,
allow_borrow,
}),
})
.signer(&self.owner)
.send()
.map_err(prettify_client_error)
}
pub fn get_oracle_price(
&self,
token_name: &str,