Skip to main content

Utilize Flash Loans

Overview

The Flash Loan feature within the Chromatic Protocol empowers users to borrow tokens of a specific amount in a single transaction. This remarkable functionality grants temporary access to substantial liquidity without requiring borrowers to provide collateral. To facilitate flash loan callbacks, the contract responsible for implementing the flash loan functionality must also adhere to the IChromaticFlashLoanCallback interface. This interface ensures seamless handling of flash loan callbacks within the Chromatic Protocol.

Interface: IChromaticFlashLoanCallback

The IChromaticFlashLoanCallback interface defines the function signature necessary for managing flash loan callbacks in the Chromatic Protocol.

Functions

The IChromaticFlashLoanCallback interface encompasses the following function:

flashLoanCallback(uint256 fee, bytes calldata data) external

Manages the flash loan callback upon successful execution of a flash loan.

  • fee: Represents the fee amount charged for the flash loan.
  • data: Signifies additional data associated with the flash loan, passed when calling the flashLoan function in the ILendingPool interface.

Example Usage

To utilize the Flash Loan feature and handle the flash loan callback, a contract must implement both the ILendingPool interface for executing flash loans and the IChromaticFlashLoanCallback interface for handling the callback. Here's an example:

To effectively leverage the Flash Loan feature and appropriately handle flash loan callbacks, a contract must implement the IChromaticFlashLoanCallback interface. Consider the following example:

// Import the required interfaces
import {SafeERC20, IERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@chromatic-protocol/contracts/core/interfaces/vault/ILendingPool.sol";
import "@chromatic-protocol/contracts/core/interfaces/callback/IChromaticFlashLoanCallback.sol";

contract FlashLoanExample is IChromaticFlashLoanCallback {
ILendingPool lendingPool;

struct FlashLoanCallbackData {
address token;
address recipient;
uint256 amount;
}

constructor(address _lendingPoolAddress) {
lendingPool = ILendingPool(_lendingPoolAddress);
}

function executeFlashLoan(address token, uint256 amount, address recipient) external {
// Perform necessary operations before the flash loan

// Create callback data
bytes memory data = abi.encode(
FlashLoanCallbackData({token: token, recipient: msg.sender, amount: amount})
);

// Execute the flash loan
lendingPool.flashLoan(token, amount, recipient, data);

// Perform necessary operations after the flash loan
}

function flashLoanCallback(uint256 fee, bytes calldata data) external override {
// Handle the flash loan callback

// Access and utilize the `data` parameter for additional processing
FlashLoanCallbackData memory callbackData = abi.decode(data, (FlashLoanCallbackData));

// Perform necessary operations after the flash loan has been executed
SafeERC20.safeTransferFrom(
IERC20(callbackData.token),
callbackData.recipient,
address(lendingPool),
callbackData.amount + fee
);
}
}

Ensure that the flashLoanCallback function within your contract implements the logic required to manage the flash loan callback. Additionally, utilize the data parameter to retrieve any supplementary information passed during the flash loan execution.

By implementing the IChromaticFlashLoanCallback interface to manage flash loan callbacks, developers can seamlessly integrate flash loan functionality into their applications. This opens up avenues for innovative use cases within the Chromatic Protocol ecosystem.