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:
priceFeed.latestRoundData()
returns 5 data points:I'm assuming you want just the price which you can isolate with a line like this in your code:
(,int price,,,) = priceFeed.latestRoundData();
price
is of typeint
while yourcost
and_payAmount
variables are of typeuint
. I recommend changing them to typeint
to match the feeds type. If you really need to you can typecast the price variable touint
or yourcost
and_payAmount
variables toint
with a line like this:int cost = int(_payAmount) * 6 * price;