CTO linkcard.app 💸 Web3 Researcher & Investor


CTO linkcard.app 💸 Web3 Researcher & Investor

Subscribe to Fabrizio G

Subscribe to Fabrizio G
Share Dialog
Share Dialog
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 amount 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 are going to be using Avalanche testnet Fuji. In the documentation there are details about what contract address we have to use in each blockchain/tesnet.

In this case 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 a lot of 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 need 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
const Web3 = require("web3");
//ABI
const lendingPoolABI = require("../abi/Pool.json")
const ERC20_ABI =require("../abi/ERC20.json")
//Providers
const web3 = new Web3(new Web3.providers.HttpProvider('https://api.avax-test.network/ext/bc/C/rpc'))
//Wallet from
const privateKey1 = '' // Private key of wallet created
const signer = web3.eth.accounts.privateKeyToAccount(privateKey1)
web3.eth.accounts.wallet.add(signer);
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.
//Check balance. We use 6 as the decimals places that USDC has. Each token has each own decimal places. For reference Ether has 18.
//We also send the wallet that we want to check the balance of. Doesn’t need to be signed or ours, but in this case for the example we use our same wallet.
//Contracts
const usdc_avax_address='0x3E937B4881CBd500d05EeDAB7BA203f2b7B3f74f'
const usdc_contract = new web3.eth.Contract(ERC20_ABI, usdc_avax_address)
await checkBalanceUSDC(signer.address,6)
const checkBalanceUSDC = async (wallet,decimals) => {
let balance = await usdc_contract.methods.balanceOf(wallet).call()
balance = parseInt(balance) / 10**decimals;
console.log(`1- USDC Balance: ${((balance))}\n`)
}
This will print USDC Blaance: 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 need to allow this contract to request assets from the USDC contract. Finally, we can supply the pool sending the transaction to the AVAX blockchain.
const pool_contract_addr='0xb47673b7a73D78743AFF1487AF69dBB5763F00cA'
const pool_contract = new web3.eth.Contract(lendingPoolABI,pool_contract_addr)
//This means 10 USDC, multiplied to fit as uint265.
const underlyingTokensToSupply = 10 * Math.pow(10, 6)
// Token approval
await usdc_contract.methods.approve(pool_contract_addr, underlyingTokensToSupply).send({from: signer.address, gas: 100000})
.on('transactionHash', hash => {
console.log('TX Hash Approve', hash)
})
.on('error', error => {
console.log('Approve Error', error)
})
// Supply
await pool_contract.methods.supply(usdc_avax_address, underlyingTokensToSupply/10, signer.address, "0").send({from: signer.address, gas: 500000})
.on('transactionHash', hash => {
console.log('TX Hash Supply', hash)
})
.on('error', error => {
console.log('Supply Error', error)
})
.on('receipt', receipt => {
console.log('Mined', receipt)
if(receipt.status == '0x1' || receipt.status == 1){
console.log('Transaction Success')
}
else
console.log('Transaction Failed')
})
5- Check users’ data
We can now verify that we have provided the desired collateral to the Pool by calling the getUserAccountData method.
let data=await pool_contract.methods.getUserAccountData(signer.address).call()
let collateral=(parseInt(data.totalCollateralBase/10) / 10**18)
let borrow=(parseInt(data.totalDebtBase/10) / 10**18)
console.log(`2- Balances:\n`)
console.log('collateral:',collateral,'\n')
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.
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 amount 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 are going to be using Avalanche testnet Fuji. In the documentation there are details about what contract address we have to use in each blockchain/tesnet.

In this case 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 a lot of 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 need 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
const Web3 = require("web3");
//ABI
const lendingPoolABI = require("../abi/Pool.json")
const ERC20_ABI =require("../abi/ERC20.json")
//Providers
const web3 = new Web3(new Web3.providers.HttpProvider('https://api.avax-test.network/ext/bc/C/rpc'))
//Wallet from
const privateKey1 = '' // Private key of wallet created
const signer = web3.eth.accounts.privateKeyToAccount(privateKey1)
web3.eth.accounts.wallet.add(signer);
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.
//Check balance. We use 6 as the decimals places that USDC has. Each token has each own decimal places. For reference Ether has 18.
//We also send the wallet that we want to check the balance of. Doesn’t need to be signed or ours, but in this case for the example we use our same wallet.
//Contracts
const usdc_avax_address='0x3E937B4881CBd500d05EeDAB7BA203f2b7B3f74f'
const usdc_contract = new web3.eth.Contract(ERC20_ABI, usdc_avax_address)
await checkBalanceUSDC(signer.address,6)
const checkBalanceUSDC = async (wallet,decimals) => {
let balance = await usdc_contract.methods.balanceOf(wallet).call()
balance = parseInt(balance) / 10**decimals;
console.log(`1- USDC Balance: ${((balance))}\n`)
}
This will print USDC Blaance: 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 need to allow this contract to request assets from the USDC contract. Finally, we can supply the pool sending the transaction to the AVAX blockchain.
const pool_contract_addr='0xb47673b7a73D78743AFF1487AF69dBB5763F00cA'
const pool_contract = new web3.eth.Contract(lendingPoolABI,pool_contract_addr)
//This means 10 USDC, multiplied to fit as uint265.
const underlyingTokensToSupply = 10 * Math.pow(10, 6)
// Token approval
await usdc_contract.methods.approve(pool_contract_addr, underlyingTokensToSupply).send({from: signer.address, gas: 100000})
.on('transactionHash', hash => {
console.log('TX Hash Approve', hash)
})
.on('error', error => {
console.log('Approve Error', error)
})
// Supply
await pool_contract.methods.supply(usdc_avax_address, underlyingTokensToSupply/10, signer.address, "0").send({from: signer.address, gas: 500000})
.on('transactionHash', hash => {
console.log('TX Hash Supply', hash)
})
.on('error', error => {
console.log('Supply Error', error)
})
.on('receipt', receipt => {
console.log('Mined', receipt)
if(receipt.status == '0x1' || receipt.status == 1){
console.log('Transaction Success')
}
else
console.log('Transaction Failed')
})
5- Check users’ data
We can now verify that we have provided the desired collateral to the Pool by calling the getUserAccountData method.
let data=await pool_contract.methods.getUserAccountData(signer.address).call()
let collateral=(parseInt(data.totalCollateralBase/10) / 10**18)
let borrow=(parseInt(data.totalDebtBase/10) / 10**18)
console.log(`2- Balances:\n`)
console.log('collateral:',collateral,'\n')
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.
<100 subscribers
<100 subscribers
No activity yet