Ganache-cli: TypeError: web3.eth.contract is not a function

Created on 2 Sep 2017  ·  18Comments  ·  Source: trufflesuite/ganache-cli

https://medium.com/@jamesray_56543/i-got-the-following-output-for-this-command-506cf84ead90

Command
> var Web3 = require(‘web3’);

Expected Behavior

I am not exactly sure what behaviour to expect.

Current Behavior

The output is:

TypeError: web3.eth.contract is not a function
    at repl:1:25
    at sigintHandlersWrap (vm.js:22:35)
    at sigintHandlersWrap (vm.js:73:12)
    at ContextifyScript.Script.runInThisContext (vm.js:21:12)
    at REPLServer.defaultEval (repl.js:346:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:545:10)
    at emitOne (events.js:101:20)
    at REPLServer.emit (events.js:188:7)
    at REPLServer.Interface._onLine (readline.js:230:10)
    at REPLServer.Interface._line (readline.js:576:8)
    at REPLServer.Interface._ttyWrite (readline.js:853:14)
    at REPLServer.self._ttyWrite (repl.js:618:7)
    at ReadStream.onkeypress (readline.js:117:10)
    at emitTwo (events.js:106:13)
    at ReadStream.emit (events.js:191:7)
    at emitKeys (internal/readline.js:389:14)
    at next (native)
    at ReadStream.onData (readline.js:963:36)
    at emitOne (events.js:96:13)
    at ReadStream.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at ReadStream.Readable.push (_stream_readable.js:134:10)
    at TTY.onread (net.js:547:20)

Steps to Reproduce (for bugs)

I'm not sure how to consistently reproduce this error, but here is the steps that I have followed to get to this error: https://medium.com/@jamesray_56543/i-got-the-following-output-for-this-command-506cf84ead90

Possible solution

Maybe being able to use testrpc will fix the issue. I'm not sure.

Context

https://medium.com/@jamesray_56543/i-got-the-following-output-for-this-command-506cf84ead90

Your Environment

  • Version used:
  • Environment name and version (e.g. PHP 5.4 on nginx 1.9.1):
  • Server type and version:
  • Operating System and version: Ubuntu Desktop 16.04 LTS

Most helpful comment

try with VotingContract = new web3.eth.Contract(abiDefinition)

All 18 comments

Did you set the NODE_PATH variable to node_modules? I did that and was able to load web3?
Contract can be loaded by
mycontract = new web3.eth.Contract(abi)
This is explained here.
https://ethereum.stackexchange.com/questions/22983/web3-eth-contract-is-not-a-function-when-making-contract
I am able to get through all this, but unable to use the new operator on contract.

I don't recall using NODE_PATH, I just followed the instructions in the LLL tutorials by Daniel Ellison.

I typed node first, then it prompted with >

I edited my post.

So according to that article:
I changed the web3 version to 0.20.0 in my package.json.

I then entered:
/opt/node/lib/node_modules/ethereumjs-testrpc/build/cli.node.js
in a new terminal window.
The output listed the testrpc v4.1.1, available accounts, private keys, HD wallet and "Listening on localhost:8545"
I then opened a new terminal, typed

$ node
> var Web3 = require('web3');
undefined

Well it seems like undefined is not a good output, but moving on...

> var web3 = new Web3(new Web3.providers.HttpProvider(“http://localhost:8545"));
undefined
> var abi = [{"name":"identity","type":"function","constant":true,"payable":false,"inputs":[{"name":"input","type":"uint256"}],"outputs":[{"name":"","type":"uint256"}]}]
undefined
var Tutorial = web3.eth.contract(abi);
TypeError: web3.eth.contract is not a function
    at repl:1:25
    at sigintHandlersWrap (vm.js:22:35)
    at sigintHandlersWrap (vm.js:73:12)
    at ContextifyScript.Script.runInThisContext (vm.js:21:12)
    at REPLServer.defaultEval (repl.js:346:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:545:10)
    at emitOne (events.js:101:20)
    at REPLServer.emit (events.js:188:7)
    at REPLServer.Interface._onLine (readline.js:230:10)
    at REPLServer.Interface._line (readline.js:576:8)
    at REPLServer.Interface._ttyWrite (readline.js:853:14)
    at REPLServer.self._ttyWrite (repl.js:618:7)
    at ReadStream.onkeypress (readline.js:117:10)
    at emitTwo (events.js:106:13)
    at ReadStream.emit (events.js:191:7)
    at emitKeys (internal/readline.js:389:14)
    at next (native)
    at ReadStream.onData (readline.js:963:36)
    at emitOne (events.js:96:13)
    at ReadStream.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at ReadStream.Readable.push (_stream_readable.js:134:10)
    at TTY.onread (net.js:547:20)

Did you try

Web3.eth.contract

+1

Sorry I haven't got time to look at this now. I'll revisit if I want to use LLL. At the moment I'm finishing editing the yellow paper, so I'll probably go back to learning LLL for testing after that.

try with VotingContract = new web3.eth.Contract(abiDefinition)

@jamesray1 thanks for reporting, but I'm not sure that this is a TestRPC issue? You can see how we instantiate a web3 contract from the solidity compiler's output in the ganache-core test suite. At the moment we use web3 ^0.19.1. I'd imagine this could easily be adapted to work from the LLL example you mentioned.

I recommend raising an issue with the web3 project if this is still a problem.

I'm still not interested in using LLL (and thus trying to use web3 and testRPC) as I have other priorities related to focusing on research, e.g. scalability and Casper.

Requires 'c' capital of contract. Additionally requires new keyword. So it should be

VotingContract = new web3.eth.Contract(abiDefinition)

The output

Contract {
  currentProvider: [Getter/Setter],
  _requestManager: 
   RequestManager {
     provider: HttpProvider { host: 'http://localhost:8545', timeout: 0, connected: false },
     providers: 
      { WebsocketProvider: [Function: WebsocketProvider],
        HttpProvider: [Function: HttpProvider],
        IpcProvider: [Function: IpcProvider] },
     subscriptions: {} },
  givenProvider: null,
  providers: 
   { WebsocketProvider: [Function: WebsocketProvider],
     HttpProvider: [Function: HttpProvider],
     IpcProvider: [Function: IpcProvider] },
  _provider: HttpProvider { host: 'http://localhost:8545', timeout: 0, connected: false },
  setProvider: [Function],
  BatchRequest: [Function: bound Batch],
  extend: 
   { [Function: ex]
     formatters: 
      { inputDefaultBlockNumberFormatter: [Function: inputDefaultBlockNumberFormatter],
        inputBlockNumberFormatter: [Function: inputBlockNumberFormatter],
        inputCallFormatter: [Function: inputCallFormatter],
        inputTransactionFormatter: [Function: inputTransactionFormatter],
        inputAddressFormatter: [Function: inputAddressFormatter],
        inputPostFormatter: [Function: inputPostFormatter],
        inputLogFormatter: [Function: inputLogFormatter],
        inputSignFormatter: [Function: inputSignFormatter],
        outputBigNumberFormatter: [Function: outputBigNumberFormatter],
        outputTransactionFormatter: [Function: outputTransactionFormatter],
        outputTransactionReceiptFormatter: [Function: outputTransactionReceiptFormatter],
        outputBlockFormatter: [Function: outputBlockFormatter],
        outputLogFormatter: [Function: outputLogFormatter],
        outputPostFormatter: [Function: outputPostFormatter],
        outputSyncingFormatter: [Function: outputSyncingFormatter] },
     utils: 
      { _fireError: [Function: _fireError],
        _jsonInterfaceMethodToString: [Function: _jsonInterfaceMethodToString],
        randomHex: [Function: randomHex],
        _: [Object],
        BN: [Object],
        isBN: [Function: isBN],
        isBigNumber: [Function: isBigNumber],
        isHex: [Function: isHex],
        isHexStrict: [Function: isHexStrict],
        sha3: [Object],
        keccak256: [Object],
        soliditySha3: [Function: soliditySha3],
        isAddress: [Function: isAddress],
        checkAddressChecksum: [Function: checkAddressChecksum],
        toChecksumAddress: [Function: toChecksumAddress],
        toHex: [Function: toHex],
        toBN: [Function: toBN],
        bytesToHex: [Function: bytesToHex],
        hexToBytes: [Function: hexToBytes],
        hexToNumberString: [Function: hexToNumberString],
        hexToNumber: [Function: hexToNumber],
        toDecimal: [Function: hexToNumber],
        numberToHex: [Function: numberToHex],
        fromDecimal: [Function: numberToHex],
        hexToUtf8: [Function: hexToUtf8],
        hexToString: [Function: hexToUtf8],
        toUtf8: [Function: hexToUtf8],
        utf8ToHex: [Function: utf8ToHex],
        stringToHex: [Function: utf8ToHex],
        fromUtf8: [Function: utf8ToHex],
        hexToAscii: [Function: hexToAscii],
        toAscii: [Function: hexToAscii],
        asciiToHex: [Function: asciiToHex],
        fromAscii: [Function: asciiToHex],
        unitMap: [Object],
        toWei: [Function: toWei],
        fromWei: [Function: fromWei],
        padLeft: [Function: leftPad],
        leftPad: [Function: leftPad],
        padRight: [Function: rightPad],
        rightPad: [Function: rightPad],
        toTwosComplement: [Function: toTwosComplement] },
     Method: [Function: Method] },
  clearSubscriptions: [Function],
  options: { address: [Getter/Setter], jsonInterface: [Getter/Setter] },
  defaultAccount: [Getter/Setter],
  defaultBlock: [Getter/Setter],
  methods: 
   { totalVotesFor: [Function: bound _createTxObject],
     '0x2f265cf7': [Function: bound _createTxObject],
     'totalVotesFor(bytes32)': [Function: bound _createTxObject],
     validCandidate: [Function: bound _createTxObject],
     '0x392e6678': [Function: bound _createTxObject],
     'validCandidate(bytes32)': [Function: bound _createTxObject],
     votesReceived: [Function: bound _createTxObject],
     '0x7021939f': [Function: bound _createTxObject],
     'votesReceived(bytes32)': [Function: bound _createTxObject],
     candidateList: [Function: bound _createTxObject],
     '0xb13c744b': [Function: bound _createTxObject],
     'candidateList(uint256)': [Function: bound _createTxObject],
     voteForCandidate: [Function: bound _createTxObject],
     '0xcc9ab267': [Function: bound _createTxObject],
     'voteForCandidate(bytes32)': [Function: bound _createTxObject] },
  events: { allEvents: [Function: bound ] },
  _address: null,
  _jsonInterface: 
   [ { constant: false,
       inputs: [Object],
       name: 'totalVotesFor',
       outputs: [Object],
       payable: false,
       stateMutability: 'nonpayable',
       type: 'function',
       signature: '0x2f265cf7' },
     { constant: false,
       inputs: [Object],
       name: 'validCandidate',
       outputs: [Object],
       payable: false,
       stateMutability: 'nonpayable',
       type: 'function',
       signature: '0x392e6678' },
     { constant: true,
       inputs: [Object],
       name: 'votesReceived',
       outputs: [Object],
       payable: false,
       stateMutability: 'view',
       type: 'function',
       signature: '0x7021939f' },
     { constant: true,
       inputs: [Object],
       name: 'candidateList',
       outputs: [Object],
       payable: false,
       stateMutability: 'view',
       type: 'function',
       signature: '0xb13c744b' },
     { constant: false,
       inputs: [Object],
       name: 'voteForCandidate',
       outputs: [],
       payable: false,
       stateMutability: 'nonpayable',
       type: 'function',
       signature: '0xcc9ab267' },
     { inputs: [Object],
       payable: false,
       stateMutability: 'nonpayable',
       type: 'constructor' } ] }

Thanks for the tip. I'll keep it in mind if I use LLL in future. Perhaps the code should be corrected in the LLL tutorials.

Actually I think that my issue was caused by using ‘web3’ rather than 'web3'. Similarly "foo" should be used rather than “foo”.

also a source of confusion web3.eth.contract(...) was from web3 0.x, while new web3.eth.Contract(...) is from the 1.0 beta. So if you're using the latest _stable_ version, it's the prior, or if you're on the beta (which I think they've tagged as latest so it's what you get when you npm install web3), it's the latter.

help me,I use this :
var abiToken = new web3.eth.Contract(abiAdress).at(aAddress);

end:
TypeError: (intermediate value).at is not a function

@tangjielong Did you get qet a fix? Stuck with the same issue.

same issue,,pls solve........
contractInstance = VotingContract.at('0x2a9c1d265d06d47e8f7b00ffa987c9185aecf672');
^
TypeError: VotingContract.at is not a function

same issue,,pls solve........
contractInstance = VotingContract.at('0x2a9c1d265d06d47e8f7b00ffa987c9185aecf672');
^
TypeError: VotingContract.at is not a function

怎么解决的?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

varasev picture varasev  ·  3Comments

zatsepinvl picture zatsepinvl  ·  5Comments

Tectract picture Tectract  ·  3Comments

kumavis picture kumavis  ·  3Comments

DavidKuennen picture DavidKuennen  ·  4Comments