Search code examples
solidityhardhatchainlink

How to Read Chainlink AggregatorV3Interface in Hardhat Deployment


Total beginner here apologies in advance.

I'm learning Solidity and using Hardhat and trying to figure out how to return the value of the Chainlink price feed in this tutorial contract after deployment. I know how to return the function output in remix but having trouble figuring out how to use console.log or another method in Hardhat. I am able to use console.log for built in functions like token address but can't figure out how to apply to other functions. This is using the Goerli Testnet btw.

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

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

contract TestChainlink {
    AggregatorV3Interface internal priceFeed;

    constructor() {
        // ETH / USD
        priceFeed = AggregatorV3Interface(0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e);
    }

    function getLatestPrice() public view returns (int) {
        (
            uint80 roundID,
            int price,
            uint startedAt,
            uint timeStamp,
            uint80 answeredInRound
        ) = priceFeed.latestRoundData();
        // for ETH / USD price is scaled up by 10 ** 8
        return price;
    }

}



I tried emulating console.log usage that work for built in functions like token address to apply them to the Chainlink getLatestPrice() function.

const Token = await ethers.getContractFactory("TestChainlink");
const token = await Token.deploy();
console.log("Token address:", token.address);

i.e.

I tried a ton of different combinations this was the last one I will spare all the error messages since it probably isn't a complex solve for a non novice. 

console.log("ETH Price:", getLatestPrice());

Solution

  • try

    let price = await token.getLatestPrice();
    console.log(`Eth price is: ${price}`);