尝试部署智能合约时未处理的承诺拒绝

Unhandled promise rejection trying to deploy smart contract

我正在尝试将一个简单的合约部署到 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

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

的文档

data - String: The byte code of the contract. Used when the contract gets deployed.

但是,如果未指定编码,readFileSync returns 缓冲区。

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