Borrow

We need to find out how much we can borrow against our deposits/collateral.

getUserAccountData()

  • function getUserAccountData(address user)

  • values are returned in WEI, we will convert to ETH

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))
  • return multiple variables using tuple syntax.

  • float allows for decimal places.

Into main()

add the following code

DAI

We now know much we can borrow in terms of ETH, {available_borrow_ETH}, from above.

How much is that in terms of DAI?

We will need to get DAI/ETH price and convert -> via chainlink pricefeed contract

Price of DAI/ETH

Chainlink has a standard interface contract AggregatorV3Interface which works with all of its different price feed contracts.

With this interface, we simply need to pass the corresponding price feed address into it, to create the contract object in brownie.

  • create price feed contract object

  • calls latestRoundData from price feed contract, slicing out price variable from return tuple

  • convert price from Wei to Ether (decimal place modification)

Into main()

Lending_pool.borrow

call borrow() from lendingPool contract

We will need to add the DAI token address into brownie-config.yaml

  • mainnet-fork: get DAI address off etherscan

  • Kovan/Rinkeby: refer to Aave documentation to get their testnet version of DAI

Testnet assets addresses are different depending on stable vs variable rate of borrowing

Last updated