Comments (2)
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.
from web3-ethereum-defi.
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}"
from web3-ethereum-defi.
Related Issues (20)
- Low-level transactions to open and close Aave positions
- Low-level transactions to open and close short - involves selling the borrowed tokens on an exchange like Uniswap v2 and v3
- Error when loading last N blocks using JSONRPCReorganisationMonitor HOT 2
- Most things do not work? HOT 1
- Ankr specific timestamp reader HOT 1
- Better tracking for Uniswap v3 price estimation
- ValueError: {'code': -32000, 'message': 'the method eth_sendTransaction does not exist/is not available'} HOT 1
- More selective number → string conversion in `fetch_aave_reserves_shapshot` HOT 1
- Something wierd with the log HOT 1
- how to use analyse_trade_by_hash()? HOT 1
- Closed
- Handle chain reorg in event reader HOT 4
- Support Uniswap v3 swap
- Support Uniswap V3 increaseLiquidity() and decreaseLiquidity() HOT 1
- Enzyme walkthrough test suite
- fix eth_defi uniswap_v2 tests
- Uniswap V3 swap doesn't show up in API documentation HOT 1
- Websocket support HOT 1
- Aave position management code
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from web3-ethereum-defi.