Aave - Lending and Borrowing

Objectives

  1. Swap some ETH for WETH.

  2. Deposit some WETH into Aave.

  3. Borrow some asset against the collateral(deposit).

    1. Challenge: Sell that borrowed asset (short selling)

  4. Repay everything back.

aave testnet v2 is on kovan: http://testnet.aave.com/ aave testnet v3 is on rinkeby: https://v3-test.aave.com/#/

Testing

  • Integration test: Kovan (v2) / Rinkeby (v3)

  • Unit tests: Mainnet-fork

If you are not working with oracles and don't need to mock off-chain responses, use a mainnet fork to run unit tests.

Deposit ETH

When you deposit ETH into AAVE, it gets swapped to WEth (ERC20 version of ETH) and then deposited.

Why must we convert to WETH?

  • Aave requires ERC20 properties of tokens for its ecosystem functionality.

  • ETH does not conform to ERC-20.

  • Wrapping ETH allows you to trade directly with Alt Tokens.

https://weth.io/

You will receive aETH which is the interest bearing token, reflective of your deposit and the interest it accrues.

The Aave deposit function will handle all the necessary conversions from ETH. However, we can save on gas by locking our ETH for WETH directly with the WETHGateway contract.

Create get_weth()

Objective: Get WETH by depositing ETH [call deposit function on WEth contract].

  • To interact with the contract, we need ABI + Address.

  • For ABI, use interface, IWEth.sol, from https://github.com/PatrickAlphaC/aave_brownie_py_freecode/tree/main/interfaces

  • create IWeth.sol in interfaces folder -> copy and paste github code into it.

from brownie import config, network, interface 
from scripts.helpful_scripts import get_account


def get_weth():
    """ 
    how to get Weth?
    deposit eth to WETH contract, it gives us ETH
    https://kovan.etherscan.io/token/0xd0a1e359811322d97991e03f863a0c30c2cf029c#writeContract
    
    to interact with this contract we need: address + ABI
    ABI: use interface, IWEth.sol,  from https://github.com/PatrickAlphaC/aave_brownie_py_freecode/tree/main/interfaces
    create IWeth.sol in interfaces folder -> copy and paste github code into it.
    
    """
    account = get_account()
    weth = interface.IWeth(config["networks"][network.show_active()]["weth_token"])

    #deposit 0.1 ETH, should get back 0.1 WETH
    tx = weth.deposit({"from": account, "value": 0.1 * 10**18})
    tx.wait(1)
    print("...Received 0.1WETH...")
    return tx

     
def main():
    get_weth()

On running get_weth.py successfully, you should see 0.1 WEth in your Metamask wallet.

If you want to convert Weth back to Eth, call the withdraw function. The function will return us the ETH we stored with the contract, on reclaiming the WETH.

Last updated