5/5
# `FlashLev.close` exercise Write your code inside the [`FlashLev` contract](https://github.com/Cyfrin/defi-reth/blob/main/foundry/src/exercises/FlashLev.sol). This exercise is designed to test your understanding of how to structure a smart contract function for closing a leveraged position using a flash loan. ```solidity function close(CloseParams calldata params) external { // Write your code here } ``` ## Instructions 1. **Get debt owed to Aave** - Get the amount of debt owed to Aave > **Hint:** Look for a function inside `AaveHelper` to get the debt of this contract. 2. **Initiate a flash loan** - Encode flash loan data into `FlashLoanData`. - Call `flashLoan` to initiate a flash loan. > **Hint:** Look inside `AaveHelper` to see how to call the function `flashLoan`. # `_flashLoanCallback` ```solidity function _flashLoanCallback( address token, uint256 amount, uint256 fee, bytes memory params ) internal override { // Write your code here // Code for opening a position is omitted } ``` ## Instructions 1. **Implement logic for closing a position** - Implement flash loan callback logic for closing a position. > **Hint:** > > - Look into `AaveHelper` for how to repay stablecoin debt and withdraw collateral. > - Look into `SwapHelper` for how to swap tokens. 2. **Handle profit and loss** - If there is profit, transfer it to the caller that initiated a call to close. - Otherwise, there is a loss. Pull the loss from the caller to repay the flash loan. > **Hint:** Caller is stored in `FlashLoanData.caller` 3. **Repay flash loan** - Approve Aave to repay flash loan amount. > **Hint:** > > - Address to approve is `pool`, initialized inside `AaveHelper`. > - Amount to approve for repayment is amount borrowed + fee. ## Testing ```shell forge test --fork-url $FORK_URL --match-path test/exercise-aave-flash-lev.sol --match-test test_flashLev -vvv ```
FlashLev.close
exerciseWrite your code inside the FlashLev
contract.
This exercise is designed to test your understanding of how to structure a smart contract function for closing a leveraged position using a flash loan.
Get debt owed to Aave
Get the amount of debt owed to Aave
Hint: Look for a function inside
AaveHelper
to get the debt of this contract.
Initiate a flash loan
Encode flash loan data into FlashLoanData
.
Call flashLoan
to initiate a flash loan.
Hint: Look inside
AaveHelper
to see how to call the functionflashLoan
.
_flashLoanCallback
Implement logic for closing a position
Implement flash loan callback logic for closing a position.
Hint:
Look into
AaveHelper
for how to repay stablecoin debt and withdraw collateral.Look into
SwapHelper
for how to swap tokens.
Handle profit and loss
If there is profit, transfer it to the caller that initiated a call to close.
Otherwise, there is a loss. Pull the loss from the caller to repay the flash loan.
Hint: Caller is stored in
FlashLoanData.caller
Repay flash loan
Approve Aave to repay flash loan amount.
Hint:
Address to approve is
pool
, initialized insideAaveHelper
.Amount to approve for repayment is amount borrowed + fee.
An in-depth guide to `FlashLev.close` exercise - Implementing `close` function with flash loan callback to close a leveraged position. This lesson provides step-by-step instructions on how to get debt owed to Aave, initiate a flash loan using `AaveHelper`, handle profit and loss, and repay the flash loan within the `_flashLoanCallback` function.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Stay in the loop with the latest Rocket Pool updates and connect with over 5,000 developers in the community.
Course Overview
About the course
Rocket Pool rETH staking, yield mechanisms, trading options, and contract architecture
How to distribute rewards and calculate rETH/ETH exchange rate
How to acquire and redeem rETH
Integrating rETH into DeFi protocols like AAVE V3
How to how to create leveraged ETH positions using rETH
How to provide rETH as liquidity to Balancer and Aura
Getting the USD price of rETH using Rocket Pool NAV oracle
Re-staking with EigenLayer
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Blockchain Financial Analyst
$100,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on July 8, 2025
Duration: 4min
Duration: 48min
Duration: 57min
Duration: 30min
Duration: 7min
Duration: 23min
Duration: 6min
Course Overview
About the course
Rocket Pool rETH staking, yield mechanisms, trading options, and contract architecture
How to distribute rewards and calculate rETH/ETH exchange rate
How to acquire and redeem rETH
Integrating rETH into DeFi protocols like AAVE V3
How to how to create leveraged ETH positions using rETH
How to provide rETH as liquidity to Balancer and Aura
Getting the USD price of rETH using Rocket Pool NAV oracle
Re-staking with EigenLayer
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Blockchain Financial Analyst
$100,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on July 8, 2025