Skip to content

Commit

Permalink
Add overload to getContractFactory that accepts Artifacts
Browse files Browse the repository at this point in the history
Allow passing of artifacts (resolved via custom code) into the `getContractFactory` call, this is instead of resolving by contract name.

Relates to #1716.
  • Loading branch information
kanej committed Nov 30, 2021
1 parent a67aa5e commit 33f5376
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
53 changes: 45 additions & 8 deletions packages/hardhat-ethers/src/internal/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,28 @@ interface Link {

const pluginName = "hardhat-ethers";

function isArtifact(artifact: any): artifact is Artifact {
const {
contractName,
sourceName,
abi,
bytecode,
deployedBytecode,
linkReferences,
deployedLinkReferences,
} = artifact;

return (
typeof contractName === "string" &&
typeof sourceName === "string" &&
Array.isArray(abi) &&
typeof bytecode === "string" &&
typeof deployedBytecode === "string" &&
linkReferences !== undefined &&
deployedLinkReferences !== undefined
);
}

export async function getSigners(
hre: HardhatRuntimeEnvironment
): Promise<SignerWithAddress[]> {
Expand Down Expand Up @@ -46,7 +68,7 @@ export async function getSigner(

export function getContractFactory(
hre: HardhatRuntimeEnvironment,
name: string,
name: string | Artifact,
signerOrOptions?: ethers.Signer | FactoryOptions
): Promise<ethers.ContractFactory>;

Expand All @@ -59,24 +81,39 @@ export function getContractFactory(

export async function getContractFactory(
hre: HardhatRuntimeEnvironment,
nameOrAbi: string | any[],
bytecodeOrFactoryOptions?:
nameOrArtifactOrAbi: string | Artifact | any[],
signerOrFactoryOptionsOrBytecode?:
| (ethers.Signer | FactoryOptions)
| ethers.utils.BytesLike,
signer?: ethers.Signer
) {
if (typeof nameOrAbi === "string") {
if (typeof nameOrArtifactOrAbi === "string") {
return getContractFactoryByName(
hre,
nameOrAbi,
bytecodeOrFactoryOptions as ethers.Signer | FactoryOptions | undefined
nameOrArtifactOrAbi,
signerOrFactoryOptionsOrBytecode as
| ethers.Signer
| FactoryOptions
| undefined
);
}

if (isArtifact(nameOrArtifactOrAbi)) {
const { abi, bytecode } = nameOrArtifactOrAbi;
const hiddenSigner = signerOrFactoryOptionsOrBytecode as ethers.Signer;

return getContractFactoryByAbiAndBytecode(
hre,
abi,
bytecode as ethers.utils.BytesLike,
hiddenSigner
);
}

return getContractFactoryByAbiAndBytecode(
hre,
nameOrAbi,
bytecodeOrFactoryOptions as ethers.utils.BytesLike,
nameOrArtifactOrAbi,
signerOrFactoryOptionsOrBytecode as ethers.utils.BytesLike,
signer
);
}
Expand Down
4 changes: 3 additions & 1 deletion packages/hardhat-ethers/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type * as ethers from "ethers";
import { Artifact } from "hardhat/src/types";

import type { SignerWithAddress } from "../signers";

Expand All @@ -12,9 +13,10 @@ export interface FactoryOptions {
}

export declare function getContractFactory(
name: string,
name: string | Artifact,
signerOrOptions?: ethers.Signer | FactoryOptions
): Promise<ethers.ContractFactory>;

export declare function getContractFactory(
abi: any[],
bytecode: ethers.utils.BytesLike,
Expand Down

0 comments on commit 33f5376

Please sign in to comment.