0

我想使用最新的 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 转换为单位

4

1 回答 1

1

您可以使用以下语法对intto进行类型转换:uintuint256(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 {
    }
}
于 2021-11-01T02:09:56.133 回答