My code run successfully without errors, but it never does actually borrow anything from Aave when i call lending_pool.borrow(). Tried running this on both a mainnet-fork into local environment, as well as on kovan testnet.
Screenshot of output below is from Kovan testnet.
Borrow transaction hash on Kovan testnet: 0x44278d3d74c607a5417f6c7d1c81b43cecb872ee22a635280ad3bf0edabb29fc
My code:
from scripts.helpful_scripts import get_account
from brownie import network, config, interface
from scripts.get_weth import get_weth
from web3 import Web3
#0.1 ETH - 0.1*(10**18)
deposit_amount = Web3.toWei(0.1, "ether")
def get_lendingpool():
# create lending_pool_addressess_provider contract object
lending_pool_addressess_provider = interface.ILendingPoolAddressesProvider(config["networks"][network.show_active()]["lending_pool_addressess_provider"])
#get lending pool address
lending_pool = lending_pool_addressess_provider.getLendingPool()
return lending_pool
def approve_erc20(amount,spender, erc20_address, account):
print("....Approving ERC20 token...")
# get ERC20 interface: https://github.com/PatrickAlphaC/aave_brownie_py_freecode/tree/main/interfaces
# approve(address spender, uint256 value)
erc20 = interface.IERC20(erc20_address)
tx = erc20.approve(spender,amount,{"from": account})
tx.wait(1)
print("....Approved....")
return tx
def get_borrowable_data(lending_pool,account):
(totalCollateralETH, totalDebtETH, availableBorrowsETH, currentLiquidationThreshold, ltv, healthFactor) = lending_pool.getUserAccountData(account)
# all ETH values are denominated in WEI - convert to ETH
total_collateral_eth = Web3.fromWei(totalCollateralETH, "ether")
total_debt_eth = Web3.fromWei(totalDebtETH, "ether")
available_borrow_ETH = Web3.fromWei(availableBorrowsETH, "ether")
print(f"...You have {total_collateral_eth} worth of ETH deposited...")
print(f"...You have {total_debt_eth} worth of ETH borrowed...")
print(f"...You can borrow {available_borrow_ETH} worth of ETH...")
return (float(available_borrow_ETH), float(total_debt_eth))
def get_token_price(token_price_feed_address):
# use chainlink pricefeed
token_eth_price_feed = interface.AggregatorV3Interface(token_price_feed_address)
price = token_eth_price_feed.latestRoundData()[1] #price is index 1
norm_price = Web3.fromWei(price, "ether")
print(f"...DAI/ETH price is {norm_price}")
return float(price)
def main():
account = get_account()
deposit_token_address = config["networks"][network.show_active()]["weth_token"]
# if no WETH, get_weth()
# local mainnet fork can use dummy acocunts to get WETH
# since local mainnet fork, can use dummy accounts. if actual mainnet/testnet then use private key account.
if network.show_active() in ["mainnet-fork"]:
get_weth()
# Get lending pool contract
lending_pool_address = get_lendingpool()
lending_pool = interface.ILendingPool(lending_pool_address)
print(f"....Lending pool contract: {lending_pool_address}....")
## Approve before sending our ERC20(WETH) tokens: approve allowance of deposit_amount for Aave lendingPool contract
approve_erc20(deposit_amount, lending_pool.address, deposit_token_address, account)
# Deposit: deposit(address asset, uint256 amount, address onBehalfOf, uint16 referralCode)
## referralCode is deprecated - just pass a 0 as parameter
print("....Depositing....")
tx = lending_pool.deposit(deposit_token_address, deposit_amount, account.address, 0, {"from": account})
tx.wait(1) # wait one block
print("....Deposited!....")
# Borrow DAI
## how much can we borrow - pull account stats - getUserAccountData(address user)
(borrowable_eth, total_debt_eth) = get_borrowable_data(lending_pool,account)
## DAI in terms of ETH
dai_eth_price = get_token_price(config["networks"][network.show_active()]["dai_eth_price_feed"])
## how much DAI can we borrow?
dai_to_borrow = (1/ dai_eth_price) * (borrowable_eth *0.95)#use 90% of collateral: liquidation concerns
print(f"...We will borrow {dai_to_borrow} DAI...")
dai_in_wei = Web3.toWei(dai_to_borrow, "ether")
print(f"...We will borrow {dai_in_wei} DAI...")
## Borrow! call borrow() from lendingPool contract
## function borrow(address asset, uint256 amount, uint256 interestRateMode, uint16 referralCode, address onBehalfOf)
# interestRateMode: 1 - stable | 2 - variable
dai_address = config["networks"][network.show_active()]["dai_token_address"]
borrow = lending_pool.borrow(dai_address, dai_in_wei, 1, 0, account.address, {"from": account})
borrow.wait(1)
print("...Borrowed!...")
# Account Info
get_borrowable_data(lending_pool,account)
# Return DAI
## repay(address asset, uint256 amount, uint256 rateMode, address onBehalfOf)
repay_all(dai_in_wei, lending_pool, account)
get_borrowable_data(lending_pool, account)
def repay_all(amount, lending_pool, account):
approve_erc20(Web3.toWei(amount, "ether"), lending_pool.address, config["networks"][network.show_active()]["dai_token_address"], account)
repay_tx = lending_pool.repay(
config["networks"][network.show_active()]["dai_token_address"],
amount, 1, account.address, {"from": account})
repay_tx.wait(1)
print("...REPAID....")