9

Following https://medium.com/@mvmurthy/full-stack-hello-world-voting-ethereum-dapp-tutorial-part-1-40d2d0d807c2

I've tried to create an instance of a contract using: VotingContract = web3.eth.contract(abiDefinition) but I get: TypeError: web3.eth.contract is not a function So I've tried: VotingContract = new web3.eth.Contract(abiDefinition) as suggested here: web3.eth.contract is not a function when making contract

But then when I try to deploy the contract on my test blockchaing (launched with EthereumJS TestRPC) I get the following:

deployedContract = VotingContract.new(['Rama','Nick','Jose'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000})
TypeError: VotingContract.new is not a function
    at repl:1:38
    at ContextifyScript.Script.runInThisContext (vm.js:44:33)
    at REPLServer.defaultEval (repl.js:239:29)
    at bound (domain.js:301:14)
    at REPLServer.runBound [as eval] (domain.js:314:12)
    at REPLServer.onLine (repl.js:433:10)
    at emitOne (events.js:120:20)
    at REPLServer.emit (events.js:210:7)
    at REPLServer.Interface._onLine (readline.js:278:10)
    at REPLServer.Interface._line (readline.js:625:8)
    at REPLServer.Interface._ttyWrite (readline.js:905:14)
    at REPLServer.self._ttyWrite (repl.js:502:7)
    at ReadStream.onkeypress (readline.js:157:10)
    at emitTwo (events.js:125:13)
    at ReadStream.emit (events.js:213:7)
    at emitKeys (internal/readline.js:420:14)
    at emitKeys.next (<anonymous>)
    at ReadStream.onData (readline.js:1006:36)
    at emitOne (events.js:115:13)
    at ReadStream.emit (events.js:210:7)
    at addChunk (_stream_readable.js:252:12)
    at readableAddChunk (_stream_readable.js:239:11)
    at ReadStream.Readable.push (_stream_readable.js:197:10)
    at TTY.onread (net.js:589:20)

Any suggestions?

  • 1
    What's your web3.version? I found today that at some point after 1.0.0-beta.11 a bunch of the contract creation functionality broke, so I reverted from .15 to .11 and it's working again. – benjaminion Aug 7 '17 at 20:56
  • I had web3@1.0.0-beta.16, I've tried to move to web3@1.0.0-beta.11 using npm install web3@1.0.0-beta.11 as you suggested but I get the same error. – ilopezluna Aug 8 '17 at 10:20
  • With web3@0.20.1 I've been able to deploy the contract – ilopezluna Aug 11 '17 at 18:39
  • I am facing same issue and I am new to this kind of environment. Found any solution?? – Jimit Patel Nov 16 '17 at 7:09
3

The API for the web3.js v1.0 changed. The tutorial is using the web3 api v0.x.x. Thus, you must either update your node to use the proper API

npm install ethereumjs-testrpc web3@0.20.1

or, you must update the code to use the v1.0 implementation. I.E:

web3.eth.getAccounts(console.log);

You can also refer to the github from mjhm who did a great job converting the tutorial code to v.1.0. https://github.com/mjhm/hello_world_dapp

0

Use VotingContract = new web3.eth.Contract(abiDefinition). Worked for me.

  • 1
    The question is asking about deployedContract = VotingContract.new , he has already added the VotingContract = new web3.eth.Contract(abiDefinition) in the previous line. – Biranchi Dec 23 '17 at 4:16
  • In your package.json file, add: "dependencies": { "web3": "0.14.0" } instead of version 1.0 beta – Mr. T Dec 23 '17 at 16:46
  • The tutorial uses the 0.x not version 1.0. Took me a while to realise that. – Mr. T Dec 23 '17 at 16:47
0

You have to use the deploy function as shown in web3.js docs: https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#deploy as well as the asciiToHex utility function.

Prior to initializing the contract instance, type the following into your node console:

// to use the asciiToHex utility function
asciiToHex = Web3.utils.asciiToHex;

// put array of candidates into a variable
candidates = ['Rama', 'Nick', 'Jose'];

// set first test account testrpc created into account variable
web3.eth.getAccounts().then((accounts) => { account = accounts[0] });

Deploy the contract to the blockchain.

VotingContract.deploy({data: byteCode, arguments: [candidates.map(asciiToHex)]}).send({from: account, gas: 4700000}).then((result) => {deployedContract = result});

deployedContract is now the instance of the contract. You can check this by typing deployedContract.options.address into your node console.

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.