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' } ] }
contract.getWord is not a function
contract.getWord.call()
Cannot read property 'call' of undefined
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.`