1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { ethers } from 'ethers'
/**
* Gets the hardhat artifact for the given contract name.
* Will throw an error if the contract artifact is not found.
*
* @param name Contract name.
* @returns The artifact for the given contract name.
*/
export const getContractDefinition = (name: string): any => {
// We import this using `require` because hardhat tries to build this file when compiling
// the contracts, but we need the contracts to be compiled before the contract-artifacts.ts
// file can be generated.
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { getContractArtifact } = require('./contract-artifacts')
const artifact = getContractArtifact(name)
if (artifact === undefined) {
throw new Error(`Unable to find artifact for contract: ${name}`)
}
return artifact
}
/**
* Gets the deployed hardhat artifact for the given contract name.
* Will throw an error if the contract artifact is not found.
*
* @param name Contract name.
* @param network Network name.
* @returns The artifact for the given contract name.
*/
export const getDeployedContractDefinition = (
name: string,
network: string
): {
address: string
abi: any
} => {
const {
getDeployedContractArtifact,
// eslint-disable-next-line @typescript-eslint/no-var-requires
} = require('./contract-deployed-artifacts')
const artifact = getDeployedContractArtifact(name, network)
if (artifact === undefined) {
throw new Error(
`Unable to find artifact for contract on network ${network}: ${name}`
)
}
return artifact
}
/**
* Gets an ethers Interface instance for the given contract name.
*
* @param name Contract name.
* @returns The interface for the given contract name.
*/
export const getContractInterface = (name: string): ethers.utils.Interface => {
const definition = getContractDefinition(name)
return new ethers.utils.Interface(definition.abi)
}
/**
* Gets an ethers ContractFactory instance for the given contract name.
*
* @param name Contract name.
* @param signer The signer for the ContractFactory to use.
* @returns The contract factory for the given contract name.
*/
export const getContractFactory = (
name: string,
signer?: ethers.Signer
): ethers.ContractFactory => {
const definition = getContractDefinition(name)
const contractInterface = getContractInterface(name)
return new ethers.ContractFactory(
contractInterface,
definition.bytecode,
signer
)
}