0

I'm trying to fetch data through the etherscan API from a proxy aave lending pool smart contract. This works absolutely fine for AaveV1 but for AaveV2, it just sends back garbage values (uint min and uint256 max). I'm using the below code:

Aave Version 1 Smart contract
// Reading contract https://etherscan.io/address/0x398eC7346DcD622eDc5ae82352F02bE94C62d119#readProxyContract
// Works "ABI for the implementation contract at 0xc1ec30dfd855c287084bf6e14ae2fdd0246baf0d"
// Doesn't work "Previously recorded to be on 0xdb9217fad3c1463093fc2801dd0a22c930850a61"

const fetch = require('node-fetch');
const ethers = require("ethers");

async function main() {

    sampleAddressV2 = '0xd5c66bb825cf398d2b2035dcd8cdd102d9704f1c'
    sampleAddressV1 = '0x440efbba3f3d971fd8411c580197ecb53ef71f87'

    aaveLendingPoolV2Address = '0xc6845a5c768bf8d7681249f8927877efda425baf'
    aaveLendingPoolV1Address = '0xc1ec30dfd855c287084bf6e14ae2fdd0246baf0d'

    contractAddress = aaveLendingPoolV2Address
    sampleAddress = sampleAddressV2


    // creating a new Provider, and passing in our node URL
    const node = "wss://eth-mainnet.g.alchemy.com/v2/2z9JYK0TXfU2vWoNQgpjOltZ0amERORc"
    const provider = new ethers.providers.WebSocketProvider(node);
    //TODO: Remove API key
    apiKey = 'YourAlchemyAPIKey'


    // make an API call to the ABIs endpoint
    const response = await fetch('https://api.etherscan.io/api?module=contract&action=getabi&address='+contractAddress+'&apikey='+apiKey);
    const data = await response.json();
    let abi = data.result;
    console.log(abi);


    //TODO: Remove private key
    let privatekey = "YourWalletPrivateKey";
    let wallet = new ethers.Wallet(privatekey, provider);
    console.log("Using wallet address to sign txn: " + wallet.address);


    let contract = new ethers.Contract(contractAddress, abi, wallet);
    // console.log(contract)

    // getUserAccountData returns
    //       uint256 totalCollateralETH,
    //       uint256 totalDebtETH,
    //       uint256 availableBorrowsETH,
    //       uint256 currentLiquidationThreshold,
    //       uint256 ltv,
    //       uint256 healthFactor
    let read = await contract.getUserAccountData(sampleAddress);
    console.log("Value stored in contract is " + read.toString());
    // let read2 = await contract.resolvedAddress();
    // console.log("Value stored in contract is " + read2.toString());
}

const runMain = async () => {
    try {
        await main();
        process.exit(0);
    } catch (error) {
        console.log(error);
        process.exit(1);
    }
};

runMain();

When

    contractAddress = aaveLendingPoolV2Address
    sampleAddress = sampleAddressV2

I get

Value stored in contract is 0,0,0,0,0,115792089237316195423570985008687907853269984665640564039457584007913129639935

However, when

    contractAddress = aaveLendingPoolV1Address
    sampleAddress = sampleAddressV1

I get the correct values.

Value stored in contract is 48654124646394704110,48654124646394704110,27358021958082421204,1930253792696,4267156705104630589,70,65,1244895734848615672

The etherscan API works just fine from the website here as shown in attached screenshotenter image description here

1 Answer 1

2

The problem is that the address 0xc6845a5c768bf8d7681249f8927877efda425baf is the proxy bytecode contract. It doesn't store any data.

To make it work you have call 0x7d2768dE32b0b80b7a3454c06BdAc94A69DDc7A9 with the ABI at the 0xc6845a5c768bf8d7681249f8927877efda425baf.

// For V2
contractAddress = aaveLendingPoolV2Address
proxyAddress = "0x7d2768dE32b0b80b7a3454c06BdAc94A69DDc7A9"

[..]

let contract = new ethers.Contract(proxyAddress, abi, wallet)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.