混音:调用 Send2MySC.getRate 出错:执行恢复 > 从导入的合约调用函数时

Remix: call to Send2MySC.getRate errored: execution reverted > When call a function from an imported contract

在 Remix Rinkeby Network 中,我在同一文件夹中构建了 2 个合约:

  1. GetETHUSD.sol: 检索eth/usd汇率

  2. Send_2_SC:发送wei到这个合约,通过导入GetETHUSD.sol和硬编码合约地址

    查看eth/usd汇率

GetETHUSD.sol:

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.6.0 <0.9.0;

// Price Feed using ChainLink Interface: Aggregator V3

// Import the interface
import "@chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";

contract GetETHUSD {

// Call interface function version()
function getVersion() public view returns(uint256) {
    AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
    return priceFeed.version();
}

// Call function latestRoundData()
    //   uint80 roundId,
    //   int256 answer,
    //   uint256 startedAt,
    //   uint256 updatedAt,
    //   uint80 answeredInRound

// EthUsd Rate: answer 
function getLastPrice() public view returns(int256) {
    AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
    (,int256 answer,,,) = priceFeed.latestRoundData();
    return (answer * 10000000000);
}
}

Send_2_SC:

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.6.0 <0.9.0;

import "@chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
import "./GetETHUSD.sol";

contract Send2MySC {
    // Payable
    // tracking accounts that send: mapping + array
    // Calc accumalate amount
    // msg.sender msg.value

    // GetETHUSD.sol contract address
    address public contractAddress= 0x6629f30985449bC1F90F2AD8D6eCFA8b35821f79;

    // Get ETH/USD rate
    function getRate() public view returns(int256) {
        GetETHUSD b = GetETHUSD(contractAddress);
        return b.getLastPrice();
    }

    // Mapping 
    mapping( address => uint256) public addressToAmountSent;

    // To send eth we use payable
    function send() public payable {
        addressToAmountSent[msg.sender] += msg.value;
    }
     function scBalance() public view returns(uint256) {
        return address(this).balance;
    }

}

他们都编译没有错误:

我在哪里可以解决这个问题?我应该有 ABI 还是合约应该是一个接口?

将 GetETHUSD 智能合约地址等参数传递给 getRate() 函数。试试这个:

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.6.0 <0.9.0;

import "@chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
import "./GetETHUSD.sol";

contract Send2MySC {
    // Payable
    // tracking accounts that send: mapping + array
    // Calc accumalate amount
    // msg.sender msg.value

    // Get ETH/USD rate
    // NOTE: You must to specify the address of your GetETHUSDT smart contract to call its functions!
    function getRate(address contractAddress) public view returns(int256) {
        GetETHUSD b = GetETHUSD(contractAddress);
        return b.getLastPrice();
    }

    // Mapping 
    mapping( address => uint256) public addressToAmountSent;

    // To send eth we use payable
    function send() public payable {
        addressToAmountSent[msg.sender] += msg.value;
    }
     function scBalance() public view returns(uint256) {
        return address(this).balance;
    }

} 

注意:具体参见getRate()函数和注释。