1/5
We will be finishing our rebalance of our USDC and WETH tokens. ```python def print_token_balances( ): print (f"WETH BALANCE: {weth.balanceOf( boa.env.eoa )}") print (f"aWETH BALANCE: {a_weth.balanceOf( boa.env.eoa )}") print (f"aUSDC BALANCE: {a_usdc.balanceOf( boa.env.eoa )}") print (f"aAMETH BALANCE: {a_ameth.balanceOf( boa.env.eoa )}") ``` We wrote a little script to handle our deposit. Let's create our deposit function: ```python def deposit_pool_contract (pool_contract, token, amount ): allowed_amount = token.allowance(boa.env.eoa, pool_contract.address ) if allowed_amount < amount: token.approve (pool_contract.address, amount ) print (f"Approving {token.name()} into Aave contract: {pool_contract.address}") print (f"Depositing {token.name()} into Aave contract: {pool_contract.address}") pool_contract.supply( token.address, amount, boa.env.eoa, REFERRAL_CODE ) ``` ```python REFERRAL_CODE = 0 ``` Now, let's get our token balances. ```python print_token_balances( ) ``` We will grab the USDC token balance and use it to deposit into our pool contract. ```python amount = usdc.balanceOf( boa.env.eoa ) deposit_pool_contract (pool_contract, usdc, amount) ``` We will do the same for WETH: ```python print_token_balances( ) a_usdc_balance = a_usdc.balanceOf( boa.env.eoa ) a_weth_balance = a_weth.balanceOf( boa.env.eoa ) a_usdc_balance_normalized = a_usdc_balance / (1000 * 1000) a_weth_balance_normalized = a_weth_balance / (1000 * 1000 * 1000 * 1000 * 1000 * 1000) usdc_value = a_usdc_balance_normalized * usdc_price weth_value = a_weth_balance_normalized * weth_price total_value = usdc_value + weth_value target_usdc_value = 0.3 target_weth_value = 0.7 weth_percent_allocation = weth_value / (usdc_value + weth_value) usdc_percent_allocation = usdc_value / (usdc_value + weth_value) print (f"Current percent allocation of USDC: {usdc_percent_allocation}") print (f"Current percent allocation of WETH: {weth_percent_allocation}") ``` We will go ahead and deposit our WETH now. ```python amount = weth.balanceOf( boa.env.eoa ) deposit_pool_contract(pool_contract, weth, amount) ``` We successfully rebalanced our portfolio!
A comprehensive guide to rebalancing a USDC/WETH portfolio in Aave V3 - The lesson covers calculating the needed trades to rebalance a portfolio, depositing USDC and WETH tokens, and checking the current allocation percentages of USDC and WETH.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
How to build a DeFi stablecoin and customized NFT
How to deploy your smart contract on ZKsync with Moccasin
Advanced testing techniques like stateful and stateless Python fuzzing
How to write algorithmic trading scripts in Python
Hashing signatures, proxies, delegate calls, upgradable contracts, random numbers, and more!
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
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)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on April 21, 2025
Duration: 2h 21min
Duration: 1h 58min
Duration: 2h 55min
Duration: 1h 55min
Duration: 46min
Course Overview
About the course
How to build a DeFi stablecoin and customized NFT
How to deploy your smart contract on ZKsync with Moccasin
Advanced testing techniques like stateful and stateless Python fuzzing
How to write algorithmic trading scripts in Python
Hashing signatures, proxies, delegate calls, upgradable contracts, random numbers, and more!
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
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)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on April 21, 2025