34

Whenever I try and make a contract, I get:

web3.eth.contract is not a function

I'm using this code:

const path = require('path');
const fs = require('fs');
const solc = require('solc');
const Web3 = require('web3');

const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

// Compile the source code
let input = fs.readFileSync('./contracts/ProofOfExistence3.sol', 'utf8');
let output = solc.compile(input, 1);

let abi = JSON.parse(output.contracts[':ProofOfExistence3'].interface);
let bytecode = output.contracts[':ProofOfExistence3'].bytecode;

let gasEstimate = web3.eth.estimateGas({data: bytecode}).then(console.log);

// Contract object
let MyContract = web3.eth.contract(abi);

And if I only run this code below, I get undefined. The same code works in the browser but not in nodejs with the official web3 package.

const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

console.log(web3.eth.contract);

Returns undefined.

Any ideas? I'm running "testrpc" locally.

3
  • 2
    I think this may be because you are using the new web3 1.0.0-beta.11. Try the documentation from web3js.readthedocs.io/en/1.0/…, or with the previous version 0.20.0.
    – Ismael
    Jul 25, 2017 at 1:33
  • 1
    Maybe if you try with MyContract = web3.eth.contract(abi).at(contractAdress)
    – Gawey
    Jul 25, 2017 at 12:17
  • I'm experiencing the same issue but i'm using geth and its default console running a private chain builted up with geth+puppeth. I deploy my contract simply pasting the web3deply code from remix in the client console (opened by geth) then i instanciate the contract on a var using var myContract = web3.eth.contract(ABI); on any client of my chain. Now i call its functions using: myContract.at("0x9f8cedf1f93e46fac74cb9415db8bbec85f239b4").method(); Is this the right procedure? I mean, the other one listed on web3 documentation does not work for me, since i get back Contract is not a function I a Apr 21, 2018 at 10:49

7 Answers 7

33

Solution:

MyContract = new web3.eth.Contract(abi)

Docs https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#eth-contract

When using version 1.0.x you need to pass in the Application Binary Interface, i.e. abi. And use the new keyword.

10

I came this issue when I use web3 0.19.

For web3 1.0:

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider('http://127.0.0.1:8545/'));

new web3.eth.Contract(abiArray, contract_address, {from: address})
1
  • 3
    With a capital C on Contract ! Jul 2, 2018 at 8:29
9

As Ismael suggested, setting the web3 version to 0.20.0 in my package.json allowed me to use this code.

npm install ethereumjs-testrpc [email protected]

Ideally, I would update my code to work with latest package but happy with this solution for now.

3
  • 2
    switching to 0.20.0 also worked for me, I could get the instance of the contract with: let svd = new web3.eth.Contract(abi, scAddress); but wasn't able to call functions, always throw an error cannot read property length
    – Sepultura
    Jul 25, 2017 at 18:13
  • @Sepultura I encountered the same issue
    – ErwanLent
    Jul 25, 2017 at 18:37
  • It worked for me as well - my usage: npm install ethereumjs-testrpc [email protected]
    – Jazzmine
    Oct 22, 2017 at 4:09
5

Adding to answer from @David

Smartcontract with web3 1.0.0-beta.31

In 1.0.0-beta... most is handled via Promises now, hence you could do:

var MyContract = new web3.eth.Contract({abi}, '0x123....', {
    from: '0x456...', // default from address
    gasPrice: '20000000000' // default gas price in wei, 20 gwei in this case
});

Example: Get Token Balance:

MyContract.methods.balanceOf('0x456...').call()
    .then(function(result){
    //the result holds your Token Balance that you can assign to a var
    var myTokenBalance = result;
    return result;
});
  • Where {abi} is the ABI Interface of your Contract 0x123.....
  • Where 0x123.... is the address of your Contract.
  • Where 0x456.... is the address of your Ethereum Account that holds the Tokens (i.e. the ETH account you wish to retrieve its Token Balance from)

Note: I am using the IPC Provider geth.ipc to interact with my node (as I am on the same PC, localhost, hence more save instead of using HTTP request).

  if (typeof web3 !== 'undefined') {
    web3 = new Web3(web3.currentProvider);
  } else {
    var net = require('net');
    var web3 = new Web3('/home/yourHomeFolder/.ethereum/geth.ipc', net);
  };

That way you shouldn't get the error anymore

web3.eth.contract is not a function

1

Requires c capital of contract(Here: web3.eth.Contract), So it should be

var contractAbi = new web3.eth.Contract(abi)

look that is C and not c in: web3.eth.Contract

0

Installing web3 version 0.20.0 will resolve the issue. To install,

npm install ethereumjs-testrpc [email protected]
0

I have also answered it here

You just need to install a stable version of Web3:

npm install web3@^0.19.0 --save
1
  • I'm using this version itself and yet getting error: contract.deploy is not a function. Any ideas?
    – Ani
    Nov 16, 2018 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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