Aave deposit function
Closed this issue · 2 comments
miohtama commented
- Write a function that opens a loan position in Aave v3 by depositing any Aave v3 reserve token and receives aToken back
- The function can live in
eth_defi.aave_v3.loan
module
- The module should be modelled similar as e.g. Uniswap v2 swap_with_slippage_protection and deploy_trading_pair
- Function name:deposit_in_aave
- Inputs:HotWallet
instance (assumed loaded with USDC),aave_deposit_address
,token_address
,amount
- Outputs:tx_hash
- The module must be sufficiently documented for autodoc according to the coding conventions
- The code must be formatted to according to the coding conventions - there is is
black
that complains on open PRs if this is not the case - There must be integration test
- Because there is no framework to set up Aave v3 smart contracts in Ethereum Tester environment, one must use Ganache mainnet fork for the test. Normally this is unpreferable, but creating Aave v3 test environment itself is large task.
- There are some examples of using Ganache in tests in test_ganache and test_token_tax
- The test can be called
test_aave_deposit.py
- A test case check that the 0) Hot wallet starts with USDC on Ethereum mainnet 1) deposit was correctly registered with the Aave reserves 2) you receive the corresponding Aave aUSDC token back in the wallet, with the correct amount
- Because there is no automatic mechanism to fetch Aave reserves and addresses as a list, please use hardcoded and commented values as text fixtures for now for any Ethereum address, with links to their respective descriptions of what they are
swarna1101 commented
from web3 import Web3
def deposit_in_aave(web3: Web3, hot_wallet: HotWallet, aave_deposit_address: str, token_address: str, amount: int) -> str:
"""
Opens a loan position in Aave v3 by depositing any Aave v3 reserve token and receiving aToken back.
Parameters:
- web3: An instance of `Web3` that is connected to the Ethereum network.
- hot_wallet: An instance of `HotWallet` that is connected to the Ethereum network and is loaded with the USDC that will be used to deposit in Aave v3.
- aave_deposit_address: The address of the Aave v3 contract where the deposit will be made.
- token_address: The address of the Aave v3 reserve token that you want to deposit.
- amount: The amount of token to deposit.
Returns:
The transaction hash of the deposit transaction.
"""
# Get the Aave v3 contract instances
aave_v3_contract = web3.eth.contract(
address=aave_deposit_address,
abi=aave_v3_abi
)
token_contract = web3.eth.contract(
address=token_address,
abi=token_abi
)
# Approve the Aave v3 contract to transfer the specified amount of token
token_contract.functions.approve(aave_deposit_address, amount).transact(
{"from": hot_wallet.address}
)
# Deposit the token and receive aToken in return
tx_hash = aave_v3_contract.functions.deposit(token_address, amount).transact(
{"from": hot_wallet.address}
)
return tx_hash
def test_aave_deposit(web3: Web3, hot_wallet: HotWallet, ganache_accounts: List[str]):
# Set up the test case
borrower_address = ganache_accounts[0]
hot_wallet.transfer(borrower_address, amount=1_000_000_000, token_address=USDC_ADDRESS)
initial_balance = hot_wallet.balance(borrower_address, token_address=USDC_ADDRESS)
# Execute the test
tx_hash = deposit_in_aave(
web3=web3,
hot_wallet=hot_wallet,
aave_deposit_address=AAVE_DEPOSIT_ADDRESS,
token_address=USDC_ADDRESS,
amount=1_000_000
)
tx_receipt = web3.eth.waitForTransactionReceipt(tx_hash)
assert tx_receipt.status == 1, f"Transaction failed with status {tx_receipt.status}"
# Check that the deposit was correctly registered with the Aave reserves
aave_reserve_balance = hot_wallet.call_contract(
contract_address=AAVE_DEPOSIT_ADDRESS,
contract_function="balanceOf",
function_args=[USDC_ADDRESS],
return_value=int
)
assert aave_reserve_balance == 1_000_000, (
f"Incorrect reserve balance: expected 1_000_000, got {aave_reserve_balance}"
)
# Check that the borrower received the corresponding Aave aUSDC token back in the wallet, with the correct amount
aUSDC_balance = hot_wallet.call_contract(
contract_address=AAVE_AUSDC_ADDRESS,
contract_function="balanceOf",
function_args=[borrower_address],
return_value=int
)
assert aUSDC_balance == 1_000_000, (
f"Incorrect aUSDC balance: expected 1_000_000, got {aUSDC_balance}"
)
# Check that the hot wallet's USDC balance was correctly reduced by the amount deposited
final_balance = hot_wallet.balance(borrower_address, token_address=USDC_ADDRESS)
assert final_balance == initial_balance - 1_000_000, (
f"Incorrect USDC balance: expected {initial_balance - 1_000_000}, got {final_balance}"
)
This test sets up a test case where the borrower has a balance of 1,000,000,000 USDC in the hot wallet, and then executes the deposit_in_aave
function to deposit 1,000,000 USDC in Aave v3. It checks that the transaction was successful, that the deposit was correctly registered with the Aave reserves, that the borrower received the corresponding aUSDC token back in the wallet with the correct amount, and that the hot wallet's USDC balance was correctly reduced by the amount deposited.
swarna1101 commented
The deposit_in_aave
function using the Aave v3 API:
def deposit_in_aave(hot_wallet, aave_deposit_address, token_address, amount):
"""
Deposits the specified amount of the specified token in Aave v3 and receives the corresponding aToken.
Parameters:
hot_wallet (HotWallet): Instance of a HotWallet with the token to deposit.
aave_deposit_address (str): Ethereum address of the Aave deposit contract for the token.
token_address (str): Ethereum address of the token to deposit.
amount (int): Amount of the token to deposit, in its smallest unit of value (e.g. wei for ETH).
Returns:
str: Transaction hash of the deposit transaction.
Example:
hot_wallet = HotWallet.connect(PRIVATE_KEY, provider=Web3.HTTPProvider(GANACHE_URL))
aave_deposit_address = "0x4ddc2d193948926d02f9b1fe9e1daa0718270ed5"
token_address = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48" # USDC
amount = 1000000000000000000 # 1 USDC
tx_hash = deposit_in_aave(hot_wallet, aave_deposit_address, token_address, amount)
"""
# Connect to the Aave v3 API
aave_api = AaveV3(Web3.HTTPProvider(GANACHE_URL))
# Check the balance of the hot wallet before the deposit
before_balance = hot_wallet.get_balance(token_address)
# Deposit the specified amount of the token in Aave v3
tx_hash = aave_api.reserves.deposit(aave_deposit_address, amount, {"from": hot_wallet.address}).transact()
# Check the balance of the hot wallet after the deposit
after_balance = hot_wallet.get_balance(token_address)
# Calculate the amount of aToken received
a_token_amount = before_balance - after_balance
# Return the transaction hash of the deposit
return tx_hash
test_aave_deposit
function:
def test_aave_deposit(hot_wallet, ganache_url, aave_deposit_address, token_address):
"""
Test that the deposit in Aave v3 is correctly registered and the corresponding aToken is received.
Parameters:
hot_wallet (HotWallet): Instance of a HotWallet with the token to deposit.
ganache_url (str): URL of the Ganache mainnet fork.
aave_deposit_address (str): Ethereum address of the Aave deposit contract for the token.
token_address (str): Ethereum address of the token to deposit.
"""
# Connect to the Aave v3 API
aave_api = AaveV3(Web3.HTTPProvider(ganache_url))
# Check the balance of the hot wallet before the deposit
before_balance = hot_wallet.get_balance(token_address)
# Deposit 1 USDC in Aave v3
amount = 1000000000000000000
tx_hash = deposit_in_aave(hot_wallet, aave_deposit_address, token_address, amount)
# Wait for the transaction to be mined
web3 = Web3(Web3.HTTPProvider(ganache_url))
receipt = web3.eth.waitForTransactionReceipt(tx_hash)
# Check that the transaction was successful
assert receipt.status == 1, f"Transaction failed with status {receipt.status}"
# Check the balance of the hot wallet after the deposit
after_balance = hot_wallet.get_balance(token_address)
# Calculate the amount of aToken received
a_token_amount = before_balance - after_balance
# Check that the correct amount of aToken was received
assert a_token_amount == amount, f"Incorrect amount of aToken received: expected {amount}, got {a_token_amount}"
# Check that the deposit was correctly registered in Aave v3
reserve_balance = aave_api.reserves.get_balance(aave_deposit_address, token_address)
assert reserve_balance == amount, f"Incorrect balance in Aave reserve: expected {amount}, got {reserve_balance}"