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

feat requrest: custom ethereum chain with hardhat-viem plugin #5928

Closed
y0sher opened this issue Nov 2, 2024 · 2 comments
Closed

feat requrest: custom ethereum chain with hardhat-viem plugin #5928

y0sher opened this issue Nov 2, 2024 · 2 comments
Assignees

Comments

@y0sher
Copy link

y0sher commented Nov 2, 2024

Describe the feature

Description

My project is using hardhat-viem

When setting up a local Ethereum development network ( not hardhat/foundry, a whole ethereum network ), in hardhat.config.ts, e.g:

  config.networks = {
    devnet: {
      chainId: 3113031,
      url: `localhost:mygeth`,
      accounts: [
        "myaccount"
      ],
      gasPrice: '',
      gas: '',
    }
}

I'm getting this error:

Error in plugin @nomicfoundation/hardhat-viem: No network with chain id 3113031 found. You can override the chain by passing it as a parameter to the client getter:

import { someChain } from "viem/chains";
const client = await hre.viem.getPublicClient({
  chain: someChain,
  ...
});

You can find a list of supported networks here: https://github.com/wevm/viem/blob/main/src/chains/index.ts

For more info run Hardhat with --show-stack-traces

Feature request

I want to be able to direct hardhat-viem to my defined chainId and node url, should probably use defineChain from viem, somewhere around this codepath I guess. I tried to sketch up some code, but it seems not as trivial since EthereumProvider doesn't exposes the json url.

Search terms

No response

@iosh
Copy link
Contributor

iosh commented Nov 3, 2024

Yes, you can use defineChain to define your own chain, as shown in the code below.

import hre from "hardhat";
import { defineChain } from "viem";

async function main() {
  const myChain = defineChain({
    name: "mygeth",
    id: 3113031,
    nativeCurrency: {
      name: "mygeth",
      symbol: "mygeth",
      decimals: 18,
    },
    rpcUrls: {
      default: {
        http: ["http://localhost:mygeth"], 
      },
    },
  });

  const publicClient = await hre.viem.getPublicClient({ chain: myChain });

  const walletClient = await hre.viem.getWalletClient("0x......", {
    chain: myChain,
  });
  console.log(await publicClient.getBlock());

  console.log(await walletClient.getChainId());
}

@ChristopherDedominici
Copy link
Contributor

Closing this issue as the solution proposed by @iosh works as expected.

@github-project-automation github-project-automation bot moved this from Backlog to Done in Hardhat Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

No branches or pull requests

3 participants