-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
VyperDeployer.sol
311 lines (283 loc) · 11.6 KB
/
VyperDeployer.sol
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// SPDX-License-Identifier: WTFPL
pragma solidity ^0.8.28;
import {Create} from "create-util/Create.sol";
import {console, StdStyle} from "forge-std/Test.sol";
/**
* @dev Error that occurs when compiling a contract has failed.
* @param emitter The contract that emits the error.
*/
error CompilationFailed(address emitter);
/**
* @dev Error that occurs when deploying a contract has failed.
* @param emitter The contract that emits the error.
*/
error DeploymentFailed(address emitter);
/**
* @dev The interface of this cheat code is called `_CheatCodes`,
* so you can use the `CheatCodes` interface (see here:
* https://book.getfoundry.sh/cheatcodes/?highlight=CheatCodes#cheatcode-types)
* in other test files without errors.
*/
// solhint-disable-next-line contract-name-camelcase
interface _CheatCodes {
function ffi(string[] calldata) external returns (bytes memory);
}
/**
* @title Vyper Contract Deployer
* @author pcaversaccio
* @notice Forked and adjusted accordingly from here:
* https://github.com/0xKitsune/Foundry-Vyper/blob/main/lib/utils/VyperDeployer.sol.
* @dev The Vyper Contract Deployer is a pre-built contract containing functions that
* use a path, a filename, any ABI-encoded constructor arguments, and optionally the
* target EVM version and the compiler optimisation mode, and deploy the corresponding
* Vyper contract, returning the address that the bytecode was deployed to.
*/
contract VyperDeployer is Create {
address private constant HEVM_ADDRESS =
address(uint160(uint256(keccak256("hevm cheat code"))));
address private self = address(this);
/**
* @dev Initialises `cheatCodes` in order to use the foreign function interface (ffi)
* to compile the Vyper contracts.
*/
_CheatCodes private cheatCodes = _CheatCodes(HEVM_ADDRESS);
/**
* @dev Compiles a Vyper contract and returns the address that the contract
* was deployed to. If the deployment fails, an error is thrown.
* @param path The directory path of the Vyper contract.
* For example, the path of "utils" is "src/utils/".
* @param fileName The file name of the Vyper contract.
* For example, the file name for "ECDSA.vy" is "ECDSA".
* @return deployedAddress The address that the contract was deployed to.
*/
function deployContract(
string calldata path,
string calldata fileName
) public returns (address deployedAddress) {
/**
* @dev Create a list of strings with the commands necessary
* to compile Vyper contracts.
*/
string[] memory cmds = new string[](3);
cmds[0] = "python";
cmds[1] = "lib/utils/compile.py";
cmds[2] = string.concat(path, fileName, ".vy");
/**
* @dev Compile the Vyper contract and return the bytecode.
*/
bytes memory bytecode = cheatCodes.ffi(cmds);
/**
* @dev Revert if the Vyper compilation failed or is a zero-length
* contract creation bytecode.
*/
if (bytecode.length == 0) {
// solhint-disable-next-line no-console
console.log(
StdStyle.red(
"Vyper compilation failed! Please ensure that you have a valid Vyper version installed."
)
);
revert CompilationFailed({emitter: self});
}
/**
* @dev Deploy the bytecode with the `CREATE` instruction.
*/
deployedAddress = deploy({amount: 0, bytecode: bytecode});
/**
* @dev Check that the deployment was successful.
*/
if (deployedAddress == address(0))
revert DeploymentFailed({emitter: self});
}
/**
* @dev Compiles a Vyper contract with constructor arguments and returns the
* address that the contract was deployed to. If the deployment fails, an error
* is thrown.
* @notice Function overload of `deployContract` that allows any ABI-encoded
* constructor arguments to be passed.
* @param path The directory path of the Vyper contract.
* For example, the path of "utils" is "src/utils/".
* @param fileName The file name of the Vyper contract.
* For example, the file name for "ECDSA.vy" is "ECDSA".
* @param args The ABI-encoded constructor arguments.
* @return deployedAddress The address that the contract was deployed to.
*/
function deployContract(
string calldata path,
string calldata fileName,
bytes calldata args
) public returns (address deployedAddress) {
/**
* @dev Create a list of strings with the commands necessary
* to compile Vyper contracts.
*/
string[] memory cmds = new string[](3);
cmds[0] = "python";
cmds[1] = "lib/utils/compile.py";
cmds[2] = string.concat(path, fileName, ".vy");
/**
* @dev Compile the Vyper contract and return the bytecode.
*/
bytes memory bytecode = cheatCodes.ffi(cmds);
/**
* @dev Revert if the Vyper compilation failed or is a zero-length
* contract creation bytecode.
*/
if (bytecode.length == 0) {
// solhint-disable-next-line no-console
console.log(
StdStyle.red(
"Vyper compilation failed! Please ensure that you have a valid Vyper version installed."
)
);
revert CompilationFailed({emitter: self});
}
/**
* @dev Add the ABI-encoded constructor arguments to the
* deployment bytecode.
*/
bytecode = abi.encodePacked(bytecode, args);
/**
* @dev Deploy the bytecode with the `CREATE` instruction.
*/
deployedAddress = deploy({amount: 0, bytecode: bytecode});
/**
* @dev Check that the deployment was successful.
*/
if (deployedAddress == address(0))
revert DeploymentFailed({emitter: self});
}
/**
* @dev Compiles a Vyper contract and returns the address that the contract
* was deployed to. If the deployment fails, an error is thrown.
* @notice Function overload of `deployContract` that allows the configuration
* of the target EVM version and the compiler optimisation mode.
* @param path The directory path of the Vyper contract.
* For example, the path of "utils" is "src/utils/".
* @param fileName The file name of the Vyper contract.
* For example, the file name for "ECDSA.vy" is "ECDSA".
* @param evmVersion The EVM version used for compilation.
* For example, the EVM version for the Cancun-Deneb hard fork is "cancun".
* You can retrieve all available Vyper EVM versions by invoking `vyper -h`.
* @param optimiserMode The optimisation mode used for compilation.
* For example, the default optimisation mode since Vyper `0.3.10` is "gas".
* You can retrieve all available Vyper optimisation modes by invoking `vyper -h`.
* @return deployedAddress The address that the contract was deployed to.
*/
function deployContract(
string calldata path,
string calldata fileName,
string calldata evmVersion,
string calldata optimiserMode
) public returns (address deployedAddress) {
/**
* @dev Create a list of strings with the commands necessary
* to compile Vyper contracts.
*/
string[] memory cmds = new string[](7);
cmds[0] = "python";
cmds[1] = "lib/utils/compile.py";
cmds[2] = string.concat(path, fileName, ".vy");
cmds[3] = "--evm-version";
cmds[4] = evmVersion;
cmds[5] = "--optimize";
cmds[6] = optimiserMode;
/**
* @dev Compile the Vyper contract and return the bytecode.
*/
bytes memory bytecode = cheatCodes.ffi(cmds);
/**
* @dev Revert if the Vyper compilation failed or is a zero-length
* contract creation bytecode.
*/
if (bytecode.length == 0) {
// solhint-disable-next-line no-console
console.log(
StdStyle.red(
"Vyper compilation failed! Please ensure that you have a valid Vyper version installed."
)
);
revert CompilationFailed({emitter: self});
}
/**
* @dev Deploy the bytecode with the `CREATE` instruction.
*/
deployedAddress = deploy({amount: 0, bytecode: bytecode});
/**
* @dev Check that the deployment was successful.
*/
if (deployedAddress == address(0))
revert DeploymentFailed({emitter: self});
}
/**
* @dev Compiles a Vyper contract with constructor arguments and returns the
* address that the contract was deployed to. If the deployment fails, an error
* is thrown.
* @notice Function overload of `deployContract`, which allows the passing of
* any ABI-encoded constructor arguments and enables the configuration of the
* target EVM version and the compiler optimisation mode.
* @param path The directory path of the Vyper contract.
* For example, the path of "utils" is "src/utils/".
* @param fileName The file name of the Vyper contract.
* For example, the file name for "ECDSA.vy" is "ECDSA".
* @param args The ABI-encoded constructor arguments.
* @param evmVersion The EVM version used for compilation.
* For example, the EVM version for the Cancun-Deneb hard fork is "cancun".
* You can retrieve all available Vyper EVM versions by invoking `vyper -h`.
* @param optimiserMode The optimisation mode used for compilation.
* For example, the default optimisation mode since Vyper `0.3.10` is "gas".
* You can retrieve all available Vyper optimisation modes by invoking `vyper -h`.
* @return deployedAddress The address that the contract was deployed to.
*/
function deployContract(
string calldata path,
string calldata fileName,
bytes calldata args,
string calldata evmVersion,
string calldata optimiserMode
) public returns (address deployedAddress) {
/**
* @dev Create a list of strings with the commands necessary
* to compile Vyper contracts.
*/
string[] memory cmds = new string[](7);
cmds[0] = "python";
cmds[1] = "lib/utils/compile.py";
cmds[2] = string.concat(path, fileName, ".vy");
cmds[3] = "--evm-version";
cmds[4] = evmVersion;
cmds[5] = "--optimize";
cmds[6] = optimiserMode;
/**
* @dev Compile the Vyper contract and return the bytecode.
*/
bytes memory bytecode = cheatCodes.ffi(cmds);
/**
* @dev Revert if the Vyper compilation failed or is a zero-length
* contract creation bytecode.
*/
if (bytecode.length == 0) {
// solhint-disable-next-line no-console
console.log(
StdStyle.red(
"Vyper compilation failed! Please ensure that you have a valid Vyper version installed."
)
);
revert CompilationFailed({emitter: self});
}
/**
* @dev Add the ABI-encoded constructor arguments to the
* deployment bytecode.
*/
bytecode = abi.encodePacked(bytecode, args);
/**
* @dev Deploy the bytecode with the `CREATE` instruction.
*/
deployedAddress = deploy({amount: 0, bytecode: bytecode});
/**
* @dev Check that the deployment was successful.
*/
if (deployedAddress == address(0))
revert DeploymentFailed({emitter: self});
}
}