简体   繁体   中英

Why is Chainlink oracle function call failing?

While attempting to fund me contract is tell me it encountered an error without specifying the error. I attempted to fund 0.1 eth through the fund function, and in the terminal it says:

[block:8404521 txIndex:12]
from: 0x8a9...e4303
to: FundMe.fund() 0x542...E109C
value: 100000000000000000 wei
data: 0xb60...d4288
logs: 0
hash: 0x29a...97939

and in the etherscan it says:status fail:

Contract 0x5422f3458be343e378e7a399e16fff548e7e109c
 Warning! Error encountered during contract execution [execution reverted] 

I tried looking for problems with my code and found none.

// SPDX-License-Identifier: MIT

pragma solidity >=0.6.6 <0.9.0;

import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
//import "@chainlink/contracts/src/v0.8/vendor/SafeMathChainlink.sol"; won't need in later complier versions.
contract FundMe {
    

    mapping(address => uint256) public addressToAmountFunded;

    function fund() public payable {
        uint256 minimumUSD = 50 * 10 ** 18;
        require( getConversionRate(msg.value) >= minimumUSD,"You need to send more Eth");
        addressToAmountFunded[msg.sender] += msg.value;
    }

    function getVersion() public view returns (uint256){
        AggregatorV3Interface priceFeed = AggregatorV3Interface(0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419);
        return priceFeed.version();
    }

    function getPrice() public view returns (uint256){
        AggregatorV3Interface priceFeed = AggregatorV3Interface(0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419);
        (,int256 answer,,,)=priceFeed.latestRoundData();
        return uint256(answer * 10000000000);
    }
    //10000000000 = Gwei which is why we added 10 zeros to getPrice(answer) to convert it to Wei amount
    function getConversionRate(uint256 ethAmount) public view returns (uint256){
        uint256 ethPrice = getPrice();
        uint256 ethAmountInUsd = (ethPrice * ethAmount)/ 1000000000000000000; //divide 2x because we added 10*((getPrice)answer))
        return ethAmountInUsd;
    }
}
  • Aggregator contract address "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419" belongs to mai.net

From here get the ETH/USD goerli tes.net address:"0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e"

  • Now in order to call chainlink oracle you have to have LINK token in your contract. Get some link token to your conract address from the faucet

  • Import the token in metamask. you will see the amount

  • send link token from your metamask to your contract

  • deploy your contract. if you are using Remix IDE chose the injected provider to connect to metamask. Because chainlink contract is on goerli, so you need to be on Goerli tes.net. Once deployment goes through you can call the fund function.

  • Since the fund function has no argument, you need to send the value alongside the transaction. That is why inside the function you have msg.value to access to the sent amount.

  • In Remix ide, under "GAS LIMIT input there is VALUE input. you need to pass the amount in there before you call the fund` function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM