Search code examples
functionsmartcontractsremiximport-contacts

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


In Remix Rinkeby Network, I have built 2 contracts in the same folder:

  1. GetETHUSD.sol: to retrieve eth/usd exchange rate

  2. Send_2_SC: send wei to this contract and check eth/usd exchange rate by importing GetETHUSD.sol and hard code contract address

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;
    }

}

They both compile with no error:

  • GetETHUSD.sol works when I use it directly
  • Send2MySC: --- when I use its own functions (send(), scBalance()): it works --- when I use getRate() imported: I get call to Send2MySC.getRate errored: execution reverted

Where do can I look to fix this? Should I have the ABI or should the contract be an interface?


Solution

  • Pass the GetETHUSD smart contract address like parameter into getRate() function. Try this:

    // 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;
        }
    
    } 
    

    Note: In particular see getRate() function and the note.