Flash Loan
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.
Application of Flash Loans
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 Lista loan liquidation, 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.
Involved entities
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) Lista destablecoins, with a fee, in one transaction. EOA needs to interact with their own deployed copy of flashBorrower, which in turn interacts with flashLender.
Flash Loan Fee
To get the fee, call the flashFee(address token, uint256 amount)
function that returns the fee amount in 18 Decimals.
Code example
flashLender — flash.sol
flashBorrower — flashBorrower.sol
Step-by-step
1. Set up your flashBorrower contract
Your contract must conform to the ERC3156FlashBorrower interface by implementing the onFlashLoan()
function.
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.
The flashBorrower must implement the IERC3156FlashBorrower, and onFlashLoan() must return the CALLBACK_SUCCESS hash.
2. Understand how to interact with flashLender
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 theflashLoan()
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 tokenamount
toreceiver
with extra data (if any), and expects a return equal toCALLBACK_SUCCESS
.function accrue()
— sends the surplus fee to vow.sol.
If you're curious, understand the MakerDao parameters/constants used in the flash.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.
For a deeper understanding of the MakerDao contract, such as var, hay, vow, etc, start with the vat docs and proceed to other smart contracts documented there.
3. Interact with flashLender
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, thenonFlashLoan()
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.
The flashBorrower must implement the I, and onFlashLoan() must return the CALLBACK_SUCCESS hash.
Close-to-life usage example
Look into the tests to find a close-to-life usage example.
Last updated