function getMaticAmountAndPrice(uint256 tokensToBuy, address priceFeed) public view returns (uint256, uint256) {
// get the latest MATIC price in USD
AggregatorV3Interface aggregator = AggregatorV3Interface(priceFeed);
(, int256 price, , , ) = aggregator.latestRoundData();
require(block.timestamp >= 10, "Price feed not ready");
uint256 maticPriceInUSD = uint256(price);
// get the total cost in MATIC for the desired amount of tokens
uint256 costInWei = tokenPrice * tokensToBuy;
uint256 costInMatic = costInWei / 1 ether;
// calculate the equivalent cost in USD based on the current MATIC price
uint256 costInUSD = costInMatic * maticPriceInUSD / 1e18;
return (costInMatic, costInUSD);
}发布于 2023-04-08 03:14:36
根据所提供的信息,为了解决问题,您应该检查两件事:
如果您能够共享整个合同,并提供您正在测试的环境的一些细节,这样我就可以提供更精确的建议,这将是有帮助的。
https://ethereum.stackexchange.com/questions/148668
复制相似问题