INFURA / infura

Official Public Repository for INFURA
https://infura.io
381 stars 62 forks source link

Error "json: cannot unmarshal number into Go value of type string" when default block set #194

Open prographo opened 4 years ago

prographo commented 4 years ago

According to the documentation here: https://web3js.readthedocs.io/en/v1.2.0/web3-eth.html#defaultblock the default block can be set to a number. However when I try to set the number to the previous block, I get an error, as you can see below.

Recreate by making a file test_call.js, then execute using node:

const w3provider = 'wss://goerli.infura.io/ws/v3/<XXXXX>'
const Web3 = require('web3')
const web3 = new Web3(
  new Web3.providers.WebsocketProvider(w3provider)
)
;
( async () => {
  console.log("Start")
  const latestBlock = await web3.eth.getBlockNumber()
  console.log("latestBlock: " + latestBlock) 
  const previousBlock = latestBlock - 1 
  console.log("previousBlock: " + previousBlock) 
  web3.eth.defaultBlock = previousBlock
  console.log("Get balance of an address") 
  const accountBal = await web3.eth.getBalance( '0x1addbb6462cb45c91f0dd116c6480aa586de0a29' )
  console.log("Done")
  console.log(`(${accountBal})`)
})()

$ node test_call.js 
Start
latestBlock: 1693494
previousBlock: 1693493
Get balance of an address
(node:98199) UnhandledPromiseRejectionWarning: Error: Returned error: invalid argument 1: json: cannot unmarshal number into Go value of type string
HM999 commented 4 years ago

also fails using https endpoint