Interact with Aave v3 protocol using web3js

Fabri
4 min readJul 19, 2022

Coding with blockchains and smart contracts is hard. We are still very early in the development of the technology and because of this, there are little to none documentation or examples. The blockchain developer community is growing fast, but still is incomparable to the number of resources that exists in web2 communities.

AAVE

Today we are going to supply collateral and also borrow from one of the most popular Defi Protocols, AAVE. AAVE currently has 12 billion in TVL and is one of the most trusted Defi Protocols.

Demo Script

In this article, I will be showing you how to create a NodeJS script that check out balances and interacts with the Lending Pool contract of AAVE.

Checkout the GitHub repo | Check out AAVE docs

What we will learn:

  • Find an ABI
  • Setting up a wallet with test USDC tokens
  • Check USDC balances
  • Supply USDC to the Lending pool
  • Check user’s account data

1) Find an ABI

First we need to find the ABI of the contract we are going to interact with. This can be sometimes a difficult task. In this case, we will be using Avalanche testnet Fuji. In the documentation there are details about what contract address we have to use in each blockchain/tesnet.

If we search for that contract address in Avax testnet explorer we will find that something is missing. There is no ABI in the contract section, actually there is no contract. So, what now?

Ok let’s try in the mainnet explorer. Now we have a contract, but still no ABI

This time we will have to get creative. Maybe when you are reading this article the docs are updated, but right now, the only way for me to find it was first going to mainnet contract in Avalanche.

After looking for this contract we get the implementation! We can now celebrate 👯

No, I was kidding. This ABI is not what we need. It’s missing many methods of the V3 implementation. So, what now? Let’s pay attention to this message.

Looks that there is other contract, which doesn’t appear on AAVE docs. After checking if some of the methods we require are in the ABI we can confirm it is the correct ABI. These methods are supply() borrow() and getUserAccountData()

2- Setting up a wallet with test USDC tokens

We will create a wallet in metamask and go to the Avax Fuji faucet to add the network and request Avax test tokens. Faucet link.

After this, we are going to get USDC test assets. Go to app.aave.com → click top right → enable testnet mode → go to Faucet in the main menu → select Fuji V3 Market → Scroll to the USDC asset and click the faucet link.

Great! we now have USDC and Avax to start playing around with the Aave contract.

3- Check USDC balances

We will start checking the USDC balance of the wallet, for we will initialize the script importing web3js and setting up our wallet and Avax testnet provider

For the USDC balance we will need the ABI of USDC which is a standard ERC20 token. The standard ERC20 token ABI can be easily found in google. We will use the method balanceOf, which is in the ABI.

This will print USDC Balance: 10000.000000

4- Supply USDC to the Lending pool

For supplying USDC to the pool we need to instantiate the Lending Pool contract. Then we have to allow this contract to request assets from the USDC contract. Finally, we can supply the pool sending the transaction to the AVAX blockchain.

5- Check users’ data

We can now verify that we have provided the desired collateral to the Pool by calling the getUserAccountData method.

Conclusion

That’s it! We now have a running script working the latest AAVE protocol version (v3) and the web3js library. Don’t forget to download the code from GitHub to check other methods I used like borrow().

To learn more about web3 technology, check out my other posts. For more information, feel free to reach out on Twitter, or fguespe@gmail.com

--

--

Fabri

I like software ecosystems. ⚡️ DevRel @xmtp_ ⚡️