合约代码无法存储,请检查您的gas限制:以太坊智能合约部署失败:

Posted

技术标签:

【中文标题】合约代码无法存储,请检查您的gas限制:以太坊智能合约部署失败:【英文标题】:The contract code couldn't be stored, please check your gas limit : Ethereum Smart Contract Deployment Failed: 【发布时间】:2018-11-29 14:51:09 【问题描述】:

我正在学习如何使用solidity、web3 和javascript 开发和部署以太坊智能合约

我已在 Ganache 上成功部署了合同。现在,当我尝试使用 truffle-hdwallet-providerRinkby Test Net 上部署它时,它只是失败了。

我使用 truffle-hdwallet-provider 成功创建了 web3 对象,并且我成功获取了帐户列表,但部署到测试网总是失败。

您可以在此处查看我的部署是否失败。

https://rinkeby.etherscan.io/address/0x2f20b8F61813Df4e114D06123Db555325173F178

这是我的deploy script

const HDWalletProvider = require('truffle-hdwallet-provider');
const Web3 = require ('web3');
const interface, bytecode = require('./compile');

const provider = new HDWalletProvider(
    'memonics',                         // this is correct 
    'https://rinkeby.infura.io/mylink'  // this is correct 
    );

const web3 = new Web3(provider);

const deploy = async() =>
    const accounts = await web3.eth.getAccounts();
    console.log('Attempting to deploy from account:', accounts[0]); //This excute fine
    try 
    const result = await new web3.eth.Contract(JSON.parse(interface)).deploy( data: bytecode, arguments: ['Hi There!']).send( from: accounts[0], gas: '1000000');
    console.log('Contract deployed to ', result.options.address);
    
    catch(err) 
        console.log('ERROR'); // Here I get error 
    



;
deploy();

这是我的合同

pragma solidity ^0.4.17;

contract Inbox
    string public message;

    constructor (string initialMessage) public 
        message = initialMessage;
    
    function setMessage(string newMessage) public 
        message = newMessage;
    

编辑:我尝试使用 Remix 并成功部署,但尝试使用 truffle-hdwallet-provider 时出现错误:无法使用合约代码已存储,请检查您的气体限制。我绑定了不同的气体值(可能达到最大值),但仍然没有结果。

【问题讨论】:

【参考方案1】:

我发现 bytecode 不包括 0x 导致了这个问题。

通过将 0x 与字节码连接解决了这个问题。

【讨论】:

【参考方案2】:

我遇到了这个问题,并通过将扁平代码粘贴到 Remix 中解决了这个问题,然后给出了更好的错误描述:我在合约中有一个接口我没有正确实现(错误的函数签名)。检查您的接口实现。尝试部署“抽象合约”会出现此错误。

【讨论】:

以上是关于合约代码无法存储,请检查您的gas限制:以太坊智能合约部署失败:的主要内容,如果未能解决你的问题,请参考以下文章

智能合约实战 gas gasprice gaslimit

智能合约实战 gas gasprice gaslimit

以太坊gas limit啥意思

支付用户的gas:以太坊

以太坊搭建常见错误,以太坊部署智能合约,Error: insufficient funds for gas * price + value

以太坊:从入门到实践