fix name and error message check in flash loan (#145)
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
parent
2880a2ec88
commit
59d28c97a1
|
@ -77,19 +77,20 @@ pub fn flash_loan_begin<'key, 'accounts, 'remaining, 'info>(
|
||||||
require_keys_eq!(bank.group, ctx.accounts.group.key());
|
require_keys_eq!(bank.group, ctx.accounts.group.key());
|
||||||
require_keys_eq!(bank.vault, *vault_ai.key);
|
require_keys_eq!(bank.vault, *vault_ai.key);
|
||||||
|
|
||||||
|
let vault = Account::<TokenAccount>::try_from(vault_ai)?;
|
||||||
let token_account = Account::<TokenAccount>::try_from(token_account_ai)?;
|
let token_account = Account::<TokenAccount>::try_from(token_account_ai)?;
|
||||||
|
|
||||||
bank.flash_loan_approved_amount = *amount;
|
bank.flash_loan_approved_amount = *amount;
|
||||||
bank.flash_loan_vault_initial = token_account.amount;
|
bank.flash_loan_token_account_initial = token_account.amount;
|
||||||
|
|
||||||
// Transfer the loaned funds
|
// Transfer the loaned funds
|
||||||
if *amount > 0 {
|
if *amount > 0 {
|
||||||
// Provide a readable error message in case the vault doesn't have enough tokens
|
// Provide a readable error message in case the vault doesn't have enough tokens
|
||||||
if token_account.amount < *amount {
|
if vault.amount < *amount {
|
||||||
return err!(MangoError::InsufficentBankVaultFunds).with_context(|| {
|
return err!(MangoError::InsufficentBankVaultFunds).with_context(|| {
|
||||||
format!(
|
format!(
|
||||||
"bank vault {} does not have enough tokens, need {} but have {}",
|
"bank vault {} does not have enough tokens, need {} but have {}",
|
||||||
vault_ai.key, amount, token_account.amount
|
vault_ai.key, amount, vault.amount
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -235,7 +236,7 @@ pub fn flash_loan_end<'key, 'accounts, 'remaining, 'info>(
|
||||||
// The Begin instruction only checks that End ends with the same vault accounts -
|
// The Begin instruction only checks that End ends with the same vault accounts -
|
||||||
// but there could be an extra vault account in End, or a different bank could be
|
// but there could be an extra vault account in End, or a different bank could be
|
||||||
// used for the same vault.
|
// used for the same vault.
|
||||||
require_neq!(bank.flash_loan_vault_initial, u64::MAX);
|
require_neq!(bank.flash_loan_token_account_initial, u64::MAX);
|
||||||
|
|
||||||
// Create the token position now, so we can compute the pre-health with fixed order health accounts
|
// Create the token position now, so we can compute the pre-health with fixed order health accounts
|
||||||
let (_, raw_token_index, _) = account.token_get_mut_or_create(bank.token_index)?;
|
let (_, raw_token_index, _) = account.token_get_mut_or_create(bank.token_index)?;
|
||||||
|
@ -243,7 +244,7 @@ pub fn flash_loan_end<'key, 'accounts, 'remaining, 'info>(
|
||||||
// Transfer any excess over the inital balance of the token account back
|
// Transfer any excess over the inital balance of the token account back
|
||||||
// into the vault. Compute the total change in the vault balance.
|
// into the vault. Compute the total change in the vault balance.
|
||||||
let mut change = -I80F48::from(bank.flash_loan_approved_amount);
|
let mut change = -I80F48::from(bank.flash_loan_approved_amount);
|
||||||
if token_account.amount > bank.flash_loan_vault_initial {
|
if token_account.amount > bank.flash_loan_token_account_initial {
|
||||||
let transfer_ctx = CpiContext::new(
|
let transfer_ctx = CpiContext::new(
|
||||||
ctx.accounts.token_program.to_account_info(),
|
ctx.accounts.token_program.to_account_info(),
|
||||||
token::Transfer {
|
token::Transfer {
|
||||||
|
@ -252,7 +253,7 @@ pub fn flash_loan_end<'key, 'accounts, 'remaining, 'info>(
|
||||||
authority: ctx.accounts.owner.to_account_info(),
|
authority: ctx.accounts.owner.to_account_info(),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
let repay = token_account.amount - bank.flash_loan_vault_initial;
|
let repay = token_account.amount - bank.flash_loan_token_account_initial;
|
||||||
token::transfer(transfer_ctx, repay)?;
|
token::transfer(transfer_ctx, repay)?;
|
||||||
|
|
||||||
let repay = I80F48::from(repay);
|
let repay = I80F48::from(repay);
|
||||||
|
@ -324,7 +325,7 @@ pub fn flash_loan_end<'key, 'accounts, 'remaining, 'info>(
|
||||||
}
|
}
|
||||||
|
|
||||||
bank.flash_loan_approved_amount = 0;
|
bank.flash_loan_approved_amount = 0;
|
||||||
bank.flash_loan_vault_initial = u64::MAX;
|
bank.flash_loan_token_account_initial = u64::MAX;
|
||||||
|
|
||||||
token_loan_details.push(FlashLoanTokenDetail {
|
token_loan_details.push(FlashLoanTokenDetail {
|
||||||
token_index: position.token_index,
|
token_index: position.token_index,
|
||||||
|
|
|
@ -109,7 +109,7 @@ pub fn token_edit(
|
||||||
|
|
||||||
// unchanged -
|
// unchanged -
|
||||||
// dust
|
// dust
|
||||||
// flash_loan_vault_initial
|
// flash_loan_token_account_initial
|
||||||
// flash_loan_approved_amount
|
// flash_loan_approved_amount
|
||||||
// token_index
|
// token_index
|
||||||
// bump
|
// bump
|
||||||
|
|
|
@ -131,7 +131,7 @@ pub fn token_register(
|
||||||
init_liab_weight: I80F48::from_num(init_liab_weight),
|
init_liab_weight: I80F48::from_num(init_liab_weight),
|
||||||
liquidation_fee: I80F48::from_num(liquidation_fee),
|
liquidation_fee: I80F48::from_num(liquidation_fee),
|
||||||
dust: I80F48::ZERO,
|
dust: I80F48::ZERO,
|
||||||
flash_loan_vault_initial: u64::MAX,
|
flash_loan_token_account_initial: u64::MAX,
|
||||||
flash_loan_approved_amount: 0,
|
flash_loan_approved_amount: 0,
|
||||||
token_index,
|
token_index,
|
||||||
bump: *ctx.bumps.get("bank").ok_or(MangoError::SomeError)?,
|
bump: *ctx.bumps.get("bank").ok_or(MangoError::SomeError)?,
|
||||||
|
|
|
@ -103,7 +103,7 @@ pub fn token_register_trustless(
|
||||||
init_liab_weight: I80F48::from_num(1.5),
|
init_liab_weight: I80F48::from_num(1.5),
|
||||||
liquidation_fee: I80F48::from_num(0.125),
|
liquidation_fee: I80F48::from_num(0.125),
|
||||||
dust: I80F48::ZERO,
|
dust: I80F48::ZERO,
|
||||||
flash_loan_vault_initial: u64::MAX,
|
flash_loan_token_account_initial: u64::MAX,
|
||||||
flash_loan_approved_amount: 0,
|
flash_loan_approved_amount: 0,
|
||||||
token_index,
|
token_index,
|
||||||
bump: *ctx.bumps.get("bank").ok_or(MangoError::SomeError)?,
|
bump: *ctx.bumps.get("bank").ok_or(MangoError::SomeError)?,
|
||||||
|
|
|
@ -82,7 +82,7 @@ pub struct Bank {
|
||||||
// Collection of all fractions-of-native-tokens that got rounded away
|
// Collection of all fractions-of-native-tokens that got rounded away
|
||||||
pub dust: I80F48,
|
pub dust: I80F48,
|
||||||
|
|
||||||
pub flash_loan_vault_initial: u64,
|
pub flash_loan_token_account_initial: u64,
|
||||||
pub flash_loan_approved_amount: u64,
|
pub flash_loan_approved_amount: u64,
|
||||||
|
|
||||||
// Index into TokenInfo on the group
|
// Index into TokenInfo on the group
|
||||||
|
@ -145,7 +145,10 @@ impl std::fmt::Debug for Bank {
|
||||||
"flash_loan_approved_amount",
|
"flash_loan_approved_amount",
|
||||||
&self.flash_loan_approved_amount,
|
&self.flash_loan_approved_amount,
|
||||||
)
|
)
|
||||||
.field("flash_loan_vault_initial", &self.flash_loan_vault_initial)
|
.field(
|
||||||
|
"flash_loan_token_account_initial",
|
||||||
|
&self.flash_loan_token_account_initial,
|
||||||
|
)
|
||||||
.field("reserved", &self.reserved)
|
.field("reserved", &self.reserved)
|
||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
|
@ -185,7 +188,7 @@ impl Bank {
|
||||||
liquidation_fee: existing_bank.liquidation_fee,
|
liquidation_fee: existing_bank.liquidation_fee,
|
||||||
dust: I80F48::ZERO,
|
dust: I80F48::ZERO,
|
||||||
flash_loan_approved_amount: 0,
|
flash_loan_approved_amount: 0,
|
||||||
flash_loan_vault_initial: u64::MAX,
|
flash_loan_token_account_initial: u64::MAX,
|
||||||
token_index: existing_bank.token_index,
|
token_index: existing_bank.token_index,
|
||||||
bump: existing_bank.bump,
|
bump: existing_bank.bump,
|
||||||
mint_decimals: existing_bank.mint_decimals,
|
mint_decimals: existing_bank.mint_decimals,
|
||||||
|
|
|
@ -63,7 +63,7 @@ export class Bank {
|
||||||
initLiabWeight: I80F48Dto;
|
initLiabWeight: I80F48Dto;
|
||||||
liquidationFee: I80F48Dto;
|
liquidationFee: I80F48Dto;
|
||||||
dust: I80F48Dto;
|
dust: I80F48Dto;
|
||||||
flashLoanVaultInitial: BN;
|
flashLoanTokenAccountInitial: BN;
|
||||||
flashLoanApprovedAmount: BN;
|
flashLoanApprovedAmount: BN;
|
||||||
tokenIndex: number;
|
tokenIndex: number;
|
||||||
mintDecimals: number;
|
mintDecimals: number;
|
||||||
|
@ -102,7 +102,7 @@ export class Bank {
|
||||||
obj.initLiabWeight,
|
obj.initLiabWeight,
|
||||||
obj.liquidationFee,
|
obj.liquidationFee,
|
||||||
obj.dust,
|
obj.dust,
|
||||||
obj.flashLoanVaultInitial,
|
obj.flashLoanTokenAccountInitial,
|
||||||
obj.flashLoanApprovedAmount,
|
obj.flashLoanApprovedAmount,
|
||||||
obj.tokenIndex,
|
obj.tokenIndex,
|
||||||
obj.mintDecimals,
|
obj.mintDecimals,
|
||||||
|
@ -142,7 +142,7 @@ export class Bank {
|
||||||
initLiabWeight: I80F48Dto,
|
initLiabWeight: I80F48Dto,
|
||||||
liquidationFee: I80F48Dto,
|
liquidationFee: I80F48Dto,
|
||||||
dust: I80F48Dto,
|
dust: I80F48Dto,
|
||||||
flashLoanVaultInitial: BN,
|
flashLoanTokenAccountInitial: BN,
|
||||||
flashLoanApprovedAmount: BN,
|
flashLoanApprovedAmount: BN,
|
||||||
public tokenIndex: number,
|
public tokenIndex: number,
|
||||||
public mintDecimals: number,
|
public mintDecimals: number,
|
||||||
|
|
|
@ -2830,7 +2830,7 @@ export type MangoV4 = {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "flashLoanVaultInitial",
|
"name": "flashLoanTokenAccountInitial",
|
||||||
"type": "u64"
|
"type": "u64"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -7843,7 +7843,7 @@ export const IDL: MangoV4 = {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "flashLoanVaultInitial",
|
"name": "flashLoanTokenAccountInitial",
|
||||||
"type": "u64"
|
"type": "u64"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue