Search code examples
javascriptsoliditysmartcontractschainlink

How can I make the data provided by some ChainLink aggregator get updated every time I click on my "getLatestPrice" button?


this is my very first time deploying a contract on Remix as well as learning how to code on Solidity.

I have already read this guide and deployed successfully the Smart Contract template provided:

pragma solidity ^0.6.7;

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

contract PriceConsumerV3 {

AggregatorV3Interface internal priceFeed;

/**
 * Network: Kovan
 * Aggregator: BTC/USD
 * Address: 0x6135b13325bfC4B00278B4abC5e20bbce2D6580e
 */
constructor() public {
    priceFeed = AggregatorV3Interface(0x6135b13325bfC4B00278B4abC5e20bbce2D6580e);
}

/**
 * Returns the latest price
 */
function getThePrice() public view returns (int) {
    (
        uint80 roundID, 
        int price,
        uint startedAt,
        uint timeStamp,
        uint80 answeredInRound
    ) = priceFeed.latestRoundData();
    return price;
}
}

However, I thought that after deploying the template above, whenever I clicked on the getLatestPrice button the price of such pair would get instantly updated, I was very wrong, the price actually turned out getting "frozen" after the first click.

So, I would like to know what would be mandatory to type in the template above to fulfill that aim

Also, I tried to print the timeStamp by typing return timeStamp; right below return price; but when compiling, the Remix compiler replied:

TypeError: Return argument type uint256 is not implicitly convertible to expected type (type of first return variable) int256. return timeStamp; ^-------^

So, just for curiosity, how do I convert a uint256 variable to an int256 one in order to get the timeStamp of each updated price (for every time I click on getLatestPrice button) ?

thanks for reading


Solution

  • Testnet pricefeeds only update sporadically (so they will not update the price or time every second or even every minute)

    Here's an example if you want to see that the function is getting called

    // SPDX-License-Identifier: MIT
    pragma solidity ^0.6.6;
    pragma experimental ABIEncoderV2;
    
    //To run on remix use Injected Web3 with Metamask on Rinkeby network activated
    
    import "@chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
    
    contract ChainlinkPriceFeed {
        
        int public countButtonClicks;
        int public kovanEthPrice;
        uint public kovanTimestamp;
        uint80 public kovanRoundID;
        
        /**
         * Network: Kovan
         * Aggregator: ETH/USD
         */
        constructor() public {
            countButtonClicks = 0;
            kovanEthPrice = -1;
            kovanTimestamp = 0;
            kovanRoundID = 0;
            // Examples -> Rinkeby network 
            // See https://docs.chain.link/docs/reference-contracts/ for available feeds and blockchains
            // priceData["ETHUSD"] = 0x8A753747A1Fa494EC906cE90E9f37563A8AF630e;
            // priceData["BTCUSD"] = 0xECe365B379E1dD183B20fc5f022230C044d51404;
            // priceData["LINKUSD"] = 0xd8bD0a1cB028a31AA859A21A3758685a95dE4623;
            // priceData["AUDUSD"]= 0x21c095d2aDa464A294956eA058077F14F66535af;
            //Examples -> Kovan Netowrk see https://docs.chain.link/docs/ethereum-addresses/
        }
    
        /**
         * Returns the latest price information from the asset address
         */
        function getLatestPrice(address assetAddress) public view returns 
        (int price, uint80 roundID, int decimals, string memory description, uint timestamp) 
        {
            AggregatorV3Interface priceFeed = AggregatorV3Interface(assetAddress);
            (            
                roundID, 
                price,
                uint startedAt,
                timeStamp,
                uint80 answeredInRound
            ) = priceFeed.latestRoundData();
            description = priceFeed.description();
            decimals = priceFeed.decimals();
    
            return (price, roundID, decimals, description, timestamp);
        }
        
        function getKovanEthPrice() public view returns (int price, uint timeStamp, uint80 roundID) {
            AggregatorV3Interface priceFeed = AggregatorV3Interface(0x9326BFA02ADD2366b30bacB125260Af641031331);
            (            
                uint80 roundID, 
                int price,
                uint startedAt,
                uint timeStamp,
                uint80 answeredInRound
            ) = priceFeed.latestRoundData();
            
            return (price, timeStamp, roundID);
        }
        
        function counterKovanEthPrice() public {
            countButtonClicks = countButtonClicks+1;
            (kovanEthPrice, kovanTimestamp, kovanRoundID) = getKovanEthPrice();
            
        }
    }