Skip to content
6 changes: 3 additions & 3 deletions v-next/hardhat-viem/src/internal/clients.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {
GetPublicClientReturnType,
PublicClientType,
GetWalletClientReturnType,
TestClient,
} from "../types.js";
Expand Down Expand Up @@ -28,7 +28,7 @@ export async function getPublicClient<ChainTypeT extends ChainType | string>(
provider: EthereumProvider,
chainType: ChainTypeT,
publicClientConfig?: Partial<ViemPublicClientConfig>,
): Promise<GetPublicClientReturnType<ChainTypeT>> {
): Promise<PublicClientType<ChainTypeT>> {
const chain =
publicClientConfig?.chain ?? (await getChain(provider, chainType));
const { defaultClientParams, defaultTransportParams } =
Expand All @@ -47,7 +47,7 @@ export async function getPublicClient<ChainTypeT extends ChainType | string>(

/* 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<ChainTypeT>;
return publicClient as PublicClientType<ChainTypeT>;
}

export async function getWalletClients<ChainTypeT extends ChainType | string>(
Expand Down
18 changes: 11 additions & 7 deletions v-next/hardhat-viem/src/internal/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ContractName extends string>(
provider: EthereumProvider,
artifactManager: ArtifactManager,
defaultPublicClient: PublicClient,
contractName: ContractName,
constructorArgs: unknown[] = [],
deployContractConfig: DeployContractConfig = {},
Expand Down Expand Up @@ -53,8 +54,8 @@ export async function deployContract<ContractName extends string>(
);
}

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),
]);
Expand Down Expand Up @@ -113,6 +114,7 @@ export async function deployContract<ContractName extends string>(
export async function sendDeploymentTransaction<ContractName extends string>(
provider: EthereumProvider,
artifactManager: ArtifactManager,
defaultPublicClient: PublicClient,
contractName: ContractName,
constructorArgs: unknown[] = [],
sendDeploymentTransactionConfig: SendDeploymentTransactionConfig = {},
Expand All @@ -125,8 +127,8 @@ export async function sendDeploymentTransaction<ContractName extends string>(
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),
]);
Expand Down Expand Up @@ -176,12 +178,14 @@ export async function sendDeploymentTransaction<ContractName extends string>(
export async function getContractAt<ContractName extends string>(
provider: EthereumProvider,
artifactManager: ArtifactManager,
defaultPublicClient: PublicClient,
contractName: ContractName,
address: ViemAddress,
getContractAtConfig: GetContractAtConfig = {},
): Promise<ContractReturnType<ContractName>> {
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),
Expand Down
2 changes: 1 addition & 1 deletion v-next/hardhat-viem/src/internal/hook-handlers/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default async (): Promise<Partial<NetworkHooks>> => {
) {
const connection: NetworkConnection<ChainTypeT> = await next(context);

connection.viem = initializeViem(
connection.viem = await initializeViem(
connection.chainType,
connection.provider,
context.artifacts,
Expand Down
11 changes: 9 additions & 2 deletions v-next/hardhat-viem/src/internal/initialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ import {
sendDeploymentTransaction,
} from "./contracts.js";

export function initializeViem<ChainTypeT extends ChainType | string>(
export async function initializeViem<ChainTypeT extends ChainType | string>(
chainType: ChainTypeT,
provider: EthereumProvider,
artifactManager: ArtifactManager,
): HardhatViemHelpers<ChainTypeT> {
): Promise<HardhatViemHelpers<ChainTypeT>> {
const defaultPublicClient = await getPublicClient(provider, chainType);

return {
publicClient: defaultPublicClient,

getPublicClient: (publicClientConfig) =>
getPublicClient(provider, chainType, publicClientConfig),

Expand All @@ -37,6 +41,7 @@ export function initializeViem<ChainTypeT extends ChainType | string>(
deployContract(
provider,
artifactManager,
defaultPublicClient,
contractName,
constructorArgs,
deployContractConfig,
Expand All @@ -50,6 +55,7 @@ export function initializeViem<ChainTypeT extends ChainType | string>(
sendDeploymentTransaction(
provider,
artifactManager,
defaultPublicClient,
contractName,
constructorArgs,
sendDeploymentTransactionConfig,
Expand All @@ -59,6 +65,7 @@ export function initializeViem<ChainTypeT extends ChainType | string>(
getContractAt(
provider,
artifactManager,
defaultPublicClient,
contractName,
address,
getContractAtConfig,
Expand Down
6 changes: 4 additions & 2 deletions v-next/hardhat-viem/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import type {
export interface HardhatViemHelpers<
ChainTypeT extends ChainType | string = DefaultChainType,
> {
publicClient: PublicClientType<ChainTypeT>;

/**
* Creates a public client configured with the provided settings.
*
Expand All @@ -39,7 +41,7 @@ export interface HardhatViemHelpers<
*/
getPublicClient: (
publicClientConfig?: Partial<ViemPublicClientConfig>,
) => Promise<GetPublicClientReturnType<ChainTypeT>>;
) => Promise<PublicClientType<ChainTypeT>>;
/**
* Creates a wallet client configured with the provided settings for each
* account in the provider.
Expand Down Expand Up @@ -132,7 +134,7 @@ export interface HardhatViemHelpers<
) => Promise<ContractReturnType<ContractName>>;
}

export type GetPublicClientReturnType<ChainTypeT extends ChainType | string> =
export type PublicClientType<ChainTypeT extends ChainType | string> =
ChainTypeT extends "optimism" ? OpPublicClient : PublicClient;

export type GetWalletClientReturnType<ChainTypeT extends ChainType | string> =
Expand Down
5 changes: 4 additions & 1 deletion v-next/hardhat-viem/test/hook-handlers/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading