【问题标题】:Unhandled promise rejection trying to deploy smart contract尝试部署智能合约时未处理的承诺拒绝
【发布时间】:2020-07-29 16:16:32
【问题描述】:

我正在尝试向 Ganache 部署一个简单的合约,如下所示:

const fs = require('fs');
const Web3 = require('web3');
const web3 = new Web3('http://localhost:8545');
const bytecode = fs.readFileSync('./build/FirstContract.bin');
const abi = JSON.parse(fs.readFileSync('./build/FirstContract.abi'));

(async function () {
  const ganacheAccounts = await web3.eth.getAccounts();
  const myWalletAddress = ganacheAccounts[0];

  const myContract = new web3.eth.Contract(abi);

  myContract.deploy({
    data: bytecode
  }).send({
    from: myWalletAddress,
    gas: 5000000
  }).then((deployment) => {
    console.log('FirstContract was successfully deployed!');
    console.log('FirstContract can be interfaced with at this address:');
    console.log(deployment.options.address);
  }).catch((err) => {
    console.error(err);
  });
})();

编译.sol文件没有任何错误后,我尝试使用“node deploy.js”部署代码,最终得到以下错误:

(node:25233) UnhandledPromiseRejectionWarning: TypeError: this._deployData.startsWith is not a function
    at Object._encodeMethodABI (/opt/bak/eth/node_modules/web3-eth-contract/src/index.js:539:30)
    at Object._processExecuteArguments (/opt/bak/eth/node_modules/web3-eth-contract/src/index.js:858:39)
    at Object._executeMethod (/opt/bak/eth/node_modules/web3-eth-contract/src/index.js:883:54)
    at /opt/bak/eth/deploy.js:19:6
    ....
(node:25233) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

提前感谢您的帮助。

s1b

【问题讨论】:

    标签: javascript solidity smartcontracts


    【解决方案1】:

    来自他们在https://web3js.readthedocs.io/en/v1.2.11/web3-eth-contract.html#eth-contract的文档

    data - String:合约的字节码。在部署合约时使用。

    但是,如果没有指定编码,readFileSync 会返回一个缓冲区。

    您需要将正在读取的文件的编码传递给readFileSync,例如fs.readFileSync('./build/FirstContract.bin', <your_encoding>);,或者将缓冲区转换为字符串,然后再将其作为data 传递给myContract.deploy

    【讨论】:

      猜你喜欢
      • 2016-11-24
      • 1970-01-01
      • 1970-01-01
      • 2020-10-04
      • 2017-10-21
      • 2016-12-15
      • 1970-01-01
      • 2023-01-04
      相关资源
      最近更新 更多