aave / aave-v3-core

This repository contains the core smart contracts of the Aave V3 protocol.

Home Page:https://aave.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Asking help on interacting with aave-v3 contracts Error(Error: VM Exception while processing transaction: reverted with reason string '31')

L1xus opened this issue · comments

commented

Hi guys I need your help on this please!
I'm trying to create a JavaScript code that interacts with aave-v3 contracts
I imported and setup everything well
As you can see in my code below I managed to approve WETH spending and give a collateral but when I try to borrow I got the error :/

Here is my code:

const { formatUnits } = require("ethers")
const { getNamedAccounts, ethers } = require("hardhat")
const { getWeth, AMOUNT } = require("../scripts/getWeth")

async function main(){
    // the protocol treats everything as ERC20 token
    await getWeth()
    const {deployer} = await getNamedAccounts()
    // Pool Address Provider : 0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e
    const pool = await getPool(deployer)
    console.log(`Pool Address : ${pool.address}`)
    // deposit!
    const wethTokenAddress = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
    // approve!
    await approveERC20(wethTokenAddress, pool.address, AMOUNT, deployer)
    console.log("Depositing...")
    await pool.deposit(wethTokenAddress, AMOUNT, deployer, 0)
    console.log("Deposited!");

    let { availableBorrowsBase, totalDebtBase } = await getBorrowUserData(pool, deployer)
    // borrow
    // how much we have borrowed, how much we have in collateral, how much we can borrow
    // // what is the conversion rate on DAI
    const daiPrice = await getDaiPrice()
    const daiAmount = await getEthPrice(availableBorrowsBase)
    const amountDaiToBorrow = daiAmount.toString() * 0.8 * (1/daiPrice.toNumber())
    // console.log(`You can borrow ${daiAmount.toString()} DAI`)
    const amountDaiToBorrowWei = ethers.utils.parseEther(amountDaiToBorrow.toString())  
    const daiAddress = "0x6B175474E89094C44Da98b954EedeAC495271d0F"
    await borrowDai(daiAddress, pool, amountDaiToBorrowWei, deployer)
    await getBorrowUserData(pool, deployer)
}

async function borrowDai(daiAddress, pool, amountDaiToBorrow, account){
    const borrowTx = await pool.borrow(daiAddress, amountDaiToBorrow, 1, 0, account)
    await borrowTx.wait(1)
    console.log("You've borrowed!");
}

async function getDaiPrice(){
    const daiEthPriceFeed = await ethers.getContractAt("AggregatorV3Interface", "0x773616E4d11A78F511299002da57A0a94577F1f4")
    const price = (await daiEthPriceFeed.latestRoundData())[1]
    console.log(`The DAI/ETH price is ${price.toString()}`);
    return price
}
async function getEthPrice(basePrice){
    const EthPriceFeed = await ethers.getContractAt("AggregatorV3Interface", "0x773616E4d11A78F511299002da57A0a94577F1f4")
    const price = (await EthPriceFeed.latestRoundData())[1]
    // console.log(`The ETH/USD price is ${price.toString()}`);
    const ethPrice = await price * basePrice / 10**8
    return ethPrice
}

async function getBorrowUserData(pool, account){
    const { totalCollateralBase, totalDebtBase, availableBorrowsBase } = await pool.getUserAccountData(account)
    console.log(`You have ${(await getEthPrice(totalCollateralBase)).toString()} worth of ETH deposited.`)
    console.log(`You have ${(await getEthPrice(totalDebtBase)).toString()} worth of ETH borrowed.`)
    console.log(`You can borrow ${(await getEthPrice(availableBorrowsBase)).toString()} worth of ETH.`)
    return { availableBorrowsBase, totalDebtBase }
}

async function getPool(account){
    const poolAddressesProvider = await ethers.getContractAt("IPoolAddressesProvider", "0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e", account)
    const poolAddress = await poolAddressesProvider.getPool()
    const pool = await ethers.getContractAt("IPool", poolAddress, account)
    return pool
}

async function approveERC20(erc20Address, spenderAddress, amountToSpend, account){
    const erc20Token = await ethers.getContractAt("IERC20", erc20Address, account)
    const tx = await erc20Token.approve(spenderAddress, amountToSpend)
    await tx.wait(1)
    console.log("Approved!")
}

main()
    .then(()=> process.exit(0))
    .catch((error) => {
        console.error(error)
        process.exit(1)
    })

Here is the error:

Got 20000000000000000 WETH
Pool Address : 0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2
Approved!
Depositing...
Deposited!
You have 19956196244183668 worth of ETH deposited.
You have 0 worth of ETH borrowed.
You can borrow 15964956997560616 worth of ETH.
The DAI/ETH price is 553420831269495
Error: VM Exception while processing transaction: reverted with reason string '31'
    at <UnrecognizedContract>.<unknown> (0x87870bca3f3fd6335c3f4ce8392d69350b4fa4e2)
    at <UnrecognizedContract>.<unknown> (0x87870bca3f3fd6335c3f4ce8392d69350b4fa4e2)
    at <UnrecognizedContract>.<unknown> (0x87870bca3f3fd6335c3f4ce8392d69350b4fa4e2)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at HardhatNode._mineBlockWithPendingTxs (/home/astro/Astro_sol_js/Defi/node_modules/hardhat/src/internal/hardhat-network/provider/node.ts:1840:23)
    at HardhatNode.mineBlock (/home/astro/Astro_sol_js/Defi/node_modules/hardhat/src/internal/hardhat-network/provider/node.ts:517:16)
    at EthModule._sendTransactionAndReturnHash (/home/astro/Astro_sol_js/Defi/node_modules/hardhat/src/internal/hardhat-network/provider/modules/eth.ts:1532:18)
    at HardhatNetworkProvider.request (/home/astro/Astro_sol_js/Defi/node_modules/hardhat/src/internal/hardhat-network/provider/provider.ts:123:18)
    at EthersProviderWrapper.send (/home/astro/Astro_sol_js/Defi/node_modules/@nomiclabs/hardhat-ethers/src/internal/ethers-provider-wrapper.ts:13:20)
error Command failed with exit code 1.

Hi @Astrofox1 ,
It seems you are trying to borrow DAI in stable mode when it's disabled for that specific reserve. I recommend you to switch to Variable mode.
Here the complete list of errors of the protocol: https://github.com/aave/aave-v3-core/blob/master/contracts/protocol/libraries/helpers/Errors.sol#L40

commented

Thank you @miguelmtzinf I understand now 🙏