Solidity Chainlink – Fixing Errors When Using Latest Price Data in Remix

blockchainchainlinksolidity

Hello iam trying to get the latest price data using Chainlink Data Feeds but it throws this error :

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

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


contract DonateContract {
AggregatorV3Interface internal priceFeed;
address payable owner; // contract creator's address


constructor() {

priceFeed = 
AggregatorV3Interface(0x2514895c72f50D8bd4B4F9b1110F0D6bD2c97526);
owner = payable(msg.sender); // setting the contract creator

}

function donate(uint256 _payAmount) public payable  {

(bool success,) = owner.call{value: _payAmount}("");
 require(success, "Failed to send money");
 CRON token = CRON(0x2F718Bc4390F8662bB664D1FDd88494ac6bE71eC);
 uint256 cost =  _payAmount * 6 * priceFeed.latestRoundData ;
 token.transfer(owner,cost);
}  

}

Best Answer

Two things:

  1. priceFeed.latestRoundData() returns 5 data points:
uint80 roundID,  
int price,  
uint startedAt,
uint timeStamp,
uint80 answeredInRound

I'm assuming you want just the price which you can isolate with a line like this in your code:
(,int price,,,) = priceFeed.latestRoundData();

  1. price is of type int while your cost and _payAmount variables are of type uint. I recommend changing them to type int to match the feeds type. If you really need to you can typecast the price variable to uint or your cost and _payAmount variables to int with a line like this:
    int cost = int(_payAmount) * 6 * price;
Related Topic