You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
hardhat3 node isn't available yet, so I created a guide on how to get it working
Add @ignored/hardhat-vnext-viem to your package.json. Ideally this setup wouldn't depend on viem (since you might be using ethers), but this doesn't hurt and I need viem to query some of the state when starting the node
Add viem to your plugins in hardhat.config.ts
importHardhatViemfrom"@ignored/hardhat-vnext-viem";// you add the plugin to your existing configurationconstconfig: HardhatUserConfig={plugins: [HardhatViem,// <- this is the line you want to add],}
Add a devDependency on hardhat v2 (you won't actually use hardhat v2 at all, we just need it for one utility function): npm install --save-dev hardhat
Define a task that creates the hardhat3 node command. To do that, copy the following block of code to you hardhat.config.ts (will explain more in below)
import{ArgumentType}from"@ignored/hardhat-vnext/types/arguments";import{JsonRpcServer}from"hardhat/internal/hardhat-network/jsonrpc/server";importtype{NetworkConfig}from"@ignored/hardhat-vnext/types/config";import{task}from"@ignored/hardhat-vnext/config";importfsfrom"node:fs";functiongetNetworkList(networks: Record<string,NetworkConfig>){constnetworkEntries=Object.entries(networks);// ensure we always run the `hardhat` network firstnetworkEntries.sort((a,b)=>{if(a[0]==="hardhat")return-1;if(b[0]==="hardhat")return1;return0;});returnnetworkEntries.filter(([name])=>// skip the builtin localhost network, since hardhat node is meant to explicitly not use itname!=="localhost"&&// hardhat network seems broken and the block number never advances on itname!=="hardhat");}constnodeTask=task("node").addOption({name: "port",type: ArgumentType.INT,defaultValue: 8545,}).addOption({name: "hostname",type: ArgumentType.STRING,defaultValue: "127.0.0.1",}).setAction(async(args,hre): Promise<void>=>{consthostname=(()=>{if(args.hostname!=="127.0.0.1"){returnargs.hostname;}constinsideDocker=fs.existsSync("/.dockerenv");returninsideDocker ? "0.0.0.0" : "127.0.0.1";})();letport=args.port;constconnections: JsonRpcServer[]=[];constnetworkEntries=getNetworkList(hre.config.networks);for(const[name,network]ofnetworkEntries){constconnection=awaithre.network.connect(name,network.chainType);constserver=newJsonRpcServer({
hostname,
port,provider: connection.provider,});port++;// increase port so next network has a unique port numberconstpublicClient=awaitconnection.viem.getPublicClient();publicClient.watchBlocks({onBlock: (block)=>{// there seems to be a bug on block 0 where it triggers watchBlock in an infinite loopif(block.number===0n)return;console.log(`${name}: block ${block.number} (${block.hash})`);if(block.transactions.length>0){console.log("Transactions: ",block.transactions.map((tx)=>tx.hash),);}},includeTransactions: true,},);const{port: actualPort, address }=awaitserver.listen();console.log(`Started HTTP and WebSocket JSON-RPC server at ${address}:${actualPort}`,);console.log();// new lineconnections.push(server);console.log("Accounts for ",name);console.log("========");// we use this over network.genesisAccounts// since we don't have access to some of the hardhat v3 util functions otherwiseconstwallets=awaitconnection.viem.getWalletClients();for(leti=0;i<wallets.length;i++){constweiBalance=awaitpublicClient.getBalance({address: wallets[i].account.address,});constbalance=(weiBalance/10n**18n).toString(10);console.log(`Account #${i}: ${wallets[i].account.address} (${balance} ETH)`,);}console.log();// new line}awaitPromise.all(connections.map((connection)=>connection.waitUntilClosed()),);},).build();
Add this new task to your hardhat.config.ts
importHardhatViemfrom"@ignored/hardhat-vnext-viem";// you add the plugin to your existing configurationconstconfig: HardhatUserConfig={tasks: [nodeTask,// <- this is the line you want to add],}
Add some network to your configuration in hardhat.config.ts
importHardhatViemfrom"@ignored/hardhat-vnext-viem";// you add the plugin to your existing configurationconstconfig: HardhatUserConfig={networks: {edrHardhat: {type: "edr",chainType: "l1",chainId: 31337,automine: true,intervalMining: 2000,},},}
run npx hardhat3 node
It should now run!
If you want to add more networks, you can just add them to the networks like shown with edrHardhat
How to wait on the networks
In Hardhat v2, there was always only one network, so if you have a command you wanted to run after your local chain was setup, you could just wait on the 8545 port to start. However, since Hardhat v3 supports running multiple networks, waiting on all of them requires knowing how many networks are in your configuration and their port number
As a limited workaround for this (this just waits until the networks are started - it doesn't check if any contracts have been deployed to them so far), you can add a task that waits for all the networks to be started with the following steps:
This guide depends on the viem plugin which is unfortunate, but I need some client to rebuild
the log messages when new blocks are made
logHardhatNetworkAccounts to list the accounts for the network
This depends on hardhat v2 just for the JsonRpcServer because it's not exported in hardhat3
I had to add an explicitly edrHardhat and disable the built-in hardhat network because the built-in hardhat network seems to permanently be stuck at block 0 and never moves
I had to filter out block 0 for publicClient.watchBlocks otherwise the watch would trigger continuously in a loop. Not sure if this is a viem bug or a edr bug
The text was updated successfully, but these errors were encountered:
hardhat3 node
isn't available yet, so I created a guide on how to get it working@ignored/hardhat-vnext-viem
to yourpackage.json
. Ideally this setup wouldn't depend on viem (since you might be using ethers), but this doesn't hurt and I need viem to query some of the state when starting the nodehardhat.config.ts
npm install --save-dev hardhat
hardhat3 node
command. To do that, copy the following block of code to youhardhat.config.ts
(will explain more in below)hardhat.config.ts
hardhat.config.ts
npx hardhat3 node
It should now run!
If you want to add more networks, you can just add them to the networks like shown with
edrHardhat
How to wait on the networks
In Hardhat v2, there was always only one network, so if you have a command you wanted to run after your local chain was setup, you could just wait on the
8545
port to start. However, since Hardhat v3 supports running multiple networks, waiting on all of them requires knowing how many networks are in your configuration and their port numberAs a limited workaround for this (this just waits until the networks are started - it doesn't check if any contracts have been deployed to them so far), you can add a task that waits for all the networks to be started with the following steps:
npm install --save-dev wait-on
Bugs and comments
viem
plugin which is unfortunate, but I need some client to rebuildlogHardhatNetworkAccounts
to list the accounts for the networkJsonRpcServer
because it's not exported in hardhat3edrHardhat
and disable the built-inhardhat
network because the built-in hardhat network seems to permanently be stuck at block 0 and never movespublicClient.watchBlocks
otherwise the watch would trigger continuously in a loop. Not sure if this is a viem bug or a edr bugThe text was updated successfully, but these errors were encountered: