获取 Chainlink ETH/USD 价格反馈答案为 uint256 而不是 int solidity
Posted
技术标签:
【中文标题】获取 Chainlink ETH/USD 价格反馈答案为 uint256 而不是 int solidity【英文标题】:Get Chainlink ETH/USD Price Feed answer as uint256 instead of int solidity 【发布时间】:2021-12-15 09:21:55 【问题描述】:我想使用最新的 ETH 美元价格来计算我可以从 AAVE 借多少 USDC。
我遵循了所有教程:
interface AggregatorV3Interface
function decimals() external view returns (uint8);
function description() external view returns (string memory);
function version() external view returns (uint256);
// getRoundData and latestRoundData should both raise "No data present"
// if they do not have data to report, instead of returning unset values
// which could be misinterpreted as actual reported values.
function getRoundData(uint80 _roundId)
external
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
);
function latestRoundData()
external
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
);
将合约用于 ETH/USD 价格馈送:
https://etherscan.io/address/0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419
AggregatorV3Interface internal priceFeed = AggregatorV3Interface(0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419);
创建了获取价格的函数:
function getLatestPrice() public view returns (int)
(
uint80 roundID,
int price,
uint startedAt,
uint timeStamp,
uint80 answeredInRound
) = priceFeed.latestRoundData();
return price;
而我要调用的函数就是这个:
AaveLendingPool.borrow(address(USDT),getLatestPrice(), 1, 0, address(this));
这是我得到的错误:
TypeError: Type int256 is not implicitly convertible to expected type uint256.
我需要将 int 转换为单位
【问题讨论】:
【参考方案1】:您可以使用以下语法将int
类型转换为uint
:uint256(input)
例子:
pragma solidity ^0.8;
contract MyContract
function foo() external
borrow(uint256(getLatestPrice()));
function getLatestPrice() internal returns (int256)
return 1;
function borrow(uint256 _number) internal
【讨论】:
以上是关于获取 Chainlink ETH/USD 价格反馈答案为 uint256 而不是 int solidity的主要内容,如果未能解决你的问题,请参考以下文章