I'm aware this is a duplicate, but the other never got authoritative answers and this is a simpler demonstration of the problem.
I created this Contract:
https://etherscan.io/address/0xfc7e86dbd205d02f97316995d8ace5d0afb9fe62#readContract
You can see in the Read Contract section that ownersNum is a uint with a current value of 1.
I copied the address
0xFc7e86dBD205D02F97316995d8AcE5D0AFB9fe62 and validated with https://tokenmarket.net/ ethereum-address-validator
cat keyaddress.js
module.exports = '0xFc7e86dBD205D02F97316995d8AcE5D0AFB9fe62'
In my node server, I created abi.js and copied the abi from etherscan
cat abi.js
module.exports = [{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"owners","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"ownersNum","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"payout","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"developer","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"buyKey","outputs":[{"name":"success","type":"bool"}],"payable":true,"stateMutability":"payable","type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"buyer","type":"address"}],"name":"QuantumPilotKeyPurchased","type":"event"}];
Now I try to read the ownersNum value in memory:
const fs = require('fs');
const express = require('express');
const Web3 = require('web3');
const BigNumber = require('bignumber.js');
const sigUtil = require('eth-sig-util');
const abi = require('./abi');
const keyaddress = require('./keyaddress');
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
let c = new web3.eth.Contract(abi, keyaddress);
console.log(c);
let on = c.methods.ownersNum().call();
console.log(on);
return;
// below is never reached, the above crashes
// on.then(function(a,b) {
// console.log(a);
// console.log(b);
// });
And this happens
... (contract details)
name: 'buyKey',
outputs: [Array],
payable: true,
stateMutability: 'payable',
type: 'function',
signature: '0xe95db6f9' },
{ anonymous: false,
inputs: [Array],
name: 'QuantumPilotKeyPurchased',
type: 'event',
signature: '0x55985f5332be13e699aaa51a5d3c003941f60e74af84533ebab5626b72cf3f51' } ] }
Promise { <pending> }
(node:5079) UnhandledPromiseRejectionWarning: Error: Couldn't decode uint256 from ABI: 0x
at SolidityTypeUInt.formatOutputUInt [as _outputFormatter] (/root/scan/node_modules/web3-eth-abi/src/formatters.js:174:15)
at SolidityTypeUInt.SolidityType.decode (/root/scan/node_modules/web3-eth-abi/src/type.js:252:17)
at /root/scan/node_modules/web3-eth-abi/src/index.js:327:49
at Array.forEach (<anonymous>)
at ABICoder.decodeParameters (/root/scan/node_modules/web3-eth-abi/src/index.js:326:13)
at Contract._decodeMethodReturn (/root/scan/node_modules/web3-eth-contract/src/index.js:459:22)
at Method.outputFormatter (/root/scan/node_modules/web3-eth-contract/src/index.js:812:46)
at Method.formatOutput (/root/scan/node_modules/web3-core-method/src/index.js:163:54)
at sendTxCallback (/root/scan/node_modules/web3-core-method/src/index.js:475:33)
at /root/scan/node_modules/web3-core-requestmanager/src/index.js:147:9
at XMLHttpRequest.request.onreadystatechange (/root/scan/node_modules/web3-providers-http/src/index.js:77:13)
at XMLHttpRequestEventTarget.dispatchEvent (/root/scan/node_modules/xhr2/lib/xhr2.js:64:18)
at XMLHttpRequest._setReadyState (/root/scan/node_modules/xhr2/lib/xhr2.js:354:12)
at XMLHttpRequest._onHttpResponseEnd (/root/scan/node_modules/xhr2/lib/xhr2.js:509:12)
at IncomingMessage.<anonymous> (/root/scan/node_modules/xhr2/lib/xhr2.js:469:24)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
(node:5079) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:5079) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
This used to work fine while I was testing on Kovan. Now I switch to MainNet, upgrade web3, getting this error. I was on beta27, upgraded today to 34, same error, slightly different printout but same result.
EDIT:
I tried installing 0.20, here's my results:
const fs = require('fs');
const express = require('express');
const Web3 = require('web3');
const BigNumber = require('bignumber.js');
const sigUtil = require('eth-sig-util');
const abi = require('./abi');
const keyaddress = require('./keyaddress');
l = console.log;
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
let Keystore = web3.eth.contract(abi);
let c = Keystore.at(keyaddress);
console.log(c.ownersNum.call());
let x = c.ownersNum.call();
console.log(x.toString());
console.log(x.toNumber());
return
Output
BigNumber { s: 1, e: 0, c: [ 0 ] }
0
0
this is incorrect. The value of ownersNum is 1.
