web3 / web3.js

Collection of comprehensive TypeScript libraries for Interaction with the Ethereum JSON RPC API and utility functions.
https://web3js.org/
Other
19.16k stars 4.91k forks source link

getFeeHistory gives error when running local fork with hardhat #4510

Closed gndelia closed 2 years ago

gndelia commented 2 years ago

Is there an existing issue for this?

Current Behavior

Not sure if this is an error in web3.js or in hardhat, but after reading some docs and tracing the possible bug fix I think it should be fixed here (I might be wrong!)

When calling getFeeHistory in my app, using a local fork with hardhat, I get the following error

Error: Internal JSON-RPC error.
{
  "code": -32602,
  "message": "Errors encountered in param 0: Invalid value 1 supplied to : QUANTITY"
}

Expected Behavior

I expect the proper response of eth_feeHistory, something like

{
        "oldestBlock": "0xce1806",
        "baseFeePerGas": [
            "0x26a51093d0",
            "0x2532e26b6d"
        ],
        "gasUsedRatio": [
            0.35032733868710536
        ]
    }

Steps to Reproduce

(according to the docs, these are valid parameters)


web3.eth.getFeeHistory('0x1', 'latest', []).then(console.log).catch(console.error)

By running this with a local fork with hardhat, I get the above-mentioned error (See below for hardhat config)

Web3.js Version

1.5.3

Environment

Anything Else?

hardhat config is pretty simple

.'use strict'
require('dotenv').config()

module.exports = {
  defaultNetwork: 'hardhat',
  networks: {
    hardhat: {
      accounts: {
        accountsBalance: '10000000000000000000000',
        mnemonic: process.env.MNEMONIC
      },
      forking: {
        url: process.env.BASE_NODE_URL,
        blockNumber: 8
      }
    },
    mainnet: {
      url: process.env.BASE_NODE_URL,
      chainId: 1
    }
  }
}

hardhat version: 2.6.7

so I just run npx hardhat node and connect with Metamask to that local fork

Then, the code of the app just do what I wrote in the "Steps to Reproduce" section After a couple of hours of debugging; I found that if I change this line right here, updating the formatters for eth_feeHistory from

new Method({
  name: 'getFeeHistory',
  call: 'eth_feeHistory',
  params: 3,
  inputFormatter: [utils.toNumber, formatter.inputBlockNumberFormatter, null]
}),

to

new Method({
  name: 'getFeeHistory',
  call: 'eth_feeHistory',
  params: 3,
  // replace utils.toNumber here with utils.numberToHex
  inputFormatter: [utils.numberToHex, formatter.inputBlockNumberFormatter, null]
}),

then it works. I found this as part of implementing EIP-1559 for a dapp.

I tried to use this method with a fork with ganache-cli, but I also failed to make it work, but I am still unsure of the reasons, as the error was different.

Not sure if this is a problem in web3 or in hardhat though 😓

luu-alex commented 2 years ago

Hey, sorry to hear you've been having issues with an RPC call.

I installed the same version of web3js and was able to sucessfully perform getFeeHistory, perhaps it is hardhat?

Web3.eth.getFeeHistory('0x1', 'latest', []).then(console.log).catch(console.error)
Promise { <pending> }
> {
  baseFeePerGas: [ '0x30aea7270d', '0x2deb22f72d' ],
  gasUsedRatio: [ 0.27291628440852755 ],
  oldestBlock: '0xce36b8'
}

If theres anything that points to web3js being an issue let us know and we'll try to investigate it when we can :)

gndelia commented 2 years ago

Thanks for your response! I'll try to ping them on their side. I'm ok if you want to close this in the meantime. Thank you!

fvictorio commented 2 years ago

Hey @luu-alex, are you sure this isn't a problem in web3?

The eth_feeHistory section of the spec says that the first argument should be a hex-encoded unsigned string:

image

But here:

https://github.com/ChainSafe/web3.js/blob/3a3cb32b6434656131f0bfc4a8622130d19d2338/packages/web3-eth/src/index.js#L411

Web3 seems to be converting it to a plain number (as far as I understand, at least).

Some nodes might be more permissive in what they accept, which makes sense. But we try to follow the spec as much as possible, so that code that runs on Hardhat will run in any spec-compliant node.

luu-alex commented 2 years ago

Hey @fvictorio thank you for taking a look, that makes alot of sense. I have reopened the issue and will try to tackle this problem. @gndelia I was curious are you still facing issues with a fork from ganache-cli? If so what was the error.

gndelia commented 2 years ago

I get Method eth_feeHistory not supported. - but that might be that I just need to update ganache-cli 😬