AAVE Deposit/Borrowing Tutorial Part III

Depositing in Aave

This is part 3 of a 4 part series. Now that we have some DAI, we can deposit onto the Aave contract.

This code is on https://github.com/cryptocamtech/aave-borrow-tutorial. Remember to create your .env with the account and private key.

Security tip: never commit .env – there are bots looking for them, ready to siphon your accounts within a few seconds of your commit.

Let’s make our deposit.

  1. Install the other node libraries as per the previous tutorial.
  1. Load the contracts and the amount of DAI (not ETH) that we’ll be depositing.
const web3 = new Web3('http://localhost:8545');
const ERC20ABI = require('./ABIs/ERC20.json');
const LendingPoolAddressProviderABI = require('./ABIs/AddressProvider.json);
const LendingPoolABI = require('./ABIs/LendingPool.json'));

const daiAmountinWei = web3.utils.toWei("10", "ether").toString(); 
const daiAddress = '0x6b175474e89094c44da98b954eedeac495271d0f';
const lpAddressProviderAddress = "0x24a42fD28C976A61Df5D00D0599C34c4f90748c8";
const lpAddressProviderContract = new web3.eth.Contract(LendingPoolAddressProviderABI, lpAddressProviderAddress);
const referralCode = "0";
  1. In order to approve the transaction without explicitly signing the transaction, the private key can be imported into the wallet. The lending pool core address needs to be retrieved via the lending pool address provider:
const account = web3.eth.accounts.privateKeyToAccount(process.env.PRIVATE_KEY);
web3.eth.accounts.wallet.add(account);
const myAddress = account.address;
const lpCoreAddress = await getLendingPoolCoreAddress();
  1. Send the approval to the DAI contract:
const daiContract = new web3.eth.Contract(ERC20ABI, daiAddress);
await daiContract.methods
        .approve(lpCoreAddress, daiAmountinWei)
        .send({
            from: myAddress,
            gasLimit: web3.utils.toHex(60000),
            gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei'))
        })
        .catch((e) => {throw Error(`Error approving DAI allowance: ${e.message}`)});
  1. The lending pool address needs to be retrieved via the lending pool address provider. The deposit into Aave can then be done:
const lpAddress = await getLendingPoolAddress();
const lpContract = new web3.eth.Contract(LendingPoolABI, lpAddress);
await lpContract.methods
        .deposit(daiAddress, daiAmountinWei, referralCode)
        .send({
            from: myAddress,
            gasLimit: web3.utils.toHex(250000),
            gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei'))
        })
        .catch((e) => {throw Error(`Error depositing to the LendingPool contract: ${e.message}`)});

See how much you’ve deposited by adding the aDAI token from address 0xfc1e690f61efd961294b3e1ce3313fbd8aa4f85d and you should see approximately $10USD worth of tokens:

You’ve just deposited $10USD!

Next, we’ll borrow some DAI from Aave in Part IV.

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: