mango-v4/programs/mango-v4/src/instructions/token_withdraw.rs

195 lines
6.5 KiB
Rust

use crate::accounts_zerocopy::*;
use crate::error::*;
use crate::health::*;
use crate::state::*;
use anchor_lang::prelude::*;
use anchor_spl::token;
use anchor_spl::token::Token;
use anchor_spl::token::TokenAccount;
use fixed::types::I80F48;
use crate::logs::{
LoanOriginationFeeInstruction, TokenBalanceLog, WithdrawLoanOriginationFeeLog, WithdrawLog,
};
use crate::util::checked_math as cm;
#[derive(Accounts)]
pub struct TokenWithdraw<'info> {
pub group: AccountLoader<'info, Group>,
#[account(mut, has_one = group, has_one = owner)]
pub account: AccountLoader<'info, MangoAccountFixed>,
pub owner: Signer<'info>,
#[account(
mut,
has_one = group,
has_one = vault,
has_one = oracle,
// the mints of bank/vault/token_account are implicitly the same because
// spl::token::transfer succeeds between token_account and vault
)]
pub bank: AccountLoader<'info, Bank>,
#[account(mut)]
pub vault: Account<'info, TokenAccount>,
/// CHECK: The oracle can be one of several different account types
pub oracle: UncheckedAccount<'info>,
#[account(mut)]
pub token_account: Box<Account<'info, TokenAccount>>,
pub token_program: Program<'info, Token>,
}
impl<'info> TokenWithdraw<'info> {
pub fn transfer_ctx(&self) -> CpiContext<'_, '_, '_, 'info, token::Transfer<'info>> {
let program = self.token_program.to_account_info();
let accounts = token::Transfer {
from: self.vault.to_account_info(),
to: self.token_account.to_account_info(),
authority: self.group.to_account_info(),
};
CpiContext::new(program, accounts)
}
}
pub fn token_withdraw(ctx: Context<TokenWithdraw>, amount: u64, allow_borrow: bool) -> Result<()> {
require_msg!(amount > 0, "withdraw amount must be positive");
let group = ctx.accounts.group.load()?;
let token_index = ctx.accounts.bank.load()?.token_index;
// Create the account's position for that token index
let mut account = ctx.accounts.account.load_full_mut()?;
let (_, raw_token_index, _) = account.ensure_token_position(token_index)?;
// Health check _after_ the token position is guaranteed to exist
let pre_health_opt = if !account.fixed.is_in_health_region() {
let retriever =
new_fixed_order_account_retriever(ctx.remaining_accounts, &account.borrow())?;
let health_cache =
new_health_cache(&account.borrow(), &retriever).context("pre-withdraw init health")?;
let pre_health = account.check_health_pre(&health_cache)?;
Some((health_cache, pre_health))
} else {
None
};
let mut bank = ctx.accounts.bank.load_mut()?;
let position = account.token_position_mut_by_raw_index(raw_token_index);
let native_position = position.native(&bank);
// Handle amount special case for withdrawing everything
let amount = if amount == u64::MAX && !allow_borrow {
if native_position.is_positive() {
// TODO: This rounding may mean that if we deposit and immediately withdraw
// we can't withdraw the full amount!
native_position.floor().to_num::<u64>()
} else {
return Ok(());
}
} else {
amount
};
let is_borrow = amount > native_position;
require!(allow_borrow || !is_borrow, MangoError::SomeError);
if bank.is_reduce_only() {
require!(!is_borrow, MangoError::TokenInReduceOnlyMode);
}
let amount_i80f48 = I80F48::from(amount);
let now_slot = Clock::get()?.slot;
let oracle_price = bank.oracle_price(
&AccountInfoRef::borrow(ctx.accounts.oracle.as_ref())?,
Some(now_slot),
)?;
// Update the bank and position
let (position_is_active, loan_origination_fee) = bank.withdraw_with_fee(
position,
amount_i80f48,
Clock::get()?.unix_timestamp.try_into().unwrap(),
oracle_price,
)?;
// Provide a readable error message in case the vault doesn't have enough tokens
if ctx.accounts.vault.amount < amount {
return err!(MangoError::InsufficentBankVaultFunds).with_context(|| {
format!(
"bank vault does not have enough tokens, need {} but have {}",
amount, ctx.accounts.vault.amount
)
});
}
// Transfer the actual tokens
let group_seeds = group_seeds!(group);
token::transfer(
ctx.accounts.transfer_ctx().with_signer(&[group_seeds]),
amount,
)?;
let native_position_after = position.native(&bank);
emit!(TokenBalanceLog {
mango_group: ctx.accounts.group.key(),
mango_account: ctx.accounts.account.key(),
token_index,
indexed_position: position.indexed_position.to_bits(),
deposit_index: bank.deposit_index.to_bits(),
borrow_index: bank.borrow_index.to_bits(),
});
// Update the net deposits - adjust by price so different tokens are on the same basis (in USD terms)
let amount_usd = cm!(amount_i80f48 * oracle_price).to_num::<i64>();
cm!(account.fixed.net_deposits -= amount_usd);
//
// Health check
//
if let Some((mut health_cache, pre_health)) = pre_health_opt {
health_cache.adjust_token_balance(&bank, cm!(native_position_after - native_position))?;
account.check_health_post(&health_cache, pre_health)?;
}
//
// Deactivate the position only after the health check because the user passed in
// remaining_accounts for all banks/oracles, including the account that will now be
// deactivated.
//
if !position_is_active {
account.deactivate_token_position_and_log(raw_token_index, ctx.accounts.account.key());
}
emit!(WithdrawLog {
mango_group: ctx.accounts.group.key(),
mango_account: ctx.accounts.account.key(),
signer: ctx.accounts.owner.key(),
token_index,
quantity: amount,
price: oracle_price.to_bits(),
});
if loan_origination_fee.is_positive() {
emit!(WithdrawLoanOriginationFeeLog {
mango_group: ctx.accounts.group.key(),
mango_account: ctx.accounts.account.key(),
token_index,
loan_origination_fee: loan_origination_fee.to_bits(),
instruction: LoanOriginationFeeInstruction::TokenWithdraw,
});
}
// Enforce min vault to deposits ratio
if is_borrow {
ctx.accounts.vault.reload()?;
bank.enforce_min_vault_to_deposits_ratio(ctx.accounts.vault.as_ref())?;
}
Ok(())
}