@@ -10,6 +10,12 @@ contract HuffConfig {
1010 /// @notice Initializes cheat codes in order to use ffi to compile Huff contracts
1111 Vm public constant vm = Vm (address (bytes20 (uint160 (uint256 (keccak256 ("hevm cheat code " ))))));
1212
13+ /// @notice Enum that declares two possible values for a CodeType parameter
14+ enum CodeType {
15+ CREATION,
16+ RUNTIME
17+ }
18+
1319 /// @notice Struct that represents a constant to be passed to the `-c` flag
1420 struct Constant {
1521 string key;
@@ -144,82 +150,20 @@ contract HuffConfig {
144150
145151 /// @notice Get the creation bytecode of a contract
146152 function creation_code (string memory file ) public payable returns (bytes memory bytecode ) {
147- binary_check ();
148-
149- // Split the file into its parts
150- strings.slice memory s = file.toSlice ();
151- strings.slice memory delim = "/ " .toSlice ();
152- string [] memory parts = new string [](s.count (delim) + 1 );
153- for (uint256 i = 0 ; i < parts.length ; i++ ) {
154- parts[i] = s.split (delim).toString ();
155- }
156-
157- // Get the system time with our script
158- string [] memory time = new string [](1 );
159- time[0 ] = "./lib/foundry-huff/scripts/rand_bytes.sh " ;
160- bytes memory retData = vm.ffi (time);
161- string memory rand_bytes = bytes32ToString (keccak256 (abi.encode (bytes32 (retData))));
162-
163- // Re-concatenate the file with a "__TEMP__" prefix
164- string memory tempFile = parts[0 ];
165- if (parts.length <= 1 ) {
166- tempFile = string .concat ("__TEMP__ " , rand_bytes, tempFile);
167- } else {
168- for (uint256 i = 1 ; i < parts.length - 1 ; i++ ) {
169- tempFile = string .concat (tempFile, "/ " , parts[i]);
170- }
171- tempFile = string .concat (tempFile, "/ " , "__TEMP__ " , rand_bytes, parts[parts.length - 1 ]);
172- }
173-
174- // Paste the code in a new temp file
175- string [] memory create_cmds = new string [](3 );
176- // TODO: create_cmds[0] = "$(find . -name \"file_writer.sh\")";
177- create_cmds[0 ] = "./lib/foundry-huff/scripts/file_writer.sh " ;
178- create_cmds[1 ] = string .concat ("src/ " , tempFile, ".huff " );
179- create_cmds[2 ] = string .concat (code, "\n " );
180- vm.ffi (create_cmds);
181-
182- // Append the real code to the temp file
183- string [] memory append_cmds = new string [](3 );
184- append_cmds[0 ] = "./lib/foundry-huff/scripts/read_and_append.sh " ;
185- append_cmds[1 ] = string .concat ("src/ " , tempFile, ".huff " );
186- append_cmds[2 ] = string .concat ("src/ " , file, ".huff " );
187- vm.ffi (append_cmds);
188-
189- /// Create a list of strings with the commands necessary to compile Huff contracts
190- string [] memory cmds = new string [](5 );
191- if (const_overrides.length > 0 ) {
192- cmds = new string [](6 + const_overrides.length );
193- cmds[5 ] = "-c " ;
194-
195- Constant memory cur_const;
196- for (uint256 i; i < const_overrides.length ; i++ ) {
197- cur_const = const_overrides[i];
198- cmds[6 + i] = string .concat (cur_const.key, "= " , cur_const.value);
199- }
200- }
201-
202- cmds[0 ] = "huffc " ;
203- cmds[1 ] = string (string .concat ("src/ " , tempFile, ".huff " ));
204- cmds[2 ] = "-b " ;
205- cmds[3 ] = "-e " ;
206- cmds[4 ] = get_evm_version ();
207-
208- /// @notice compile the Huff contract and return the bytecode
209- bytecode = vm.ffi (cmds);
210-
211- // Clean up temp files
212- string [] memory cleanup = new string [](2 );
213- cleanup[0 ] = "rm " ;
214- cleanup[1 ] = string .concat ("src/ " , tempFile, ".huff " );
215-
216- // set `msg.sender` for upcoming create context
217- vm.prank (deployer);
218-
219- vm.ffi (cleanup);
153+ return get_code (file, CodeType.CREATION);
220154 }
221155
156+ /// @notice Get the runtime bytecode of a contract
222157 function runtime_code (string memory file ) public payable returns (bytes memory bytecode ) {
158+ return get_code (file, CodeType.RUNTIME);
159+ }
160+
161+ /// @notice Get the bytecode of a contract
162+ function get_code (string memory file , CodeType codeType )
163+ public
164+ payable
165+ returns (bytes memory bytecode )
166+ {
223167 binary_check ();
224168
225169 // Split the file into its parts
@@ -277,7 +221,11 @@ contract HuffConfig {
277221
278222 cmds[0 ] = "huffc " ;
279223 cmds[1 ] = string (string .concat ("src/ " , tempFile, ".huff " ));
280- cmds[2 ] = "-r " ;
224+ if (codeType == CodeType.CREATION) {
225+ cmds[2 ] = "-b " ;
226+ } else {
227+ cmds[2 ] = "-r " ;
228+ }
281229 cmds[3 ] = "-e " ;
282230 cmds[4 ] = get_evm_version ();
283231
0 commit comments