0
# Contract Deployment
1
2
Simplified contract deployment functionality that combines factory creation and deployment in a single function call. This provides a streamlined way to deploy contracts without manually creating factories.
3
4
## Capabilities
5
6
### Deploy Contract with Arguments
7
8
Deploys a contract with constructor arguments in a single function call.
9
10
```typescript { .api }
11
/**
12
* Deploys a contract with constructor arguments
13
* @param name - Contract name as defined in your Solidity files
14
* @param args - Constructor arguments array
15
* @param signerOrOptions - Signer to use for deployment or factory options
16
* @returns Promise resolving to deployed Contract instance
17
*/
18
function deployContract(
19
name: string,
20
args?: any[],
21
signerOrOptions?: ethers.Signer | FactoryOptions
22
): Promise<ethers.Contract>;
23
```
24
25
**Usage Examples:**
26
27
```typescript
28
import { ethers } from "hardhat";
29
30
// Deploy contract without constructor arguments
31
const contract = await ethers.deployContract("SimpleContract");
32
await contract.deployed();
33
34
// Deploy contract with constructor arguments
35
const contract = await ethers.deployContract("MyToken", [
36
"Token Name",
37
"TKN",
38
ethers.utils.parseEther("1000000") // 1M tokens
39
]);
40
await contract.deployed();
41
42
// Deploy with specific signer
43
const [deployer] = await ethers.getSigners();
44
const contract = await ethers.deployContract("MyContract", ["arg1", 42], deployer);
45
46
// Deploy with library linking
47
const contract = await ethers.deployContract("MyContract", ["arg1"], {
48
libraries: {
49
"MathLib": "0x1234567890123456789012345678901234567890"
50
}
51
});
52
```
53
54
### Deploy Contract without Arguments
55
56
Deploys a contract without constructor arguments, providing signer or factory options directly.
57
58
```typescript { .api }
59
/**
60
* Deploys a contract without constructor arguments
61
* @param name - Contract name as defined in your Solidity files
62
* @param signerOrOptions - Signer to use for deployment or factory options
63
* @returns Promise resolving to deployed Contract instance
64
*/
65
function deployContract(
66
name: string,
67
signerOrOptions?: ethers.Signer | FactoryOptions
68
): Promise<ethers.Contract>;
69
```
70
71
**Usage Examples:**
72
73
```typescript
74
import { ethers } from "hardhat";
75
76
// Deploy with default signer
77
const contract = await ethers.deployContract("SimpleStorage");
78
79
// Deploy with specific signer
80
const [deployer] = await ethers.getSigners();
81
const contract = await ethers.deployContract("SimpleStorage", deployer);
82
83
// Deploy with factory options
84
const contract = await ethers.deployContract("SimpleStorage", {
85
signer: deployer,
86
libraries: {
87
"UtilsLib": libraryAddress
88
}
89
});
90
```
91
92
## Deployment Process
93
94
The `deployContract` function internally:
95
96
1. Creates a contract factory using `getContractFactory`
97
2. Calls the factory's `deploy` method with provided arguments
98
3. Returns the deployed contract instance
99
100
This is equivalent to:
101
102
```typescript
103
// Manual approach
104
const factory = await ethers.getContractFactory("MyContract");
105
const contract = await factory.deploy(arg1, arg2);
106
107
// Simplified with deployContract
108
const contract = await ethers.deployContract("MyContract", [arg1, arg2]);
109
```
110
111
## Factory Options Support
112
113
The `deployContract` function supports the same `FactoryOptions` as `getContractFactory`:
114
115
```typescript { .api }
116
interface FactoryOptions {
117
signer?: ethers.Signer;
118
libraries?: Libraries;
119
}
120
121
interface Libraries {
122
[libraryName: string]: string;
123
}
124
```
125
126
### Library Linking Examples
127
128
```typescript
129
import { ethers } from "hardhat";
130
131
// Deploy contract with linked libraries
132
const contract = await ethers.deployContract("ComplexContract", ["param1"], {
133
libraries: {
134
"MathUtils": "0x1111111111111111111111111111111111111111",
135
"StringUtils": "0x2222222222222222222222222222222222222222"
136
}
137
});
138
139
// Deploy with specific signer and libraries
140
const [deployer] = await ethers.getSigners();
141
const contract = await ethers.deployContract("MyContract", [], {
142
signer: deployer,
143
libraries: {
144
"SafeMath": mathLibAddress
145
}
146
});
147
```
148
149
## Deployment Patterns
150
151
### Basic Contract Deployment
152
153
```typescript
154
import { ethers } from "hardhat";
155
156
async function deployBasicContract() {
157
// Deploy simple contract
158
const storage = await ethers.deployContract("SimpleStorage");
159
await storage.deployed();
160
161
console.log("SimpleStorage deployed to:", storage.address);
162
return storage;
163
}
164
```
165
166
### Token Contract Deployment
167
168
```typescript
169
import { ethers } from "hardhat";
170
171
async function deployToken() {
172
const name = "MyToken";
173
const symbol = "MTK";
174
const initialSupply = ethers.utils.parseEther("1000000");
175
176
const token = await ethers.deployContract("ERC20Token", [
177
name,
178
symbol,
179
initialSupply
180
]);
181
await token.deployed();
182
183
console.log(`${name} deployed to:`, token.address);
184
return token;
185
}
186
```
187
188
### Contract with Dependencies
189
190
```typescript
191
import { ethers } from "hardhat";
192
193
async function deployWithDependencies() {
194
// First deploy library
195
const mathLib = await ethers.deployContract("MathLibrary");
196
await mathLib.deployed();
197
198
// Deploy main contract with library
199
const calculator = await ethers.deployContract("Calculator", [], {
200
libraries: {
201
"MathLibrary": mathLib.address
202
}
203
});
204
await calculator.deployed();
205
206
return { mathLib, calculator };
207
}
208
```
209
210
### Multi-Signer Deployment
211
212
```typescript
213
import { ethers } from "hardhat";
214
215
async function deployWithMultipleSigners() {
216
const [deployer, admin, user] = await ethers.getSigners();
217
218
// Deploy with specific deployer
219
const contract = await ethers.deployContract("MyContract", [admin.address], deployer);
220
await contract.deployed();
221
222
// Transfer ownership to admin
223
const tx = await contract.transferOwnership(admin.address);
224
await tx.wait();
225
226
return contract;
227
}
228
```
229
230
## Transaction Options
231
232
You can provide transaction overrides by accessing the underlying factory:
233
234
```typescript
235
import { ethers } from "hardhat";
236
237
// For transaction overrides, use the factory approach
238
const factory = await ethers.getContractFactory("MyContract");
239
const contract = await factory.deploy("arg1", {
240
gasLimit: 500000,
241
gasPrice: ethers.utils.parseUnits("20", "gwei"),
242
value: ethers.utils.parseEther("1") // Send 1 ETH with deployment
243
});
244
await contract.deployed();
245
```
246
247
## Error Handling
248
249
The `deployContract` function can throw the same errors as `getContractFactory`:
250
251
- **Contract not found**: When the specified contract name doesn't exist
252
- **Abstract contract**: When trying to deploy an abstract contract
253
- **Missing libraries**: When required libraries are not provided
254
- **Invalid library address**: When provided library addresses are invalid
255
- **Deployment failure**: When the deployment transaction fails or reverts
256
257
## Post-Deployment Actions
258
259
After deployment, you typically want to:
260
261
```typescript
262
import { ethers } from "hardhat";
263
264
async function deployAndSetup() {
265
// Deploy contract
266
const contract = await ethers.deployContract("MyContract", ["arg1"]);
267
await contract.deployed();
268
269
console.log("Contract deployed to:", contract.address);
270
console.log("Transaction hash:", contract.deployTransaction.hash);
271
272
// Wait for confirmations
273
await contract.deployTransaction.wait(2); // Wait for 2 confirmations
274
275
// Verify deployment
276
const code = await ethers.provider.getCode(contract.address);
277
if (code === "0x") {
278
throw new Error("Contract deployment failed");
279
}
280
281
// Setup contract state
282
const setupTx = await contract.initialize();
283
await setupTx.wait();
284
285
return contract;
286
}
287
```