From caa49e583e3ebe21ca7e58a120aaf32abc9b581a Mon Sep 17 00:00:00 2001 From: johnpm-12 <39016062+johnpm-12@users.noreply.github.com> Date: Thu, 13 Jun 2024 20:58:03 -0700 Subject: [PATCH 1/3] add optional chain param to viem ignition.deploy --- .../src/viem-ignition-helper.ts | 53 ++++++++++++++----- 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/packages/hardhat-plugin-viem/src/viem-ignition-helper.ts b/packages/hardhat-plugin-viem/src/viem-ignition-helper.ts index 52c492aa1..a85170bb9 100644 --- a/packages/hardhat-plugin-viem/src/viem-ignition-helper.ts +++ b/packages/hardhat-plugin-viem/src/viem-ignition-helper.ts @@ -1,4 +1,5 @@ import type { GetContractReturnType } from "@nomicfoundation/hardhat-viem/types"; +import type { Chain } from "viem"; import { HardhatArtifactResolver, @@ -71,6 +72,7 @@ export class ViemIgnitionHelper { strategy, strategyConfig, deploymentId: givenDeploymentId = undefined, + chain, }: { parameters?: DeploymentParameters; config?: Partial; @@ -78,6 +80,7 @@ export class ViemIgnitionHelper { strategy?: StrategyT; strategyConfig?: StrategyConfig[StrategyT]; deploymentId?: string; + chain?: Chain; } = { parameters: {}, config: {}, @@ -85,6 +88,7 @@ export class ViemIgnitionHelper { strategy: undefined, strategyConfig: undefined, deploymentId: undefined, + chain: undefined, } ): Promise< IgnitionModuleResultsToViemContracts @@ -152,7 +156,8 @@ export class ViemIgnitionHelper { return ViemIgnitionHelper._toViemContracts( this._hre, ignitionModule, - result + result, + chain ); } @@ -167,7 +172,8 @@ export class ViemIgnitionHelper { ContractNameT, IgnitionModuleResultsT >, - result: SuccessfulDeploymentResult + result: SuccessfulDeploymentResult, + chain?: Chain ): Promise< IgnitionModuleResultsToViemContracts > { @@ -179,7 +185,8 @@ export class ViemIgnitionHelper { await ViemIgnitionHelper._getContract( hre, contractFuture, - result.contracts[contractFuture.id] + result.contracts[contractFuture.id], + chain ), ] ) @@ -190,7 +197,8 @@ export class ViemIgnitionHelper { private static async _getContract( hre: HardhatRuntimeEnvironment, future: Future, - deployedContract: { address: string } + deployedContract: { address: string }, + chain?: Chain ): Promise { if (!isContractFuture(future)) { throw new HardhatPluginError( @@ -202,14 +210,16 @@ export class ViemIgnitionHelper { return ViemIgnitionHelper._convertContractFutureToViemContract( hre, future, - deployedContract + deployedContract, + chain ); } private static async _convertContractFutureToViemContract( hre: HardhatRuntimeEnvironment, future: ContractFuture, - deployedContract: { address: string } + deployedContract: { address: string }, + chain?: Chain ) { switch (future.type) { case FutureType.NAMED_ARTIFACT_CONTRACT_DEPLOYMENT: @@ -218,7 +228,8 @@ export class ViemIgnitionHelper { return ViemIgnitionHelper._convertHardhatContractToViemContract( hre, future, - deployedContract + deployedContract, + chain ); case FutureType.CONTRACT_DEPLOYMENT: case FutureType.LIBRARY_DEPLOYMENT: @@ -226,22 +237,35 @@ export class ViemIgnitionHelper { return ViemIgnitionHelper._convertArtifactToViemContract( hre, future, - deployedContract + deployedContract, + chain ); } } - private static _convertHardhatContractToViemContract( + private static async _convertHardhatContractToViemContract( hre: HardhatRuntimeEnvironment, future: | NamedArtifactContractDeploymentFuture | NamedArtifactLibraryDeploymentFuture | NamedArtifactContractAtFuture, - deployedContract: { address: string } + deployedContract: { address: string }, + chain?: Chain ): Promise { + const publicClient = await hre.viem.getPublicClient({ chain }); + const [walletClient] = await hre.viem.getWalletClients({ chain }); + + if (walletClient === undefined) { + throw new HardhatPluginError( + "hardhat-ignition-viem", + "No default wallet client found" + ); + } + return hre.viem.getContractAt( future.contractName, - ViemIgnitionHelper._ensureAddressFormat(deployedContract.address) + this._ensureAddressFormat(deployedContract.address), + { client: { public: publicClient, wallet: walletClient } } ); } @@ -251,10 +275,11 @@ export class ViemIgnitionHelper { | ContractDeploymentFuture | LibraryDeploymentFuture | ContractAtFuture, - deployedContract: { address: string } + deployedContract: { address: string }, + chain?: Chain ): Promise { - const publicClient = await hre.viem.getPublicClient(); - const [walletClient] = await hre.viem.getWalletClients(); + const publicClient = await hre.viem.getPublicClient({ chain }); + const [walletClient] = await hre.viem.getWalletClients({ chain }); if (walletClient === undefined) { throw new HardhatPluginError( From 027cec2419b5c70f4f37dbc7ee175255b676fefe Mon Sep 17 00:00:00 2001 From: johnpm-12 <39016062+johnpm-12@users.noreply.github.com> Date: Thu, 13 Jun 2024 21:08:42 -0700 Subject: [PATCH 2/3] fix tests --- .../src/viem-ignition-helper.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/hardhat-plugin-viem/src/viem-ignition-helper.ts b/packages/hardhat-plugin-viem/src/viem-ignition-helper.ts index a85170bb9..7d2a45dd3 100644 --- a/packages/hardhat-plugin-viem/src/viem-ignition-helper.ts +++ b/packages/hardhat-plugin-viem/src/viem-ignition-helper.ts @@ -252,8 +252,12 @@ export class ViemIgnitionHelper { deployedContract: { address: string }, chain?: Chain ): Promise { - const publicClient = await hre.viem.getPublicClient({ chain }); - const [walletClient] = await hre.viem.getWalletClients({ chain }); + const publicClient = await hre.viem.getPublicClient( + chain ? { chain } : undefined + ); + const [walletClient] = await hre.viem.getWalletClients( + chain ? { chain } : undefined + ); if (walletClient === undefined) { throw new HardhatPluginError( @@ -278,8 +282,12 @@ export class ViemIgnitionHelper { deployedContract: { address: string }, chain?: Chain ): Promise { - const publicClient = await hre.viem.getPublicClient({ chain }); - const [walletClient] = await hre.viem.getWalletClients({ chain }); + const publicClient = await hre.viem.getPublicClient( + chain ? { chain } : undefined + ); + const [walletClient] = await hre.viem.getWalletClients( + chain ? { chain } : undefined + ); if (walletClient === undefined) { throw new HardhatPluginError( From 98e7706afdc496fb23536b16bd046b6a11db8aac Mon Sep 17 00:00:00 2001 From: johnpm-12 <39016062+johnpm-12@users.noreply.github.com> Date: Fri, 14 Jun 2024 08:37:21 -0700 Subject: [PATCH 3/3] fix lint --- packages/hardhat-plugin-viem/src/viem-ignition-helper.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/hardhat-plugin-viem/src/viem-ignition-helper.ts b/packages/hardhat-plugin-viem/src/viem-ignition-helper.ts index 7d2a45dd3..44911d29a 100644 --- a/packages/hardhat-plugin-viem/src/viem-ignition-helper.ts +++ b/packages/hardhat-plugin-viem/src/viem-ignition-helper.ts @@ -253,10 +253,10 @@ export class ViemIgnitionHelper { chain?: Chain ): Promise { const publicClient = await hre.viem.getPublicClient( - chain ? { chain } : undefined + typeof chain !== "undefined" ? { chain } : undefined ); const [walletClient] = await hre.viem.getWalletClients( - chain ? { chain } : undefined + typeof chain !== "undefined" ? { chain } : undefined ); if (walletClient === undefined) { @@ -283,10 +283,10 @@ export class ViemIgnitionHelper { chain?: Chain ): Promise { const publicClient = await hre.viem.getPublicClient( - chain ? { chain } : undefined + typeof chain !== "undefined" ? { chain } : undefined ); const [walletClient] = await hre.viem.getWalletClients( - chain ? { chain } : undefined + typeof chain !== "undefined" ? { chain } : undefined ); if (walletClient === undefined) {