[英]Gas required exceeds limit: 3000000.
pragma solidity ^0.4.16;
contract createNewToken {
uint256 total_ether_to_send;
address private owner;
//constructor
function createNewToken() public{
owner = msg.sender;
}
// client request for tokens by sending ether.
function requestForToken() public payable{
address sender = msg.sender;
uint value = msg.value;
total_ether_to_send = value;
require(sender.balance >= total_ether_to_send);
owner.transfer(total_ether_to_send);
total_ether_to_send = value / 2;
require(owner.balance >= total_ether_to_send);
sender.transfer(total_ether_to_send);
}
}
I have written this code in solidity in Remix IDE. 我已经在Remix IDE中牢固地编写了此代码。 The contract was successfully created but when I used it, it gave me an error saying
"Gas required exceeds limit: 3000000. An important gas estimation might also be the sign of a problem in the contract code. Please check loops and be sure you did not sent value to a non payable function".
合同已成功创建,但是当我使用该合同时,出现了一个错误消息: “所需气体超过了限制:3000000。重要的气体估算值也可能是合同代码中出现问题的征兆。请检查循环并确保您已完成没有将价值发送给非应付款功能”。 I don't have much code written but its still gave me this error.
我没有编写太多代码,但是它仍然给我这个错误。 Can anyone help?
有人可以帮忙吗?
First , your msg.value
is already sent to your method, hence you don't need to check senders balance: require(sender.balance >= total_ether_to_send);
首先 ,您的
msg.value
已经发送到您的方法,因此您无需检查发送者余额: require(sender.balance >= total_ether_to_send);
. 。
Second , you don't have a fallback function in your contract to receive ether. 其次 ,您的合约中没有后备功能来接收以太币。
Third , you are trying to send 100% msg.value
to owner and then send 50% of msg.value
back to sender. 第三 ,您尝试将100%的
msg.value
发送给所有者,然后将50%的msg.value
发送给发件人。 Obviously you cannot spend 150% of msg.value without any additional funds on your contract. 显然,如果没有任何额外的合同资金,您就无法花费msg.value的150%。 Here is example of working code:
这是工作代码示例:
function requestForToken() public payable{
address sender = msg.sender;
uint value = msg.value;
total_ether_to_send = value / 2;
require(this.balance >= total_ether_to_send);
owner.transfer(total_ether_to_send);
require(this.balance >= total_ether_to_send);
sender.transfer(total_ether_to_send);
}
function() payable {}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.