AAVE Deposit/Borrowing Tutorial Part I

Obtaining ETH

This is part 1 of a 4 part series. In this tutorial we’ll be forking the mainnet and obtaining some Ether so we can play with Aave’s smart contracts.

We could probably do most most of these on a testnet, but Aave lives on the Robsten testnet and Uniswap lives on the Rinkeby testnet. Then you have to get your Ether via faucets that give you some Ether, but it a bit of mucking around using social media (OK if you don’t mind making your Twitter account public, and that’s if you have a Twitter account!).

One of the really cool things you can do as a blockchain developer is fork the mainnet and do a number of things:

  • All the contracts are already there.
  • Do whatever you like without using the real mainnet, but with all of the idiosyncrasies.
  • Unlock any account or contract.
  1. Lets being by installing ganache-cli:

npm install -g ganache-cli

When you run ganache-cli you’ll see some temporary accounts displayed with 100 Ether each in them.

  1. Then we need to go to Infura and set up a project. Infura is great for accessing the main/testnet nodes, but is limited to a number of requests per day and 30 minutes at a time (full access can be expensive). Once you get your project you can start ganache-cli with a “fork” option pointing to your Infura project. ganache-cli works by consuming new requests and passing old requests to Infura.
  2. Go to etherscan and find some rich miner. This miner has almost 4 million ether!

https://etherscan.io/address/0x742d35cc6634c0532925a3b844bc454e4438f44e

I’ve set up a script to start ganache-cli in verbose mode on port 8545:

#!/bin/sh
ganache-cli --fork https://mainnet.infura.io/v3/<project id> --unlock 0x742d35Cc6634C0532925a3b844Bc454e4438f44e -i 1 -v

  • Fork using your mainnet Infura project URL.
  • Unlock the account/contract that you wish to use.
  • -i 1 specifies the mainnet.
  • -v is verbose mode. I have found this useful for working out why web3 calls fail. Debugging why your contract calls aren’t working is hard enough.
  1. Install/start Metamask in your browser and select “Localhost 8545”

After connecting to ganache-cli you should be able to see 0 Ether in your account. Now it is time to get some Ether:

npm init -y

And just hit enter on all of the parameters. Install web3:

npm install web3

Create a script such as getSomeEth.js:

const Web3 = require('web3');
const web3 = new Web3('http://127.0.0.1:8545');

const myAccount = "<your Metamask account>";
const ethBorrowAccount = "0x742d35Cc6634C0532925a3b844Bc454e4438f44e";

web3.eth.sendTransaction({from: ethBorrowAccount, to: myAccount, value: web3.utils.toWei('100', 'ether')}, (err, hash) => {
    console.log(hash);
});

Get your account number from Metamask and cut/paste into the above.

A version of this code is here: https://github.com/cryptocamtech/aave-borrow-tutorial, but be sure to copy/create “.env” with your account number.

Now run it:

node getSomeEth.js

After running it should hopefully see a transaction hash and 100 Ether in your account:

You’re rich!

I normally run all of this at the start so my account is prepared. In fork_main.sh:

#!/bin/bash
source .env

for (( ; ; ))
do
    pkill -f "node /usr/local/bin/ganache-cli"
    ganache-cli --fork https://mainnet.infura.io/v3/$INFURA_PROJECT_ID -i 1 -v --unlock 0x742d35Cc6634C0532925a3b844Bc454e4438f44e &
    sleep 2
    node getSomeEth.js;
    # wait for < 30 mins
    sleep 1750
done

Killing ganache-cli was a bit tricky as it uses node, so you need to specify the full path (look via ps -ef etc).

This script gives me 100 Ether every time I run it (or whatever I need). This just runs in a terminal which I can view whenever web3 code is run.

Eventually you are going to see this error:

Error: Returned error: project ID does not have access to archive state

This is due to your free Infura account. The loop above waits <30 minutes and automatically restarts and gets the 100 ether each time.

Next, we’ll interact with the Uniswap contract in Part II.

Published by Crypto Cam Tech

Professional developer for 30 years + with the passion for helping others in their journey.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: