@t-rex-network/t-rex 4.2.0-beta1

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 (208) hide show
  1. package/LICENSE.md +674 -0
  2. package/README.md +137 -0
  3. package/contracts/ERC-3643/ERC3643EventsLib.sol +150 -0
  4. package/contracts/ERC-3643/IERC3643.sol +408 -0
  5. package/contracts/ERC-3643/IERC3643ClaimTopicsRegistry.sol +66 -0
  6. package/contracts/ERC-3643/IERC3643Compliance.sol +127 -0
  7. package/contracts/ERC-3643/IERC3643IdentityRegistry.sol +180 -0
  8. package/contracts/ERC-3643/IERC3643IdentityRegistryStorage.sol +120 -0
  9. package/contracts/ERC-3643/IERC3643TrustedIssuersRegistry.sol +120 -0
  10. package/contracts/compliance/modular/IModularCompliance.sol +143 -0
  11. package/contracts/compliance/modular/ModularCompliance.sol +381 -0
  12. package/contracts/compliance/modular/modules/AbstractModuleUpgradeable.sol +193 -0
  13. package/contracts/compliance/modular/modules/IModule.sol +158 -0
  14. package/contracts/compliance/modular/modules/ModuleProxy.sol +72 -0
  15. package/contracts/factory/ITREXFactory.sol +180 -0
  16. package/contracts/factory/ITREXGateway.sol +234 -0
  17. package/contracts/factory/TREXFactory.sol +358 -0
  18. package/contracts/factory/TREXGateway.sol +274 -0
  19. package/contracts/libraries/ErrorsLib.sol +168 -0
  20. package/contracts/libraries/EventsLib.sol +147 -0
  21. package/contracts/proxy/AbstractProxy.sol +146 -0
  22. package/contracts/proxy/ClaimTopicsRegistryProxy.sol +83 -0
  23. package/contracts/proxy/IdentityRegistryProxy.sol +95 -0
  24. package/contracts/proxy/IdentityRegistryStorageProxy.sol +83 -0
  25. package/contracts/proxy/ModularComplianceProxy.sol +88 -0
  26. package/contracts/proxy/TokenProxy.sol +97 -0
  27. package/contracts/proxy/TrustedIssuersRegistryProxy.sol +88 -0
  28. package/contracts/proxy/authority/IAFactory.sol +122 -0
  29. package/contracts/proxy/authority/IIAFactory.sol +87 -0
  30. package/contracts/proxy/authority/ITREXImplementationAuthority.sol +239 -0
  31. package/contracts/proxy/authority/TREXImplementationAuthority.sol +350 -0
  32. package/contracts/proxy/interface/IProxy.sol +72 -0
  33. package/contracts/registry/implementation/ClaimTopicsRegistry.sol +152 -0
  34. package/contracts/registry/implementation/IdentityRegistry.sol +352 -0
  35. package/contracts/registry/implementation/IdentityRegistryStorage.sol +227 -0
  36. package/contracts/registry/implementation/TrustedIssuersRegistry.sol +272 -0
  37. package/contracts/registry/interface/IClaimTopicsRegistry.sol +70 -0
  38. package/contracts/registry/interface/IIdentityRegistry.sol +102 -0
  39. package/contracts/registry/interface/IIdentityRegistryStorage.sol +70 -0
  40. package/contracts/registry/interface/ITrustedIssuersRegistry.sol +70 -0
  41. package/contracts/roles/AbstractAgentRole.sol +122 -0
  42. package/contracts/roles/AgentRole.sol +82 -0
  43. package/contracts/roles/AgentRoleUpgradeable.sol +82 -0
  44. package/contracts/roles/IERC173.sol +21 -0
  45. package/contracts/roles/Roles.sol +102 -0
  46. package/contracts/token/IToken.sol +97 -0
  47. package/contracts/token/Token.sol +664 -0
  48. package/contracts/token/TokenStructs.sol +74 -0
  49. package/contracts/utils/IUtilityChecker.sol +135 -0
  50. package/contracts/utils/InterfaceIdCalculator.sol +223 -0
  51. package/contracts/utils/UtilityChecker.sol +210 -0
  52. package/contracts/utils/UtilityCheckerProxy.sol +72 -0
  53. package/contracts/vendor/openzeppelin/Create3.sol +143 -0
  54. package/out/AbstractAgentRole.sol/AbstractAgentRole.json +1 -0
  55. package/out/AbstractModuleUpgradeable.sol/AbstractModuleUpgradeable.json +1 -0
  56. package/out/AbstractProxy.sol/AbstractProxy.json +1 -0
  57. package/out/Address.sol/Address.json +1 -0
  58. package/out/AgentRole.sol/AgentRole.json +1 -0
  59. package/out/AgentRoleUpgradeable.sol/AgentRoleUpgradeable.json +1 -0
  60. package/out/Arrays.sol/Arrays.json +1 -0
  61. package/out/Base.sol/CommonBase.json +1 -0
  62. package/out/Base.sol/ScriptBase.json +1 -0
  63. package/out/Base.sol/TestBase.json +1 -0
  64. package/out/Bytes.sol/Bytes.json +1 -0
  65. package/out/ClaimIssuer.sol/ClaimIssuer.json +1 -0
  66. package/out/ClaimIssuerTrick.sol/ClaimIssuerTrick.json +1 -0
  67. package/out/ClaimTopicsRegistry.sol/ClaimTopicsRegistry.json +1 -0
  68. package/out/ClaimTopicsRegistryProxy.sol/ClaimTopicsRegistryProxy.json +1 -0
  69. package/out/Comparators.sol/Comparators.json +1 -0
  70. package/out/Context.sol/Context.json +1 -0
  71. package/out/ContextUpgradeable.sol/ContextUpgradeable.json +1 -0
  72. package/out/Countries.sol/Countries.json +1 -0
  73. package/out/Create2.sol/Create2.json +1 -0
  74. package/out/Create3.sol/Create3.json +1 -0
  75. package/out/ECDSA.sol/ECDSA.json +1 -0
  76. package/out/EIP712.sol/EIP712.json +1 -0
  77. package/out/EIP712Upgradeable.sol/EIP712Upgradeable.json +1 -0
  78. package/out/ERC1967Proxy.sol/ERC1967Proxy.json +1 -0
  79. package/out/ERC1967Utils.sol/ERC1967Utils.json +1 -0
  80. package/out/ERC20.sol/ERC20.json +1 -0
  81. package/out/ERC20Pausable.sol/ERC20Pausable.json +1 -0
  82. package/out/ERC20PermitUpgradeable.sol/ERC20PermitUpgradeable.json +1 -0
  83. package/out/ERC20Upgradeable.sol/ERC20Upgradeable.json +1 -0
  84. package/out/ERC2771Context.sol/ERC2771Context.json +1 -0
  85. package/out/ERC2771ContextUpgradeable.sol/ERC2771ContextUpgradeable.json +1 -0
  86. package/out/ERC2771Forwarder.sol/ERC2771Forwarder.json +1 -0
  87. package/out/ERC3643EventsLib.sol/ERC3643EventsLib.json +1 -0
  88. package/out/EnumerableSet.sol/EnumerableSet.json +1 -0
  89. package/out/Errors.sol/Errors.json +1 -0
  90. package/out/ErrorsLib.sol/ErrorsLib.json +1 -0
  91. package/out/EventsLib.sol/EventsLib.json +1 -0
  92. package/out/IAFactory.sol/IAFactory.json +1 -0
  93. package/out/IBeacon.sol/IBeacon.json +1 -0
  94. package/out/IClaimIssuer.sol/IClaimIssuer.json +1 -0
  95. package/out/IClaimTopicsRegistry.sol/IClaimTopicsRegistry.json +1 -0
  96. package/out/IERC1363.sol/IERC1363.json +1 -0
  97. package/out/IERC165.sol/IERC165.json +1 -0
  98. package/out/IERC173.sol/IERC173.json +1 -0
  99. package/out/IERC1967.sol/IERC1967.json +1 -0
  100. package/out/IERC20.sol/IERC20.json +1 -0
  101. package/out/IERC20Metadata.sol/IERC20Metadata.json +1 -0
  102. package/out/IERC20Permit.sol/IERC20Permit.json +1 -0
  103. package/out/IERC3643.sol/IERC3643.json +1 -0
  104. package/out/IERC3643ClaimTopicsRegistry.sol/IERC3643ClaimTopicsRegistry.json +1 -0
  105. package/out/IERC3643Compliance.sol/IERC3643Compliance.json +1 -0
  106. package/out/IERC3643IdentityRegistry.sol/IERC3643IdentityRegistry.json +1 -0
  107. package/out/IERC3643IdentityRegistryStorage.sol/IERC3643IdentityRegistryStorage.json +1 -0
  108. package/out/IERC3643TrustedIssuersRegistry.sol/IERC3643TrustedIssuersRegistry.json +1 -0
  109. package/out/IERC5267.sol/IERC5267.json +1 -0
  110. package/out/IERC734.sol/IERC734.json +1 -0
  111. package/out/IERC735.sol/IERC735.json +1 -0
  112. package/out/IIAFactory.sol/IIAFactory.json +1 -0
  113. package/out/IIdFactory.sol/IIdFactory.json +1 -0
  114. package/out/IIdentity.sol/IIdentity.json +1 -0
  115. package/out/IIdentityRegistry.sol/IIdentityRegistry.json +1 -0
  116. package/out/IIdentityRegistryStorage.sol/IIdentityRegistryStorage.json +1 -0
  117. package/out/IImplementationAuthority.sol/IImplementationAuthority.json +1 -0
  118. package/out/IModularCompliance.sol/IModularCompliance.json +1 -0
  119. package/out/IModule.sol/IModule.json +1 -0
  120. package/out/IMulticall3.sol/IMulticall3.json +1 -0
  121. package/out/IProxy.sol/IProxy.json +1 -0
  122. package/out/ITREXFactory.sol/ITREXFactory.json +1 -0
  123. package/out/ITREXGateway.sol/ITREXGateway.json +1 -0
  124. package/out/ITREXImplementationAuthority.sol/ITREXImplementationAuthority.json +1 -0
  125. package/out/IToken.sol/IToken.json +1 -0
  126. package/out/ITrustedIssuersRegistry.sol/ITrustedIssuersRegistry.json +1 -0
  127. package/out/IUtilityChecker.sol/IUtilityChecker.json +1 -0
  128. package/out/IdFactory.sol/IdFactory.json +1 -0
  129. package/out/Identity.sol/Identity.json +1 -0
  130. package/out/IdentityProxy.sol/IdentityProxy.json +1 -0
  131. package/out/IdentityRegistry.sol/IdentityRegistry.json +1 -0
  132. package/out/IdentityRegistryProxy.sol/IdentityRegistryProxy.json +1 -0
  133. package/out/IdentityRegistryStorage.sol/IdentityRegistryStorage.json +1 -0
  134. package/out/IdentityRegistryStorageProxy.sol/IdentityRegistryStorageProxy.json +1 -0
  135. package/out/ImplementationAuthority.sol/ImplementationAuthority.json +1 -0
  136. package/out/Initializable.sol/Initializable.json +1 -0
  137. package/out/InterfaceIdCalculator.sol/InterfaceIdCalculator.json +1 -0
  138. package/out/KeyPurposes.sol/KeyPurposes.json +1 -0
  139. package/out/KeyTypes.sol/KeyTypes.json +1 -0
  140. package/out/LowLevelCall.sol/LowLevelCall.json +1 -0
  141. package/out/Math.sol/Math.json +1 -0
  142. package/out/MessageHashUtils.sol/MessageHashUtils.json +1 -0
  143. package/out/ModularCompliance.sol/ModularCompliance.json +1 -0
  144. package/out/ModularComplianceProxy.sol/ModularComplianceProxy.json +1 -0
  145. package/out/ModuleNotPnP.sol/ModuleNotPnP.json +1 -0
  146. package/out/ModuleProxy.sol/ModuleProxy.json +1 -0
  147. package/out/MulticallUpgradeable.sol/MulticallUpgradeable.json +1 -0
  148. package/out/Nonces.sol/Nonces.json +1 -0
  149. package/out/NoncesUpgradeable.sol/NoncesUpgradeable.json +1 -0
  150. package/out/Ownable.sol/Ownable.json +1 -0
  151. package/out/Ownable2Step.sol/Ownable2Step.json +1 -0
  152. package/out/Ownable2StepUpgradeable.sol/Ownable2StepUpgradeable.json +1 -0
  153. package/out/OwnableUpgradeable.sol/OwnableUpgradeable.json +1 -0
  154. package/out/Panic.sol/Panic.json +1 -0
  155. package/out/Pausable.sol/Pausable.json +1 -0
  156. package/out/PausableUpgradeable.sol/PausableUpgradeable.json +1 -0
  157. package/out/Proxy.sol/Proxy.json +1 -0
  158. package/out/Roles.sol/Roles.json +1 -0
  159. package/out/SafeCast.sol/SafeCast.json +1 -0
  160. package/out/SafeERC20.sol/SafeERC20.json +1 -0
  161. package/out/ShortStrings.sol/ShortStrings.json +1 -0
  162. package/out/SignedMath.sol/SignedMath.json +1 -0
  163. package/out/SlotDerivation.sol/SlotDerivation.json +1 -0
  164. package/out/StdAssertions.sol/StdAssertions.json +1 -0
  165. package/out/StdChains.sol/StdChains.json +1 -0
  166. package/out/StdCheats.sol/StdCheats.json +1 -0
  167. package/out/StdCheats.sol/StdCheatsSafe.json +1 -0
  168. package/out/StdConstants.sol/StdConstants.json +1 -0
  169. package/out/StdError.sol/stdError.json +1 -0
  170. package/out/StdInvariant.sol/StdInvariant.json +1 -0
  171. package/out/StdJson.sol/stdJson.json +1 -0
  172. package/out/StdMath.sol/stdMath.json +1 -0
  173. package/out/StdStorage.sol/stdStorage.json +1 -0
  174. package/out/StdStorage.sol/stdStorageSafe.json +1 -0
  175. package/out/StdStyle.sol/StdStyle.json +1 -0
  176. package/out/StdToml.sol/stdToml.json +1 -0
  177. package/out/StdUtils.sol/StdUtils.json +1 -0
  178. package/out/Storage.sol/Storage.json +1 -0
  179. package/out/StorageSlot.sol/StorageSlot.json +1 -0
  180. package/out/Strings.sol/Strings.json +1 -0
  181. package/out/Structs.sol/Structs.json +1 -0
  182. package/out/TREXFactory.sol/TREXFactory.json +1 -0
  183. package/out/TREXGateway.sol/TREXGateway.json +1 -0
  184. package/out/TREXImplementationAuthority.sol/TREXImplementationAuthority.json +1 -0
  185. package/out/TestAgentRole.sol/TestAgentRole.json +1 -0
  186. package/out/TestAgentRoleUpgradeable.sol/TestAgentRoleUpgradeable.json +1 -0
  187. package/out/TestERC20.sol/TestERC20.json +1 -0
  188. package/out/TestModule.sol/TestModule.json +1 -0
  189. package/out/TestTREXFactory.sol/TestTREXFactory.json +1 -0
  190. package/out/Token.sol/Token.json +1 -0
  191. package/out/TokenProxy.sol/TokenProxy.json +1 -0
  192. package/out/TrustedIssuersRegistry.sol/TrustedIssuersRegistry.json +1 -0
  193. package/out/TrustedIssuersRegistryProxy.sol/TrustedIssuersRegistryProxy.json +1 -0
  194. package/out/UUPSUpgradeable.sol/UUPSUpgradeable.json +1 -0
  195. package/out/UtilityChecker.sol/UtilityChecker.json +1 -0
  196. package/out/UtilityCheckerProxy.sol/UtilityCheckerProxy.json +1 -0
  197. package/out/Utils.sol/Utils.json +1 -0
  198. package/out/Version.sol/Version.json +1 -0
  199. package/out/Vm.sol/Vm.json +1 -0
  200. package/out/Vm.sol/VmSafe.json +1 -0
  201. package/out/console.sol/console.json +1 -0
  202. package/out/draft-IERC1822.sol/IERC1822Proxiable.json +1 -0
  203. package/out/draft-IERC6093.sol/IERC1155Errors.json +1 -0
  204. package/out/draft-IERC6093.sol/IERC20Errors.json +1 -0
  205. package/out/draft-IERC6093.sol/IERC721Errors.json +1 -0
  206. package/out/libraries/Errors.sol/Errors.json +1 -0
  207. package/out/safeconsole.sol/safeconsole.json +1 -0
  208. package/package.json +47 -0
@@ -0,0 +1,664 @@
1
+ // SPDX-License-Identifier: GPL-3.0
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
+ * NOTICE
42
+ *
43
+ * The T-REX software is licensed under a proprietary license or the GPL v.3.
44
+ * If you choose to receive it under the GPL v.3 license, the following applies:
45
+ * T-REX is a suite of smart contracts implementing the ERC-3643 standard and
46
+ * developed by Tokeny to manage and transfer financial assets on EVM blockchains
47
+ *
48
+ * Copyright (C) 2025, Tokeny sàrl.
49
+ *
50
+ * This program is free software: you can redistribute it and/or modify
51
+ * it under the terms of the GNU General Public License as published by
52
+ * the Free Software Foundation, either version 3 of the License, or
53
+ * (at your option) any later version.
54
+ *
55
+ * This program is distributed in the hope that it will be useful,
56
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
57
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
58
+ * GNU General Public License for more details.
59
+ *
60
+ * You should have received a copy of the GNU General Public License
61
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
62
+ */
63
+
64
+ pragma solidity 0.8.30;
65
+
66
+ import { IIdentity } from "@onchain-id/solidity/contracts/interface/IIdentity.sol";
67
+
68
+ import {
69
+ ContextUpgradeable,
70
+ ERC2771ContextUpgradeable
71
+ } from "@openzeppelin/contracts-upgradeable/metatx/ERC2771ContextUpgradeable.sol";
72
+ import {
73
+ ERC20PermitUpgradeable,
74
+ ERC20Upgradeable,
75
+ IERC20Permit
76
+ } from "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/ERC20PermitUpgradeable.sol";
77
+ import { PausableUpgradeable } from "@openzeppelin/contracts-upgradeable/utils/PausableUpgradeable.sol";
78
+ import { EIP712Upgradeable } from "@openzeppelin/contracts-upgradeable/utils/cryptography/EIP712Upgradeable.sol";
79
+ import { IERC20Errors } from "@openzeppelin/contracts/interfaces/draft-IERC6093.sol";
80
+ import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
81
+ import { IERC20Metadata } from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
82
+ import { IERC165 } from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
83
+
84
+ import { ERC3643EventsLib } from "../ERC-3643/ERC3643EventsLib.sol";
85
+ import { IERC3643 } from "../ERC-3643/IERC3643.sol";
86
+ import { IERC3643Compliance } from "../ERC-3643/IERC3643Compliance.sol";
87
+ import { IERC3643IdentityRegistry } from "../ERC-3643/IERC3643IdentityRegistry.sol";
88
+ import { ErrorsLib } from "../libraries/ErrorsLib.sol";
89
+ import { EventsLib } from "../libraries/EventsLib.sol";
90
+ import { AgentRoleUpgradeable } from "../roles/AgentRoleUpgradeable.sol";
91
+ import { IERC173 } from "../roles/IERC173.sol";
92
+ import { IToken } from "./IToken.sol";
93
+ import { TokenRoles } from "./TokenStructs.sol";
94
+
95
+ contract Token is
96
+ ERC20PermitUpgradeable,
97
+ PausableUpgradeable,
98
+ AgentRoleUpgradeable,
99
+ ERC2771ContextUpgradeable,
100
+ IToken,
101
+ IERC165
102
+ {
103
+
104
+ string internal constant VERSION = "5.0.0";
105
+
106
+ struct FrozenStatus {
107
+ bool addressFrozen;
108
+ uint256 amount;
109
+ }
110
+
111
+ /// @custom:storage-location erc7201:token.storage.main
112
+ struct TokenStorage {
113
+ string name;
114
+ string symbol;
115
+ uint8 decimals;
116
+ address onchainId;
117
+ IERC3643Compliance compliance;
118
+ IERC3643IdentityRegistry identityRegistry;
119
+ address trustedForwarder;
120
+
121
+ mapping(address user => FrozenStatus) frozenStatus;
122
+ mapping(address spender => bool) defaultAllowances;
123
+ mapping(address user => bool) defaultAllowanceOptOuts;
124
+
125
+ mapping(address agent => TokenRoles) agentsRestrictions;
126
+ }
127
+
128
+ // keccak256(abi.encode(uint256(keccak256("token.storage.main")) - 1)) & ~bytes32(uint256(0xff));
129
+ bytes32 private constant TOKEN_STORAGE_LOCATION =
130
+ 0x3eb201768b0b55c18fa93955aeb38c6bf0f381d8227d53e1b0e5b066883d4e00;
131
+
132
+ constructor() ERC2771ContextUpgradeable(address(0)) {
133
+ _disableInitializers();
134
+ }
135
+
136
+ /// @dev the constructor initiates the token contract
137
+ /// @param tokenName the name of the token
138
+ /// @param tokenSymbol the symbol of the token
139
+ /// @param tokenDecimals the decimals of the token
140
+ /// @param identityRegistryAddress the address of the Identity registry linked to the token
141
+ /// @param complianceAddress the address of the compliance contract linked to the token
142
+ /// The factory pre-binds the predicted Token CREATE3 address in MC.init, so Token.init does NOT
143
+ /// call `bindToken` itself — the wiring is already in place by the time this code runs.
144
+ /// @param onchainIdAddress the address of the onchainID of the token
145
+ /// onchainID can be zero address if not set, can be set later by the owner
146
+ /// @param owner final owner of the token, applied directly via `__Ownable_init(owner)` — no transient
147
+ /// factory ownership
148
+ /// @param tokenAgents addresses to grant the agent role at init time; capped at 5
149
+ /// emits an `UpdatedTokenInformation` event
150
+ /// emits an `IdentityRegistryAdded` event
151
+ /// emits a `ComplianceAdded` event
152
+ /// emits an `AgentAdded` event for each entry in `tokenAgents`
153
+ function init(
154
+ string memory tokenName,
155
+ string memory tokenSymbol,
156
+ uint8 tokenDecimals,
157
+ address identityRegistryAddress,
158
+ address complianceAddress,
159
+ address onchainIdAddress,
160
+ address owner,
161
+ address[] calldata tokenAgents
162
+ ) external initializer {
163
+ require(
164
+ identityRegistryAddress != address(0) && complianceAddress != address(0) && owner != address(0),
165
+ ErrorsLib.ZeroAddress()
166
+ );
167
+ require(bytes(tokenName).length > 0 && bytes(tokenSymbol).length > 0, ErrorsLib.EmptyString());
168
+ require(tokenDecimals <= 18, ErrorsLib.DecimalsOutOfRange(tokenDecimals));
169
+ require(tokenAgents.length <= 5, ErrorsLib.MaxAgentsReached(5));
170
+
171
+ __ERC20_init(tokenName, tokenSymbol);
172
+ __ERC20Permit_init(tokenName);
173
+ __Pausable_init();
174
+ __Ownable_init(owner);
175
+
176
+ TokenStorage storage s = _tokenStorage();
177
+ s.name = tokenName;
178
+ s.symbol = tokenSymbol;
179
+ s.decimals = tokenDecimals;
180
+ s.onchainId = onchainIdAddress;
181
+
182
+ s.identityRegistry = IERC3643IdentityRegistry(identityRegistryAddress);
183
+ s.compliance = IERC3643Compliance(complianceAddress);
184
+ _emitUpdatedTokenInformation();
185
+
186
+ for (uint256 i = 0; i < tokenAgents.length; i++) {
187
+ _addAgent(tokenAgents[i]);
188
+ }
189
+
190
+ _pause();
191
+ }
192
+
193
+ /* ----- Main token properties ----- */
194
+
195
+ /// @inheritdoc IERC3643
196
+ function setName(string calldata tokenName) external override onlyOwner {
197
+ require(bytes(tokenName).length > 0, ErrorsLib.EmptyString());
198
+ _tokenStorage().name = tokenName;
199
+ _emitUpdatedTokenInformation();
200
+ }
201
+
202
+ /// @inheritdoc IERC3643
203
+ function setSymbol(string calldata tokenSymbol) external override onlyOwner {
204
+ require(bytes(tokenSymbol).length > 0, ErrorsLib.EmptyString());
205
+ _tokenStorage().symbol = tokenSymbol;
206
+ _emitUpdatedTokenInformation();
207
+ }
208
+
209
+ /// @inheritdoc IERC3643
210
+ /// @dev if _onchainID is set at zero address it means no ONCHAINID is bound to this token
211
+ function setOnchainID(address onchainIdAddress) external override onlyOwner {
212
+ _tokenStorage().onchainId = onchainIdAddress;
213
+ _emitUpdatedTokenInformation();
214
+ }
215
+
216
+ /// @inheritdoc IERC3643
217
+ function setIdentityRegistry(address _identityRegistry) public override onlyOwner {
218
+ require(_identityRegistry != address(0), ErrorsLib.ZeroAddress());
219
+
220
+ _tokenStorage().identityRegistry = IERC3643IdentityRegistry(_identityRegistry);
221
+ emit ERC3643EventsLib.IdentityRegistryAdded(_identityRegistry);
222
+ }
223
+
224
+ /// @inheritdoc IERC3643
225
+ function setCompliance(address _compliance) public override onlyOwner {
226
+ require(_compliance != address(0), ErrorsLib.ZeroAddress());
227
+
228
+ TokenStorage storage s = _tokenStorage();
229
+ if (address(s.compliance) != address(0)) {
230
+ s.compliance.unbindToken(address(this));
231
+ }
232
+ s.compliance = IERC3643Compliance(_compliance);
233
+ s.compliance.bindToken(address(this));
234
+ emit ERC3643EventsLib.ComplianceAdded(_compliance);
235
+ }
236
+
237
+ /// @inheritdoc IERC20Metadata
238
+ function name() public view override(ERC20Upgradeable, IERC20Metadata) returns (string memory) {
239
+ return _tokenStorage().name;
240
+ }
241
+
242
+ /// @inheritdoc IERC20Metadata
243
+ function symbol() public view override(ERC20Upgradeable, IERC20Metadata) returns (string memory) {
244
+ return _tokenStorage().symbol;
245
+ }
246
+
247
+ /// @inheritdoc IERC20Metadata
248
+ function decimals() public view override(ERC20Upgradeable, IERC20Metadata) returns (uint8) {
249
+ return _tokenStorage().decimals;
250
+ }
251
+
252
+ /// @inheritdoc IERC3643
253
+ function onchainID() external view override returns (address) {
254
+ return _tokenStorage().onchainId;
255
+ }
256
+
257
+ /// @inheritdoc IERC3643
258
+ function identityRegistry() external view override returns (IERC3643IdentityRegistry) {
259
+ return _tokenStorage().identityRegistry;
260
+ }
261
+
262
+ /// @inheritdoc IERC3643
263
+ function compliance() external view override returns (IERC3643Compliance) {
264
+ return _tokenStorage().compliance;
265
+ }
266
+
267
+ /// @inheritdoc IERC3643
268
+ function version() public pure override returns (string memory) {
269
+ return VERSION;
270
+ }
271
+
272
+ function _EIP712Name() internal view override returns (string memory) {
273
+ return name();
274
+ }
275
+
276
+ /* ----- Pause Functions ----- */
277
+
278
+ /// @inheritdoc IERC3643
279
+ function pause() external override onlyAgent whenNotPaused {
280
+ require(
281
+ !getAgentRestrictions(_msgSender()).disablePause,
282
+ ErrorsLib.AgentNotAuthorized(_msgSender(), "pause disabled")
283
+ );
284
+
285
+ _pause();
286
+ }
287
+
288
+ /// @inheritdoc IERC3643
289
+ function unpause() external override onlyAgent whenPaused {
290
+ require(
291
+ !getAgentRestrictions(_msgSender()).disablePause,
292
+ ErrorsLib.AgentNotAuthorized(_msgSender(), "pause disabled")
293
+ );
294
+
295
+ _unpause();
296
+ }
297
+
298
+ /// @inheritdoc IERC3643
299
+ function paused() public view override(PausableUpgradeable, IERC3643) returns (bool) {
300
+ return super.paused();
301
+ }
302
+
303
+ /* ----- Minting & Burning Functions ----- */
304
+
305
+ /// @inheritdoc IERC3643
306
+ function mint(address to, uint256 amount) public override onlyAgent {
307
+ require(
308
+ !getAgentRestrictions(_msgSender()).disableMint, ErrorsLib.AgentNotAuthorized(_msgSender(), "mint disabled")
309
+ );
310
+ _mint(to, amount);
311
+ }
312
+
313
+ /// @inheritdoc IERC3643
314
+ function burn(address from, uint256 amount) public override onlyAgent {
315
+ require(
316
+ !getAgentRestrictions(_msgSender()).disableBurn, ErrorsLib.AgentNotAuthorized(_msgSender(), "burn disabled")
317
+ );
318
+ _burn(from, amount);
319
+ }
320
+
321
+ /// @inheritdoc IERC3643
322
+ function batchMint(address[] calldata tos, uint256[] calldata amounts) external override {
323
+ for (uint256 i = 0; i < tos.length; i++) {
324
+ mint(tos[i], amounts[i]);
325
+ }
326
+ }
327
+
328
+ /// @inheritdoc IERC3643
329
+ function batchBurn(address[] calldata froms, uint256[] calldata amounts) external override {
330
+ for (uint256 i = 0; i < froms.length; i++) {
331
+ burn(froms[i], amounts[i]);
332
+ }
333
+ }
334
+
335
+ /* ----- Freezing Functions ----- */
336
+
337
+ /// @inheritdoc IERC3643
338
+ function freezePartialTokens(address user, uint256 amount) public override onlyAgent {
339
+ require(
340
+ !getAgentRestrictions(_msgSender()).disablePartialFreeze,
341
+ ErrorsLib.AgentNotAuthorized(_msgSender(), "partial freeze disabled")
342
+ );
343
+
344
+ TokenStorage storage s = _tokenStorage();
345
+ uint256 balance = balanceOf(user);
346
+ require(
347
+ balance >= s.frozenStatus[user].amount + amount,
348
+ IERC20Errors.ERC20InsufficientBalance(user, balance, amount)
349
+ );
350
+ s.frozenStatus[user].amount += amount;
351
+ emit ERC3643EventsLib.TokensFrozen(user, amount);
352
+ }
353
+
354
+ /// @inheritdoc IERC3643
355
+ function unfreezePartialTokens(address user, uint256 amount) public override onlyAgent {
356
+ require(
357
+ !getAgentRestrictions(_msgSender()).disablePartialFreeze,
358
+ ErrorsLib.AgentNotAuthorized(_msgSender(), "partial freeze disabled")
359
+ );
360
+
361
+ TokenStorage storage s = _tokenStorage();
362
+
363
+ require(
364
+ s.frozenStatus[user].amount >= amount,
365
+ ErrorsLib.AmountAboveFrozenTokens(amount, s.frozenStatus[user].amount)
366
+ );
367
+ s.frozenStatus[user].amount -= amount;
368
+ emit ERC3643EventsLib.TokensUnfrozen(user, amount);
369
+ }
370
+
371
+ /// @inheritdoc IERC3643
372
+ function setAddressFrozen(address user, bool freeze) public override onlyAgent {
373
+ require(
374
+ !getAgentRestrictions(_msgSender()).disableAddressFreeze,
375
+ ErrorsLib.AgentNotAuthorized(_msgSender(), "address freeze disabled")
376
+ );
377
+
378
+ _tokenStorage().frozenStatus[user].addressFrozen = freeze;
379
+
380
+ emit ERC3643EventsLib.AddressFrozen(user, freeze, _msgSender());
381
+ }
382
+
383
+ /// @inheritdoc IERC3643
384
+ function batchFreezePartialTokens(address[] calldata users, uint256[] calldata amounts) external {
385
+ for (uint256 i = 0; i < users.length; i++) {
386
+ freezePartialTokens(users[i], amounts[i]);
387
+ }
388
+ }
389
+
390
+ /// @inheritdoc IERC3643
391
+ function batchUnfreezePartialTokens(address[] calldata users, uint256[] calldata amounts) external override {
392
+ for (uint256 i = 0; i < users.length; i++) {
393
+ unfreezePartialTokens(users[i], amounts[i]);
394
+ }
395
+ }
396
+
397
+ /// @inheritdoc IERC3643
398
+ function batchSetAddressFrozen(address[] calldata users, bool[] calldata freezes) external override {
399
+ for (uint256 i = 0; i < users.length; i++) {
400
+ setAddressFrozen(users[i], freezes[i]);
401
+ }
402
+ }
403
+
404
+ /// @inheritdoc IERC3643
405
+ function isFrozen(address user) external view override returns (bool) {
406
+ return _tokenStorage().frozenStatus[user].addressFrozen;
407
+ }
408
+
409
+ /// @inheritdoc IERC3643
410
+ function getFrozenTokens(address user) external view override returns (uint256) {
411
+ return _tokenStorage().frozenStatus[user].amount;
412
+ }
413
+
414
+ /* ----- Recovery Functions ----- */
415
+
416
+ /// @inheritdoc IERC3643
417
+ function recoveryAddress(address lostWallet, address newWallet, address investorOnchainId)
418
+ external
419
+ override
420
+ onlyAgent
421
+ returns (bool)
422
+ {
423
+ require(
424
+ !getAgentRestrictions(_msgSender()).disableRecovery,
425
+ ErrorsLib.AgentNotAuthorized(_msgSender(), "recovery disabled")
426
+ );
427
+
428
+ TokenStorage storage s = _tokenStorage();
429
+
430
+ uint256 investorTokens = balanceOf(lostWallet);
431
+ require(investorTokens != 0, ErrorsLib.NoTokenToRecover());
432
+ require(
433
+ s.identityRegistry.contains(lostWallet) || s.identityRegistry.contains(newWallet),
434
+ ErrorsLib.RecoveryNotPossible()
435
+ );
436
+
437
+ uint256 frozenTokens = s.frozenStatus[lostWallet].amount;
438
+ _forceUpdate(lostWallet, newWallet, investorTokens);
439
+
440
+ if (frozenTokens > 0) {
441
+ s.frozenStatus[newWallet].amount += frozenTokens;
442
+ emit ERC3643EventsLib.TokensFrozen(newWallet, frozenTokens);
443
+ }
444
+
445
+ if (s.frozenStatus[lostWallet].addressFrozen) {
446
+ s.frozenStatus[lostWallet].addressFrozen = false;
447
+ emit ERC3643EventsLib.AddressFrozen(lostWallet, false, address(this));
448
+
449
+ if (!s.frozenStatus[newWallet].addressFrozen) {
450
+ s.frozenStatus[newWallet].addressFrozen = true;
451
+ emit ERC3643EventsLib.AddressFrozen(newWallet, true, address(this));
452
+ }
453
+ }
454
+ if (s.identityRegistry.contains(lostWallet)) {
455
+ if (!s.identityRegistry.contains(newWallet)) {
456
+ s.identityRegistry
457
+ .registerIdentity(
458
+ newWallet, IIdentity(investorOnchainId), s.identityRegistry.investorCountry(lostWallet)
459
+ );
460
+ }
461
+ s.identityRegistry.deleteIdentity(lostWallet);
462
+ }
463
+
464
+ emit ERC3643EventsLib.RecoverySuccess(lostWallet, newWallet, investorOnchainId);
465
+
466
+ return true;
467
+ }
468
+
469
+ /* ----- Transfer Functions ----- */
470
+
471
+ /// @inheritdoc IERC3643
472
+ function forcedTransfer(address from, address to, uint256 amount) public override onlyAgent returns (bool) {
473
+ require(
474
+ !getAgentRestrictions(_msgSender()).disableForceTransfer,
475
+ ErrorsLib.AgentNotAuthorized(_msgSender(), "force transfer disabled")
476
+ );
477
+ TokenStorage storage s = _tokenStorage();
478
+ require(s.identityRegistry.isVerified(to), ErrorsLib.UnverifiedIdentity());
479
+ _forceUpdate(from, to, amount);
480
+ s.compliance.transferred(from, to, amount);
481
+ return true;
482
+ }
483
+
484
+ /// @inheritdoc IERC3643
485
+ function batchTransfer(address[] calldata tos, uint256[] calldata amounts) external override {
486
+ for (uint256 i = 0; i < tos.length; i++) {
487
+ transfer(tos[i], amounts[i]);
488
+ }
489
+ }
490
+
491
+ /// @inheritdoc IERC3643
492
+ function batchForcedTransfer(address[] calldata froms, address[] calldata tos, uint256[] calldata amounts)
493
+ external
494
+ override
495
+ {
496
+ for (uint256 i = 0; i < froms.length; i++) {
497
+ forcedTransfer(froms[i], tos[i], amounts[i]);
498
+ }
499
+ }
500
+
501
+ /* ----- Default Allowance Functions ----- */
502
+
503
+ /// @inheritdoc IToken
504
+ function setAllowanceForAll(address[] calldata targets, bool allow) external override onlyOwner {
505
+ uint256 targetsCount = targets.length;
506
+ require(targetsCount <= 100, ErrorsLib.ArraySizeLimited(100));
507
+
508
+ TokenStorage storage s = _tokenStorage();
509
+ for (uint256 i = 0; i < targetsCount; i++) {
510
+ require(s.defaultAllowances[targets[i]] != allow, ErrorsLib.DefaultAllowanceAlreadySet(targets[i], allow));
511
+ s.defaultAllowances[targets[i]] = allow;
512
+ emit EventsLib.DefaultAllowanceUpdated(targets[i], allow, _msgSender());
513
+ }
514
+ }
515
+
516
+ /// @inheritdoc IToken
517
+ function setDefaultAllowance(bool allow) external override {
518
+ TokenStorage storage s = _tokenStorage();
519
+ address sender = _msgSender();
520
+ require(s.defaultAllowanceOptOuts[sender] == allow, ErrorsLib.DefaultAllowanceOptOutAlreadySet(sender, allow));
521
+
522
+ s.defaultAllowanceOptOuts[sender] = !allow;
523
+ emit EventsLib.DefaultAllowanceOptOutUpdated(sender, !allow);
524
+ }
525
+
526
+ /// @inheritdoc IERC20
527
+ function allowance(address _owner, address _spender)
528
+ public
529
+ view
530
+ override(ERC20Upgradeable, IERC20)
531
+ returns (uint256)
532
+ {
533
+ TokenStorage storage s = _tokenStorage();
534
+ if (s.defaultAllowances[_spender] && !s.defaultAllowanceOptOuts[_owner]) {
535
+ return type(uint256).max;
536
+ }
537
+
538
+ return super.allowance(_owner, _spender);
539
+ }
540
+
541
+ /* ----- Agent Restrictions Functions ----- */
542
+
543
+ /// @inheritdoc IToken
544
+ function setAgentRestrictions(address agent, TokenRoles memory restrictions) external override onlyOwner {
545
+ if (!isAgent(agent)) {
546
+ revert ErrorsLib.AddressNotAgent(agent);
547
+ }
548
+ _tokenStorage().agentsRestrictions[agent] = restrictions;
549
+ emit EventsLib.AgentRestrictionsSet(
550
+ agent,
551
+ restrictions.disableMint,
552
+ restrictions.disableBurn,
553
+ restrictions.disableAddressFreeze,
554
+ restrictions.disableForceTransfer,
555
+ restrictions.disablePartialFreeze,
556
+ restrictions.disablePause,
557
+ restrictions.disableRecovery
558
+ );
559
+ }
560
+
561
+ /// @inheritdoc IToken
562
+ function getAgentRestrictions(address agent) public view override returns (TokenRoles memory) {
563
+ return _tokenStorage().agentsRestrictions[agent];
564
+ }
565
+
566
+ /* ----- ERC2771 Context Functions ----- */
567
+
568
+ /// @inheritdoc ERC2771ContextUpgradeable
569
+ function trustedForwarder() public view virtual override returns (address) {
570
+ return _tokenStorage().trustedForwarder;
571
+ }
572
+
573
+ /// @inheritdoc IToken
574
+ function setTrustedForwarder(address newTrustedForwarder) external onlyOwner {
575
+ _tokenStorage().trustedForwarder = newTrustedForwarder;
576
+
577
+ emit EventsLib.TrustedForwarderSet(newTrustedForwarder);
578
+ }
579
+
580
+ function _msgSender() internal view override(ContextUpgradeable, ERC2771ContextUpgradeable) returns (address) {
581
+ return super._msgSender();
582
+ }
583
+
584
+ function _msgData() internal view override(ContextUpgradeable, ERC2771ContextUpgradeable) returns (bytes calldata) {
585
+ return super._msgData();
586
+ }
587
+
588
+ function _contextSuffixLength()
589
+ internal
590
+ view
591
+ override(ContextUpgradeable, ERC2771ContextUpgradeable)
592
+ returns (uint256)
593
+ {
594
+ return super._contextSuffixLength();
595
+ }
596
+
597
+ function _checkIsAgent() internal view override {
598
+ require(isAgent(_msgSender()), ErrorsLib.CallerDoesNotHaveAgentRole());
599
+ }
600
+
601
+ /* ----- Utility Functions ----- */
602
+
603
+ /// @inheritdoc IERC165
604
+ function supportsInterface(bytes4 interfaceId) public pure virtual override returns (bool) {
605
+ return interfaceId == type(IERC20).interfaceId || interfaceId == type(IToken).interfaceId
606
+ || interfaceId == type(IERC173).interfaceId || interfaceId == type(IERC165).interfaceId
607
+ || interfaceId == type(IERC3643).interfaceId || interfaceId == type(IERC20Permit).interfaceId;
608
+ }
609
+
610
+ function _tokenStorage() private pure returns (TokenStorage storage $) {
611
+ assembly {
612
+ $.slot := TOKEN_STORAGE_LOCATION
613
+ }
614
+ }
615
+
616
+ function _update(address from, address to, uint256 value) internal override {
617
+ TokenStorage storage s = _tokenStorage();
618
+ bool isMint = from == address(0);
619
+ bool isBurn = to == address(0);
620
+
621
+ if (!isMint && !isBurn) {
622
+ _requireNotPaused();
623
+ require(!s.frozenStatus[from].addressFrozen, ErrorsLib.FrozenWallet(from));
624
+ require(!s.frozenStatus[to].addressFrozen, ErrorsLib.FrozenWallet(to));
625
+ uint256 freeBalance = balanceOf(from) - s.frozenStatus[from].amount;
626
+ require(value <= freeBalance, IERC20Errors.ERC20InsufficientBalance(from, freeBalance, value));
627
+ } else if (isBurn) {
628
+ _autoUnfreezeFor(from, value);
629
+ }
630
+
631
+ if (!isBurn) {
632
+ require(s.identityRegistry.isVerified(to), ErrorsLib.UnverifiedIdentity());
633
+ require(s.compliance.canTransfer(from, to, value), ErrorsLib.ComplianceNotFollowed());
634
+ }
635
+
636
+ super._update(from, to, value);
637
+
638
+ if (isMint) s.compliance.created(to, value);
639
+ else if (isBurn) s.compliance.destroyed(from, value);
640
+ else s.compliance.transferred(from, to, value);
641
+ }
642
+
643
+ function _forceUpdate(address from, address to, uint256 value) private {
644
+ _autoUnfreezeFor(from, value);
645
+ super._update(from, to, value);
646
+ }
647
+
648
+ function _autoUnfreezeFor(address from, uint256 value) private {
649
+ TokenStorage storage s = _tokenStorage();
650
+ uint256 balance = balanceOf(from);
651
+ require(value <= balance, IERC20Errors.ERC20InsufficientBalance(from, balance, value));
652
+ uint256 freeBalance = balance - s.frozenStatus[from].amount;
653
+ if (value > freeBalance) {
654
+ uint256 toUnfreeze = value - freeBalance;
655
+ s.frozenStatus[from].amount -= toUnfreeze;
656
+ emit ERC3643EventsLib.TokensUnfrozen(from, toUnfreeze);
657
+ }
658
+ }
659
+
660
+ function _emitUpdatedTokenInformation() internal {
661
+ emit ERC3643EventsLib.UpdatedTokenInformation(name(), symbol(), decimals(), VERSION, _tokenStorage().onchainId);
662
+ }
663
+
664
+ }