-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
VyperDeployer.sol
234 lines (211 loc) · 8.62 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
// SPDX-License-Identifier: WTFPL
pragma solidity ^0.8.21;
import {Create} from "create-util/Create.sol";
/**
* @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 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[](2);
cmds[0] = "vyper";
cmds[1] = string.concat(path, fileName, ".vy");
/**
* @dev Compile the Vyper contract and return the bytecode.
*/
bytes memory bytecode = cheatCodes.ffi(cmds);
/**
* @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[](2);
cmds[0] = "vyper";
cmds[1] = string.concat(path, fileName, ".vy");
/**
* @dev Compile the Vyper contract and return the bytecode.
*/
bytes memory bytecode = cheatCodes.ffi(cmds);
/**
* @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.
* @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 Paris hard fork is "paris".
* You can retrieve all available Vyper EVM versions 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,
bool /*overloadPlaceholder*/
) public returns (address deployedAddress) {
/**
* @dev Create a list of strings with the commands necessary
* to compile Vyper contracts.
*/
string[] memory cmds = new string[](4);
cmds[0] = "vyper";
cmds[1] = string.concat(path, fileName, ".vy");
cmds[2] = "--evm-version";
cmds[3] = evmVersion;
/**
* @dev Compile the Vyper contract and return the bytecode.
*/
bytes memory bytecode = cheatCodes.ffi(cmds);
/**
* @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.
* @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 Paris hard fork is "paris".
* You can retrieve all available Vyper EVM versions 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,
bool /*overloadPlaceholder*/
) public returns (address deployedAddress) {
/**
* @dev Create a list of strings with the commands necessary
* to compile Vyper contracts.
*/
string[] memory cmds = new string[](4);
cmds[0] = "vyper";
cmds[1] = string.concat(path, fileName, ".vy");
cmds[2] = "--evm-version";
cmds[3] = evmVersion;
/**
* @dev Compile the Vyper contract and return the bytecode.
*/
bytes memory bytecode = cheatCodes.ffi(cmds);
/**
* @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});
}
}