In this exerecise, you are expected to implement a smart contract which performs a flash loan based liquidation.
-
The smart contract should allow you to perform a flash loan, a liquidation, and an asset exchange in one blockchain transaction.
-
To ease marking, we require your contract to provide a unified interface
run. You can encode the parameters needed intoparams. By callingrun, the flash loan, liquidation, and exchange should be executed properly.
function run(bytes calldata params) externalYou are expected to liquidate 0x on Aave at block 1234. Check out the original liquidation transaction.
Your grade is determined by the following metrics:
- The gas consumed to deploy your smart contract
- The gas consumed to execute your transaction
- The actual profit you earn in the test case
We provide the following background information for this exercise.
To trigger a liquidation on Aave, you need to call a public function liquidationCall provided by the Aave smart contracts. In the function, you can specify user representing the borrowing position you would like to liquidate, debtAsset, the cryptocurrency you would like to repay (let's say token D), and collateralAsset, the collateral cryptocurrency you would like claim from the borrowing position (let's say token C). You also specify the amount of debt you want to repay, debtToCover.
function liquidationCall(
address collateralAsset,
address debtAsset,
address user,
uint256 debtToCover,
bool receiveAToken
) external;By calling this function, you then repay some amount of token D to Aave and in return, some token C is sent to your account.
You should make sure that the user is in a liquidatable state. Otherwise, the aave smart contract would revert your transaction and you would pay transaction fees for an unsuccessful liquidation.
What if you don't have any upfront token D, but you do need some to repay in the liquidation? You can use flash loans! A Aave flash loan can grant you the cryptocurrenies available in the pool without any collateral as long as you repay in the same transaction. To use a flash loan, you can call the public function flashloan. Once the function is triggered, Aave smart contracts would send you the requested cryptocurrencies to the receiverAddress specified by you.
function flashLoan(
address receiverAddress,
address[] calldata assets,
uint256[] calldata amounts,
uint256[] calldata modes,
address onBehalfOf,
bytes calldata params,
uint16 referralCode
) external;Importantly, Aave would attempt to call into the receiver to invoke the function executeOperation after sending the flash loan assets. This means that you need a smart contract to accept a flash loan. The smart contract should have an executeOperation function and you can program how you use the flash loan assets in this function.
receiver.executeOperation(assets, amounts, premiums, msg.sender, params)Back to liquidation, you can program the liquidation logic in the executeOperation function. So, when you don't have enough token D to perform a liquidation, you can request a flash loan and your smart contract can do the liquidation after receiving token D.
With the flash loan, you now have enough token D. You can repay the debt for the borrowing position and claim the collateral token C. Congratulation! A successful liquidation is completed, but, wait, you still need to repay the flash loan. You now only have token C, while you need token D to repay the flash loan (as you borrow token D). One possible way is to exchange your token C for some token D through an on-chain market. For example, you could exchange all claimed token C for D on Uniswap. Parts of the exchanged token C are used to repay the flash loan. What left is the profit (deducting the transaction fee) you earn in this liquiation.