Flash Loan
Last updated
Last updated
Flash Loans are uncollateralized loans that allow the user to borrow lisUSD as long as the borrowed amount (and a fee) is returned before the end of the transaction.
To use Flash Loans and get profit from them, you need a good understanding of BNB Chain (and Smart Chain), programming, and smart contracts.
There are various applications of Flash Loans.
An obvious example is arbitrage between assets, where the user can flash-loan lisUSD to purchase BNB in a Dutch auction that happens during somebody's , immediately swap lisUSD for another asset on a DEX, then immediately swap the obtained asset for lisUSD on another DEX where the asset's ratio is higher, and repay Lista the flash loan + interest, keeping the difference — all within one loan transaction.
flashLender — Lista smart contract implementing the "server side" of the flash loans functionality.
flashBorrower — the smart contract implementing the "client side", which EOA can copy, modify, and deploy to interact through with flashLender. A stub example of the smart contract is available later on this page.
EOA (Externally Owned Account) — an account that interacts with the copy of flashBorrower they deploy. Effectively, EOA is a developer who copies flashBorrower, modifies, and deploys it on BSC to interact with flashLender through.
flashLender can be used to borrow (mint) and repay (burn) , with a fee, in one transaction. EOA needs to interact with their own deployed copy of flashBorrower, which in turn interacts with flashLender.
To get the fee, call the flashFee(address token, uint256 amount)
function that returns the fee amount in 18 Decimals.
flashLender —
flashBorrower —
To interact with flashLender, your contract must implement flashBorrow(token, amount)
and onFlashLoan(initiator, token, amount, fee, data)
, which is a callback function called during the execution of flashLoan()
.
Implement any custom logic within onFlashLoan()
.
Here's an example stub contract you can look through to better understand how to implement flashBorrower.
Understand the parameters used in flashLender:
CALLBACK_SUCCESS
— Hash of custom string, returned on success.
token
(address) — address of the BNB ERC-20 token that EOA flash-loans.
amount
(uint256) — amount of the flash loan.
receiver
(IERC3156FlashBorrower) — address of the flashBorrowed deployed by the EOA.
data
(bytes calldata) — rudimentary non-used parameter left not to change the flashLoan()
signature.
Understand the functions you want to interact with:
maxFlashLoan(address token)
— returns “max” if token is supported destablecoin.
flashFee(address token, uint256 amount)
— applies “toll” on amount and returns if token is a supported destablecoin.
flashLoan(IERC3156FlashBorrower receiver, address token, uint256 amount, bytes calldata data)
— mints token amount
to receiver
with extra data (if any), and expects a return equal to CALLBACK_SUCCESS
.
function accrue()
— sends the surplus fee to vow.sol.
vat
— Address of vat.sol.
hayJoin
— Address of hayJoin.sol.
hay
— Address of hay.sol.
vow
— Address of vow.sol.
max
— Maximum allowed borrowable amount.
toll
— Fee on return of loan.
WAD
— 18 Decimals.
RAY
— 27 Decimals.
RAD
— 45 Decimals.
CALLBACK_SUCCESS
— Hash of custom string, returned on success.
flashLender is available by the following addresses:
Testnet — coming soon
A typical interaction follows this workflow:
An EOA calls flashBorrow(token, amount)
on a borrowing contract flashBorrower.sol.
flashBorrower approves flashLender in advance to repay the loan with a fee. Then it calls the flashLoan(receiver, token, amount, data)
function on flashLender which mints a specified amount to the flashBorrower.
The same function flashLoan(receiver, token, amount, data) then calls (CALLBACK) the the onFlashLoan(initiator, token, amount, fee, data)
function on the flashBorrower, which implements the custom logic of whatever the EOA wants to do with the borrowed lisUSD, then onFlashLoan()
returns the KECCAK256 of “ERC3156FlashBorrower.onFlashLoan
" if its execution was successful. The custom logic is thought-up and implemented completely by the EOA.
flashLender then burns the minted loan and stores the fee as surplus.
Your contract must conform to the interface by implementing the onFlashLoan()
function.
The flashBorrower must implement the , and must return the hash.
If you're curious, understand the MakerDao parameters/constants used in the .
For a deeper understanding of the MakerDao contract, such as var, hay, vow, etc, start with the and proceed to other smart contracts documented there.
Mainnet —
The flashBorrower must implement the , and must return the hash.
Look into to find a close-to-life usage example.