Skip to main content

Flash Loans

1

Import Dependencies

Import the required packages for Solana RPC communication, Kamino SDK operations, and Kit transaction building.
2

Setup Market and Signer

Load your keypair, initialize RPC connections, and load the Kamino market.
3

Configure Flash Loan Parameters

Set the token, the flash loan amount in lamports (1 USDC), and the wallet’s token account that receives it.
4

Build Flash Loan Instructions

Generate the flash borrow and flash repay instructions using the Kamino SDK.
borrowIxIndex: 0 says that the flash borrow instruction is at index 0 in the transaction. The SDK uses this to validate that repayment occurs in the same transaction.
The flash repay takes the amount plus the reserve’s flash loan fee (reserve.state.config.fees.flashLoanFeeSf, 0.001% for USDC on the SOL/BTC Market). The wallet must already hold enough USDC in that token account to pay the fee, or the repay fails with insufficient funds.
5

Add Your Custom Instructions

Create your custom instructions to execute with the borrowed funds. In this example, we use a simple memo instruction.
6

Build and Send Transaction

Build the transaction with a fresh blockhash and send it to the network.
Your flash loan transaction is complete! The borrowed funds were used for your operations and automatically repaid within the same atomic transaction.