简体   繁体   中英

Unhandled promise rejection trying to deploy smart contract

I'm trying to deploy a simple contract to Ganache, like so:

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

After compiling the.sol file without any error, I tried to deploy the code using "node deploy.js", and end up getting the following error:

(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.

Thanks in advance for any help.

s1b

From their documentation at 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.

However, readFileSync returns a buffer if no encoding is specified.

You will need to pass the encoding for the file you are reading to readFileSync like fs.readFileSync('./build/FirstContract.bin', <your_encoding>); or convert the Buffer into a string before passing it as data to myContract.deploy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM