代码之家  ›  专栏  ›  技术社区  ›  Jason

未处理的PromisejectionWarning:错误:无法存储合同代码,请检查您的气体限值

  •  17
  • Jason  · 技术社区  · 7 年前

    我正在尝试将我的简单solidity智能合约部署到Rinkeby网络上,但我不断遇到错误:

    UnhandledPromisejectionWarning:错误:合同代码无法 已储存,请检查您的气体限值。

    我的实体代码很简单

    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();
    

    有趣的是,我过去能够成功部署,但当我创建一个新项目并重新编写相同的代码时,我现在遇到了这个错误。请帮忙!

    4 回复  |  直到 7 年前
        1
  •  36
  •   Kutay Ozdogru    7 年前

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

    如果你做到以下几点应该没问题,这对我很有用。

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

    然后,我运行了已成功部署的同一个契约。

    祝你好运

        2
  •  17
  •   Huy Vo    6 年前

    可以通过添加“0x”作为字节码的前缀来解决此问题:

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

    更多信息请访问 https://ethereum.stackexchange.com/a/47654

        3
  •  3
  •   alernerdev    6 年前

    我认为字节码被视为单个数字,而不是一系列字节。请尝试以下操作,而不是提交数据:字节码:

    data:'0x0' + bytecode
    

    它将字节码值“保留”为字符串

        4
  •  0
  •   Nitanshu Lokhande    3 年前

    也只需移除气田,让metamask决定气体极限。这种方式对我很有效。