@lukso/lsp-smart-contracts 0.10.0 → 0.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/README.md +14 -1
  2. package/artifacts/Create2Factory.json +2 -2
  3. package/artifacts/LSP0ERC725Account.json +4 -4
  4. package/artifacts/LSP0ERC725AccountInit.json +4 -4
  5. package/artifacts/LSP11BasicSocialRecovery.json +2 -2
  6. package/artifacts/LSP11BasicSocialRecoveryInit.json +2 -2
  7. package/artifacts/LSP16UniversalFactory.json +27 -21
  8. package/artifacts/LSP1UniversalReceiverDelegateUP.json +2 -2
  9. package/artifacts/LSP1UniversalReceiverDelegateVault.json +2 -2
  10. package/artifacts/LSP6KeyManager.json +2 -2
  11. package/artifacts/LSP6KeyManagerInit.json +2 -2
  12. package/artifacts/LSP7CompatibleERC20Mintable.json +2 -2
  13. package/artifacts/LSP7CompatibleERC20MintableInit.json +2 -2
  14. package/artifacts/LSP7Mintable.json +2 -2
  15. package/artifacts/LSP7MintableInit.json +2 -2
  16. package/artifacts/LSP8CompatibleERC721Mintable.json +2 -2
  17. package/artifacts/LSP8CompatibleERC721MintableInit.json +2 -2
  18. package/artifacts/LSP8Mintable.json +2 -2
  19. package/artifacts/LSP8MintableInit.json +2 -2
  20. package/artifacts/LSP9Vault.json +2 -2
  21. package/artifacts/LSP9VaultInit.json +2 -2
  22. package/artifacts/UniversalProfile.json +4 -4
  23. package/artifacts/UniversalProfileInit.json +4 -4
  24. package/artifacts/UniversalReceiverAddressStore.json +2 -2
  25. package/contracts/Factories/Create2Factory.sol +11 -3
  26. package/contracts/LSP0ERC725Account/ILSP0ERC725Account.sol +13 -5
  27. package/contracts/LSP0ERC725Account/LSP0ERC725Account.sol +11 -6
  28. package/contracts/LSP0ERC725Account/LSP0ERC725AccountCore.sol +256 -186
  29. package/contracts/LSP0ERC725Account/LSP0ERC725AccountInit.sol +20 -6
  30. package/contracts/LSP0ERC725Account/LSP0ERC725AccountInitAbstract.sol +23 -9
  31. package/contracts/LSP0ERC725Account/LSP0Utils.sol +3 -5
  32. package/contracts/LSP10ReceivedVaults/LSP10Utils.sol +25 -10
  33. package/contracts/LSP11BasicSocialRecovery/ILSP11BasicSocialRecovery.sol +4 -2
  34. package/contracts/LSP11BasicSocialRecovery/LSP11BasicSocialRecovery.sol +3 -1
  35. package/contracts/LSP11BasicSocialRecovery/LSP11BasicSocialRecoveryCore.sol +65 -23
  36. package/contracts/LSP11BasicSocialRecovery/LSP11BasicSocialRecoveryInit.sol +7 -2
  37. package/contracts/LSP11BasicSocialRecovery/LSP11BasicSocialRecoveryInitAbstract.sol +14 -4
  38. package/contracts/LSP11BasicSocialRecovery/LSP11Errors.sol +4 -1
  39. package/contracts/LSP14Ownable2Step/ILSP14Ownable2Step.sol +17 -16
  40. package/contracts/LSP14Ownable2Step/LSP14Errors.sol +4 -1
  41. package/contracts/LSP14Ownable2Step/LSP14Ownable2Step.sol +22 -11
  42. package/contracts/LSP16UniversalFactory/LSP16UniversalFactory.sol +333 -128
  43. package/contracts/LSP17ContractExtension/LSP17Extendable.sol +38 -16
  44. package/contracts/LSP17ContractExtension/LSP17Extension.sol +20 -8
  45. package/contracts/LSP17ContractExtension/LSP17Utils.sol +4 -5
  46. package/contracts/LSP1UniversalReceiver/ILSP1UniversalReceiver.sol +5 -5
  47. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateUP/LSP1UniversalReceiverDelegateUP.sol +38 -14
  48. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateVault/LSP1UniversalReceiverDelegateVault.sol +31 -11
  49. package/contracts/LSP1UniversalReceiver/LSP1Utils.sol +34 -9
  50. package/contracts/LSP20CallVerification/{ILSP20CallVerification.sol → ILSP20CallVerifier.sol} +7 -2
  51. package/contracts/LSP20CallVerification/LSP20CallVerification.sol +24 -9
  52. package/contracts/LSP20CallVerification/LSP20Errors.sol +1 -1
  53. package/contracts/LSP2ERC725YJSONSchema/LSP2Utils.sol +62 -36
  54. package/contracts/LSP4DigitalAssetMetadata/ILSP4Compatibility.sol +3 -1
  55. package/contracts/LSP4DigitalAssetMetadata/LSP4DigitalAssetMetadata.sol +15 -4
  56. package/contracts/LSP4DigitalAssetMetadata/LSP4DigitalAssetMetadataInitAbstract.sol +18 -5
  57. package/contracts/LSP5ReceivedAssets/LSP5Utils.sol +32 -12
  58. package/contracts/LSP6KeyManager/ILSP6KeyManager.sol +17 -8
  59. package/contracts/LSP6KeyManager/LSP6Constants.sol +3 -9
  60. package/contracts/LSP6KeyManager/LSP6Errors.sol +4 -1
  61. package/contracts/LSP6KeyManager/LSP6KeyManagerCore.sol +114 -72
  62. package/contracts/LSP6KeyManager/LSP6KeyManagerInitAbstract.sol +7 -2
  63. package/contracts/LSP6KeyManager/LSP6Modules/LSP6ExecuteModule.sol +96 -46
  64. package/contracts/LSP6KeyManager/LSP6Modules/LSP6OwnershipModule.sol +10 -5
  65. package/contracts/LSP6KeyManager/LSP6Modules/LSP6SetDataModule.sol +98 -32
  66. package/contracts/LSP6KeyManager/LSP6Utils.sol +42 -34
  67. package/contracts/LSP7DigitalAsset/ILSP7DigitalAsset.sol +8 -6
  68. package/contracts/LSP7DigitalAsset/LSP7DigitalAsset.sol +13 -10
  69. package/contracts/LSP7DigitalAsset/LSP7DigitalAssetCore.sol +96 -26
  70. package/contracts/LSP7DigitalAsset/LSP7DigitalAssetInitAbstract.sol +11 -9
  71. package/contracts/LSP7DigitalAsset/LSP7Errors.sol +8 -2
  72. package/contracts/LSP7DigitalAsset/extensions/ILSP7CompatibleERC20.sol +9 -2
  73. package/contracts/LSP7DigitalAsset/extensions/LSP7Burnable.sol +1 -5
  74. package/contracts/LSP7DigitalAsset/extensions/LSP7BurnableInitAbstract.sol +4 -6
  75. package/contracts/LSP7DigitalAsset/extensions/LSP7CappedSupplyInitAbstract.sol +6 -2
  76. package/contracts/LSP7DigitalAsset/extensions/LSP7CompatibleERC20.sol +32 -12
  77. package/contracts/LSP7DigitalAsset/extensions/LSP7CompatibleERC20InitAbstract.sol +28 -7
  78. package/contracts/LSP7DigitalAsset/presets/LSP7CompatibleERC20MintableInit.sol +8 -2
  79. package/contracts/LSP7DigitalAsset/presets/LSP7CompatibleERC20MintableInitAbstract.sol +6 -2
  80. package/contracts/LSP7DigitalAsset/presets/LSP7MintableInit.sol +6 -1
  81. package/contracts/LSP7DigitalAsset/presets/LSP7MintableInitAbstract.sol +13 -3
  82. package/contracts/LSP8IdentifiableDigitalAsset/ILSP8IdentifiableDigitalAsset.sol +14 -5
  83. package/contracts/LSP8IdentifiableDigitalAsset/LSP8Errors.sol +3 -1
  84. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAsset.sol +12 -10
  85. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAssetCore.sol +95 -28
  86. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAssetInitAbstract.sol +14 -10
  87. package/contracts/LSP8IdentifiableDigitalAsset/extensions/ILSP8CompatibleERC721.sol +23 -10
  88. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8Burnable.sol +3 -1
  89. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CappedSupply.sol +3 -1
  90. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CappedSupplyInitAbstract.sol +6 -2
  91. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721.sol +72 -30
  92. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721InitAbstract.sol +77 -27
  93. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8Enumerable.sol +6 -2
  94. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8EnumerableInitAbstract.sol +6 -2
  95. package/contracts/LSP8IdentifiableDigitalAsset/presets/ILSP8Mintable.sol +3 -1
  96. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8CompatibleERC721MintableInit.sol +8 -2
  97. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8CompatibleERC721MintableInitAbstract.sol +6 -2
  98. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8Mintable.sol +3 -1
  99. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8MintableInitAbstract.sol +5 -1
  100. package/contracts/LSP9Vault/ILSP9Vault.sol +13 -5
  101. package/contracts/LSP9Vault/LSP9Vault.sol +7 -2
  102. package/contracts/LSP9Vault/LSP9VaultCore.sol +108 -51
  103. package/contracts/LSP9Vault/LSP9VaultInitAbstract.sol +10 -3
  104. package/contracts/Legacy/Registries/AddressRegistry.sol +13 -4
  105. package/contracts/Legacy/Registries/AddressRegistryRequiresERC725.sol +6 -2
  106. package/contracts/Legacy/UniversalReceiverAddressStore.sol +17 -6
  107. package/contracts/UniversalProfile.sol +14 -5
  108. package/contracts/UniversalProfileInit.sol +17 -5
  109. package/contracts/UniversalProfileInitAbstract.sol +17 -9
  110. package/devdocs/LSP0ERC725Account.json +33 -19
  111. package/devdocs/LSP0ERC725AccountInit.json +33 -20
  112. package/devdocs/LSP16UniversalFactory.json +87 -22
  113. package/devdocs/LSP9Vault.json +3 -3
  114. package/devdocs/LSP9VaultInit.json +3 -3
  115. package/devdocs/UniversalProfile.json +32 -18
  116. package/devdocs/UniversalProfileInit.json +33 -19
  117. package/dist/constants.cjs.js +310 -182
  118. package/dist/constants.d.ts +178 -48
  119. package/dist/constants.es.js +310 -182
  120. package/package.json +19 -17
  121. package/userdocs/LSP0ERC725Account.json +5 -2
  122. package/userdocs/LSP0ERC725AccountInit.json +8 -2
  123. package/userdocs/UniversalProfile.json +3 -3
  124. package/userdocs/UniversalProfileInit.json +6 -3
  125. package/contracts/Custom/EIP191Signer.sol +0 -12
@@ -8,121 +8,148 @@ import {Clones} from "@openzeppelin/contracts/proxy/Clones.sol";
8
8
  // errors
9
9
 
10
10
  /**
11
- * @dev reverts with this error when there is no revert reason bubbled up by the target contract when initializing
11
+ * @dev Reverts when there is no revert reason bubbled up by the contract created when initializing
12
12
  */
13
- error CannotInitializeContract();
13
+ error ContractInitializationFailed();
14
14
 
15
15
  /**
16
- * @dev reverts when msg.value sent to {deployCreate2Init} function is not equal to the sum of the
16
+ * @dev Reverts when msg.value sent to {deployCreate2AndInitialize} function is not equal to the sum of the
17
17
  * `initializeCalldataMsgValue` and `constructorMsgValue`
18
18
  */
19
- error InvalidMsgValueDistribution();
19
+ error InvalidValueSum();
20
20
 
21
21
  /**
22
- * @dev UniversalFactory contract can be used to deploy normal or minimal proxy contracts (EIP-1167) using CREATE2.
23
- * This gives the ability to deploy the same contract at the same address on different chains.
24
- * If the contract has a constructor, the arguments will be part of the bytecode
25
- * If the contract has an `initialize` function, the parameters of this function will be included in
26
- * the salt to ensure that the parameters of the contract should be the same on each chain.
22
+ * @title LSP16 Universal Factory
23
+ * @dev Factory contract to deploy different types of contracts using the CREATE2 opcode
24
+ * standardized as LSP16 - UniversalFactory:
25
+ * https://github.com/lukso-network/LIPs/blob/main/LSPs/LSP-16-UniversalFactory.md
27
26
  *
28
- * When initializeCallData or the constructor includes non-crosschain parameters, the deployed contract
29
- * will not be recreated at the same address on another network, thus defeating the purpose of LSP16UniversalFactory.
30
-
31
- * Therefore, the initializeCallData and the constructor must not include any network-specific parameters,
32
- * such as a local non-crosschain token contract address, chain-id, etc ..
27
+ * The UniversalFactory will be deployed using Nick's Factory (0x4e59b44847b379578588920ca78fbf26c0b4956c)
28
+ *
29
+ * The deployed address can be found in the LSP16 specification.
30
+ * Please refer to the LSP16 Specification to obtain the exact bytecode and salt that
31
+ * should be used to produce the address of the UniversalFactory on different chains.
32
+ *
33
+ * This factory contract is designed to deploy contracts at the same address on multiple chains.
34
+ *
35
+ * The UniversalFactory can deploy 2 types of contracts:
36
+ * - non-initializable (normal deployment)
37
+ * - initializable (external call after deployment, e.g: proxy contracts)
38
+ *
39
+ * The `providedSalt` parameter given by the deployer is not used directly as the salt by the CREATE2 opcode.
40
+ * Instead, it is used along with these parameters:
41
+ * - `initializable` boolean
42
+ * - `initializeCalldata` (when the contract is initializable and `initializable` is set to `true`).
43
+ * These three parameters are concatenated together and hashed to generate the final salt for CREATE2.
44
+ *
45
+ * See {generateSalt} function for more details.
33
46
  *
34
- * One way to solve this problem is to set an EOA owner in the initializeCallData/constructor
47
+ * The constructor and `initializeCalldata` SHOULD NOT include any network-specific parameters (e.g: chain-id,
48
+ * a local token contract address), otherwise the deployed contract will not be recreated at the same address
49
+ * across different networks, thus defeating the purpose of the UniversalFactory.
50
+ *
51
+ * One way to solve this problem is to set an EOA owner in the `initializeCalldata`/constructor
35
52
  * that can later call functions that set these parameters as variables in the contract.
36
53
  *
37
- * This contract should be deployed using Nick's Method.
38
- * More information: https://weka.medium.com/how-to-send-ether-to-11-440-people-187e332566b7
54
+ * The UniversalFactory must be deployed at the same address on different chains to successfully deploy
55
+ * contracts at the same address across different chains.
39
56
  */
40
57
  contract LSP16UniversalFactory {
58
+ /**
59
+ * @dev placeholder for the `initializeCallData` param when the `initializable` boolean is set to `false`.
60
+ */
41
61
  bytes private constant _EMPTY_BYTE = "";
42
62
 
43
63
  /**
44
64
  * @dev Emitted whenever a contract is created
45
65
  * @param contractCreated The address of the contract created
46
- * @param providedSalt The bytes32 salt provided by the deployer
47
- * @param initializable The Boolean that specifies if the contract is a initializable or not
48
- * @param initializeCalldata The bytes provided as initializeCalldata
66
+ * @param providedSalt The salt provided by the deployer, which will be used to generate the final salt
67
+ * that will be used by the `CREATE2` opcode for contract deployment
68
+ * @param generatedSalt The salt used by the `CREATE2` opcode for contract deployment
69
+ * @param initialized The Boolean that specifies if the contract must be initialized or not
70
+ * @param initializeCalldata The bytes provided as initializeCalldata (Empty string when `initialized` is set to false)
49
71
  */
50
72
  event ContractCreated(
51
73
  address indexed contractCreated,
52
74
  bytes32 indexed providedSalt,
53
- bool indexed initializable,
75
+ bytes32 generatedSalt,
76
+ bool indexed initialized,
54
77
  bytes initializeCalldata
55
78
  );
56
79
 
57
80
  /**
58
- * @dev Returns the address where a contract will be stored if deployed via `CREATE2`. The address is
59
- * constructed using the parameters below. Any change in one of them will result in a new destination address.
60
- */
61
- function calculateAddress(
62
- bytes32 byteCodeHash,
63
- bytes32 providedSalt,
64
- bool initializable,
65
- bytes calldata initializeCallData
66
- ) public view virtual returns (address) {
67
- bytes32 generatedSalt = generateSalt(initializable, initializeCallData, providedSalt);
68
- return Create2.computeAddress(generatedSalt, byteCodeHash);
69
- }
70
-
71
- /**
72
- * @dev Returns the address of an EIP1167 proxy contract. The address is constructed using
73
- * the parameters below. Any change in one of them will result in a new destination address.
74
- */
75
- function calculateProxyAddress(
76
- address baseContract,
77
- bytes32 providedSalt,
78
- bool initializable,
79
- bytes calldata initializeCallData
80
- ) public view virtual returns (address) {
81
- bytes32 generatedSalt = generateSalt(initializable, initializeCallData, providedSalt);
82
- return Clones.predictDeterministicAddress(baseContract, generatedSalt);
83
- }
84
-
85
- /**
86
- * @dev Deploys a contract using `CREATE2`. The address where the contract will be deployed
87
- * can be known in advance via {calculateAddress}. The salt is a hash of the `providedSalt`
88
- * together with an empty byte, to prevent mimicing the `deployCreate2Init()` and other functions.
81
+ * @dev Deploys a contract using the CREATE2 opcode. The address where the contract will be deployed
82
+ * can be known in advance via the {computeAddress} function.
83
+ *
84
+ * This function deploys contracts without initialization (external call after deployment).
89
85
  *
90
- * This method allow users to have the same contracts at the same address across different
91
- * chains with the same parameters.
86
+ * The `providedSalt` parameter is not used directly as the salt by the CREATE2 opcode. Instead, it is hashed with
87
+ * keccak256: `keccak256(abi.encodePacked(false, providedSalt))`. See {generateSalt} function for more details.
92
88
  *
93
- * Using the same `byteCode` and salt multiple time will revert, since
94
- * the contract cannot be deployed twice at the same address.
89
+ * Using the same `byteCode` and `providedSalt` multiple times will revert, as the contract cannot be deployed
90
+ * twice at the same address.
91
+ *
92
+ * If the constructor of the contract to deploy is payable, value can be sent to this function to fund
93
+ * the created contract. However, sending value to this function while the constructor is not payable will
94
+ * result in a revert.
95
+ *
96
+ * @param byteCode The bytecode of the contract to be deployed
97
+ * @param providedSalt The salt provided by the deployer, which will be used to generate the final salt
98
+ * that will be used by the `CREATE2` opcode for contract deployment
99
+ *
100
+ * @return The address of the deployed contract
95
101
  */
96
- function deployCreate2(bytes calldata byteCode, bytes32 providedSalt)
97
- public
98
- payable
99
- virtual
100
- returns (address)
101
- {
102
- bytes32 generatedSalt = generateSalt(false, _EMPTY_BYTE, providedSalt);
103
- address contractCreated = Create2.deploy(msg.value, generatedSalt, byteCode);
104
- emit ContractCreated(contractCreated, providedSalt, false, _EMPTY_BYTE);
102
+ function deployCreate2(
103
+ bytes calldata byteCode,
104
+ bytes32 providedSalt
105
+ ) public payable virtual returns (address) {
106
+ bytes32 generatedSalt = generateSalt(providedSalt, false, _EMPTY_BYTE);
107
+ address contractCreated = Create2.deploy(
108
+ msg.value,
109
+ generatedSalt,
110
+ byteCode
111
+ );
112
+ emit ContractCreated(
113
+ contractCreated,
114
+ providedSalt,
115
+ generatedSalt,
116
+ false,
117
+ _EMPTY_BYTE
118
+ );
105
119
 
106
120
  return contractCreated;
107
121
  }
108
122
 
109
123
  /**
110
- * @dev Deploys a contract using `CREATE2`. The address where the contract will be deployed
111
- * can be known in advance via {calculateAddress}. The salt is a hash of the `providedSalt` and
112
- * and the `initializeCallData`.
124
+ * @dev Deploys a contract using the CREATE2 opcode. The address where the contract will be deployed
125
+ * can be known in advance via the {computeAddress} function.
126
+ *
127
+ * This function deploys contracts with initialization (external call after deployment).
128
+ *
129
+ * The `providedSalt` parameter is not used directly as the salt by the CREATE2 opcode. Instead, it is hashed with
130
+ * keccak256: `keccak256(abi.encodePacked(true, initializeCalldata, providedSalt))`.
131
+ * See {generateSalt} function for more details.
113
132
  *
114
- * This method allow users
115
- * to have the same contracts at the same address across different chains with the same parameters.
133
+ * Using the same `byteCode`, `providedSalt` and `initializeCalldata` multiple times will revert, as the
134
+ * contract cannot be deployed twice at the same address.
116
135
  *
117
- * The msg.value is split according to the parameters of the function
136
+ * If the constructor or the initialize function of the contract to deploy is payable, value can be sent along
137
+ * with the deployment/initialization to fund the created contract. However, sending value to this function while
138
+ * the constructor/initialize function is not payable will result in a revert.
118
139
  *
119
- * The msg.value sent to this contract MUST be the sum of the two parameters: `constructorMsgValue`
120
- * and `initializeCalldataMsgValue`
140
+ * Will revert if the `msg.value` sent to the function is not equal to the sum of `constructorMsgValue` and
141
+ * `initializeCalldataMsgValue`.
121
142
  *
122
- * Using the same `byteCode` and salt multiple time will revert, since
123
- * the contract cannot be deployed twice at the same address.
143
+ * @param byteCode The bytecode of the contract to be deployed
144
+ * @param providedSalt The salt provided by the deployer, which will be used to generate the final salt
145
+ * that will be used by the `CREATE2` opcode for contract deployment
146
+ * @param initializeCalldata The calldata to be executed on the created contract
147
+ * @param constructorMsgValue The value sent to the contract during deployment
148
+ * @param initializeCalldataMsgValue The value sent to the contract during initialization
149
+ *
150
+ * @return The address of the deployed contract
124
151
  */
125
- function deployCreate2Init(
152
+ function deployCreate2AndInitialize(
126
153
  bytes calldata byteCode,
127
154
  bytes32 providedSalt,
128
155
  bytes calldata initializeCalldata,
@@ -130,11 +157,25 @@ contract LSP16UniversalFactory {
130
157
  uint256 initializeCalldataMsgValue
131
158
  ) public payable virtual returns (address) {
132
159
  if (constructorMsgValue + initializeCalldataMsgValue != msg.value)
133
- revert InvalidMsgValueDistribution();
160
+ revert InvalidValueSum();
134
161
 
135
- bytes32 generatedSalt = generateSalt(true, initializeCalldata, providedSalt);
136
- address contractCreated = Create2.deploy(constructorMsgValue, generatedSalt, byteCode);
137
- emit ContractCreated(contractCreated, providedSalt, true, initializeCalldata);
162
+ bytes32 generatedSalt = generateSalt(
163
+ providedSalt,
164
+ true,
165
+ initializeCalldata
166
+ );
167
+ address contractCreated = Create2.deploy(
168
+ constructorMsgValue,
169
+ generatedSalt,
170
+ byteCode
171
+ );
172
+ emit ContractCreated(
173
+ contractCreated,
174
+ providedSalt,
175
+ generatedSalt,
176
+ true,
177
+ initializeCalldata
178
+ );
138
179
 
139
180
  (bool success, bytes memory returndata) = contractCreated.call{
140
181
  value: initializeCalldataMsgValue
@@ -145,89 +186,253 @@ contract LSP16UniversalFactory {
145
186
  }
146
187
 
147
188
  /**
148
- * @dev Deploys and returns the address of a clone that mimics the behaviour of `baseContract`.
149
- * The address where the contract will be deployed can be known in advance via {calculateProxyAddress}.
189
+ * @dev Deploys an ERC1167 minimal proxy contract using the CREATE2 opcode. The address where the contract will be deployed
190
+ * can be known in advance via the {computeERC1167Address} function.
191
+ *
192
+ * This function deploys contracts without initialization (external call after deployment).
150
193
  *
151
- * This function uses the CREATE2 opcode and a salt to deterministically deploy
152
- * the clone. The salt is a hash of the `providedSalt`
153
- * toegther with an empty byte, to prevent mimicing the `deployCreate2ProxyInit()` and other functions.
194
+ * The `providedSalt` parameter is not used directly as the salt by the CREATE2 opcode. Instead, it is hashed with
195
+ * keccak256: `keccak256(abi.encodePacked(false, providedSalt))`. See {generateSalt} function for more details.
154
196
  *
155
- * This method allow users to have the same contracts at the same address across different
156
- * chains with the same parameters.
197
+ * See {generateSalt} function for more details.
157
198
  *
158
- * Using the same `baseContract` and salt multiple time will revert, since
159
- * the clones cannot be deployed twice at the same address.
199
+ * Using the same `implementationContract` and `providedSalt` multiple times will revert, as the contract cannot be deployed
200
+ * twice at the same address.
201
+ *
202
+ * Sending value to the contract created is not possible since the constructor of the ERC1167 minimal proxy is not payable.
203
+ *
204
+ * @param implementationContract The contract address to use as the base implementation behind the proxy that will be deployed
205
+ * @param providedSalt The salt provided by the deployer, which will be used to generate the final salt
206
+ * that will be used by the `CREATE2` opcode for contract deployment
207
+ *
208
+ * @return The address of the minimal proxy deployed
160
209
  */
161
- function deployCreate2Proxy(address baseContract, bytes32 providedSalt)
162
- public
163
- virtual
164
- returns (address)
165
- {
166
- bytes32 generatedSalt = generateSalt(false, _EMPTY_BYTE, providedSalt);
210
+ function deployERC1167Proxy(
211
+ address implementationContract,
212
+ bytes32 providedSalt
213
+ ) public virtual returns (address) {
214
+ bytes32 generatedSalt = generateSalt(providedSalt, false, _EMPTY_BYTE);
167
215
 
168
- address proxy = Clones.cloneDeterministic(baseContract, generatedSalt);
169
- emit ContractCreated(proxy, providedSalt, false, _EMPTY_BYTE);
216
+ address proxy = Clones.cloneDeterministic(
217
+ implementationContract,
218
+ generatedSalt
219
+ );
220
+ emit ContractCreated(
221
+ proxy,
222
+ providedSalt,
223
+ generatedSalt,
224
+ false,
225
+ _EMPTY_BYTE
226
+ );
170
227
 
171
228
  return proxy;
172
229
  }
173
230
 
174
231
  /**
175
- * @dev Deploys and returns the address of a clone that mimics the behaviour of `baseContract`.
176
- * The address where the contract will be deployed can be known in advance via {calculateProxyAddress}.
232
+ * @dev Deploys an ERC1167 minimal proxy contract using the CREATE2 opcode. The address where the contract will be deployed
233
+ * can be known in advance via the {computeERC1167Address} function.
234
+ *
235
+ * This function deploys contracts with initialization (external call after deployment).
177
236
  *
178
- * This function uses the CREATE2 opcode and a salt to deterministically deploy
179
- * the clone. The salt is a hash of the `providedSalt` and
180
- * and the `initializeCallData`.
237
+ * The `providedSalt` parameter is not used directly as the salt by the CREATE2 opcode. Instead, it is hashed with
238
+ * keccak256: `keccak256(abi.encodePacked(true, initializeCalldata, providedSalt))`.
239
+ * See {generateSalt} function for more details.
181
240
  *
182
- * This method allow users to have the same contracts at the same address
183
- * across different chains with the same parameters.
241
+ * Using the same `implementationContract`, `providedSalt` and `initializeCalldata` multiple times will revert, as the
242
+ * contract cannot be deployed twice at the same address.
184
243
  *
185
- * Using the same `baseContract` and salt multiple time will revert, since
186
- * the clones cannot be deployed twice at the same address.
244
+ * If the initialize function of the contract to deploy is payable, value can be sent along to fund the created
245
+ * contract while initializing. However, sending value to this function while the initialize function is not
246
+ * payable will result in a revert.
247
+ *
248
+ * @param implementationContract The contract address to use as the base implementation behind the proxy that will be deployed
249
+ * @param providedSalt The salt provided by the deployer, which will be used to generate the final salt
250
+ * that will be used by the `CREATE2` opcode for contract deployment
251
+ * @param initializeCalldata The calldata to be executed on the created contract
252
+ *
253
+ * @return The address of the minimal proxy deployed
187
254
  */
188
- function deployCreate2ProxyInit(
189
- address baseContract,
255
+ function deployERC1167ProxyAndInitialize(
256
+ address implementationContract,
190
257
  bytes32 providedSalt,
191
258
  bytes calldata initializeCalldata
192
259
  ) public payable virtual returns (address) {
193
- bytes32 generatedSalt = generateSalt(true, initializeCalldata, providedSalt);
260
+ bytes32 generatedSalt = generateSalt(
261
+ providedSalt,
262
+ true,
263
+ initializeCalldata
264
+ );
194
265
 
195
- address proxy = Clones.cloneDeterministic(baseContract, generatedSalt);
196
- emit ContractCreated(proxy, providedSalt, true, initializeCalldata);
266
+ address proxy = Clones.cloneDeterministic(
267
+ implementationContract,
268
+ generatedSalt
269
+ );
270
+ emit ContractCreated(
271
+ proxy,
272
+ providedSalt,
273
+ generatedSalt,
274
+ true,
275
+ initializeCalldata
276
+ );
197
277
 
198
- (bool success, bytes memory returndata) = proxy.call{value: msg.value}(initializeCalldata);
278
+ (bool success, bytes memory returndata) = proxy.call{value: msg.value}(
279
+ initializeCalldata
280
+ );
199
281
  _verifyCallResult(success, returndata);
200
282
 
201
283
  return proxy;
202
284
  }
203
285
 
204
286
  /**
205
- * @dev Calculates the salt used to deploy the contract by hashing (Keccak256) the following parameters
206
- * as packed encoded respectively: an initializable boolean, the initializeCallData if and only if
207
- * the contract is initializable, and the salt provided by the deployer.
208
- *
209
- * The initializable boolean was added before the provided arguments as if it was not used,
210
- * and we are deploying proxies on another chain, people can use the `deployCreate2(..)` function
211
- * to deploy the same bytecode + the same salt to get the same address of the contract on
212
- * another chain without applying the effect of initializing.
287
+ * @dev Computes the address of a contract to be deployed using CREATE2, based on the input parameters.
288
+ * Any change in one of these parameters will result in a different address. When the `initializable`
289
+ * boolean is set to `false`, `initializeCalldata` will not affect the function output.
290
+ *
291
+ * @param byteCodeHash The keccak256 hash of the bytecode to be deployed
292
+ * @param providedSalt The salt provided by the deployer, which will be used to generate the final salt
293
+ * that will be used by the `CREATE2` opcode for contract deployment
294
+ * @param initializable A boolean that indicates whether an external call should be made to initialize the
295
+ * contract after deployment
296
+ * @param initializeCalldata The calldata to be executed on the created contract if `initializable` is set to `true`
297
+ *
298
+ * @return The address where the contract will be deployed
299
+ */
300
+ function computeAddress(
301
+ bytes32 byteCodeHash,
302
+ bytes32 providedSalt,
303
+ bool initializable,
304
+ bytes calldata initializeCalldata
305
+ ) public view virtual returns (address) {
306
+ bytes32 generatedSalt = generateSalt(
307
+ providedSalt,
308
+ initializable,
309
+ initializeCalldata
310
+ );
311
+ return Create2.computeAddress(generatedSalt, byteCodeHash);
312
+ }
313
+
314
+ /**
315
+ * @dev Computes the address of an ERC1167 proxy contract based on the input parameters.
316
+ * Any change in one of these parameters will result in a different address. When the `initializable`
317
+ * boolean is set to `false`, `initializeCalldata` will not affect the function output.
318
+ *
319
+ * @param implementationContract The contract to create a clone of according to ERC1167
320
+ * @param providedSalt The salt provided by the deployer, which will be used to generate the final salt
321
+ * that will be used by the `CREATE2` opcode for contract deployment
322
+ * @param initializable A boolean that indicates whether an external call should be made to initialize the
323
+ * proxy contract after deployment
324
+ * @param initializeCalldata The calldata to be executed on the created contract if `initializable` is set to `true`
325
+ *
326
+ * @return The address where the ERC1167 proxy contract will be deployed
327
+ */
328
+ function computeERC1167Address(
329
+ address implementationContract,
330
+ bytes32 providedSalt,
331
+ bool initializable,
332
+ bytes calldata initializeCalldata
333
+ ) public view virtual returns (address) {
334
+ bytes32 generatedSalt = generateSalt(
335
+ providedSalt,
336
+ initializable,
337
+ initializeCalldata
338
+ );
339
+ return
340
+ Clones.predictDeterministicAddress(
341
+ implementationContract,
342
+ generatedSalt
343
+ );
344
+ }
345
+
346
+ /**
347
+ * @dev Generates the salt used to deploy the contract by hashing the following parameters (concatenated
348
+ * together) with keccak256:
349
+ *
350
+ * - the `providedSalt`
351
+ * - the `initializable` boolean
352
+ * - the `initializeCalldata`, only if the contract is initializable (the `initializable` boolean is set to `true`)
353
+ *
354
+ * The `providedSalt` parameter is not used directly as the salt by the CREATE2 opcode. Instead, it is
355
+ * used along with these parameters:
356
+ * - `initializable` boolean
357
+ * - `initializeCalldata` (when the contract is initializable and `initializable` is set to `true`).
358
+ * These three parameters are concatenated together and hashed to generate the final salt for CREATE2.
359
+ *
360
+ * This approach ensures that in order to reproduce an initializable contract at the same address on another chain,
361
+ * not only the `providedSalt` is required to be the same, but also the initialize parameters within the `initializeCalldata`
362
+ * must also be the same.
363
+ *
364
+ * This maintains consistent deployment behaviour. Users are required to initialize contracts with the same parameters across
365
+ * different chains to ensure contracts are deployed at the same address across different chains.
366
+ *
367
+ * -----------
368
+ * Example (for initializable contracts)
369
+ *
370
+ * For an existing contract A on chain 1 owned by X, to replicate the same contract at the same address with
371
+ * the same owner X on chain 2, the salt used to generate the address should include the initializeCalldata
372
+ * that assigns X as the owner of contract A.
373
+ *
374
+ * For instance, if another user, Y, tries to deploy the contract at the same address
375
+ * on chain 2 using the same providedSalt, but with a different initializeCalldata to make Y the owner instead of X,
376
+ * the generated address would be different, preventing Y from deploying the contract with different ownership
377
+ * at the same address.
378
+ * -----------
379
+ *
380
+ * However, for non-initializable contracts, if the constructor has arguments that specify the deployment behavior, they
381
+ * will be included in the bytecode. Any change in the constructor arguments will lead to a different contract's bytecode
382
+ * which will result in a different address on other chains.
383
+ *
384
+ * -----------
385
+ * Example (for non-initializable contracts)
386
+ *
387
+ * If a contract is deployed with specific constructor arguments on chain 1, these arguments are embedded within the bytecode.
388
+ * For instance, if contract B is deployed with a specific `tokenName` and `tokenSymbol` on chain 1, and a user wants to deploy
389
+ * the same contract with the same `tokenName` and `tokenSymbol` on chain 2, they must use the same constructor arguments to
390
+ * produce the same bytecode. This ensures that the same deployment behaviour is maintained across different chains,
391
+ * as long as the same bytecode is used.
392
+ *
393
+ * If another user Z, tries to deploy the same contract B at the same address on chain 2 using the same `providedSalt`
394
+ * but different constructor arguments (a different `tokenName` and/or `tokenSymbol`), the generated address will be different.
395
+ * This prevents user Z from deploying the contract with different constructor arguments at the same address on chain 2.
396
+ * -----------
397
+ *
398
+ * The providedSalt was hashed to produce the salt used by CREATE2 opcode to prevent users from deploying initializable contracts
399
+ * using non-initializable functions such as {deployCreate2} without having the initialization call.
400
+ *
401
+ * In other words, if the providedSalt was not hashed and was used as it is as the salt by the CREATE2 opcode, malicious users
402
+ * can check the generated salt used for the already deployed initializable contract on chain 1, and deploy the contract
403
+ * from {deployCreate2} function on chain 2, with passing the generated salt of the deployed contract as providedSalt
404
+ * that will produce the same address but without the initialization, where the malicious user can initialize after.
405
+ *
406
+ * @param initializable The Boolean that specifies if the contract must be initialized or not
407
+ * @param initializeCalldata The calldata to be executed on the created contract if `initializable` is set to `true`
408
+ * @param providedSalt The salt provided by the deployer, which will be used to generate the final salt
409
+ * that will be used by the `CREATE2` opcode for contract deployment
410
+ *
411
+ * @return The generated salt which will be used for CREATE2 deployment
213
412
  */
214
413
  function generateSalt(
414
+ bytes32 providedSalt,
215
415
  bool initializable,
216
- bytes memory initializeCallData,
217
- bytes32 providedSalt
416
+ bytes memory initializeCalldata
218
417
  ) public pure virtual returns (bytes32) {
219
418
  if (initializable) {
220
- return keccak256(abi.encodePacked(initializable, initializeCallData, providedSalt));
419
+ return
420
+ keccak256(
421
+ abi.encodePacked(true, initializeCalldata, providedSalt)
422
+ );
221
423
  } else {
222
- return keccak256(abi.encodePacked(initializable, providedSalt));
424
+ return keccak256(abi.encodePacked(false, providedSalt));
223
425
  }
224
426
  }
225
427
 
226
428
  /**
227
- * @dev Verifies that the contract created was initialized correctly
228
- * Bubble the revert reason if present, revert with `CannotInitializeContract` otherwise
429
+ * @dev Verifies that the contract created was initialized correctly.
430
+ * Bubble the revert reason if present, revert with `ContractInitializationFailed` otherwise.
229
431
  */
230
- function _verifyCallResult(bool success, bytes memory returndata) internal pure virtual {
432
+ function _verifyCallResult(
433
+ bool success,
434
+ bytes memory returndata
435
+ ) internal pure virtual {
231
436
  if (!success) {
232
437
  // Look for revert reason and bubble it up if present
233
438
  if (returndata.length != 0) {
@@ -239,7 +444,7 @@ contract LSP16UniversalFactory {
239
444
  revert(add(32, returndata), returndata_size)
240
445
  }
241
446
  } else {
242
- revert CannotInitializeContract();
447
+ revert ContractInitializationFailed();
243
448
  }
244
449
  }
245
450
  }