获取 Chainlink ETH/USD 喂价答案作为 uint256 而不是 int solidity
Get Chainlink ETH/USD Price Feed answer as uint256 instead of int solidity
我想使用以美元计价的最新 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
类型转换为 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 {
}
}
我想使用以美元计价的最新 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
类型转换为 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 {
}
}