Support viem tokens in hardhat-viem
EDIT:
viem has added explicit token metadata to the API: https://viem.sh/tokens
Basic support already works in hardhat-viem: users can install viem 2.54+ and pass tokens: [usdc] to getPublicClient / getWalletClient(s).
The remaining work is to make tokens work correctly on forked networks. For example:
import { usdc } from "viem/tokens";
const { viem } = await network.connect();
const client = await viem.getPublicClient({ tokens: [usdc] });
await client.token.getBalance({
token: "usdc",
account,
});On a Hardhat network forked from mainnet, usdc has an address for chain 1, but not for the local chain 31337, so viem can't resolve the token address. We should map the address from the forked chain to the local chain.
We should also preserve chain and tokens in the returned client types so usages like token: "usdc" type-check. This only works when a specific chain is passed (e.g. chain: mainnet), since otherwise the chain ID is only known at runtime. The typing change requires bumping the viem peer dependency to ^2.54.0.
We should also add a short section to the docs covering token usage.
This issue will be handled internally by the team.
Source: NomicFoundation/hardhat