Understanding Flash Loan Smart Contract
Developer guide to execute Unilend Flashloan
Last updated
Developer guide to execute Unilend Flashloan
Last updated
SETTING UP FLASHLOAN
Please note that executing a flash loan is a complex transaction and the user should have a sound understanding of Solidity to fully comprehend the structure and models used within the smart contracts.
Overview of cycle to execute flash loan:
Your contract _receiver
calls the Unilend smart contract, requesting loan of certain _amount
from the _reserve
.
Unilend smart contract runs some sanity check and then transfer the _amount
from the _reserve
to your contract _receiver
.
Your contract _receiver
holding the _amount
execute implementation of your code.
Once your code is executed you transfer the flash loan _amount
to _reserve
along with _fee
.
Parameters
Type
Defination of parameters
_receiver
address
address of the contract receiving the funds.
_reserve
address
addresses of the reserves to flash loan
_amount
uint256
amounts of _asset
to flashloan.
_fee
uint256
premium incured over the loan _amount
_params
bytes
bytes-encoded parameters to be used by the _receiver
contract
To develop your own flash loan contract, start building contract interface over Unilend library UnilendFlashLoanReceiverBase
2. Executing flash loan
Create a executeOperation()
function within the contract for implementing your arbitary logic and requesting fund with local function transferInternal()
3. Calling flash loan
Define a flashloanCall()
function inside your smart contract taking relevant parameters as _asset
and _amount
that would be passed to local function flashLoan()
of UnilendFlashLoanRecieverBase
4. Completing flash loan transaction
Once you have performed your logic with the flash loaned assets (in your executeOperation()
function), you will need to pay back the flash loaned amounts. In the payback cycle ensure that *your contract* has enough of funds to pay the _fee