-
Notifications
You must be signed in to change notification settings - Fork 28
/
README.md
2265 lines (1994 loc) · 94.1 KB
/
README.md
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# [`CreateX`](./src/CreateX.sol) – A Trustless, Universal Contract Deployer
[![🕵️♂️ Test CreateX](https://github.com/pcaversaccio/createx/actions/workflows/test-createx.yml/badge.svg)](https://github.com/pcaversaccio/createx/actions/workflows/test-createx.yml)
[![Test Coverage](https://img.shields.io/badge/Coverage-100%25-green)](#test-coverage)
[![👮♂️ Sanity checks](https://github.com/pcaversaccio/createx/actions/workflows/checks.yml/badge.svg)](https://github.com/pcaversaccio/createx/actions/workflows/checks.yml)
[![🚀 UI deployment](https://github.com/pcaversaccio/createx/actions/workflows/deploy.yml/badge.svg)](https://github.com/pcaversaccio/createx/actions/workflows/deploy.yml)
[![License: AGPL-3.0-only](https://img.shields.io/badge/License-AGPL--3.0--only-blue)](https://www.gnu.org/licenses/agpl-3.0)
<img src=https://github-production-user-asset-6210df.s3.amazonaws.com/25297591/272914952-38a5989c-0113-427d-9158-47646971b7d8.png width="1050"/>
Factory smart contract to make easier and safer usage of the [`CREATE`](https://www.evm.codes/#f0?fork=shanghai) and [`CREATE2`](https://www.evm.codes/#f5?fork=shanghai) EVM opcodes as well as of [`CREATE3`](https://github.com/ethereum/EIPs/pull/3171)-based (i.e. without an initcode factor) contract creations.
> [!NOTE]
> The [`CreateX`](./src/CreateX.sol) contract should be considered as maximally extensible. Be encouraged to build on top of it! The Solidity-based interface can be found [here](./src/ICreateX.sol).
- [`CreateX` – A Trustless, Universal Contract Deployer](#createx--a-trustless-universal-contract-deployer)
- [So What on Earth Is a Contract Factory?](#so-what-on-earth-is-a-contract-factory)
- [Available Versatile Functions](#available-versatile-functions)
- [Special Features](#special-features)
- [Permissioned Deploy Protection and Cross-Chain Redeploy Protection](#permissioned-deploy-protection-and-cross-chain-redeploy-protection)
- [Pseudo-Random Salt Value](#pseudo-random-salt-value)
- [Design Principles](#design-principles)
- [Security Considerations](#security-considerations)
- [Tests](#tests)
- [Test Coverage](#test-coverage)
- [ABI (Application Binary Interface)](#abi-application-binary-interface)
- [New Deployment(s)](#new-deployments)
- [`ethers.js`](#ethersjs)
- [`cast`](#cast)
- [Contract Verification](#contract-verification)
- [`CreateX` Deployments](#createx-deployments)
- [EVM-Based Production Networks](#evm-based-production-networks)
- [Ethereum Test Networks](#ethereum-test-networks)
- [Additional EVM-Based Test Networks](#additional-evm-based-test-networks)
- [Integration With External Tooling](#integration-with-external-tooling)
- [🙏🏼 Acknowledgement](#-acknowledgement)
## So What on Earth Is a Contract Factory?
It is important to understand that Ethereum Virtual Machine (EVM) opcodes can only be called via a smart contract. A contract factory in the context of the EVM refers to a special smart contract that is used to create and deploy other smart contracts on EVM-compatible blockchains using contract creation opcodes (i.e. [`CREATE`](https://www.evm.codes/#f0?fork=shanghai) or [`CREATE2`](https://www.evm.codes/#f5?fork=shanghai)). Using a contract factory provides a flexible and efficient way to deploy and manage smart contracts that share similar functionalities but may have different configurations or settings.
Different approaches can be used to create contracts using a factory contract, and this is exactly what [`CreateX`](./src/CreateX.sol) offers: _a comprehensive range of contract creation functions that are triggered by a smart contract itself_. It is worth emphasising the two differences in the address calculation of the opcodes [`CREATE`](https://www.evm.codes/#f0?fork=shanghai) and [`CREATE2`](https://www.evm.codes/#f5?fork=shanghai) (`||` stands for byte-wise concatenation, `[12:]` refers to the last 20 bytes of a 32-byte expression, and `rlp` is an abbreviation for Ethereum's "Recursive Length Prefix" serialisation scheme):
- [`CREATE`](https://www.evm.codes/#f0?fork=shanghai): `address computedAddress = keccak256(rlpEncode([deployerAddress, deployerNonce]))[12:]`,
- [`CREATE2`](https://www.evm.codes/#f5?fork=shanghai): `address computedAddress = keccak256(0xff||deployerAddress||salt||keccak256(initCode))[12:]`.
## Available Versatile Functions
```ml
CreateX
├── CREATE
│ ├── Read-Only Functions
│ │ ├── "function computeCreateAddress(uint256) view returns (address)"
│ │ └── "function computeCreateAddress(address,uint256) view returns (address)"
│ └── Write Functions
│ ├── "function deployCreate(bytes) payable returns (address)"
│ ├── "function deployCreateAndInit(bytes,bytes,tuple(uint256,uint256)) payable returns (address)"
│ ├── "function deployCreateAndInit(bytes,bytes,tuple(uint256,uint256),address) payable returns (address)"
│ └── "function deployCreateClone(address,bytes) payable returns (address)"
├── CREATE2
│ ├── Read-Only Functions
│ │ ├── "function computeCreate2Address(bytes32,bytes32) view returns (address)"
│ │ └── "function computeCreate2Address(bytes32,bytes32,address) pure returns (address)"
│ └── Write Functions
│ ├── "function deployCreate2(bytes) payable returns (address)"
│ ├── "function deployCreate2(bytes32,bytes) payable returns (address)"
│ ├── "function deployCreate2AndInit(bytes,bytes,tuple(uint256,uint256)) payable returns (address)"
│ ├── "function deployCreate2AndInit(bytes32,bytes,bytes,tuple(uint256,uint256)) payable returns (address)"
│ ├── "function deployCreate2AndInit(bytes,bytes,tuple(uint256,uint256),address) payable returns (address)"
│ ├── "function deployCreate2AndInit(bytes32,bytes,bytes,tuple(uint256,uint256),address) payable returns (address)"
│ ├── "function deployCreate2Clone(address,bytes) payable returns (address)"
│ └── "function deployCreate2Clone(bytes32,address,bytes) payable returns (address)"
└── CREATE3
├── Read-Only Functions
│ ├── "function computeCreate3Address(bytes32) view returns (address)"
│ └── "function computeCreate3Address(bytes32,address) pure returns (address)"
└── Write Functions
├── "function deployCreate3(bytes) payable returns (address)"
├── "function deployCreate3(bytes32,bytes) payable returns (address)"
├── "function deployCreate3AndInit(bytes,bytes,tuple(uint256,uint256)) payable returns (address)"
├── "function deployCreate3AndInit(bytes32,bytes,bytes,tuple(uint256,uint256)) payable returns (address)"
├── "function deployCreate3AndInit(bytes,bytes,tuple(uint256,uint256),address) payable returns (address)"
└── "function deployCreate3AndInit(bytes32,bytes,bytes,tuple(uint256,uint256),address) payable returns (address)"
```
<details>
<summary> <a href="https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L302-L317"><code>computeCreateAddress(uint256)</code></a> </summary>
Returns the address where a contract will be stored if deployed via _this contract_ (i.e. [`CreateX`](./src/CreateX.sol)) using the [`CREATE`](https://www.evm.codes/#f0?fork=shanghai) opcode. For the specification of the Recursive Length Prefix (RLP) encoding scheme, please refer to p. 19 of the [Ethereum Yellow Paper](https://ethereum.github.io/yellowpaper/paper.pdf) and the [Ethereum Wiki](https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp). Based on the [EIP-161](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-161.md) specification, all contract accounts on the Ethereum mainnet are initiated with `nonce = 1`. Thus, the first contract address created by another contract is calculated with a non-zero nonce.
```yml
# /*:°• Function Argument •°:*/ #
- name: nonce
type: uint256
description: The next 32-byte nonce of this contract.
# /*:°• Return Value •°:*/ #
- name: computedAddress
type: address
description: The 20-byte address where a contract will be stored.
```
</details>
<details>
<summary> <a href="https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L242-L300"><code>computeCreateAddress(address,uint256)</code></a> </summary>
Returns the address where a contract will be stored if deployed via `deployer` using the [`CREATE`](https://www.evm.codes/#f0?fork=shanghai) opcode. For the specification of the Recursive Length Prefix (RLP) encoding scheme, please refer to p. 19 of the [Ethereum Yellow Paper](https://ethereum.github.io/yellowpaper/paper.pdf) and the [Ethereum Wiki](https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp). Based on the [EIP-161](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-161.md) specification, all contract accounts on the Ethereum mainnet are initiated with `nonce = 1`. Thus, the first contract address created by another contract is calculated with a non-zero nonce.
```yml
# /*:°• Function Arguments •°:*/ #
- name: deployer
type: address
description: The 20-byte deployer address.
- name: nonce
type: uint256
description: The next 32-byte nonce of the deployer address.
# /*:°• Return Value •°:*/ #
- name: computedAddress
type: address
description: The 20-byte address where a contract will be stored.
```
</details>
<details>
<summary> <a href="https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L122-L136"><code>deployCreate(bytes)</code></a> </summary>
Deploys a new contract via calling the [`CREATE`](https://www.evm.codes/#f0?fork=shanghai) opcode and using the creation bytecode `initCode` and `msg.value` as inputs. In order to save deployment costs, we do not sanity check the `initCode` length. Note that if `msg.value` is non-zero, `initCode` must have a `payable` constructor.
```yml
# /*:°• Function Argument •°:*/ #
- name: initCode
type: bytes
description: The creation bytecode.
# /*:°• Return Value •°:*/ #
- name: newContract
type: address
description: The 20-byte address where the contract was deployed.
```
</details>
<details>
<summary> <a href="https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L180-L200"><code>deployCreateAndInit(bytes,bytes,tuple(uint256,uint256))</code></a> </summary>
Deploys and initialises a new contract via calling the [`CREATE`](https://www.evm.codes/#f0?fork=shanghai) opcode and using the creation bytecode `initCode`, the initialisation code `data`, the struct for the `payable` amounts `values`, and `msg.value` as inputs. In order to save deployment costs, we do not sanity check the `initCode` length. Note that if `values.constructorAmount` is non-zero, `initCode` must have a `payable` constructor, and any excess ether is returned to `msg.sender`.
```yml
# /*:°• Function Arguments •°:*/ #
- name: initCode
type: bytes
description: The creation bytecode.
- name: data
type: bytes
description: The initialisation code that is passed to the deployed contract.
- name: values
type: tuple(uint256,uint256)
description: The specific `payable` amounts for the deployment and initialisation call.
# /*:°• Return Value •°:*/ #
- name: newContract
type: address
description: The 20-byte address where the contract was deployed.
```
> ℹ️ **Note**<br>
> This function allows for reentrancy, however we refrain from adding a mutex lock to keep it as use-case agnostic as possible. Please ensure at the protocol level that potentially malicious reentrant calls do not affect your smart contract system.
</details>
<details>
<summary> <a href="https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L138-L178"><code>deployCreateAndInit(bytes,bytes,tuple(uint256,uint256),address)</code></a> </summary>
Deploys and initialises a new contract via calling the [`CREATE`](https://www.evm.codes/#f0?fork=shanghai) opcode and using the creation bytecode `initCode`, the initialisation code `data`, the struct for the `payable` amounts `values`, the refund address `refundAddress`, and `msg.value` as inputs. In order to save deployment costs, we do not sanity check the `initCode` length. Note that if `values.constructorAmount` is non-zero, `initCode` must have a `payable` constructor.
```yml
# /*:°• Function Arguments •°:*/ #
- name: initCode
type: bytes
description: The creation bytecode.
- name: data
type: bytes
description: The initialisation code that is passed to the deployed contract.
- name: values
type: tuple(uint256,uint256)
description: The specific `payable` amounts for the deployment and initialisation call.
- name: refundAddress
type: address
description: The 20-byte address where any excess ether is returned to.
# /*:°• Return Value •°:*/ #
- name: newContract
type: address
description: The 20-byte address where the contract was deployed.
```
> ℹ️ **Note**<br>
> This function allows for reentrancy, however we refrain from adding a mutex lock to keep it as use-case agnostic as possible. Please ensure at the protocol level that potentially malicious reentrant calls do not affect your smart contract system.
</details>
<details>
<summary> <a href="https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L202-L240"><code>deployCreateClone(address,bytes)</code> </a> </summary>
Deploys a new [EIP-1167](https://eips.ethereum.org/EIPS/eip-1167) minimal proxy contract using the [`CREATE`](https://www.evm.codes/#f0?fork=shanghai) opcode, and initialises the implementation contract using the implementation address `implementation`, the initialisation code `data`, and `msg.value` as inputs. Note that if `msg.value` is non-zero, the initialiser function called via `data` must be `payable`.
```yml
# /*:°• Function Arguments •°:*/ #
- name: implementation
type: address
description: The 20-byte implementation contract address.
- name: data
type: bytes
description: The initialisation code that is passed to the deployed proxy contract.
# /*:°• Return Value •°:*/ #
- name: newContract
type: address
description: The 20-byte address where the clone was deployed.
```
> ℹ️ **Note**<br>
> This function allows for reentrancy, however we refrain from adding a mutex lock to keep it as use-case agnostic as possible. Please ensure at the protocol level that potentially malicious reentrant calls do not affect your smart contract system.
</details>
<details>
<summary> <a href="https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L600-L610"><code>computeCreate2Address(bytes32,bytes32)</code></a> </summary>
Returns the address where a contract will be stored if deployed via _this contract_ (i.e. [`CreateX`](./src/CreateX.sol)) using the [`CREATE2`](https://www.evm.codes/#f5?fork=shanghai) opcode. Any change in the `initCodeHash` or `salt` values will result in a new destination address.
```yml
# /*:°• Function Arguments •°:*/ #
- name: salt
type: bytes32
description: The 32-byte random value used to create the contract address.
- name: initCodeHash
type: bytes32
description: The 32-byte bytecode digest of the contract creation bytecode.
# /*:°• Return Value •°:*/ #
- name: computedAddress
type: address
description: The 20-byte address where a contract will be stored.
```
</details>
<details>
<summary> <a href="https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L565-L598"><code>computeCreate2Address(bytes32,bytes32,address)</code></a> </summary>
Returns the address where a contract will be stored if deployed via `deployer` using the [`CREATE2`](https://www.evm.codes/#f5?fork=shanghai) opcode. Any change in the `initCodeHash` or `salt` values will result in a new destination address.
```yml
# /*:°• Function Arguments •°:*/ #
- name: salt
type: bytes32
description: The 32-byte random value used to create the contract address.
- name: initCodeHash
type: bytes32
description: The 32-byte bytecode digest of the contract creation bytecode.
- name: deployer
type: address
description: The 20-byte deployer address.
# /*:°• Return Value •°:*/ #
- name: computedAddress
type: address
description: The 20-byte address where a contract will be stored.
```
</details>
<details>
<summary> <a href="https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L341-L354"><code>deployCreate2(bytes)</code></a> </summary>
Deploys a new contract via calling the [`CREATE2`](https://www.evm.codes/#f5?fork=shanghai) opcode and using the creation bytecode `initCode` and `msg.value` as inputs. The salt value is calculated _pseudo-randomly_ using a diverse selection of block and transaction properties. This approach does not guarantee true randomness! In order to save deployment costs, we do not sanity check the `initCode` length. Note that if `msg.value` is non-zero, `initCode` must have a `payable` constructor.
```yml
# /*:°• Function Argument •°:*/ #
- name: initCode
type: bytes
description: The creation bytecode.
# /*:°• Return Value •°:*/ #
- name: newContract
type: address
description: The 20-byte address where the contract was deployed.
```
</details>
<details>
<summary> <a href="https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L323-L339"><code>deployCreate2(bytes32,bytes)</code></a> </summary>
Deploys a new contract via calling the [`CREATE2`](https://www.evm.codes/#f5?fork=shanghai) opcode and using the salt value `salt`, the creation bytecode `initCode`, and `msg.value` as inputs. In order to save deployment costs, we do not sanity check the `initCode` length. Note that if `msg.value` is non-zero, `initCode` must have a `payable` constructor.
```yml
# /*:°• Function Arguments •°:*/ #
- name: salt
type: bytes32
description: The 32-byte random value used to create the contract address.
- name: initCode
type: bytes
description: The creation bytecode.
# /*:°• Return Value •°:*/ #
- name: newContract
type: address
description: The 20-byte address where the contract was deployed.
```
</details>
<details>
<summary> <a href="https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L467-L497"><code>deployCreate2AndInit(bytes,bytes,tuple(uint256,uint256))</code></a> </summary>
Deploys and initialises a new contract via calling the [`CREATE2`](https://www.evm.codes/#f5?fork=shanghai) opcode and using the creation bytecode `initCode`, the initialisation code `data`, the struct for the `payable` amounts `values`, and `msg.value` as inputs. The salt value is calculated _pseudo-randomly_ using a diverse selection of block and transaction properties. This approach does not guarantee true randomness! In order to save deployment costs, we do not sanity check the `initCode` length. Note that if `values.constructorAmount` is non-zero, `initCode` must have a `payable` constructor, and any excess ether is returned to `msg.sender`.
```yml
# /*:°• Function Arguments •°:*/ #
- name: initCode
type: bytes
description: The creation bytecode.
- name: data
type: bytes
description: The initialisation code that is passed to the deployed contract.
- name: values
type: tuple(uint256,uint256)
description: The specific `payable` amounts for the deployment and initialisation call.
# /*:°• Return Value •°:*/ #
- name: newContract
type: address
description: The 20-byte address where the contract was deployed.
```
> ℹ️ **Note**<br>
> This function allows for reentrancy, however we refrain from adding a mutex lock to keep it as use-case agnostic as possible. Please ensure at the protocol level that potentially malicious reentrant calls do not affect your smart contract system.
</details>
<details>
<summary> <a href="https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L401-L431"><code>deployCreate2AndInit(bytes32,bytes,bytes,tuple(uint256,uint256))</code></a> </summary>
Deploys and initialises a new contract via calling the [`CREATE2`](https://www.evm.codes/#f5?fork=shanghai) opcode and using the salt value `salt`, creation bytecode `initCode`, the initialisation code `data`, the struct for the `payable` amounts `values`, and `msg.value` as inputs. In order to save deployment costs, we do not sanity check the `initCode` length. Note that if `values.constructorAmount` is non-zero, `initCode` must have a `payable` constructor, and any excess ether is returned to `msg.sender`.
```yml
# /*:°• Function Arguments •°:*/ #
- name: salt
type: bytes32
description: The 32-byte random value used to create the contract address.
- name: initCode
type: bytes
description: The creation bytecode.
- name: data
type: bytes
description: The initialisation code that is passed to the deployed contract.
- name: values
type: tuple(uint256,uint256)
description: The specific `payable` amounts for the deployment and initialisation call.
# /*:°• Return Value •°:*/ #
- name: newContract
type: address
description: The 20-byte address where the contract was deployed.
```
> ℹ️ **Note**<br>
> This function allows for reentrancy, however we refrain from adding a mutex lock to keep it as use-case agnostic as possible. Please ensure at the protocol level that potentially malicious reentrant calls do not affect your smart contract system.
</details>
<details>
<summary> <a href="https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L433-L465"><code>deployCreate2AndInit(bytes,bytes,tuple(uint256,uint256),address)</code></a> </summary>
Deploys and initialises a new contract via calling the [`CREATE2`](https://www.evm.codes/#f5?fork=shanghai) opcode and using the creation bytecode `initCode`, the initialisation code `data`, the struct for the `payable` amounts `values`, the refund address `refundAddress`, and `msg.value` as inputs. The salt value is calculated _pseudo-randomly_ using a diverse selection of block and transaction properties. This approach does not guarantee true randomness! In order to save deployment costs, we do not sanity check the `initCode` length. Note that if `values.constructorAmount` is non-zero, `initCode` must have a `payable` constructor.
```yml
# /*:°• Function Arguments •°:*/ #
- name: initCode
type: bytes
description: The creation bytecode.
- name: data
type: bytes
description: The initialisation code that is passed to the deployed contract.
- name: values
type: tuple(uint256,uint256)
description: The specific `payable` amounts for the deployment and initialisation call.
- name: refundAddress
type: address
description: The 20-byte address where any excess ether is returned to.
# /*:°• Return Value •°:*/ #
- name: newContract
type: address
description: The 20-byte address where the contract was deployed.
```
> ℹ️ **Note**<br>
> This function allows for reentrancy, however we refrain from adding a mutex lock to keep it as use-case agnostic as possible. Please ensure at the protocol level that potentially malicious reentrant calls do not affect your smart contract system.
</details>
<details>
<summary> <a href="https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L356-L399"><code>deployCreate2AndInit(bytes32,bytes,bytes,tuple(uint256,uint256),address)</code></a> </summary>
Deploys and initialises a new contract via calling the [`CREATE2`](https://www.evm.codes/#f5?fork=shanghai) opcode and using the salt value `salt`, the creation bytecode `initCode`, the initialisation code `data`, the struct for the `payable` amounts `values`, the refund address `refundAddress`, and `msg.value` as inputs. In order to save deployment costs, we do not sanity check the `initCode` length. Note that if `values.constructorAmount` is non-zero, `initCode` must have a `payable` constructor.
```yml
# /*:°• Function Arguments •°:*/ #
- name: salt
type: bytes32
description: The 32-byte random value used to create the contract address.
- name: initCode
type: bytes
description: The creation bytecode.
- name: data
type: bytes
description: The initialisation code that is passed to the deployed contract.
- name: values
type: tuple(uint256,uint256)
description: The specific `payable` amounts for the deployment and initialisation call.
- name: refundAddress
type: address
description: The 20-byte address where any excess ether is returned to.
# /*:°• Return Value •°:*/ #
- name: newContract
type: address
description: The 20-byte address where the contract was deployed.
```
> ℹ️ **Note**<br>
> This function allows for reentrancy, however we refrain from adding a mutex lock to keep it as use-case agnostic as possible. Please ensure at the protocol level that potentially malicious reentrant calls do not affect your smart contract system.
</details>
<details>
<summary> <a href="https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L545-L563"><code>deployCreate2Clone(address,bytes)</code></a> </summary>
Deploys a new [EIP-1167](https://eips.ethereum.org/EIPS/eip-1167) minimal proxy contract using the [`CREATE2`](https://www.evm.codes/#f5?fork=shanghai) opcode and the salt value `salt`, and initialises the implementation contract using the implementation address `implementation`, the initialisation code `data`, and `msg.value` as inputs. The salt value is calculated _pseudo-randomly_ using a diverse selection of block and transaction properties. This approach does not guarantee true randomness! Note that if `msg.value` is non-zero, the initialiser function called via `data` must be `payable`.
```yml
# /*:°• Function Arguments •°:*/ #
- name: implementation
type: address
description: The 20-byte implementation contract address.
- name: data
type: bytes
description: The initialisation code that is passed to the deployed proxy contract.
# /*:°• Return Value •°:*/ #
- name: newContract
type: address
description: The 20-byte address where the clone was deployed.
```
> ℹ️ **Note**<br>
> This function allows for reentrancy, however we refrain from adding a mutex lock to keep it as use-case agnostic as possible. Please ensure at the protocol level that potentially malicious reentrant calls do not affect your smart contract system.
</details>
<details>
<summary> <a href="https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L499-L543"><code>deployCreate2Clone(bytes32,address,bytes)</code></a> </summary>
Deploys a new [EIP-1167](https://eips.ethereum.org/EIPS/eip-1167) minimal proxy contract using the [`CREATE2`](https://www.evm.codes/#f5?fork=shanghai) opcode and the salt value `salt`, and initialises the implementation contract using the implementation address `implementation`, the initialisation code `data`, and `msg.value` as inputs. Note that if `msg.value` is non-zero, the initialiser function called via `data` must be `payable`.
```yml
# /*:°• Function Arguments •°:*/ #
- name: salt
type: bytes32
description: The 32-byte random value used to create the proxy contract address.
- name: implementation
type: address
description: The 20-byte implementation contract address.
- name: data
type: bytes
description: The initialisation code that is passed to the deployed proxy contract.
# /*:°• Return Value •°:*/ #
- name: newContract
type: address
description: The 20-byte address where the clone was deployed.
```
> ℹ️ **Note**<br>
> This function allows for reentrancy, however we refrain from adding a mutex lock to keep it as use-case agnostic as possible. Please ensure at the protocol level that potentially malicious reentrant calls do not affect your smart contract system.
</details>
<details>
<summary> <a href="https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L857-L867"><code>computeCreate3Address(bytes32)</code></a> </summary>
Returns the address where a contract will be stored if deployed via _this contract_ (i.e. [`CreateX`](./src/CreateX.sol)) using the [`CREATE3`](https://github.com/ethereum/EIPs/pull/3171) pattern (i.e. without an initcode factor). Any change in the `salt` value will result in a new destination address.
```yml
# /*:°• Function Argument •°:*/ #
- name: salt
type: bytes32
description: The 32-byte random value used to create the proxy contract address.
# /*:°• Return Value •°:*/ #
- name: computedAddress
type: address
description: The 20-byte address where a contract will be stored.
```
</details>
<details>
<summary> <a href="https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L830-L855"><code>computeCreate3Address(bytes32,address)</code></a> </summary>
Returns the address where a contract will be stored if deployed via `deployer` using the [`CREATE3`](https://github.com/ethereum/EIPs/pull/3171) pattern (i.e. without an initcode factor). Any change in the `salt` value will result in a new destination address.
```yml
# /*:°• Function Arguments •°:*/ #
- name: salt
type: bytes32
description: The 32-byte random value used to create the proxy contract address.
- name: deployer
type: address
description: The 20-byte deployer address.
# /*:°• Return Value •°:*/ #
- name: computedAddress
type: address
description: The 20-byte address where a contract will be stored.
```
</details>
<details>
<summary> <a href="https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L648-L663"><code>deployCreate3(bytes)</code></a> </summary>
Deploys a new contract via employing the [`CREATE3`](https://github.com/ethereum/EIPs/pull/3171) pattern (i.e. without an initcode factor) and using the salt value `salt`, the creation bytecode `initCode`, and `msg.value` as inputs. The salt value is calculated _pseudo-randomly_ using a diverse selection of block and transaction properties. This approach does not guarantee true randomness! In order to save deployment costs, we do not sanity check the `initCode` length. Note that if `msg.value` is non-zero, `initCode` must have a `payable` constructor.
```yml
# /*:°• Function Argument •°:*/ #
- name: initCode
type: bytes
description: The creation bytecode.
# /*:°• Return Value •°:*/ #
- name: newContract
type: address
description: The 20-byte address where the contract was deployed.
```
</details>
<details>
<summary> <a href="https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L616-L646"><code>deployCreate3(bytes32,bytes)</code></a> </summary>
Deploys a new contract via employing the [`CREATE3`](https://github.com/ethereum/EIPs/pull/3171) pattern (i.e. without an initcode factor) and using the salt value `salt`, the creation bytecode `initCode`, and `msg.value` as inputs. In order to save deployment costs, we do not sanity check the `initCode` length. Note that if `msg.value` is non-zero, `initCode` must have a `payable` constructor.
```yml
# /*:°• Function Arguments •°:*/ #
- name: salt
type: bytes32
description: The 32-byte random value used to create the proxy contract address.
- name: initCode
type: bytes
description: The creation bytecode.
# /*:°• Return Value •°:*/ #
- name: newContract
type: address
description: The 20-byte address where the contract was deployed.
```
> ℹ️ **Note**<br>
> We strongly recommend implementing a permissioned deploy protection by setting the first 20 bytes equal to `msg.sender` in the `salt` to prevent maliciously intended frontrun proxy deployments on other chains.
</details>
<details>
<summary> <a href="https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L797-L828"><code>deployCreate3AndInit(bytes,bytes,tuple(uint256,uint256))</code></a> </summary>
Deploys and initialises a new contract via employing the [`CREATE3`](https://github.com/ethereum/EIPs/pull/3171) pattern (i.e. without an initcode factor) and using the creation bytecode `initCode`, the initialisation code `data`, the struct for the `payable` amounts `values`, `msg.value` as inputs. The salt value is calculated _pseudo-randomly_ using a diverse selection of block and transaction properties. This approach does not guarantee true randomness! In order to save deployment costs, we do not sanity check the `initCode` length. Note that if `values.constructorAmount` is non-zero, `initCode` must have a `payable` constructor, and any excess ether is returned to `msg.sender`.
```yml
# /*:°• Function Arguments •°:*/ #
- name: initCode
type: bytes
description: The creation bytecode.
- name: data
type: bytes
description: The initialisation code that is passed to the deployed contract.
- name: values
type: tuple(uint256,uint256)
description: The specific `payable` amounts for the deployment and initialisation call.
# /*:°• Return Value •°:*/ #
- name: newContract
type: address
description: The 20-byte address where the contract was deployed.
```
> ℹ️ **Note**<br>
> This function allows for reentrancy, however we refrain from adding a mutex lock to keep it as use-case agnostic as possible. Please ensure at the protocol level that potentially malicious reentrant calls do not affect your smart contract system.
</details>
<details>
<summary> <a href="https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L725-L760"><code>deployCreate3AndInit(bytes32,bytes,bytes,tuple(uint256,uint256))</code></a> </summary>
Deploys and initialises a new contract via employing the [`CREATE3`](https://github.com/ethereum/EIPs/pull/3171) pattern (i.e. without an initcode factor) and using the salt value `salt`, the creation bytecode `initCode`, the initialisation code `data`, the struct for the `payable` amounts `values`, and `msg.value` as inputs. In order to save deployment costs, we do not sanity check the `initCode` length. Note that if `values.constructorAmount` is non-zero, `initCode` must have a `payable` constructor, and any excess ether is returned to `msg.sender`.
```yml
# /*:°• Function Arguments •°:*/ #
- name: salt
type: bytes32
description: The 32-byte random value used to create the proxy contract address.
- name: initCode
type: bytes
description: The creation bytecode.
- name: data
type: bytes
description: The initialisation code that is passed to the deployed contract.
- name: values
type: tuple(uint256,uint256)
description: The specific `payable` amounts for the deployment and initialisation call.
# /*:°• Return Value •°:*/ #
- name: newContract
type: address
description: The 20-byte address where the contract was deployed.
```
> ℹ️ **Note**<br>
> This function allows for reentrancy, however we refrain from adding a mutex lock to keep it as use-case agnostic as possible. Please ensure at the protocol level that potentially malicious reentrant calls do not affect your smart contract system. Furthermore, we strongly recommend implementing a permissioned deploy protection by setting the first 20 bytes equal to `msg.sender` in the `salt` to prevent maliciously intended frontrun proxy deployments on other chains.
</details>
<details>
<summary> <a href="https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L762-L795"><code>deployCreate3AndInit(bytes,bytes,tuple(uint256,uint256),address)</code></a> </summary>
Deploys and initialises a new contract via employing the [`CREATE3`](https://github.com/ethereum/EIPs/pull/3171) pattern (i.e. without an initcode factor) and using the creation bytecode `initCode`, the initialisation code `data`, the struct for the `payable` amounts `values`, the refund address `refundAddress`, and `msg.value` as inputs. The salt value is calculated _pseudo-randomly_ using a diverse selection of block and transaction properties. This approach does not guarantee true randomness! In order to save deployment costs, we do not sanity check the `initCode` length. Note that if `values.constructorAmount` is non-zero, `initCode` must have a `payable` constructor.
```yml
# /*:°• Function Arguments •°:*/ #
- name: initCode
type: bytes
description: The creation bytecode.
- name: data
type: bytes
description: The initialisation code that is passed to the deployed contract.
- name: values
type: tuple(uint256,uint256)
description: The specific `payable` amounts for the deployment and initialisation call.
- name: refundAddress
type: address
description: The 20-byte address where any excess ether is returned to.
# /*:°• Return Value •°:*/ #
- name: newContract
type: address
description: The 20-byte address where the contract was deployed.
```
> ℹ️ **Note**<br>
> This function allows for reentrancy, however we refrain from adding a mutex lock to keep it as use-case agnostic as possible. Please ensure at the protocol level that potentially malicious reentrant calls do not affect your smart contract system.
</details>
<details>
<summary> <a href="https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L665-L723"><code>deployCreate3AndInit(bytes32,bytes,bytes,tuple(uint256,uint256),address)</code></a> </summary>
Deploys and initialises a new contract via employing the [`CREATE3`](https://github.com/ethereum/EIPs/pull/3171) pattern (i.e. without an initcode factor) and using the salt value `salt`, the creation bytecode `initCode`, the initialisation code `data`, the struct for the `payable` amounts `values`, the refund address `refundAddress`, and `msg.value` as inputs. In order to save deployment costs, we do not sanity check the `initCode` length. Note that if `values.constructorAmount` is non-zero, `initCode` must have a `payable` constructor.
```yml
# /*:°• Function Arguments •°:*/ #
- name: salt
type: bytes32
description: The 32-byte random value used to create the proxy contract address.
- name: initCode
type: bytes
description: The creation bytecode.
- name: data
type: bytes
description: The initialisation code that is passed to the deployed contract.
- name: values
type: tuple(uint256,uint256)
description: The specific `payable` amounts for the deployment and initialisation call.
- name: refundAddress
type: address
description: The 20-byte address where any excess ether is returned to.
# /*:°• Return Value •°:*/ #
- name: newContract
type: address
description: The 20-byte address where the contract was deployed.
```
> ℹ️ **Note**<br>
> This function allows for reentrancy, however we refrain from adding a mutex lock to keep it as use-case agnostic as possible. Please ensure at the protocol level that potentially malicious reentrant calls do not affect your smart contract system. Furthermore, we strongly recommend implementing a permissioned deploy protection by setting the first 20 bytes equal to `msg.sender` in the `salt` to prevent maliciously intended frontrun proxy deployments on other chains.
</details>
## Special Features
> [!TIP]
> Note that the separate project [`createXcrunch`](https://github.com/HrikB/createXcrunch) is a [Rust](https://www.rust-lang.org)-based program designed to efficiently find _zero-leading_, _zero-containing_, or _pattern-matching_ deployment addresses, taking into account the subsequent special features of [`CreateX`](./src/CreateX.sol).
### Permissioned Deploy Protection and Cross-Chain Redeploy Protection
The `salt` value implements different safeguarding mechanisms depending on the encoded values in the salt (`||` stands for byte-wise concatenation):
```console
=> salt (32 bytes) = 0xbebebebebebebebebebebebebebebebebebebebe||ff||1212121212121212121212
```
- The first 20 bytes (i.e. `bebebebebebebebebebebebebebebebebebebebe`) may be used to implement a permissioned deploy protection by setting them equal to `msg.sender`,
- The 21st byte (i.e. `ff`) may be used to implement a cross-chain redeploy protection by setting it equal to `0x01`,
- The last random 11 bytes (i.e. `1212121212121212121212`) allow for $2^{88}$ bits of entropy for mining a salt.
The full logic is implemented in the `internal` [`_guard`](./src/CreateX.sol#L873-L912) function:
```solidity
function _guard(bytes32 salt) internal view returns (bytes32 guardedSalt) {
(
SenderBytes senderBytes,
RedeployProtectionFlag redeployProtectionFlag
) = _parseSalt({ salt: salt });
if (
senderBytes == SenderBytes.MsgSender &&
redeployProtectionFlag == RedeployProtectionFlag.True
) {
// Configures a permissioned deploy protection as well as a cross-chain redeploy protection.
guardedSalt = keccak256(abi.encode(msg.sender, block.chainid, salt));
} else if (
senderBytes == SenderBytes.MsgSender &&
redeployProtectionFlag == RedeployProtectionFlag.False
) {
// Configures solely a permissioned deploy protection.
guardedSalt = _efficientHash({
a: bytes32(uint256(uint160(msg.sender))),
b: salt
});
} else if (senderBytes == SenderBytes.MsgSender) {
// Reverts if the 21st byte is greater than `0x01` in order to enforce developer explicitness.
revert InvalidSalt({ emitter: _SELF });
} else if (
senderBytes == SenderBytes.ZeroAddress &&
redeployProtectionFlag == RedeployProtectionFlag.True
) {
// Configures solely a cross-chain redeploy protection. In order to prevent a pseudo-randomly
// generated cross-chain redeploy protection, we enforce the zero address check for the first 20 bytes.
guardedSalt = _efficientHash({ a: bytes32(block.chainid), b: salt });
} else if (
senderBytes == SenderBytes.ZeroAddress &&
redeployProtectionFlag == RedeployProtectionFlag.Unspecified
) {
// Reverts if the 21st byte is greater than `0x01` in order to enforce developer explicitness.
revert InvalidSalt({ emitter: _SELF });
} else {
// For the non-pseudo-random cases, the salt value `salt` is hashed to prevent the safeguard mechanisms
// from being bypassed. Otherwise, the salt value `salt` is not modified.
guardedSalt = (salt != _generateSalt())
? keccak256(abi.encode(salt))
: salt;
}
}
```
Please note that when you configure a permissioned deploy protection, you **must** specify whether you want cross-chain redeploy protection (i.e. 21st byte equals `0x01`) or not (i.e. the 21st byte equals `0x00`). The underlying reason for this logic is to enforce developer explicitness. If you don't specify a cross-chain redeploy protection decision (i.e. the 21st byte is greater than `0x01`) the function reverts.
Furthermore, you can configure _only_ cross-chain redeploy protection by setting the first 20 bytes equal to the zero address `0x0000000000000000000000000000000000000000`. The rationale behind this logic is to prevent a pseudo-randomly generated 32 byte salt from inadvertently activating cross-chain redeploy protection. Also in this case, if you don't specify a cross-chain redeploy protection, i.e. the 21st byte is greater than `0x01`, the function reverts. The underlying reason for this logic is as well to enforce developer explicitness.
### Pseudo-Random Salt Value
For developer convenience, the [`CreateX`](./src/CreateX.sol) contract offers several overloaded functions that generate the salt value pseudo-randomly using a diverse selection of block and transaction properties. Please note that this approach does not guarantee true randomness!
The full logic is implemented in the `internal` [`_generateSalt`](./src/CreateX.sol#L960-L988) function:
```solidity
function _generateSalt() internal view returns (bytes32 salt) {
unchecked {
salt = keccak256(
abi.encode(
// We don't use `block.number - 256` (the maximum value on the EVM) to accommodate
// any chains that may try to reduce the amount of available historical block hashes.
// We also don't subtract 1 to mitigate any risks arising from consecutive block
// producers on a PoS chain. Therefore, we use `block.number - 32` as a reasonable
// compromise, one we expect should work on most chains, which is 1 epoch on Ethereum
// mainnet. Please note that if you use this function between the genesis block and block
// number 31, the block property `blockhash` will return zero, but the returned salt value
// `salt` will still have a non-zero value due to the hashing characteristic and the other
// remaining properties.
blockhash(block.number - 32),
block.coinbase,
block.number,
block.timestamp,
block.prevrandao,
block.chainid,
msg.sender
)
);
}
}
```
## Design Principles
- [`CreateX`](./src/CreateX.sol) should cover _most_ but not all contract creation use cases.
- [`CreateX`](./src/CreateX.sol) should be human-readable and should be simple to understand for readers with low prior experience.
- [`CreateX`](./src/CreateX.sol) should be maximally secure, i.e. no hidden footguns.
- [`CreateX`](./src/CreateX.sol) should be trustless.
- [`CreateX`](./src/CreateX.sol) should be stateless.
- [`CreateX`](./src/CreateX.sol) should be extensible (i.e. it can be used to deploy protocols, within protocols, or to deploy other types of deterministic deployer factories).
The following consequences result from these principles:
- We only use inline assembly if it is required or if the code section itself is based on short and/or audited code.
- We document the contract to the smallest detail.
- We extensively fuzz test all functions.
- We deliberately do not implement special functions for [clones with immutable arguments](https://github.com/wighawag/clones-with-immutable-args), as there is neither a finalised standard nor a properly audited contract version.
- We do not implement any special functions for [EIP-5202](https://eips.ethereum.org/EIPS/eip-5202) (a.k.a. blueprint contracts), as all existing functions in [`CreateX`](./src/CreateX.sol) are already cost-effective alternatives in our opinion.
## Security Considerations
<!-- prettier-ignore-start -->
> [!WARNING]
> **This contract is unaudited!** Special thanks go to [Oleksii Matiiasevych](https://github.com/lastperson) for his thorough review and feedback 🙏🏽.
<!-- prettier-ignore-end -->
Generally, for security issues, see our [Security Policy](./SECURITY.md). Furthermore, you must be aware of the following aspects:
- Several functions allow for reentrancy, however we refrain from adding a mutex lock to keep it as use-case agnostic as possible. Please ensure at the protocol level that potentially malicious reentrant calls do not affect your smart contract system.
- In the functions:
- [`deployCreate3(bytes32,bytes)`](https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L616-L646),
- [`deployCreate3AndInit(bytes32,bytes,bytes,tuple(uint256,uint256))`](https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L725-L760), and
- [`deployCreate3AndInit(bytes32,bytes,bytes,tuple(uint256,uint256),address)`](https://github.com/pcaversaccio/createx/blob/main/src/CreateX.sol#L665-L723)
we strongly recommend implementing a permissioned deploy protection by setting the first 20 bytes equal to `msg.sender` in the `salt` to prevent maliciously intended frontrun proxy deployments on other chains.
- The target EVM version for compilation is set to [`paris`](https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/paris.md), i.e. neither the contract creation bytecode of [`CreateX`](./src/CreateX.sol) nor the returned runtime bytecode contains a [`PUSH0`](https://www.evm.codes/#5f?fork=shanghai) instruction.
- Please refer to our comment in the discussion [here](https://github.com/pcaversaccio/createx/discussions/61#discussioncomment-7937359) for background information on the risks of our private-key-based deployment approach. We recommend verifying prior to interacting with [`CreateX`](./src/CreateX.sol) on any chain, that the `keccak256` hash of the broadcasted contract creation bytecode is `0x12ec861579b63a3ab9db3b5a23c57d56402ad3061475b088f17054e2f2daf22f` or of the deployed runtime bytecode is `0xbd8a7ea8cfca7b4e5f5041d7d4b17bc317c5ce42cfbc42066a00cf26b43eb53f`.
## Tests
For all (fuzz) tests available in the [`test`](./test) directory, we have consistently applied the [Branching Tree Technique](https://twitter.com/PaulRBerg/status/1682346315806539776) with [`bulloak`](https://github.com/alexfertel/bulloak). This means that each test file is accompanied by a `.tree` file that defines all the necessary branches to be tested.
**Example ([`CreateX._guard.tree`](./test/internal/CreateX._guard.tree)):**
```tree
CreateX_Guard_Internal_Test
├── When the first 20 bytes of the salt equals the caller
│ ├── When the 21st byte of the salt equals 0x01
│ │ └── It should return the keccak256 hash of the ABI-encoded values msg.sender, block.chainid, and the salt.
│ ├── When the 21st byte of the salt equals 0x00
│ │ └── It should return the keccak256 hash of the ABI-encoded values msg.sender and the salt.
│ └── When the 21st byte of the salt is greater than 0x01
│ └── It should revert.
├── When the first 20 bytes of the salt equals the zero address
│ ├── When the 21st byte of the salt equals 0x01
│ │ └── It should return the keccak256 hash of the ABI-encoded values block.chainid and the salt.
│ ├── When the 21st byte of the salt equals 0x00
│ │ └── It should return the keccak256 hash of the ABI-encoded value salt.
│ └── When the 21st byte of the salt is greater than 0x01
│ └── It should revert.
└── When the first 20 bytes of the salt do not equal the caller or the zero address
├── It should return the keccak256 hash of the ABI-encoded value salt.
└── When the salt value is generated pseudo-randomly
└── It should return the unmodified salt value.
```
### Test Coverage
This project repository uses [`forge coverage`](https://book.getfoundry.sh/reference/forge/forge-coverage). Simply run:
```console
forge coverage
```
In order to generate an `HTML` file with the coverage data, you can invoke:
```console
pnpm coverage:report
```
The written tests available in the directory [`test`](./test) achieve a test coverage of **100%** for the [`CreateX`](./src/CreateX.sol) contract:
```console
| File | % Lines | % Statements | % Branches | % Funcs |
|-----------------|-------------------|-------------------|-----------------|-----------------|
| src/CreateX.sol | 100.00% (149/149) | 100.00% (210/210) | 100.00% (78/78) | 100.00% (31/31) |
```
> [!IMPORTANT]
> A test coverage of 100% does not mean that there are no vulnerabilities. What really counts is the quality and spectrum of the tests themselves!
## ABI (Application Binary Interface)
> [!TIP]
> If you `forge install` this repository, the Solidity-based interface can also be found [here](./src/ICreateX.sol).
<details>
<summary> <a href="https://docs.soliditylang.org/en/latest/">Solidity</a> </summary>
```solidity
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.4;
/**
* @title CreateX Factory Interface Definition
* @author pcaversaccio (https://web.archive.org/web/20230921103111/https://pcaversaccio.com/)
* @custom:coauthor Matt Solomon (https://web.archive.org/web/20230921103335/https://mattsolomon.dev/)
*/
interface ICreateX {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* TYPES */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
struct Values {
uint256 constructorAmount;
uint256 initCallAmount;
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* EVENTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
event ContractCreation(address indexed newContract, bytes32 indexed salt);
event ContractCreation(address indexed newContract);
event Create3ProxyContractCreation(
address indexed newContract,
bytes32 indexed salt
);
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
error FailedContractCreation(address emitter);
error FailedContractInitialisation(address emitter, bytes revertData);
error InvalidSalt(address emitter);
error InvalidNonceValue(address emitter);
error FailedEtherTransfer(address emitter, bytes revertData);
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CREATE */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
function deployCreate(
bytes memory initCode
) external payable returns (address newContract);
function deployCreateAndInit(
bytes memory initCode,
bytes memory data,
Values memory values,
address refundAddress
) external payable returns (address newContract);
function deployCreateAndInit(
bytes memory initCode,
bytes memory data,
Values memory values
) external payable returns (address newContract);
function deployCreateClone(
address implementation,
bytes memory data
) external payable returns (address proxy);
function computeCreateAddress(
address deployer,
uint256 nonce
) external view returns (address computedAddress);
function computeCreateAddress(
uint256 nonce
) external view returns (address computedAddress);
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CREATE2 */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
function deployCreate2(
bytes32 salt,
bytes memory initCode
) external payable returns (address newContract);
function deployCreate2(
bytes memory initCode
) external payable returns (address newContract);
function deployCreate2AndInit(
bytes32 salt,
bytes memory initCode,
bytes memory data,
Values memory values,
address refundAddress