1

I implemented a method to retrieve a price for a currency via some chainlink price feed. This price is casted to an uint256. But if I call this method via etherscan it does not show a number, but just "object Object". Whats the reason for this and how can I get a number to show up here?

function getLatestPrice(address currencyAddress) public view returns (uint256) {
    (, int price, , , ) = priceFeeds[currencyAddress].latestRoundData();

    return uint256(price * 1e10); // Price comes with 8 digits but we need 18
}

enter image description here

1 Answer 1

1

I created a contract with the following code and deployed it to the goerli testnet at this address 0x7cEd02A72d311d7734dEF293D520752BA6CcFc02.

Contract example:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

contract ReturnPriceContract {

    mapping (address => AggregatorV3Interface) public priceFeeds;
    constructor() {
        // LINK/USD price feed on goerli
        priceFeeds[0x45442CB17bD3E3C0AeaE92BF425473E582d5e740] = AggregatorV3Interface(0x48731cF7e84dc94C5f84577882c14Be11a5B7456);
    }

    function getLatestPrice(address currencyAddress) public view returns (uint256) {
    AggregatorV3Interface priceFeed = priceFeeds[currencyAddress];
    require(address(priceFeed) != address(0), "Price feed not found");

    (, int price, , , ) = priceFeed.latestRoundData();
    require(price > 0, "Invalid price");

    uint256 scaledPrice = uint256(price) * 1e10;
    return scaledPrice;
}
}

ethers.js example:

Then I created a quick Node.js script with ethers to fetch the price from getLatestPrice for the LINK token on goerli, to show the returned value as a string:

const ethers = require("ethers");

const provider = new ethers.providers.JsonRpcProvider(
  "https://goerli.infura.io/v3/<api-key>"
);

const abi = [
  "function getLatestPrice(address currencyAddress) public view returns (uint256)",
];

// contract address for ReturnPriceContract deployed on goerli
const contractAddress = "0x7cEd02A72d311d7734dEF293D520752BA6CcFc02";
const contract = new ethers.Contract(contractAddress, abi, provider);

async function main() {
  const price = await contract.getLatestPrice(
    "0x45442CB17bD3E3C0AeaE92BF425473E582d5e740"
  );
  // this returns at the time of writing "8216626120000000000"
  console.log(price.toString());
}

main();

Etherscan Goerli example:

Additionally, I verified the contract 0x7cEd02A72d311d7734dEF293D520752BA6CcFc02 on the goerli testnet - if I read the getLatestPrice for 0x45442CB17bD3E3C0AeaE92BF425473E582d5e740 on the goerli etherscan contract page, you will see the LINK price returned as uint on etherscan.

etherscan return uint example

Hope this helps!

2
  • I forgot to mention: The code is solidity code. There is no toNumber() function.
    – jan
    Feb 19 at 6:42
  • please have a look, I have put together an example for you.
    – timsntech
    Feb 19 at 13:02

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.