Skip to content

Commit

Permalink
FlashLoan: Allow mango ix after flash loan end
Browse files Browse the repository at this point in the history
This lifts an unnecessary restriction, making it possible to do things
like flash loan and then liquidate with the resulting balances in a
single transaction.

The "no mango ix after FlashLoanEnd" limitation was unnecessary:
After the flash loan ends, the mango accounts are in a valid state
again. And it's impossible to use a two FlashLoanEnd for a single
FlashLoanBegin for the same reason that isolated FlashLoanEnds are
rejected.
  • Loading branch information
ckamm committed Aug 16, 2023
1 parent 27097fa commit d29f7bc
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions programs/mango-v4/src/instructions/flash_loan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,6 @@ pub fn flash_loan_begin<'key, 'accounts, 'remaining, 'info>(

// Check that the mango program key is not used
if ix.program_id == crate::id() {
// must be the last mango ix -- this could possibly be relaxed, but right now
// we need to guard against multiple FlashLoanEnds
require_msg!(
!found_end,
"the transaction must not contain a Mango instruction after FlashLoanEnd"
);
found_end = true;

// must be the FlashLoanEnd instruction
require!(
ix.data[0..8] == crate::instruction::FlashLoanEndV2::discriminator(),
Expand All @@ -173,6 +165,13 @@ pub fn flash_loan_begin<'key, 'accounts, 'remaining, 'info>(
for (begin_account, end_account) in begin_accounts.iter().zip(end_accounts.iter()) {
require_msg!(*begin_account.key == end_account.pubkey, "the trailing vault, token and group accounts passed to FlashLoanBegin and End must match, found {} on begin and {} on end", begin_account.key, end_account.pubkey);
}

// No need to check any instructions after the end instruction.
// "Duplicate FlashLoanEnd" is guarded against the same way as "End without Begin":
// The End instruction requires at least one bank-vault pair and that bank
// must have flash_loan_token_account_initial set - which only happens in Begin.
found_end = true;
break;
} else {
// ensure no one can cpi into mango either
for meta in ix.accounts.iter() {
Expand Down

0 comments on commit d29f7bc

Please sign in to comment.