diff --git a/v-next/hardhat-viem/src/internal/clients.ts b/v-next/hardhat-viem/src/internal/clients.ts index 2104b534cd0..4e824a46b4a 100644 --- a/v-next/hardhat-viem/src/internal/clients.ts +++ b/v-next/hardhat-viem/src/internal/clients.ts @@ -1,5 +1,5 @@ import type { - GetPublicClientReturnType, + PublicClientType, GetWalletClientReturnType, TestClient, } from "../types.js"; @@ -28,7 +28,7 @@ export async function getPublicClient( provider: EthereumProvider, chainType: ChainTypeT, publicClientConfig?: Partial, -): Promise> { +): Promise> { const chain = publicClientConfig?.chain ?? (await getChain(provider, chainType)); const { defaultClientParams, defaultTransportParams } = @@ -47,7 +47,7 @@ export async function getPublicClient( /* eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- We need to assert the type because TS gets confused with the conditional type */ - return publicClient as GetPublicClientReturnType; + return publicClient as PublicClientType; } export async function getWalletClients( diff --git a/v-next/hardhat-viem/src/internal/contracts.ts b/v-next/hardhat-viem/src/internal/contracts.ts index ca1e49c01ab..851abd618b4 100644 --- a/v-next/hardhat-viem/src/internal/contracts.ts +++ b/v-next/hardhat-viem/src/internal/contracts.ts @@ -19,11 +19,12 @@ import { resolveLinkedBytecode } from "@nomicfoundation/hardhat-utils/bytecode"; import { ensureError } from "@nomicfoundation/hardhat-utils/error"; import { getContractAddress, getContract } from "viem"; -import { getDefaultWalletClient, getPublicClient } from "./clients.js"; +import { getDefaultWalletClient } from "./clients.js"; export async function deployContract( provider: EthereumProvider, artifactManager: ArtifactManager, + defaultPublicClient: PublicClient, contractName: ContractName, constructorArgs: unknown[] = [], deployContractConfig: DeployContractConfig = {}, @@ -53,8 +54,8 @@ export async function deployContract( ); } - const [publicClient, walletClient, { abi, bytecode }] = await Promise.all([ - client?.public ?? getPublicClient(provider, "l1"), + const publicClient = client?.public ?? defaultPublicClient; + const [walletClient, { abi, bytecode }] = await Promise.all([ client?.wallet ?? getDefaultWalletClient(provider, "l1"), getContractAbiAndBytecode(artifactManager, contractName, libraries), ]); @@ -113,6 +114,7 @@ export async function deployContract( export async function sendDeploymentTransaction( provider: EthereumProvider, artifactManager: ArtifactManager, + defaultPublicClient: PublicClient, contractName: ContractName, constructorArgs: unknown[] = [], sendDeploymentTransactionConfig: SendDeploymentTransactionConfig = {}, @@ -125,8 +127,8 @@ export async function sendDeploymentTransaction( libraries = {}, ...deployContractParameters } = sendDeploymentTransactionConfig; - const [publicClient, walletClient, { abi, bytecode }] = await Promise.all([ - client?.public ?? getPublicClient(provider, "l1"), + const publicClient = client?.public ?? defaultPublicClient; + const [walletClient, { abi, bytecode }] = await Promise.all([ client?.wallet ?? getDefaultWalletClient(provider, "l1"), getContractAbiAndBytecode(artifactManager, contractName, libraries), ]); @@ -176,12 +178,14 @@ export async function sendDeploymentTransaction( export async function getContractAt( provider: EthereumProvider, artifactManager: ArtifactManager, + defaultPublicClient: PublicClient, contractName: ContractName, address: ViemAddress, getContractAtConfig: GetContractAtConfig = {}, ): Promise> { - const [publicClient, walletClient, artifact] = await Promise.all([ - getContractAtConfig.client?.public ?? getPublicClient(provider, "l1"), + const publicClient = + getContractAtConfig.client?.public ?? defaultPublicClient; + const [walletClient, artifact] = await Promise.all([ getContractAtConfig.client?.wallet ?? getDefaultWalletClient(provider, "l1"), artifactManager.readArtifact(contractName), diff --git a/v-next/hardhat-viem/src/internal/hook-handlers/network.ts b/v-next/hardhat-viem/src/internal/hook-handlers/network.ts index d502166d4db..9f3d0945c19 100644 --- a/v-next/hardhat-viem/src/internal/hook-handlers/network.ts +++ b/v-next/hardhat-viem/src/internal/hook-handlers/network.ts @@ -13,7 +13,7 @@ export default async (): Promise> => { ) { const connection: NetworkConnection = await next(context); - connection.viem = initializeViem( + connection.viem = await initializeViem( connection.chainType, connection.provider, context.artifacts, diff --git a/v-next/hardhat-viem/src/internal/initialization.ts b/v-next/hardhat-viem/src/internal/initialization.ts index dd009e7fe9d..4490813d3c4 100644 --- a/v-next/hardhat-viem/src/internal/initialization.ts +++ b/v-next/hardhat-viem/src/internal/initialization.ts @@ -15,12 +15,16 @@ import { sendDeploymentTransaction, } from "./contracts.js"; -export function initializeViem( +export async function initializeViem( chainType: ChainTypeT, provider: EthereumProvider, artifactManager: ArtifactManager, -): HardhatViemHelpers { +): Promise> { + const defaultPublicClient = await getPublicClient(provider, chainType); + return { + publicClient: defaultPublicClient, + getPublicClient: (publicClientConfig) => getPublicClient(provider, chainType, publicClientConfig), @@ -37,6 +41,7 @@ export function initializeViem( deployContract( provider, artifactManager, + defaultPublicClient, contractName, constructorArgs, deployContractConfig, @@ -50,6 +55,7 @@ export function initializeViem( sendDeploymentTransaction( provider, artifactManager, + defaultPublicClient, contractName, constructorArgs, sendDeploymentTransactionConfig, @@ -59,6 +65,7 @@ export function initializeViem( getContractAt( provider, artifactManager, + defaultPublicClient, contractName, address, getContractAtConfig, diff --git a/v-next/hardhat-viem/src/types.ts b/v-next/hardhat-viem/src/types.ts index 09fda4b19de..478cc87e37b 100644 --- a/v-next/hardhat-viem/src/types.ts +++ b/v-next/hardhat-viem/src/types.ts @@ -29,6 +29,8 @@ import type { export interface HardhatViemHelpers< ChainTypeT extends ChainType | string = DefaultChainType, > { + publicClient: PublicClientType; + /** * Creates a public client configured with the provided settings. * @@ -39,7 +41,7 @@ export interface HardhatViemHelpers< */ getPublicClient: ( publicClientConfig?: Partial, - ) => Promise>; + ) => Promise>; /** * Creates a wallet client configured with the provided settings for each * account in the provider. @@ -132,7 +134,7 @@ export interface HardhatViemHelpers< ) => Promise>; } -export type GetPublicClientReturnType = +export type PublicClientType = ChainTypeT extends "optimism" ? OpPublicClient : PublicClient; export type GetWalletClientReturnType = diff --git a/v-next/hardhat-viem/test/hook-handlers/network.ts b/v-next/hardhat-viem/test/hook-handlers/network.ts index f38cd0d96c7..85385a7a903 100644 --- a/v-next/hardhat-viem/test/hook-handlers/network.ts +++ b/v-next/hardhat-viem/test/hook-handlers/network.ts @@ -24,7 +24,10 @@ describe("hook-handlers/network", () => { it("should be extended with viem", () => { assert.ok(isObject(connection.viem), "viem should be defined"); - + assert.ok( + isObject(connection.viem.publicClient), + "viem should have publicClient object", + ); assert.ok( typeof connection.viem.getPublicClient === "function", "viem should have a getPublicClient function",