0
# Contract Factories
1
2
Functions for creating contract factories from artifacts, names, or ABI/bytecode combinations. Contract factories are used to deploy new instances of smart contracts.
3
4
## Capabilities
5
6
### Get Contract Factory by Name
7
8
Creates a contract factory from a contract name, using compiled artifacts from your Hardhat project.
9
10
```typescript { .api }
11
/**
12
* Creates a contract factory from a contract name
13
* @param name - Contract name as defined in your Solidity files
14
* @param signerOrOptions - Signer to use for deployment or factory options
15
* @returns Promise resolving to ContractFactory instance
16
*/
17
function getContractFactory(
18
name: string,
19
signerOrOptions?: ethers.Signer | FactoryOptions
20
): Promise<ethers.ContractFactory>;
21
```
22
23
**Usage Examples:**
24
25
```typescript
26
import { ethers } from "hardhat";
27
28
// Basic usage with default signer
29
const factory = await ethers.getContractFactory("MyContract");
30
31
// With specific signer
32
const [deployer] = await ethers.getSigners();
33
const factory = await ethers.getContractFactory("MyContract", deployer);
34
35
// With library linking
36
const factory = await ethers.getContractFactory("MyContract", {
37
libraries: {
38
"MyLibrary": "0x1234567890123456789012345678901234567890"
39
}
40
});
41
```
42
43
### Get Contract Factory by ABI and Bytecode
44
45
Creates a contract factory from ABI and bytecode directly, useful for dynamic contract creation.
46
47
```typescript { .api }
48
/**
49
* Creates a contract factory from ABI and bytecode
50
* @param abi - Contract ABI array
51
* @param bytecode - Contract bytecode
52
* @param signer - Signer to use for deployment
53
* @returns Promise resolving to ContractFactory instance
54
*/
55
function getContractFactory(
56
abi: any[],
57
bytecode: ethers.utils.BytesLike,
58
signer?: ethers.Signer
59
): Promise<ethers.ContractFactory>;
60
```
61
62
**Usage Examples:**
63
64
```typescript
65
import { ethers } from "hardhat";
66
67
const abi = [
68
"constructor(string memory name)",
69
"function getName() public view returns (string memory)"
70
];
71
const bytecode = "0x608060405234801561001057600080fd5b5...";
72
73
const factory = await ethers.getContractFactory(abi, bytecode);
74
```
75
76
### Get Contract Factory from Artifact
77
78
Creates a contract factory from a Hardhat compilation artifact, providing more control over the process.
79
80
```typescript { .api }
81
/**
82
* Creates a contract factory from a Hardhat artifact
83
* @param artifact - Hardhat compilation artifact
84
* @param signerOrOptions - Signer to use for deployment or factory options
85
* @returns Promise resolving to ContractFactory instance
86
*/
87
function getContractFactoryFromArtifact(
88
artifact: Artifact,
89
signerOrOptions?: ethers.Signer | FactoryOptions
90
): Promise<ethers.ContractFactory>;
91
```
92
93
**Usage Examples:**
94
95
```typescript
96
import { ethers, artifacts } from "hardhat";
97
98
// Load artifact and create factory
99
const artifact = await artifacts.readArtifact("MyContract");
100
const factory = await ethers.getContractFactoryFromArtifact(artifact);
101
102
// With library linking
103
const factory = await ethers.getContractFactoryFromArtifact(artifact, {
104
signer: deployer,
105
libraries: {
106
"MyLibrary": libraryAddress
107
}
108
});
109
```
110
111
## Library Linking
112
113
### Libraries Interface
114
115
Defines the mapping of library names to their deployed addresses.
116
117
```typescript { .api }
118
/**
119
* Mapping of library names to their deployed addresses
120
*/
121
interface Libraries {
122
[libraryName: string]: string;
123
}
124
```
125
126
### Factory Options Interface
127
128
Configuration options for contract factory creation, including signer and library linking.
129
130
```typescript { .api }
131
/**
132
* Options for contract factory creation
133
*/
134
interface FactoryOptions {
135
/** Signer to use for deployment */
136
signer?: ethers.Signer;
137
/** Library addresses for linking */
138
libraries?: Libraries;
139
}
140
```
141
142
### Library Linking Examples
143
144
```typescript
145
import { ethers } from "hardhat";
146
147
// Link a single library
148
const factory = await ethers.getContractFactory("MyContract", {
149
libraries: {
150
"SafeMath": "0x1234567890123456789012345678901234567890"
151
}
152
});
153
154
// Link multiple libraries
155
const factory = await ethers.getContractFactory("ComplexContract", {
156
libraries: {
157
"MathUtils": "0x1111111111111111111111111111111111111111",
158
"StringUtils": "0x2222222222222222222222222222222222222222"
159
}
160
});
161
162
// Link with fully qualified names (for name conflicts)
163
const factory = await ethers.getContractFactory("MyContract", {
164
libraries: {
165
"contracts/utils/Math.sol:SafeMath": "0x1234567890123456789012345678901234567890"
166
}
167
});
168
```
169
170
## Error Handling
171
172
The contract factory functions will throw `NomicLabsHardhatPluginError` in the following cases:
173
174
- **Contract not found**: When the specified contract name doesn't exist in compiled artifacts
175
- **Abstract contract**: When trying to create a factory for an abstract contract (bytecode is "0x")
176
- **Invalid artifact**: When the provided artifact is not a valid Hardhat compilation artifact
177
- **Missing libraries**: When required libraries are not provided for linking
178
- **Invalid library address**: When a provided library address is not a valid Ethereum address
179
- **Ambiguous library name**: When a library name matches multiple libraries and needs full qualification
180
- **Duplicate library**: When the same library is specified multiple times with different names
181
182
## Contract Factory Usage
183
184
Once you have a contract factory, you can deploy contracts:
185
186
```typescript
187
import { ethers } from "hardhat";
188
189
const factory = await ethers.getContractFactory("MyContract");
190
191
// Deploy without constructor arguments
192
const contract = await factory.deploy();
193
await contract.deployed();
194
195
// Deploy with constructor arguments
196
const contract = await factory.deploy("arg1", 42, true);
197
await contract.deployed();
198
199
// Deploy with transaction overrides
200
const contract = await factory.deploy("arg1", {
201
gasLimit: 500000,
202
gasPrice: ethers.utils.parseUnits("20", "gwei")
203
});
204
```