【问题标题】:Get Chainlink ETH/USD Price Feed answer as uint256 instead of int solidity获取 Chainlink ETH/USD 价格反馈答案为 uint256 而不是 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 转换为单位

【问题讨论】:

    标签: ethereum solidity chainlink hardhat


    【解决方案1】:

    您可以使用以下语法将int 类型转换为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 {
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-02-04
      • 2018-11-25
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 2017-09-05
      • 2016-05-16
      • 2020-10-22
      • 2018-01-06
      相关资源
      最近更新 更多