UnhandledPromiseRejectionWarning: Error: The contract code couldn't be stored, please check your gas limit

UnhandledPromiseRejectionWarning: Error: The contract code couldn't be stored, please check your gas limit

我正在尝试将我的简单 solidity 智能合约部署到 Rinkeby 网络上,但我一直收到错误消息:

UnhandledPromiseRejectionWarning: Error: The contract code couldn't be stored, please check your gas limit.

我的solidity代码很简单

pragma solidity ^0.4.18; 

contract Greetings{ 
  string public message; 

  function Greetings(string initialMessage) public{ 
    message = initialMessage;
  }  

  function setMessage(string newMessage) public {
    message = newMessage;
  }  
}

我的部署脚本是:

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

const provider = new HDWalletProvider(  
  'twelve word mnemonic...', 
  'https://rinkeby.infura.io/GLm6McXWuaih4gqq8nTY'    
);

const web3 = new Web3(provider);

const deploy = async () => {
    accounts = await web3.eth.getAccounts(); 

    console.log('attempting to deploy from account',accounts[0]);

    const result = await new web3.eth.Contract(JSON.parse(interface)) 
      .deploy({data:bytecode, arguments:['Hello World']})      
      .send({from: accounts[0], gas:'1000000'});                              

    console.log('Contract deployed to', result.options.address); 
};

deploy();

有趣的是,我曾经能够成功部署,但是当我创建一个新项目并重新执行相同的代码时,我现在得到这个错误。请帮忙!

有完全相同的问题!似乎是 "truffle-hdwallet-provider" 版本 0.0.5 中的错误引起的。在udemy课程中,它显然使用了“0.0.3”。

如果您执行以下操作应该没问题,它对我有用。

npm uninstall truffle-hdwallet-provider
npm install --save truffle-hdwallet-provider@0.0.3

然后我运行部署成功的同一个合约。

祝你好运!

我认为字节码被视为单个数字而不是一系列字节。不要提交 data:bytecode,而是尝试:

data:'0x0' + bytecode

它将"preserve"字节码值作为字符串

这个问题可以通过添加 '0x' 作为字节码的前缀来解决:

.deploy({ data: '0x' + bytecode, arguments: ['Hi there!'] })

更多信息位于 https://ethereum.stackexchange.com/a/47654

同样只是去掉气场让metamask决定gas limit。这种方式适合我。