Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eth.defaultBlock must be set to hex of number not regular number #3244

Closed
prographo opened this issue Nov 27, 2019 · 1 comment · Fixed by #3247
Closed

eth.defaultBlock must be set to hex of number not regular number #3244

prographo opened this issue Nov 27, 2019 · 1 comment · Fixed by #3247
Labels
1.x 1.0 related issues Bug Addressing a bug

Comments

@prographo
Copy link

Expected behavior

According to the documentation here: https://web3js.readthedocs.io/en/v1.2.0/web3-eth.html#defaultblock

defaultBlock can be set to string like 'latest' or Number, it gives the example below:

// set the default block
web3.eth.defaultBlock = 231;

Actual behavior

When you next make call you will get error:

invalid argument 1: json: cannot unmarshal number into Go value of type string

See code at bottom.

Problem is you must supply Hex String of Number, so the following will work:

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

So either, change documentation, or change something else I think.

js code to reproduce the behavior

const w3provider = 'wss://goerli.infura.io/ws/v3/<SECRETKEY>'
const Web3 = require('web3')
const web3 = new Web3(
  new Web3.providers.WebsocketProvider(w3provider)
)
;
( async () => {
  const latestBlock = await web3.eth.getBlockNumber()
  web3.eth.defaultBlock = latestBlock - 1
  const accountBal = await web3.eth.getBalance( '0x1addbb6462cb45c91f0dd116c6480aa586de0a29' )
  console.log(`(${accountBal})`)
})()
@nivida nivida added 1.x 1.0 related issues Bug Addressing a bug labels Nov 28, 2019
@nivida
Copy link
Contributor

nivida commented Nov 28, 2019

Thanks for opening this issue! I will add the missing conversion to a hex string asap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.x 1.0 related issues Bug Addressing a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants