我正在尝试在 remix ide 中编写一个简单的智能合约,并一次又一次地遇到一般错误。以下是我的代码和错误
I am trying to write a simple smart contract in remix ide and getting encountered by a generic error again and again. Below is my code and error
代码:-
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract FundMe{
mapping(address => uint256) public addressToAmountFunded;
address public owner;
constructor() public {
owner = msg.sender;
}
function fund() public payable {
uint256 minimumUSD = 50 * 10 ** 18;
require(getConversionRate(msg.value) >= minimumUSD, "You need to spend more ETH!");
addressToAmountFunded[msg.sender] += msg.value;
}
function getVersion() public view returns (uint256){
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
return priceFeed.version();
}
function getPrice() public view returns(uint256){
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
(,int256 answer,,,) = priceFeed.latestRoundData();
// ETH/USD rate in 18 digit
return uint256(answer * 10000000000);
}
function getConversionRate(uint256 ethAmount) public view returns (uint256){
uint256 ethPrice = getPrice();
uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1000000000000000000;
// the actual ETH/USD conversation rate, after adjusting the extra 0s.
return ethAmountInUsd;
}
function withdraw() payable public {
require(msg.sender == owner);
payable(msg.sender).transfer(address(this).balance);
}
}
错误:-
气体估计失败
关
气体估计错误并显示以下消息(见下文)。事务执行很可能会失败。是否要强制发送?
执行恢复
基本上,当我在部署时在我的合约中传递任何 Wei、gwei 或以太币时,我会收到此错误,否则它会被部署。
当你部署你的合约时,你的 constructor
函数被调用,它没有被标记为 payable。
constructor() payable {
owner = msg.sender;
}
付钱就可以了
代码:-
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract FundMe{
mapping(address => uint256) public addressToAmountFunded;
address public owner;
constructor() public {
owner = msg.sender;
}
function fund() public payable {
uint256 minimumUSD = 50 * 10 ** 18;
require(getConversionRate(msg.value) >= minimumUSD, "You need to spend more ETH!");
addressToAmountFunded[msg.sender] += msg.value;
}
function getVersion() public view returns (uint256){
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
return priceFeed.version();
}
function getPrice() public view returns(uint256){
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
(,int256 answer,,,) = priceFeed.latestRoundData();
// ETH/USD rate in 18 digit
return uint256(answer * 10000000000);
}
function getConversionRate(uint256 ethAmount) public view returns (uint256){
uint256 ethPrice = getPrice();
uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1000000000000000000;
// the actual ETH/USD conversation rate, after adjusting the extra 0s.
return ethAmountInUsd;
}
function withdraw() payable public {
require(msg.sender == owner);
payable(msg.sender).transfer(address(this).balance);
}
}
错误:-
气体估计失败 关 气体估计错误并显示以下消息(见下文)。事务执行很可能会失败。是否要强制发送? 执行恢复
基本上,当我在部署时在我的合约中传递任何 Wei、gwei 或以太币时,我会收到此错误,否则它会被部署。
当你部署你的合约时,你的 constructor
函数被调用,它没有被标记为 payable。
constructor() payable {
owner = msg.sender;
}
付钱就可以了