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

hre.viem.getPublicClient() fails without explicit transport parameter #6455

Open
s-di-cola opened this issue Mar 7, 2025 · 1 comment
Open
Assignees
Labels
status:needs-more-info There's not enough information to start working on this issue

Comments

@s-di-cola
Copy link
Contributor

Version of Hardhat

2.22.19

What happened?

Bug: hre.viem.getPublicClient() fails without explicit transport parameter

Description

When using the hardhat-viem plugin, calling hre.viem.getPublicClient() without parameters fails, but works correctly when explicitly providing a transport with hre.viem.getPublicClient({transport: http()}).

Expected Behavior

hre.viem.getPublicClient() should work properly without requiring an explicit transport, as the plugin should configure the transport automatically when connecting to the Hardhat Network.

Actual Behavior

Without an explicit transport, the function fails. With {transport: http()}, it works correctly.

Workaround

Use either:

// Working solution with hardhat-viem
const publicClient = await hre.viem.getPublicClient({transport: http()})

// OR use viem directly
const publicClient = createPublicClient({
  chain: hardhat,
  transport: http()
});

### Minimal reproduction steps

### Steps to Reproduce
1. Set up a Hardhat project with the hardhat-viem plugin
2. Try to get a public client with: `const publicClient = await hre.viem.getPublicClient()`
3. Observe that this fails
4. Change to: `const publicClient = await hre.viem.getPublicClient({transport: http()})`
5. Observe that this works correctly

### Search terms

_No response_
@kanej
Copy link
Member

kanej commented Mar 10, 2025

I was not able to reproduce this.

I created a new Hardhat project (v2.22.19) with Viem, then created a ./scripts/viem-public-client-test.ts:

import hre from "hardhat";

const main = async () => {
  const publicClient = await hre.viem.getPublicClient();

  const blockNumber = await publicClient.getBlockNumber();

  console.log(blockNumber);
};

main();

Then on running I get the expect 0th block number printed:

npx hardhat run ./scripts/viem-public-client-test.ts
# 0n

Is this a version issue?

@kanej kanej added status:needs-more-info There's not enough information to start working on this issue and removed status:triaging labels Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:needs-more-info There's not enough information to start working on this issue
Projects
Status: Backlog
Development

No branches or pull requests

2 participants