Strange error - pls help me resolve

I’m pretty new to Ethereum, but have been making great progress, using Infura so I don’t have to host a node… until today.

I am getting the following error:

2019-11-21 16:50:21 - XXX(0x1addbb6462cb45c91f0dd116c6480aa586de0a29)
(node:51654) UnhandledPromiseRejectionWarning: Error: Returned error: invalid argument 1: json: cannot unmarshal number into Go value of type string
    at Object.ErrorResponse (/Users/u1h/Project/bp_chainNode/node_modules/web3-core-helpers/src/errors.js:29:16)
    at Object.<anonymous> (/Users/u1h/Project/bp_chainNode/node_modules/web3-core-requestmanager/src/index.js:140:36)
    at /Users/u1h/Project/bp_chainNode/node_modules/web3-providers-ws/src/index.js:127:44
    at Array.forEach (<anonymous>)
    at W3CWebSocket.WebsocketProvider.connection.onmessage (/Users/u1h/Project/bp_chainNode/node_modules/web3-providers-ws/src/index.js:104:36)
    at W3CWebSocket._dispatchEvent [as dispatchEvent] (/Users/u1h/Project/bp_chainNode/node_modules/yaeti/lib/EventTarget.js:107:17)
    at W3CWebSocket.onMessage (/Users/u1h/Project/bp_chainNode/node_modules/websocket/lib/W3CWebSocket.js:234:14)
    at WebSocketConnection.<anonymous> (/Users/u1h/Project/bp_chainNode/node_modules/websocket/lib/W3CWebSocket.js:205:19)
    at WebSocketConnection.emit (events.js:210:5)
    at WebSocketConnection.processFrame (/Users/u1h/Project/bp_chainNode/node_modules/websocket/lib/WebSocketConnection.js:554:26)
    at /Users/u1h/Project/bp_chainNode/node_modules/websocket/lib/WebSocketConnection.js:323:40
    at processTicksAndRejections (internal/process/task_queues.js:75:11)
(node:51654) 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)

This error is caused by this line:

 const accountBal = await web3.eth.getBalance( '0x1addbb6462cb45c91f0dd116c6480aa586de0a29' )

I am connecting to Goerli.

The error looks like Infura are running Geth and this is the error Geth is giving directly to them. I think this because it talks about “Go value of type string”.

I created a simplest example program to prove the error:

const w3provider = 'wss://goerli.infura.io/ws/v3/<SECRETKEY>'
const Web3 = require('web3')
const web3 = new Web3(
  new Web3.providers.WebsocketProvider(w3provider)
)
;
( async () => {
const accountBal = await web3.eth.getBalance( '0x1addbb6462cb45c91f0dd116c6480aa586de0a29' )
console.log(`(${accountBal})`)
})()

However this worked, returning the balance of that address.

So I am just really confused and stuck, because how can I move forward now?

I have found the exact problem and can reproduce in the following short script. Should this work?

Can someone else pls run the following and let me know if it works for you.

const w3provider = 'wss://goerli.infura.io/ws/v3/<SECRETKEY>'
const Web3 = require('web3')
const web3 = new Web3(
  new Web3.providers.WebsocketProvider(w3provider)
)

;

( async () => {
  console.log("X1")
  const latestBlock = await web3.eth.getBlockNumber()
  console.log("X2")
  web3.eth.defaultBlock = latestBlock - 1
  console.log("X3")
  const accountBal = await web3.eth.getBalance( '0x1addbb6462cb45c91f0dd116c6480aa586de0a29' )
  console.log("X4")
  console.log(`(${accountBal})`)
})()

Output:

/Project/bp_chainNode$ node test_call.js 
X1
X2
X3
(node:92621) UnhandledPromiseRejectionWarning: Error: Returned error: invalid argument 1: json: cannot unmarshal number into Go value of type string
    at Object.ErrorResponse (/Users/u1h/Project/bp_chainNode/node_modules/web3-core-helpers/src/errors.js:29:16)
    at Object.<anonymous> (/Users/u1h/Project/bp_chainNode/node_modules/web3-core-requestmanager/src/index.js:140:36)
    at /Users/u1h/Project/bp_chainNode/node_modules/web3-providers-ws/src/index.js:127:44
    at Array.forEach (<anonymous>)
    at W3CWebSocket.WebsocketProvider.connection.onmessage (/Users/u1h/Project/bp_chainNode/node_modules/web3-providers-ws/src/index.js:104:36)
    at W3CWebSocket._dispatchEvent [as dispatchEvent] (/Users/u1h/Project/bp_chainNode/node_modules/yaeti/lib/EventTarget.js:107:17)
    at W3CWebSocket.onMessage (/Users/u1h/Project/bp_chainNode/node_modules/websocket/lib/W3CWebSocket.js:234:14)
    at WebSocketConnection.<anonymous> (/Users/u1h/Project/bp_chainNode/node_modules/websocket/lib/W3CWebSocket.js:205:19)
    at WebSocketConnection.emit (events.js:210:5)
    at WebSocketConnection.processFrame (/Users/u1h/Project/bp_chainNode/node_modules/websocket/lib/WebSocketConnection.js:554:26)
    at /Users/u1h/Project/bp_chainNode/node_modules/websocket/lib/WebSocketConnection.js:323:40
    at processTicksAndRejections (internal/process/task_queues.js:75:11)

Hi @prographo welcome to the community and thank you for a very detailed report.

While we dig into why this specific functionality isn’t working, I would recommend trying to send your getBalance request using HTTP instead of WSS. We typically recommend non-subscription based requests to go through HTTP. WSS primary use is for subscriptions.

Hi Mike, thank you for getting back to me.

Is there an advantage to using HTTP and WSS for different requests?

I prefer just to use one endpoint, unless there is technical reason for using two different methods.

Nevertheless, I will look into using HTTP also.

Many thanks.

Technically requesting that data over WSS or HTTP shouldn’t behave any different. However there could be different handling in place inside the library you are using that prefers subscription based connections for WSS. I would use WSS for subs only unless you are writing the connection yourself without the use of a Web3 library.

1 Like

We looked into this further, it is possible that the web3 library is sending the block number as a number instead of as a hex string. We have not confirmed this though.

1 Like

I guess the problem with using the latest block is that it is unstable, i find that it is often changed in a re-org, so I get something from infura, the next block backs it out, the following block has it again… if I could use the previous to last block this would happen very often.

We do recommend lagging 2-3 blocks for this case. We have discussed adding block hash support to more methods which would be more reliable, but by default the nodes do not support this.

To make sure I understand your usage flow, you are asking eth_blockNumber for the latest block number and than feeding that into eth_getBalance for a specific address? Is there another piece that you’re connecting here based on the latest block number?

Sort of, I was getting the latest block number, then subtracting 1 from that, then setting web3.eth.defaultBlock with that value. (see above)

Ok looked back through your code and the issue seems to be that Web3.js is sending the block parameter as an integer and not a hex string. We recommend reaching out to them to confirm.

1 Like

Indeed, this seems to work:

  const latestBlock = await web3.eth.getBlockNumber()
  web3.eth.defaultBlock = web3.utils.numberToHex( latestBlock - 1 )

thx for help

1 Like