1

I'm trying to call a function in a test contract from Node.js with the below code...

const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.WebsocketProvider('ws://139.59.240.233:8546'));
socket.on('callContract', function(data) {
    var contract = new web3.eth.Contract(JSON.parse(data.abi), data.address);
    var result = contract.call().getWord()
    console.log(result);
});

The contract address is: 0xff58d94fe918c347b325c8b6fcde72d9f32e869d

The abi is:

[{"constant":false,"inputs":[{"name":"newWord","type":"string"}],"name":"setWord","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function","stateMutability":"nonpayable"},{"constant":true,"inputs":[],"name":"getWord","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function","stateMutability":"view"}]

the contract i deployed is:

pragma solidity ^0.4.0;
contract WordContract {
string word = "test";
function getWord() constant returns(string) {
    return word;
}
function setWord(string newWord) returns(string) {
 word = newWord;
 return word;
}
}

The error im getting is TypeError: contract.call is not a function

If i console.log(contract); i get

Contract {
  currentProvider: [Getter/Setter],
  _requestManager:
   RequestManager {
 provider:
  WebsocketProvider {
    responseCallbacks: {},
    notificationCallbacks: [Object],
    connection: [Object] },
 providers:
  { WebsocketProvider: [Function: WebsocketProvider],
    HttpProvider: [Function: HttpProvider],
    IpcProvider: [Function: IpcProvider] },
 subscriptions: {} },
  givenProvider: null,
  providers:
   { WebsocketProvider: [Function: WebsocketProvider],
     HttpProvider: [Function: HttpProvider],
     IpcProvider: [Function: IpcProvider] },
  _provider:
   WebsocketProvider {
 responseCallbacks: {},
 notificationCallbacks:
  [ [Function: requestManagerNotification],
    [Function: requestManagerNotification],
    [Function: requestManagerNotification],
    [Function: requestManagerNotification],
    [Function: requestManagerNotification],
    [Function: requestManagerNotification],
    [Function: requestManagerNotification],
    [Function: requestManagerNotification],
    [Function: requestManagerNotification],
    [Function: requestManagerNotification] ],
 connection:
  W3CWebSocket {
    _listeners: {},
    addEventListener: [Function: _addEventListener],
    removeEventListener: [Function: _removeEventListener],
    dispatchEvent: [Function: _dispatchEvent],
    _url: 'ws://139.59.240.233:8546',
    _readyState: 1,
    _protocol: undefined,
    _extensions: [],
    _bufferedAmount: 0,
    _binaryType: 'arraybuffer',
    _connection: [Object],
    _client: [Object],
    onerror: [Function],
    onclose: [Function],
    onmessage: [Function] } },
  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:
   { setWord: [Function: bound _createTxObject],
 '0xcd048de6': [Function: bound _createTxObject],
 'setWord(string)': [Function: bound _createTxObject],
 getWord: [Function: bound _createTxObject],
 '0xed40a8c8': [Function: bound _createTxObject],
 'getWord()': [Function: bound _createTxObject] },
  events: { allEvents: [Function: bound ] },
  _address: null,
  _jsonInterface:
   [ { constant: false,
   inputs: [Object],
   name: 'setWord',
   outputs: [Object],
   payable: false,
   type: 'function',
   stateMutability: 'nonpayable',
   signature: '0xcd048de6' },
 { constant: true,
   inputs: [],
   name: 'getWord',
   outputs: [Object],
   payable: false,
   type: 'function',
   stateMutability: 'view',
   signature: '0xed40a8c8' } ] }
7
  • I gave youe suggestion a try and i get a new error: contract.getWord is not a function
    – Bill
    Commented Oct 22, 2017 at 15:02
  • sorry, contract.getWord.call() Commented Oct 22, 2017 at 15:06
  • Eww it looked so good but i still got Cannot read property 'call' of undefined
    – Bill
    Commented Oct 22, 2017 at 15:09
  • removing the new from var contract = new web3.eth.Contract(JSON.parse(data.abi), data.address); ` resulted in: throw new Error('You need to instantiate using the "new" keyword.'); ^ Error: You need to instantiate using the "new" keyword.`
    – Bill
    Commented Oct 22, 2017 at 15:26
  • I haven't used webscockets, but only httpProvider, let me check Commented Oct 22, 2017 at 15:27

2 Answers 2

0

Your code should look like this:

// Note no "new" on the next line, and the lowercase "contract"
var contract = web3.eth.contract(JSON.parse(data.abi), data.address);
contract.getWord.call((err, result) => {
    if (err) {
        // handle error here
    }
    else {
        // use result here, console.log it to see the structure
    }
});
7
  • Thank you @smarx but again I'm getting contract.getWord.call((err, result) => { ^ TypeError: Cannot read property 'call' of undefined
    – Bill
    Commented Oct 22, 2017 at 15:40
  • and if I remove the new I get: throw new Error('Please use the "new" keyword to instantiate a web3.eth.contract() object!'); ^
    – Bill
    Commented Oct 22, 2017 at 15:42
  • and the lowercase c gives me: var contract = web3.eth.contract(JSON.parse(data.abi), data.address); ^ TypeError: web3.eth.contract is not a function
    – Bill
    Commented Oct 22, 2017 at 15:42
  • Uh, where are you getting web3 from? I'm using the latest from github.com/ethereum/web3.js.
    – user19510
    Commented Oct 22, 2017 at 17:11
  • "web3": "^1.0.0-beta.24" from npm install --save web3
    – Bill
    Commented Oct 22, 2017 at 17:13
0

With the latest version of Web3, you simply call

var contract = new web3.eth.Contract(JSON.parse(data.abi), data.address);
const result = await contract.getWord().call();

Your Answer

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

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