Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optional chain param to viem deploy #783

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 47 additions & 14 deletions packages/hardhat-plugin-viem/src/viem-ignition-helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { GetContractReturnType } from "@nomicfoundation/hardhat-viem/types";
import type { Chain } from "viem";

import {
HardhatArtifactResolver,
Expand Down Expand Up @@ -71,20 +72,23 @@ export class ViemIgnitionHelper {
strategy,
strategyConfig,
deploymentId: givenDeploymentId = undefined,
chain,
}: {
parameters?: DeploymentParameters;
config?: Partial<DeployConfig>;
defaultSender?: string;
strategy?: StrategyT;
strategyConfig?: StrategyConfig[StrategyT];
deploymentId?: string;
chain?: Chain;
} = {
parameters: {},
config: {},
defaultSender: undefined,
strategy: undefined,
strategyConfig: undefined,
deploymentId: undefined,
chain: undefined,
}
): Promise<
IgnitionModuleResultsToViemContracts<ContractNameT, IgnitionModuleResultsT>
Expand Down Expand Up @@ -152,7 +156,8 @@ export class ViemIgnitionHelper {
return ViemIgnitionHelper._toViemContracts(
this._hre,
ignitionModule,
result
result,
chain
);
}

Expand All @@ -167,7 +172,8 @@ export class ViemIgnitionHelper {
ContractNameT,
IgnitionModuleResultsT
>,
result: SuccessfulDeploymentResult
result: SuccessfulDeploymentResult,
chain?: Chain
): Promise<
IgnitionModuleResultsToViemContracts<ContractNameT, IgnitionModuleResultsT>
> {
Expand All @@ -179,7 +185,8 @@ export class ViemIgnitionHelper {
await ViemIgnitionHelper._getContract(
hre,
contractFuture,
result.contracts[contractFuture.id]
result.contracts[contractFuture.id],
chain
),
]
)
Expand All @@ -190,7 +197,8 @@ export class ViemIgnitionHelper {
private static async _getContract(
hre: HardhatRuntimeEnvironment,
future: Future,
deployedContract: { address: string }
deployedContract: { address: string },
chain?: Chain
): Promise<GetContractReturnType> {
if (!isContractFuture(future)) {
throw new HardhatPluginError(
Expand All @@ -202,14 +210,16 @@ export class ViemIgnitionHelper {
return ViemIgnitionHelper._convertContractFutureToViemContract(
hre,
future,
deployedContract
deployedContract,
chain
);
}

private static async _convertContractFutureToViemContract(
hre: HardhatRuntimeEnvironment,
future: ContractFuture<string>,
deployedContract: { address: string }
deployedContract: { address: string },
chain?: Chain
) {
switch (future.type) {
case FutureType.NAMED_ARTIFACT_CONTRACT_DEPLOYMENT:
Expand All @@ -218,30 +228,48 @@ export class ViemIgnitionHelper {
return ViemIgnitionHelper._convertHardhatContractToViemContract(
hre,
future,
deployedContract
deployedContract,
chain
);
case FutureType.CONTRACT_DEPLOYMENT:
case FutureType.LIBRARY_DEPLOYMENT:
case FutureType.CONTRACT_AT:
return ViemIgnitionHelper._convertArtifactToViemContract(
hre,
future,
deployedContract
deployedContract,
chain
);
}
}

private static _convertHardhatContractToViemContract(
private static async _convertHardhatContractToViemContract(
hre: HardhatRuntimeEnvironment,
future:
| NamedArtifactContractDeploymentFuture<string>
| NamedArtifactLibraryDeploymentFuture<string>
| NamedArtifactContractAtFuture<string>,
deployedContract: { address: string }
deployedContract: { address: string },
chain?: Chain
): Promise<GetContractReturnType> {
const publicClient = await hre.viem.getPublicClient(
typeof chain !== "undefined" ? { chain } : undefined
);
const [walletClient] = await hre.viem.getWalletClients(
typeof chain !== "undefined" ? { chain } : undefined
);

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 } }
);
}

Expand All @@ -251,10 +279,15 @@ export class ViemIgnitionHelper {
| ContractDeploymentFuture
| LibraryDeploymentFuture
| ContractAtFuture,
deployedContract: { address: string }
deployedContract: { address: string },
chain?: Chain
): Promise<GetContractReturnType> {
const publicClient = await hre.viem.getPublicClient();
const [walletClient] = await hre.viem.getWalletClients();
const publicClient = await hre.viem.getPublicClient(
typeof chain !== "undefined" ? { chain } : undefined
);
const [walletClient] = await hre.viem.getWalletClients(
typeof chain !== "undefined" ? { chain } : undefined
);

if (walletClient === undefined) {
throw new HardhatPluginError(
Expand Down
Loading