Skip to main content

Flash Loan

Borrows tokens from the pool that must be repaid (with fee) in the same transaction via a callback.

Accounts

pair
Account<Pair>
required
The pair account.
token_vault
InterfaceAccount<TokenAccount>
required
The pair vault for the flash loan token.
user_token_account
InterfaceAccount<TokenAccount>
required
User’s token account. Will receive flash loan and must repay from here.
flashloan_receiver_program
Program
required
The program that will receive the flash loan callback. Must implement the flashloan receiver interface.
user
Signer
required
The user requesting the flash loan. Must be a signer.

Arguments

amount
u64
required
Amount to flash loan. Must be greater than 0.
receiver_instruction
Vec<u8>
required
Serialized instruction data to pass to the flashloan receiver program.
use anchor_lang::prelude::*;
use omnipair::program::Omnipair;

let flashloan_args = FlashloanArgs {
    amount: 10000 * 10u64.pow(token_decimals),
    receiver_instruction: receiver_ix_data,
};

let accounts = Flashloan {
    pair: pair_pda,
    token_vault: token1_vault,
    user_token_account: user_token1_account,
    flashloan_receiver_program: receiver_program_id,
    flashloan_receiver_accounts: receiver_accounts,
    user: user.key(),
    // ... other accounts
};

let ctx = CpiContext::new(program_id, accounts);
omnipair::cpi::flashloan(ctx, flashloan_args)?;
{
  "signature": "5VERv8NMvzbJMEkV8xnrLkEaWRtSz9CosKDYjCJjBRnbJLgp8uirBgmG5Rk88jH2k9f4w3JHFf7Pjsk3bWqeXrC",
  "slot": 123456789
}