You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chainlink预言机调用失败:FundMe合约fund函数执行回退问题

问题描述

调用FundMe合约的fund()函数转入0.1 ETH时交易失败,终端交易信息如下:

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

Etherscan显示交易状态:

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

合约代码如下:

// 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;
    }
}
问题分析与解决方案

交易失败核心是两个问题导致require条件不满足触发回滚:

  • 价格喂料地址不匹配部署网络
    你用的0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419是以太坊主网的ETH/USD价格喂料地址,但从区块高度来看,你部署合约的是测试网(主网区块高度早已远超这个数值)。测试网需要对应地址,比如Goerli测试网用0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e,Sepolia测试网用0x694AA1769357215DE4FAC081bf1f309aDC325306。错误地址会导致获取的价格数据无效,计算出的USD金额不满足要求。

  • 最小金额与实际转入金额不匹配
    当前0.1 ETH的价值远低于50USD,就算价格喂料正常,也会触发You need to send more Eth的回滚提示。可以临时调低minimumUSD(比如改成1 * 10 ** 18即1USD)测试交易,或者转入足够金额的ETH。

修复步骤:

  1. 替换价格喂料地址为对应测试网的正确地址;
  2. 调整minimumUSD数值到当前0.1 ETH能达到的金额;
  3. 重新部署合约后再测试fund()函数。

内容的提问来源于stack exchange,提问作者Warlock

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 00:41:13