0

I have written the following code:

const web3 = new Web3(
            'https://polygon-mainnet.infura.io/v3/<INFURA_PROJECT_ID>'
        )
        const aggregatorV3InterfaceABI = [{ "inputs": [], "name": "decimals", "outputs": [{ "internalType": "uint8", "name": "", "type": "uint8" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "description", "outputs": [{ "internalType": "string", "name": "", "type": "string" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "uint80", "name": "_roundId", "type": "uint80" }], "name": "getRoundData", "outputs": [{ "internalType": "uint80", "name": "roundId", "type": "uint80" }, { "internalType": "int256", "name": "answer", "type": "int256" }, { "internalType": "uint256", "name": "startedAt", "type": "uint256" }, { "internalType": "uint256", "name": "updatedAt", "type": "uint256" }, { "internalType": "uint80", "name": "answeredInRound", "type": "uint80" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "latestRoundData", "outputs": [{ "internalType": "uint80", "name": "roundId", "type": "uint80" }, { "internalType": "int256", "name": "answer", "type": "int256" }, { "internalType": "uint256", "name": "startedAt", "type": "uint256" }, { "internalType": "uint256", "name": "updatedAt", "type": "uint256" }, { "internalType": "uint80", "name": "answeredInRound", "type": "uint80" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "version", "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], "stateMutability": "view", "type": "function" }]
        const addr = '0xAB594600376Ec9fD91F8e885dADF0CE036862dE0'
        const priceFeed = web3.eth.contract(aggregatorV3InterfaceABI, addr)

        priceFeed.methods
            .latestRoundData()
            .call()
            .then((roundData) => {
                console.log(roundData.answer);
            })

I'm using the Infura API to talk to smart contracts on the Polygon mainnet.

However, I'm getting the following error on console:

Uncaught TypeError: Cannot read properties of undefined (reading 'latestRoundData')

My libraries and their versions are as follows:

> web3.js : 0.19.0
> Truffle : v5.4.32
> truffle-contract.min.js : 4.0.31
2
  • Are you sure about this address? When I go to polyscan and try 0xAB594600376Ec9fD91F8e885dADF0CE036862dE0ss, it finds nothing. Commented Feb 13, 2022 at 10:59
  • Oh, sorry. I fixed the address in my post. However, the error still remains: Uncaught TypeError: Cannot read properties of undefined (reading 'latestRoundData')
    – duskdusk13
    Commented Feb 13, 2022 at 11:09

1 Answer 1

0

The following code works fine to me. I believe your mistake was in the declarationn -> const priceFeed = web3.eth.contract(aggregatorV3InterfaceABI, addr). See mine below.

import Web3 from 'web3'

async function run() {

const web3 = new Web3(
    'https://polygon-mainnet.infura.io/v3/d34f69843c144d3ab51b1915ff36260e'
)
const aggregatorV3InterfaceABI = [{ "inputs": [], "name": "decimals", "outputs": [{ "internalType": "uint8", "name": "", "type": "uint8" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "description", "outputs": [{ "internalType": "string", "name": "", "type": "string" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "uint80", "name": "_roundId", "type": "uint80" }], "name": "getRoundData", "outputs": [{ "internalType": "uint80", "name": "roundId", "type": "uint80" }, { "internalType": "int256", "name": "answer", "type": "int256" }, { "internalType": "uint256", "name": "startedAt", "type": "uint256" }, { "internalType": "uint256", "name": "updatedAt", "type": "uint256" }, { "internalType": "uint80", "name": "answeredInRound", "type": "uint80" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "latestRoundData", "outputs": [{ "internalType": "uint80", "name": "roundId", "type": "uint80" }, { "internalType": "int256", "name": "answer", "type": "int256" }, { "internalType": "uint256", "name": "startedAt", "type": "uint256" }, { "internalType": "uint256", "name": "updatedAt", "type": "uint256" }, { "internalType": "uint80", "name": "answeredInRound", "type": "uint80" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "version", "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], "stateMutability": "view", "type": "function" }]
const addr = '0xAB594600376Ec9fD91F8e885dADF0CE036862dE0'
const priceFeed = new web3.eth.Contract(aggregatorV3InterfaceABI, addr)

priceFeed.methods
    .latestRoundData()
    .call()
    .then((roundData) => {
        console.log(roundData.answer);
    })
}

run()
2
  • My web3.js library has the version 0.19.0. In that version, you have to use web3.eth.contract instead of web3.eth.Contract
    – duskdusk13
    Commented Feb 13, 2022 at 12:59
  • use the new vesion. You can change this at tuffle config. Or maybe just installing the new version using npm. Or just update your truffle =) Commented Feb 13, 2022 at 15:04

Your Answer

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

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