2

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
| improve this question | | | | |
  • Glad to hear you sorted it out. I too found that the documentation was less than crystal-clear. – Fred Douglis Mar 21 '18 at 12:34
2

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.

| improve this answer | | | | |
  • 1
    May I suggest using truffle-contract though. – Nico Mar 21 '18 at 15:39
  • that seems to be a much better way of doing this. thanks for the advice. – X0r0N Mar 22 '18 at 12:03
-2

you need to instantiate it with .at(address)

So add

const commentInstance = comment.at(contractaddres)

Here's an example: getContract.js

  let casinoContract = web3.eth.contract(ABI)
let casinoContractInstance = casinoContract.at(address)
| improve this answer | | | | |
  • i dont have an address. because it hasn't been deployed. maybe my question was not clear. i want to deploy a new instance of a contract to the blockchain. – X0r0N Mar 20 '18 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.