[Ethereum] How Do i Create a New Instance of a Contract

web3js

i am trying to figure out how i can deploy a new instance of a contract and setting some values by calling some of its methods.

i am trying to follow the example on the official docs website: https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#

const comment = new web3.eth.Contract(Comment.abi, { from: coinbase });

// comment object is available here. all properties at this point seem to be defined. the value is not null here.
console.log('comment:', comment);

comment.deploy()
    .send({ from: coinbase })
    .then(console.log);

but i get the error:

TypeError: Cannot read property 'send' of null

Best Answer

i found the answer:

the solution was to pass in the bytecode to the deploy method.

then i recieved another error about insufficient gas. so in the send method i set some gas.

import commentContract from '../build/contracts/Comment.json';

const comment = new web3.eth.Contract(Comment.abi, { from: coinbase });

comment.deploy({ data: commentContract.bytecode })
    .send({ from: coinbase, gas: 1500000 })
    .then(console.log);

this now works.

Related Topic