简体   繁体   English

所需气体超过限制:3000000。

[英]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.

相关问题 错误:返回错误:所需气体超过限额 (12487794) 或交易总是失败 - Error: Returned error: gas required exceeds allowance (12487794) or always failing transaction 进行交易时的 Solidity 错误 - 无法估算气体; 交易可能会失败或可能需要手动限制气体 - Solidity Error when doing a transaction - cannot estimate gas; transaction may fail or may require manual gas limit web3j-从已签署的交易中获取交易明细(金额,天然气价格,天然气限额) - web3j - Get transaction details (amount, gas price, gas limit) from signed transaction 在 Kadena Chainweaver 上部署合约时,交易未成功在区块中挖掘的错误:气体错误气体限制(600)超出 - Error with transaction not successfully mined in block while deploying a contract on Kadena Chainweaver: Gas Error Gas limit(600) exceeded ValueError:气体估计失败 - ValueError: Gas estimation failed "合约代码无法存储,请检查您的gas限制:以太坊智能合约部署失败:" - The contract code couldn't be stored, please check your gas limit : Ethereum Smart Contract Deployment Failed: 智能合约gas成本 - smart contract gas cost 为 swapExactTokensForTokens 估算 gas 时出错 - Error on estimating gas for swapExactTokensForTokens 钻头操作员和坚固性节省气体 - Bit operators and gas saving on solidity GAS ERC20令牌使用什么? - What GAS ERC20 tokens use?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM