Web3.js: How to create contract instance in nodejs

Created on 28 Jul 2017  路  2Comments  路  Source: ChainSafe/web3.js

I have geth running on localhost:

geth.exe --testnet --fast --cache 1024 --ipcpath \\.\pipe\geth.ipc --rpccorsdomain * --rpcport 8545 --rpc

If I do the following:

var Web3 = require('web3');
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
var abi = ...
web3.eth.Contract(abi,"0x5cb023C894D7838Ee7C0eE43AFD8D10D75Cd89bd");

This gives me an error: TypeError: Cannot redefine property: currentProvider on the last line, error happens inside web3-core\src\index.js:42:16

Second syntax, which one can find on the net:

web3.eth.contract(abi)

gives another error: TypeError: web3.eth.contract is not a function

What is the right syntax to execute contract from nodejs with geth?

Most helpful comment

Try creating object instead of just using as function:

obj = new web3.eth.Contract(abi, "0x5cb023C894D7838Ee7C0eE43AFD8D10D75Cd89bd");

All 2 comments

Try creating object instead of just using as function:

obj = new web3.eth.Contract(abi, "0x5cb023C894D7838Ee7C0eE43AFD8D10D75Cd89bd");

Dear @jdkanani , this helps!

but I can't access events then, please be so kind to take a look at my second problem: https://github.com/ethereum/web3.js/issues/958

WBR

Was this page helpful?
0 / 5 - 0 ratings