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

Client doesn't work correctly with anvil #2450

Closed
1 task done
poolpitako opened this issue Jun 28, 2024 · 7 comments
Closed
1 task done

Client doesn't work correctly with anvil #2450

poolpitako opened this issue Jun 28, 2024 · 7 comments
Labels
needs reproduction Misc: Needs Reproduction

Comments

@poolpitako
Copy link

Check existing issues

Viem Version

2.16.4

Current Behavior

Calling client.getBlockNumber() or client.getChainId() doesn't work correctly with anvil.

Expected Behavior

viem should work correctly with anvil.

Steps To Reproduce

Start an anvil fork

anvil --chain-id 100 --fork-url "http://my_node_ip:my_node_port" --host "0.0.0.0" --block-time 30

Run the following code:

import { createPublicClient, http } from 'viem'
import { gnosis } from 'viem/chains'

const client = createPublicClient({
  chain: gnosis,
  transport: http("http://localhost:8545"),
  //transport: http("http://my_node_ip:my_node_port"),
})

const main = async () => {
    console.log("block #", await client.getBlockNumber())
    console.log("chain id", await client.getChainId())
}

main()

You will notice that using a random node it works but it fails when using anvil.

Link to Minimal Reproducible Example

No response

Anything else?

Working

$ curl --header "Content-Type: application/json" -X POST --data '{"method":"eth_chainId","id":1,"jsonrpc":"2.0"}' http://localhost:8545
{"jsonrpc":"2.0","id":1,"result":"0x64"}%

Not working

$ curl --header "Content-Type: application/json" -X POST --data '{"method":"eth_chainId"}' http://localhost:8545
{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"Invalid request"}}%
@jxom jxom added the needs reproduction Misc: Needs Reproduction label Jun 29, 2024
Copy link
Contributor

Hello @poolpitako.

Please provide a minimal reproduction using StackBlitz, TypeScript Playground (for type issues), or a separate minimal GitHub repository.

Minimal reproductions are required as they save us a lot of time reproducing your config/environment and issue, and allow us to help you faster.

Once a minimal reproduction is added, a team member will confirm it works, then re-open the issue.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 29, 2024
@poolpitako
Copy link
Author

@jxom Ticket contains information on how to reproduce.
Repro steps require an anvil instance and I can't do that online.

@jxom
Copy link
Member

jxom commented Jun 29, 2024

The second snippet you provided is an invalid JSON-RPC request (ie. missing id, jsonrpc fields). Unsure why your first snippet is breaking. I cannot reproduce. Please follow the instructions here and I will be more than happy to look into it.

@poolpitako
Copy link
Author

@jxom are you testing against an anvil node?
i can try setup a repo with a docker compose running anvil + a viem script but it should be reproducible if you run the sample code against a local anvil fork.

@jxom
Copy link
Member

jxom commented Jun 29, 2024

yes. the entire viem test suite is ran against a local anvil node.

please post a minimal reproduction with minimal dependencies (not even docker).

@poolpitako
Copy link
Author

@jxom perfect. I found my issue.

I was running on a terminal with an old node version.

$npx -v
7.10.0

$ npx ts-node src/index.ts
/Users/user/dev/chainId/node_modules/viem/utils/rpc/http.ts:141
        throw new HttpRequestError({
              ^
HttpRequestError: HTTP request failed.

URL: http://127.0.0.1:8545
Request body: {"method":"eth_blockNumber"}

Details: Request is not defined
Version: viem@2.16.4
    at Object.request (/Users/user/dev/chainId/node_modules/viem/utils/rpc/http.ts:141:15)
    at async fn (/Users/user/dev/chainId/node_modules/viem/clients/transports/http.ts:125:19)
    at async request (/Users/user/dev/chainId/node_modules/viem/clients/transports/http.ts:130:39)
    at async delay.count.count (/Users/user/dev/chainId/node_modules/viem/utils/buildRequest.ts:118:22)
    at async attemptRetry (/Users/user/dev/chainId/node_modules/viem/utils/promise/withRetry.ts:44:22) {
  details: 'Request is not defined',
  docsPath: undefined,
  metaMessages: [
    'URL: http://127.0.0.1:8545',
    'Request body: {"method":"eth_blockNumber"}'
  ],
  shortMessage: 'HTTP request failed.',
  version: 'viem@2.16.4',
  cause: ReferenceError: Request is not defined
      at response.errorInstance.request_js_1.TimeoutError.body.body (/Users/user/dev/chainId/node_modules/viem/utils/rpc/http.ts:103:29)
      at /Users/user/dev/chainId/node_modules/viem/utils/promise/withTimeout.ts:36:23
      at /Users/user/dev/chainId/node_modules/viem/utils/promise/withTimeout.ts:43:7
      at new Promise (<anonymous>)
      at withTimeout (/Users/user/dev/chainId/node_modules/viem/utils/promise/withTimeout.ts:22:10)
      at Object.request (/Users/user/dev/chainId/node_modules/viem/utils/rpc/http.ts:79:43)
      at fn (/Users/user/dev/chainId/node_modules/viem/clients/transports/http.ts:125:35)
      at request (/Users/user/dev/chainId/node_modules/viem/clients/transports/http.ts:130:45)
      at delay.count.count (/Users/user/dev/chainId/node_modules/viem/utils/buildRequest.ts:118:28)
      at attemptRetry (/Users/user/dev/chainId/node_modules/viem/utils/promise/withRetry.ts:44:28),
  body: { method: 'eth_blockNumber', params: undefined },
  headers: undefined,
  status: undefined,
  url: 'http://127.0.0.1:8545'
}

When I saw the Request body: {"method":"eth_blockNumber"} I assumed id and jsonrpc was not passed instead of the, now obvious, Request is not defined.

Sorry for the time wasted.

Copy link
Contributor

This issue has been locked since it has been closed for more than 14 days.

If you found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest Viem version. If you have any questions or comments you can create a new discussion thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs reproduction Misc: Needs Reproduction
Projects
None yet
Development

No branches or pull requests

2 participants