@etherisc/gif-next 0.0.2-743affe-003 → 0.0.2-7878646-744

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 (233) hide show
  1. package/README.md +11 -0
  2. package/artifacts/contracts/components/BaseComponent.sol/BaseComponent.dbg.json +1 -1
  3. package/artifacts/contracts/components/BaseComponent.sol/BaseComponent.json +90 -2
  4. package/artifacts/contracts/components/Distribution.sol/Distribution.dbg.json +1 -1
  5. package/artifacts/contracts/components/Distribution.sol/Distribution.json +118 -6
  6. package/artifacts/contracts/components/IBaseComponent.sol/IBaseComponent.dbg.json +1 -1
  7. package/artifacts/contracts/components/IBaseComponent.sol/IBaseComponent.json +80 -3
  8. package/artifacts/contracts/components/IDistributionComponent.sol/IDistributionComponent.dbg.json +1 -1
  9. package/artifacts/contracts/components/IDistributionComponent.sol/IDistributionComponent.json +0 -166
  10. package/artifacts/contracts/components/IPoolComponent.sol/IPoolComponent.dbg.json +1 -1
  11. package/artifacts/contracts/components/IPoolComponent.sol/IPoolComponent.json +0 -166
  12. package/artifacts/contracts/components/IProductComponent.sol/IProductComponent.dbg.json +1 -1
  13. package/artifacts/contracts/components/IProductComponent.sol/IProductComponent.json +0 -166
  14. package/artifacts/contracts/components/Pool.sol/Pool.dbg.json +1 -1
  15. package/artifacts/contracts/components/Pool.sol/Pool.json +120 -6
  16. package/artifacts/contracts/components/Product.sol/Product.dbg.json +1 -1
  17. package/artifacts/contracts/components/Product.sol/Product.json +118 -6
  18. package/artifacts/contracts/experiment/cloning/Cloner.sol/Cloner.dbg.json +4 -0
  19. package/artifacts/contracts/experiment/cloning/Cloner.sol/Cloner.json +66 -0
  20. package/artifacts/contracts/experiment/cloning/Cloner.sol/Mock1.dbg.json +4 -0
  21. package/artifacts/contracts/experiment/cloning/Cloner.sol/Mock1.json +24 -0
  22. package/artifacts/contracts/experiment/cloning/Cloner.sol/Mock2.dbg.json +4 -0
  23. package/artifacts/contracts/experiment/cloning/Cloner.sol/Mock2.json +42 -0
  24. package/artifacts/contracts/experiment/errors/Require.sol/Require.dbg.json +1 -1
  25. package/artifacts/contracts/experiment/errors/Revert.sol/Revert.dbg.json +1 -1
  26. package/artifacts/contracts/experiment/inheritance/A.sol/A.dbg.json +1 -1
  27. package/artifacts/contracts/experiment/inheritance/A.sol/AShared.dbg.json +1 -1
  28. package/artifacts/contracts/experiment/inheritance/B.sol/B.dbg.json +1 -1
  29. package/artifacts/contracts/experiment/inheritance/C.sol/C.dbg.json +1 -1
  30. package/artifacts/contracts/experiment/inheritance/IA.sol/IA.dbg.json +1 -1
  31. package/artifacts/contracts/experiment/inheritance/IA.sol/ISharedA.dbg.json +1 -1
  32. package/artifacts/contracts/experiment/inheritance/IB.sol/IB.dbg.json +1 -1
  33. package/artifacts/contracts/experiment/inheritance/IC.sol/IC.dbg.json +1 -1
  34. package/artifacts/contracts/experiment/statemachine/Dummy.sol/LifeCycleModule.dbg.json +1 -1
  35. package/artifacts/contracts/experiment/statemachine/ISM.sol/ISM.dbg.json +1 -1
  36. package/artifacts/contracts/experiment/statemachine/ISM.sol/ISMEE.dbg.json +1 -1
  37. package/artifacts/contracts/experiment/statemachine/SM.sol/SM.dbg.json +1 -1
  38. package/artifacts/contracts/experiment/statemachine/SimpleStateMachine.sol/SimpleStateMachine.dbg.json +1 -1
  39. package/artifacts/contracts/experiment/types/TypeA.sol/TypeALib.dbg.json +1 -1
  40. package/artifacts/contracts/experiment/types/TypeB.sol/TypeBLib.dbg.json +1 -1
  41. package/artifacts/contracts/instance/IInstance.sol/IInstance.dbg.json +1 -1
  42. package/artifacts/contracts/instance/IInstance.sol/IInstance.json +54 -286
  43. package/artifacts/contracts/instance/IInstanceLinked.sol/IInstanceLinked.dbg.json +1 -1
  44. package/artifacts/contracts/instance/Instance.sol/Instance.dbg.json +1 -1
  45. package/artifacts/contracts/instance/Instance.sol/Instance.json +154 -399
  46. package/artifacts/contracts/instance/base/ComponentServiceBase.sol/ComponentServiceBase.dbg.json +1 -1
  47. package/artifacts/contracts/instance/base/ComponentServiceBase.sol/ComponentServiceBase.json +90 -2
  48. package/artifacts/contracts/instance/base/IInstanceBase.sol/IInstanceBase.dbg.json +1 -1
  49. package/artifacts/contracts/instance/base/IKeyValueStore.sol/IKeyValueStore.dbg.json +1 -1
  50. package/artifacts/contracts/instance/base/ILifecycle.sol/ILifecycle.dbg.json +1 -1
  51. package/artifacts/contracts/instance/base/IService.sol/IService.dbg.json +1 -1
  52. package/artifacts/contracts/instance/base/IService.sol/IService.json +80 -3
  53. package/artifacts/contracts/instance/base/InstanceBase.sol/InstanceBase.dbg.json +1 -1
  54. package/artifacts/contracts/instance/base/InstanceBase.sol/InstanceBase.json +90 -2
  55. package/artifacts/contracts/instance/base/KeyValueStore.sol/KeyValueStore.dbg.json +1 -1
  56. package/artifacts/contracts/instance/base/Lifecycle.sol/Lifecycle.dbg.json +1 -1
  57. package/artifacts/contracts/instance/base/ModuleBase.sol/ModuleBase.dbg.json +1 -1
  58. package/artifacts/contracts/instance/base/ServiceBase.sol/ServiceBase.dbg.json +1 -1
  59. package/artifacts/contracts/instance/base/ServiceBase.sol/ServiceBase.json +90 -2
  60. package/artifacts/contracts/instance/module/bundle/BundleModule.sol/BundleModule.dbg.json +1 -1
  61. package/artifacts/contracts/instance/module/bundle/IBundle.sol/IBundle.dbg.json +1 -1
  62. package/artifacts/contracts/instance/module/bundle/IBundle.sol/IBundleModule.dbg.json +1 -1
  63. package/artifacts/contracts/instance/module/component/ComponentModule.sol/ComponentModule.dbg.json +1 -1
  64. package/artifacts/contracts/instance/module/component/IComponent.sol/IComponent.dbg.json +1 -1
  65. package/artifacts/contracts/instance/module/component/IComponent.sol/IComponentModule.dbg.json +1 -1
  66. package/artifacts/contracts/instance/module/distribution/DistributionModule.sol/DistributionModule.dbg.json +1 -1
  67. package/artifacts/contracts/instance/module/distribution/DistributionModule.sol/DistributionModule.json +2 -2
  68. package/artifacts/contracts/instance/module/distribution/IDistribution.sol/IDistribution.dbg.json +1 -1
  69. package/artifacts/contracts/instance/module/distribution/IDistribution.sol/IDistributionModule.dbg.json +1 -1
  70. package/artifacts/contracts/instance/module/policy/IPolicy.sol/IPolicy.dbg.json +1 -1
  71. package/artifacts/contracts/instance/module/policy/IPolicy.sol/IPolicyModule.dbg.json +1 -1
  72. package/artifacts/contracts/instance/module/policy/IPolicy.sol/IPolicyModule.json +0 -13
  73. package/artifacts/contracts/instance/module/policy/PolicyModule.sol/PolicyModule.dbg.json +1 -1
  74. package/artifacts/contracts/instance/module/policy/PolicyModule.sol/PolicyModule.json +0 -13
  75. package/artifacts/contracts/instance/module/pool/IPoolModule.sol/IPool.dbg.json +1 -1
  76. package/artifacts/contracts/instance/module/pool/IPoolModule.sol/IPoolModule.dbg.json +1 -1
  77. package/artifacts/contracts/instance/module/pool/PoolModule.sol/PoolModule.dbg.json +1 -1
  78. package/artifacts/contracts/instance/module/risk/IRisk.sol/IRisk.dbg.json +1 -1
  79. package/artifacts/contracts/instance/module/risk/IRisk.sol/IRiskModule.dbg.json +1 -1
  80. package/artifacts/contracts/instance/module/risk/RiskModule.sol/RiskModule.dbg.json +1 -1
  81. package/artifacts/contracts/instance/module/risk/RiskModule.sol/RiskModule.json +2 -2
  82. package/artifacts/contracts/instance/module/treasury/ITreasury.sol/ITreasury.dbg.json +1 -1
  83. package/artifacts/contracts/instance/module/treasury/ITreasury.sol/ITreasuryModule.dbg.json +1 -1
  84. package/artifacts/contracts/instance/module/treasury/TokenHandler.sol/TokenHandler.dbg.json +1 -1
  85. package/artifacts/contracts/instance/module/treasury/TokenHandler.sol/TokenHandler.json +2 -2
  86. package/artifacts/contracts/instance/module/treasury/TreasuryModule.sol/TreasuryModule.dbg.json +1 -1
  87. package/artifacts/contracts/instance/service/ComponentOwnerService.sol/ComponentOwnerService.dbg.json +1 -1
  88. package/artifacts/contracts/instance/service/ComponentOwnerService.sol/ComponentOwnerService.json +177 -40
  89. package/artifacts/contracts/instance/service/DistributionService.sol/DistributionService.dbg.json +1 -1
  90. package/artifacts/contracts/instance/service/DistributionService.sol/DistributionService.json +120 -16
  91. package/artifacts/contracts/instance/service/IComponentOwnerService.sol/IComponentOwnerService.dbg.json +1 -1
  92. package/artifacts/contracts/instance/service/IComponentOwnerService.sol/IComponentOwnerService.json +80 -3
  93. package/artifacts/contracts/instance/service/IDistributionService.sol/IDistributionService.dbg.json +1 -1
  94. package/artifacts/contracts/instance/service/IDistributionService.sol/IDistributionService.json +80 -3
  95. package/artifacts/contracts/instance/service/IPoolService.sol/IPoolService.dbg.json +1 -1
  96. package/artifacts/contracts/instance/service/IPoolService.sol/IPoolService.json +80 -3
  97. package/artifacts/contracts/instance/service/IProductService.sol/IProductService.dbg.json +1 -1
  98. package/artifacts/contracts/instance/service/IProductService.sol/IProductService.json +80 -3
  99. package/artifacts/contracts/instance/service/PoolService.sol/PoolService.dbg.json +1 -1
  100. package/artifacts/contracts/instance/service/PoolService.sol/PoolService.json +122 -18
  101. package/artifacts/contracts/instance/service/ProductService.sol/ProductService.dbg.json +1 -1
  102. package/artifacts/contracts/instance/service/ProductService.sol/ProductService.json +146 -42
  103. package/artifacts/contracts/registry/ChainNft.sol/ChainNft.dbg.json +1 -1
  104. package/artifacts/contracts/registry/ChainNft.sol/ChainNft.json +2 -2
  105. package/artifacts/contracts/registry/IChainNft.sol/IChainNft.dbg.json +1 -1
  106. package/artifacts/contracts/registry/IRegistry.sol/IRegistry.dbg.json +1 -1
  107. package/artifacts/contracts/registry/IRegistry.sol/IRegistry.json +40 -167
  108. package/artifacts/contracts/registry/IRegistryService.sol/IRegistryService.dbg.json +1 -1
  109. package/artifacts/contracts/registry/IRegistryService.sol/IRegistryService.json +214 -10
  110. package/artifacts/contracts/registry/ITransferInterceptor.sol/ITransferInterceptor.dbg.json +1 -1
  111. package/artifacts/contracts/registry/Registry.sol/Registry.dbg.json +1 -1
  112. package/artifacts/contracts/registry/Registry.sol/Registry.json +66 -188
  113. package/artifacts/contracts/registry/RegistryService.sol/RegistryService.dbg.json +1 -1
  114. package/artifacts/contracts/registry/RegistryService.sol/RegistryService.json +415 -31
  115. package/artifacts/contracts/registry/RegistryServiceManager.sol/RegistryServiceManager.dbg.json +4 -0
  116. package/artifacts/contracts/registry/RegistryServiceManager.sol/RegistryServiceManager.json +496 -0
  117. package/artifacts/contracts/shared/ContractDeployerLib.sol/ContractDeployerLib.dbg.json +4 -0
  118. package/artifacts/contracts/shared/ContractDeployerLib.sol/ContractDeployerLib.json +107 -0
  119. package/artifacts/contracts/shared/ERC165.sol/ERC165.dbg.json +1 -1
  120. package/artifacts/contracts/shared/ERC165.sol/ERC165.json +2 -2
  121. package/artifacts/contracts/shared/INftOwnable.sol/INftOwnable.dbg.json +4 -0
  122. package/artifacts/contracts/shared/INftOwnable.sol/INftOwnable.json +127 -0
  123. package/artifacts/contracts/shared/IRegisterable.sol/IRegisterable.dbg.json +1 -1
  124. package/artifacts/contracts/shared/IRegisterable.sol/IRegisterable.json +80 -3
  125. package/artifacts/contracts/shared/IVersionable.sol/IVersionable.dbg.json +1 -1
  126. package/artifacts/contracts/shared/NftOwnable.sol/NftOwnable.dbg.json +4 -0
  127. package/artifacts/contracts/shared/NftOwnable.sol/NftOwnable.json +158 -0
  128. package/artifacts/contracts/shared/ProxyManager.sol/ProxyManager.dbg.json +1 -1
  129. package/artifacts/contracts/shared/ProxyManager.sol/ProxyManager.json +121 -82
  130. package/artifacts/contracts/shared/Registerable.sol/Registerable.dbg.json +1 -1
  131. package/artifacts/contracts/shared/Registerable.sol/Registerable.json +120 -6
  132. package/artifacts/contracts/shared/UpgradableProxyWithAdmin.sol/UpgradableProxyWithAdmin.dbg.json +1 -1
  133. package/artifacts/contracts/shared/UpgradableProxyWithAdmin.sol/UpgradableProxyWithAdmin.json +2 -2
  134. package/artifacts/contracts/shared/Versionable.sol/Versionable.dbg.json +1 -1
  135. package/artifacts/contracts/test/TestDistribution.sol/TestDistribution.dbg.json +1 -1
  136. package/artifacts/contracts/test/TestDistribution.sol/TestDistribution.json +118 -6
  137. package/artifacts/contracts/test/TestFee.sol/TestFee.dbg.json +1 -1
  138. package/artifacts/contracts/test/TestFee.sol/TestFee.json +4 -4
  139. package/artifacts/contracts/test/TestPool.sol/TestPool.dbg.json +1 -1
  140. package/artifacts/contracts/test/TestPool.sol/TestPool.json +120 -6
  141. package/artifacts/contracts/test/TestProduct.sol/TestProduct.dbg.json +1 -1
  142. package/artifacts/contracts/test/TestProduct.sol/TestProduct.json +120 -8
  143. package/artifacts/contracts/test/TestRegisterable.sol/TestRegisterable.dbg.json +1 -1
  144. package/artifacts/contracts/test/TestRegisterable.sol/TestRegisterable.json +120 -6
  145. package/artifacts/contracts/test/TestRoleId.sol/TestRoleId.dbg.json +1 -1
  146. package/artifacts/contracts/test/TestRoleId.sol/TestRoleId.json +2 -2
  147. package/artifacts/contracts/test/TestService.sol/TestService.dbg.json +1 -1
  148. package/artifacts/contracts/test/TestService.sol/TestService.json +126 -14
  149. package/artifacts/contracts/test/TestToken.sol/TestUsdc.dbg.json +1 -1
  150. package/artifacts/contracts/test/TestToken.sol/TestUsdc.json +91 -53
  151. package/artifacts/contracts/test/TestVersion.sol/TestVersion.dbg.json +1 -1
  152. package/artifacts/contracts/test/TestVersionable.sol/TestVersionable.dbg.json +1 -1
  153. package/artifacts/contracts/test/TestVersionable.sol/TestVersionable.json +2 -2
  154. package/artifacts/contracts/test/Usdc.sol/USDC.dbg.json +1 -1
  155. package/artifacts/contracts/test/Usdc.sol/USDC.json +91 -53
  156. package/artifacts/contracts/types/AddressSet.sol/LibAddressSet.dbg.json +1 -1
  157. package/artifacts/contracts/types/Blocknumber.sol/BlocknumberLib.dbg.json +1 -1
  158. package/artifacts/contracts/types/ChainId.sol/ChainIdLib.dbg.json +1 -1
  159. package/artifacts/contracts/types/Fee.sol/FeeLib.dbg.json +1 -1
  160. package/artifacts/contracts/types/Fee.sol/FeeLib.json +4 -4
  161. package/artifacts/contracts/types/Key32.sol/Key32Lib.dbg.json +1 -1
  162. package/artifacts/contracts/types/NftId.sol/NftIdLib.dbg.json +1 -1
  163. package/artifacts/contracts/types/NftIdSet.sol/LibNftIdSet.dbg.json +1 -1
  164. package/artifacts/contracts/types/ObjectType.sol/ObjectTypeLib.dbg.json +1 -1
  165. package/artifacts/contracts/types/ReferralId.sol/ReferralIdLib.dbg.json +1 -1
  166. package/artifacts/contracts/types/RiskId.sol/RiskIdLib.dbg.json +1 -1
  167. package/artifacts/contracts/types/RoleId.sol/RoleIdLib.dbg.json +1 -1
  168. package/artifacts/contracts/types/RoleId.sol/RoleIdLib.json +2 -2
  169. package/artifacts/contracts/types/StateId.sol/StateIdLib.dbg.json +1 -1
  170. package/artifacts/contracts/types/Timestamp.sol/TimestampLib.dbg.json +1 -1
  171. package/artifacts/contracts/types/UFixed.sol/MathLib.dbg.json +4 -0
  172. package/artifacts/contracts/types/UFixed.sol/MathLib.json +10 -0
  173. package/artifacts/contracts/types/UFixed.sol/UFixedLib.dbg.json +4 -0
  174. package/artifacts/contracts/types/UFixed.sol/{UFixedMathLib.json → UFixedLib.json} +3 -3
  175. package/artifacts/contracts/types/Version.sol/VersionLib.dbg.json +1 -1
  176. package/artifacts/contracts/types/Version.sol/VersionPartLib.dbg.json +1 -1
  177. package/contracts/components/BaseComponent.sol +1 -1
  178. package/contracts/components/IBaseComponent.sol +1 -1
  179. package/contracts/components/IDistributionComponent.sol +1 -2
  180. package/contracts/components/IPoolComponent.sol +1 -2
  181. package/contracts/components/IProductComponent.sol +1 -3
  182. package/contracts/components/Product.sol +4 -4
  183. package/contracts/experiment/cloning/Cloner.sol +47 -0
  184. package/contracts/instance/IInstance.sol +5 -7
  185. package/contracts/instance/Instance.sol +4 -6
  186. package/contracts/instance/base/ServiceBase.sol +1 -0
  187. package/contracts/instance/module/component/ComponentModule.sol +1 -1
  188. package/contracts/instance/module/component/IComponent.sol +1 -1
  189. package/contracts/instance/module/policy/IPolicy.sol +1 -1
  190. package/contracts/instance/module/treasury/ITreasury.sol +1 -1
  191. package/contracts/instance/module/treasury/TokenHandler.sol +1 -1
  192. package/contracts/instance/module/treasury/TreasuryModule.sol +4 -4
  193. package/contracts/instance/service/ComponentOwnerService.sol +73 -30
  194. package/contracts/instance/service/ProductService.sol +7 -6
  195. package/contracts/registry/ChainNft.sol +2 -2
  196. package/contracts/registry/IChainNft.sol +1 -1
  197. package/contracts/registry/IRegistry.sol +7 -25
  198. package/contracts/registry/IRegistryService.sol +9 -3
  199. package/contracts/registry/Registry.sol +78 -157
  200. package/contracts/registry/RegistryService.sol +179 -121
  201. package/contracts/registry/RegistryServiceManager.sol +64 -0
  202. package/contracts/shared/ContractDeployerLib.sol +72 -0
  203. package/contracts/shared/ERC165.sol +1 -1
  204. package/contracts/shared/INftOwnable.sol +22 -0
  205. package/contracts/shared/IRegisterable.sol +3 -8
  206. package/contracts/shared/NftOwnable.sol +136 -0
  207. package/contracts/shared/ProxyManager.sol +16 -37
  208. package/contracts/shared/Registerable.sol +14 -42
  209. package/contracts/shared/UpgradableProxyWithAdmin.sol +2 -2
  210. package/contracts/shared/Versionable.sol +1 -1
  211. package/contracts/test/TestFee.sol +2 -2
  212. package/contracts/types/Fee.sol +3 -3
  213. package/contracts/types/RoleId.sol +7 -0
  214. package/contracts/types/UFixed.sol +128 -12
  215. package/package.json +4 -3
  216. package/artifacts/contracts/instance/module/access/Access.sol/AccessModule.dbg.json +0 -4
  217. package/artifacts/contracts/instance/module/access/Access.sol/AccessModule.json +0 -333
  218. package/artifacts/contracts/instance/module/access/IAccess.sol/IAccess.dbg.json +0 -4
  219. package/artifacts/contracts/instance/module/access/IAccess.sol/IAccess.json +0 -10
  220. package/artifacts/contracts/instance/module/access/IAccess.sol/IAccessModule.dbg.json +0 -4
  221. package/artifacts/contracts/instance/module/access/IAccess.sol/IAccessModule.json +0 -333
  222. package/artifacts/contracts/registry/IRegistryLinked.sol/IRegistryLinked.dbg.json +0 -4
  223. package/artifacts/contracts/registry/IRegistryLinked.sol/IRegistryLinked.json +0 -24
  224. package/artifacts/contracts/registry/RegistryInstaller.sol/RegistryInstaller.dbg.json +0 -4
  225. package/artifacts/contracts/registry/RegistryInstaller.sol/RegistryInstaller.json +0 -344
  226. package/artifacts/contracts/shared/IOwnable.sol/IOwnable.dbg.json +0 -4
  227. package/artifacts/contracts/shared/IOwnable.sol/IOwnable.json +0 -24
  228. package/artifacts/contracts/types/UFixed.sol/UFixedMathLib.dbg.json +0 -4
  229. package/contracts/instance/module/access/Access.sol +0 -149
  230. package/contracts/instance/module/access/IAccess.sol +0 -53
  231. package/contracts/registry/IRegistryLinked.sol +0 -8
  232. package/contracts/registry/RegistryInstaller.sol +0 -100
  233. package/contracts/shared/IOwnable.sol +0 -6
@@ -1,22 +1,17 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
2
  pragma solidity ^0.8.20;
3
3
 
4
- import {IERC165} from "@openzeppelin5/contracts/utils/introspection/IERC165.sol";
4
+ import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
5
5
 
6
+ import {INftOwnable} from "./INftOwnable.sol";
6
7
  import {IRegistry} from "../registry/IRegistry.sol";
7
8
  import {NftId} from "../types/NftId.sol";
8
9
  import {ObjectType} from "../types/ObjectType.sol";
9
10
 
10
- import {IOwnable} from "./IOwnable.sol";
11
-
12
- interface IRegisterable is IERC165, IOwnable {
13
- function getRegistry() external view returns (IRegistry registry);
14
-
15
- function getNftId() external view returns (NftId nftId);
11
+ interface IRegisterable is IERC165, INftOwnable {
16
12
 
17
13
  function getInitialInfo()
18
14
  external
19
15
  view
20
16
  returns (IRegistry.ObjectInfo memory, bytes memory data);
21
-
22
17
  }
@@ -0,0 +1,136 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.20;
3
+
4
+ import {INftOwnable} from "./INftOwnable.sol";
5
+ import {IRegistry} from "../registry/IRegistry.sol";
6
+ import {NftId, zeroNftId} from "../types/NftId.sol";
7
+
8
+ contract NftOwnable is INftOwnable {
9
+
10
+ IRegistry internal _registry;
11
+ NftId private _nftId;
12
+ address private _initialOwner;
13
+
14
+ /// @dev enforces msg.sender is owner of nft (or initial owner of nft ownable)
15
+ modifier onlyOwner() {
16
+ address owner = getOwner();
17
+
18
+ // owner == address(0) is eg uninitialized upgradable contract
19
+ if (owner != address(0) && msg.sender != owner) {
20
+ revert ErrorNotOwner(msg.sender);
21
+ }
22
+ _;
23
+ }
24
+
25
+ constructor() {
26
+ _initialOwner = msg.sender;
27
+ }
28
+
29
+ /// @dev links this contract to nft after registration
30
+ // needs to be done once per registered contract and
31
+ // reduces registry calls to check ownership
32
+ // does not need any protection as function can only do the "right thing"
33
+ function linkToRegisteredNftId() public {
34
+ if (_nftId.gtz()) {
35
+ revert ErrorAlreadyLinked(address(_registry), _nftId);
36
+ }
37
+
38
+ if (address(_registry) == address(0)) {
39
+ revert ErrorRegistryNotInitialized();
40
+ }
41
+
42
+ address contractAddress = address(this);
43
+
44
+ if (!_registry.isRegistered(contractAddress)) {
45
+ revert ErrorContractNotRegistered(contractAddress);
46
+ }
47
+
48
+ _nftId = _registry.getNftId(contractAddress);
49
+ }
50
+
51
+
52
+ function getRegistry() public view virtual override returns (IRegistry) {
53
+ return _registry;
54
+ }
55
+
56
+
57
+ function getNftId() public view virtual override returns (NftId) {
58
+ return _nftId;
59
+ }
60
+
61
+
62
+ function getOwner() public view virtual override returns (address) {
63
+ if (_nftId.gtz()) {
64
+ return _registry.ownerOf(_nftId);
65
+ }
66
+
67
+ return _initialOwner;
68
+ }
69
+
70
+
71
+ /// @dev initialization for upgradable contracts
72
+ // used in _initializeRegisterable
73
+ function _initializeNftOwnable(
74
+ address initialOwner,
75
+ address registryAddress
76
+ )
77
+ internal
78
+ virtual
79
+ {
80
+ _initialOwner = initialOwner;
81
+ _setRegistry(registryAddress);
82
+ }
83
+
84
+
85
+ /// @dev used in constructor of registry service manager
86
+ // links ownership of registry service manager ot nft owner of registry service
87
+ function _linkToNftOwnable(
88
+ address registryAddress,
89
+ address nftOwnableAddress
90
+ )
91
+ internal
92
+ onlyOwner()
93
+ returns (NftId)
94
+ {
95
+ if (_nftId.gtz()) {
96
+ revert ErrorAlreadyLinked(address(_registry), _nftId);
97
+ }
98
+
99
+ _setRegistry(registryAddress);
100
+
101
+ if (!_registry.isRegistered(nftOwnableAddress)) {
102
+ revert ErrorContractNotRegistered(nftOwnableAddress);
103
+ }
104
+
105
+ _nftId = _registry.getNftId(nftOwnableAddress);
106
+
107
+ return _nftId;
108
+ }
109
+
110
+
111
+ function _setRegistry(address registryAddress)
112
+ private
113
+ {
114
+ if (address(_registry) != address(0)) {
115
+ revert ErrorRegistryAlreadyInitialized(address(_registry));
116
+ }
117
+
118
+ if (registryAddress == address(0)) {
119
+ revert ErrorRegistryAddressZero();
120
+ }
121
+
122
+ if (registryAddress.code.length == 0) {
123
+ revert ErrorNotRegistry(registryAddress);
124
+ }
125
+
126
+ _registry = IRegistry(registryAddress);
127
+
128
+ try _registry.supportsInterface(type(IRegistry).interfaceId) returns (bool isRegistry) {
129
+ if (!isRegistry) {
130
+ revert ErrorNotRegistry(registryAddress);
131
+ }
132
+ } catch {
133
+ revert ErrorNotRegistry(registryAddress);
134
+ }
135
+ }
136
+ }
@@ -1,16 +1,18 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
2
  pragma solidity ^0.8.20;
3
3
 
4
- import {Ownable} from "@openzeppelin5/contracts/access/Ownable.sol";
5
- import {ProxyAdmin} from "@openzeppelin5/contracts/proxy/transparent/ProxyAdmin.sol";
6
- import {ITransparentUpgradeableProxy} from "@openzeppelin5/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
4
+ import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
5
+ import {ProxyAdmin} from "@openzeppelin/contracts/proxy/transparent/ProxyAdmin.sol";
6
+ import {ITransparentUpgradeableProxy} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
7
7
 
8
8
  import {IVersionable} from "./IVersionable.sol";
9
+ import {NftOwnable} from "./NftOwnable.sol";
9
10
  import {UpgradableProxyWithAdmin} from "./UpgradableProxyWithAdmin.sol";
10
- import {IVersionable} from "./IVersionable.sol";
11
11
 
12
12
  /// @dev manages proxy deployments for upgradable contracs of type IVersionable
13
- contract ProxyManager is Ownable {
13
+ contract ProxyManager is
14
+ NftOwnable
15
+ {
14
16
 
15
17
  event LogProxyDeployed(address indexed proxy, address initialImplementation);
16
18
  event LogProxyDeployedWithSalt(address indexed proxy, address initialImplementation);
@@ -20,23 +22,24 @@ contract ProxyManager is Ownable {
20
22
  error ErrorAlreadyDeployedWithSalt();
21
23
  error ErrorNotYetDeployed();
22
24
 
23
- UpgradableProxyWithAdmin private _proxy;
24
- bool private _isDeployed;
25
+ UpgradableProxyWithAdmin internal _proxy;
26
+ bool internal _isDeployed;
25
27
 
26
28
  /// @dev only used to capture proxy owner
27
29
  constructor()
28
- Ownable(msg.sender)
30
+ NftOwnable()
29
31
  { }
30
32
 
31
33
  /// @dev deploy initial contract
32
34
  function deploy(address initialImplementation, bytes memory initializationData)
33
35
  public
36
+ virtual
34
37
  onlyOwner()
35
38
  returns (IVersionable versionable)
36
39
  {
37
40
  if (_isDeployed) { revert ErrorAlreadyDeployed(); }
38
41
 
39
- address currentProxyOwner = owner(); // used by implementation
42
+ address currentProxyOwner = getOwner(); // used by implementation
40
43
  address initialProxyAdminOwner = address(this); // used by proxy
41
44
  bytes memory data = getDeployData(initialImplementation, currentProxyOwner, initializationData);
42
45
 
@@ -52,30 +55,6 @@ contract ProxyManager is Ownable {
52
55
  emit LogProxyDeployed(address(_proxy), initialImplementation);
53
56
  }
54
57
 
55
- function deployWithSalt(address initialImplementation, bytes memory initializationData, bytes32 salt)
56
- public
57
- onlyOwner()
58
- returns (IVersionable versionable)
59
- {
60
- if (_isDeployed) { revert ErrorAlreadyDeployedWithSalt(); }
61
-
62
- address currentProxyOwner = owner(); // used by implementation
63
- address initialProxyAdminOwner = address(this); // used by proxy
64
- bytes memory data = getDeployData(initialImplementation, currentProxyOwner, initializationData);
65
-
66
- // via create2
67
- _proxy = new UpgradableProxyWithAdmin{salt: salt}(
68
- initialImplementation,
69
- initialProxyAdminOwner,
70
- data
71
- );
72
-
73
- _isDeployed = true;
74
- versionable = IVersionable(address(_proxy));
75
-
76
- emit LogProxyDeployedWithSalt(address(_proxy), initialImplementation);
77
- }
78
-
79
58
  /// @dev upgrade existing contract
80
59
  function upgrade(address newImplementation, bytes memory upgradeData)
81
60
  public
@@ -85,8 +64,8 @@ contract ProxyManager is Ownable {
85
64
  {
86
65
  if (!_isDeployed) { revert ErrorNotYetDeployed(); }
87
66
 
88
- address currentProxyOwner = owner();
89
- ProxyAdmin proxyAdmin = getProxyAdmin();
67
+ address currentProxyOwner = getOwner();
68
+ ProxyAdmin proxyAdmin = getProxy().getProxyAdmin();
90
69
  ITransparentUpgradeableProxy proxy = ITransparentUpgradeableProxy(address(_proxy));
91
70
  bytes memory data = getUpgradeData(newImplementation, currentProxyOwner, upgradeData);
92
71
 
@@ -109,7 +88,7 @@ contract ProxyManager is Ownable {
109
88
  return abi.encodeWithSelector(IVersionable.upgrade.selector, implementation, proxyOwner, upgradeData);
110
89
  }
111
90
 
112
- function getProxyAdmin() public returns (ProxyAdmin) {
113
- return _proxy.getProxyAdmin();
91
+ function getProxy() public returns (UpgradableProxyWithAdmin) {
92
+ return _proxy;
114
93
  }
115
94
  }
@@ -1,9 +1,10 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
2
  pragma solidity ^0.8.20;
3
3
 
4
- import {Initializable} from "@openzeppelin5/contracts/proxy/utils/Initializable.sol";
4
+ import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
5
5
 
6
6
  import {NftId, zeroNftId} from "../types/NftId.sol";
7
+ import {NftOwnable} from "../shared/NftOwnable.sol";
7
8
  import {ObjectType} from "../types/ObjectType.sol";
8
9
 
9
10
  import {IRegistry} from "../registry/IRegistry.sol";
@@ -12,18 +13,19 @@ import {Versionable} from "./Versionable.sol";
12
13
 
13
14
  import {ERC165} from "./ERC165.sol";
14
15
 
15
- abstract contract Registerable is
16
+ contract Registerable is
16
17
  ERC165,
17
- IRegisterable,
18
- Initializable
18
+ Initializable,
19
+ NftOwnable,
20
+ IRegisterable
19
21
  {
20
22
  // keccak256(abi.encode(uint256(keccak256("gif-next.contracts.shared.Registerable.sol")) - 1)) & ~bytes32(uint256(0xff));
21
23
  bytes32 public constant REGISTERABLE_LOCATION_V1 = 0x6548007c3f4340f82f348c576c0ff69f4f529cadd5ad41f96aae61abceeaa300;
22
24
 
25
+ error ErrorRegisterableNotRegistry(address registryAddress);
26
+
23
27
  struct RegisterableStorage {
24
- IRegistry _registry;
25
28
  NftId _parentNftId;
26
- address _initialOwner;
27
29
  ObjectType _objectType;
28
30
  bool _isInterceptor;
29
31
  bytes _data;
@@ -35,14 +37,6 @@ abstract contract Registerable is
35
37
  }
36
38
  }
37
39
 
38
- modifier onlyOwner() virtual {
39
- require(
40
- msg.sender == getOwner(),
41
- "ERROR:RGB-001:NOT_OWNER"
42
- );
43
- _;
44
- }
45
-
46
40
  function _initializeRegisterable(
47
41
  address registryAddress,
48
42
  NftId parentNftId,
@@ -55,44 +49,22 @@ abstract contract Registerable is
55
49
  //onlyInitializing//TODO uncomment when "fully" upgradeable
56
50
  virtual
57
51
  {
58
- require(
59
- registryAddress != address(0),
60
- "ERROR:RGB-010:REGISTRY_ZERO"
61
- );
52
+ _initializeNftOwnable(
53
+ initialOwner,
54
+ registryAddress);
62
55
 
63
56
  // TODO check parentNftId -> registry.isRegistered(parentNftId)
64
57
  // TODO check object-parent type pair -> registry.isValidTypeCombo() or something...verify with registry that setup will be able to register...
65
58
 
66
- IRegistry registry = IRegistry(registryAddress);
67
- require(
68
- registry.supportsInterface(type(IRegistry).interfaceId),
69
- "ERROR:RGB-011:NOT_REGISTRY"
70
- );
71
-
72
59
  RegisterableStorage storage $ = _getRegisterableStorage();
73
- $._registry = registry;
74
60
  $._parentNftId = parentNftId;
75
61
  $._objectType = objectType;
76
62
  $._isInterceptor = isInterceptor;
77
- $._initialOwner = initialOwner;// not msg.sender because called in proxy constructor where msg.sender is proxy deployer
78
63
  $._data = data;
79
64
 
80
- _registerInterface(type(Registerable).interfaceId);
81
- }
82
-
83
- // from IOwnable
84
- function getOwner() public view virtual returns (address) {
85
- return _getRegisterableStorage()._registry.ownerOf(address(this));
65
+ _registerInterface(type(IRegisterable).interfaceId);
86
66
  }
87
67
 
88
- // from IRegisterable
89
- function getRegistry() public view virtual returns (IRegistry registry) {
90
- return _getRegisterableStorage()._registry;
91
- }
92
-
93
- function getNftId() public view virtual returns (NftId nftId) {
94
- return _getRegisterableStorage()._registry.getNftId(address(this));
95
- }
96
68
 
97
69
  function getInitialInfo()
98
70
  public
@@ -103,12 +75,12 @@ abstract contract Registerable is
103
75
  RegisterableStorage storage $ = _getRegisterableStorage();
104
76
  return (
105
77
  IRegistry.ObjectInfo(
106
- zeroNftId(),
78
+ getNftId(),
107
79
  $._parentNftId,
108
80
  $._objectType,
109
81
  $._isInterceptor,
110
82
  address(this),
111
- $._initialOwner,
83
+ getOwner(),
112
84
  $._data
113
85
  ),
114
86
  bytes("")
@@ -1,8 +1,8 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
2
  pragma solidity ^0.8.20;
3
3
 
4
- import {ProxyAdmin} from "@openzeppelin5/contracts/proxy/transparent/ProxyAdmin.sol";
5
- import {TransparentUpgradeableProxy, ITransparentUpgradeableProxy} from "@openzeppelin5/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
4
+ import {ProxyAdmin} from "@openzeppelin/contracts/proxy/transparent/ProxyAdmin.sol";
5
+ import {TransparentUpgradeableProxy, ITransparentUpgradeableProxy} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
6
6
 
7
7
  import {IVersionable} from "./IVersionable.sol";
8
8
 
@@ -1,7 +1,7 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
2
  pragma solidity ^0.8.20;
3
3
 
4
- import {Initializable} from "@openzeppelin5/contracts/proxy/utils/Initializable.sol";
4
+ import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
5
5
 
6
6
  import {Blocknumber, blockNumber} from "../types/Blocknumber.sol";
7
7
  import {Timestamp, blockTimestamp} from "../types/Timestamp.sol";
@@ -1,7 +1,7 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
2
  pragma solidity ^0.8.19;
3
3
 
4
- import {UFixed, UFixedMathLib} from "../types/UFixed.sol";
4
+ import {UFixed, UFixedLib} from "../types/UFixed.sol";
5
5
  import {Fee, FeeLib} from "../types/Fee.sol";
6
6
 
7
7
  contract TestFee {
@@ -15,7 +15,7 @@ contract TestFee {
15
15
  pure
16
16
  returns(Fee memory fee)
17
17
  {
18
- return FeeLib.toFee(UFixedMathLib.toUFixed(fractionalValue, exponent), fixedValue);
18
+ return FeeLib.toFee(UFixedLib.toUFixed(fractionalValue, exponent), fixedValue);
19
19
  }
20
20
 
21
21
  function getZeroFee() external pure returns(Fee memory fee) {
@@ -1,7 +1,7 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
2
  pragma solidity ^0.8.19;
3
3
 
4
- import {UFixed, UFixedMathLib} from "./UFixed.sol";
4
+ import {UFixed, UFixedLib} from "./UFixed.sol";
5
5
 
6
6
  struct Fee {
7
7
  UFixed fractionalFee;
@@ -21,7 +21,7 @@ library FeeLib {
21
21
  uint256 netAmount
22
22
  )
23
23
  {
24
- UFixed fractionalAmount = UFixedMathLib.toUFixed(amount) *
24
+ UFixed fractionalAmount = UFixedLib.toUFixed(amount) *
25
25
  fee.fractionalFee;
26
26
  feeAmount = fractionalAmount.toInt() + fee.fixedFee;
27
27
  netAmount = amount - feeAmount;
@@ -37,7 +37,7 @@ library FeeLib {
37
37
 
38
38
  /// @dev Return the percent fee struct (x%, 0)
39
39
  function percentageFee(uint8 percent) public pure returns (Fee memory fee) {
40
- return Fee(UFixedMathLib.toUFixed(percent, -2), 0);
40
+ return Fee(UFixedLib.toUFixed(percent, -2), 0);
41
41
  }
42
42
 
43
43
  /// @dev Return a zero fee struct (0, 0)
@@ -20,6 +20,13 @@ function ORACLE_OWNER_ROLE() pure returns (RoleId) { return RoleIdLib.toRoleId("
20
20
  function POOL_OWNER_ROLE() pure returns (RoleId) { return RoleIdLib.toRoleId("PoolOwnerRole"); }
21
21
  function PRODUCT_OWNER_ROLE() pure returns (RoleId) { return RoleIdLib.toRoleId("ProductOwnerRole"); }
22
22
 
23
+ function PRODUCT_REGISTRAR_ROLE() pure returns(uint64 roleId) { return 1; }
24
+ function POOL_REGISTRAR_ROLE() pure returns(uint64 roleId) { return 2; }
25
+ function DISTRIBUTION_REGISTRAR_ROLE() pure returns(uint64 roleId) { return 3; }
26
+ function ORACLE_REGISTRAR_ROLE() pure returns(uint64 roleId) { return 4; }
27
+ function POLICY_REGISTRAR_ROLE() pure returns(uint64 roleId) { return 5; }
28
+ function BUNDLE_REGISTRAR_ROLE() pure returns(uint64 roleId) { return 6; }
29
+
23
30
  // @dev Returns true iff role ids a and b are identical
24
31
  function eqRoleId(RoleId a, RoleId b) pure returns (bool isSame) {
25
32
  return RoleId.unwrap(a) == RoleId.unwrap(b);
@@ -1,7 +1,5 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.19;
3
-
4
- import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
2
+ pragma solidity ^0.8.20;
5
3
 
6
4
  /// @dev UFixed is a fixed point number with 18 decimals precision.
7
5
  type UFixed is uint256;
@@ -16,9 +14,15 @@ using {
16
14
  ltUFixed as <,
17
15
  lteUFixed as <=,
18
16
  eqUFixed as ==,
19
- UFixedMathLib.toInt
17
+ neUFixed as !=,
18
+ UFixedLib.toInt,
19
+ UFixedLib.gtz
20
20
  } for UFixed global;
21
21
 
22
+ function zeroUFixed() pure returns (UFixed zero) {
23
+ return UFixed.wrap(0);
24
+ }
25
+
22
26
  function addUFixed(UFixed a, UFixed b) pure returns (UFixed) {
23
27
  return UFixed.wrap(UFixed.unwrap(a) + UFixed.unwrap(b));
24
28
  }
@@ -30,14 +34,14 @@ function subUFixed(UFixed a, UFixed b) pure returns (UFixed) {
30
34
 
31
35
  function mulUFixed(UFixed a, UFixed b) pure returns (UFixed) {
32
36
  return
33
- UFixed.wrap(Math.mulDiv(UFixed.unwrap(a), UFixed.unwrap(b), 10 ** 18));
37
+ UFixed.wrap(MathLib.mulDiv(UFixed.unwrap(a), UFixed.unwrap(b), 10 ** 18));
34
38
  }
35
39
 
36
40
  function divUFixed(UFixed a, UFixed b) pure returns (UFixed) {
37
41
  require(UFixed.unwrap(b) > 0, "ERROR:UFM-020:DIVISOR_ZERO");
38
42
 
39
43
  return
40
- UFixed.wrap(Math.mulDiv(UFixed.unwrap(a), 10 ** 18, UFixed.unwrap(b)));
44
+ UFixed.wrap(MathLib.mulDiv(UFixed.unwrap(a), 10 ** 18, UFixed.unwrap(b)));
41
45
  }
42
46
 
43
47
  function gtUFixed(UFixed a, UFixed b) pure returns (bool isGreaterThan) {
@@ -60,6 +64,10 @@ function eqUFixed(UFixed a, UFixed b) pure returns (bool isEqual) {
60
64
  return UFixed.unwrap(a) == UFixed.unwrap(b);
61
65
  }
62
66
 
67
+ function neUFixed(UFixed a, UFixed b) pure returns (bool isEqual) {
68
+ return UFixed.unwrap(a) != UFixed.unwrap(b);
69
+ }
70
+
63
71
  function gtzUFixed(UFixed a) pure returns (bool isZero) {
64
72
  return UFixed.unwrap(a) > 0;
65
73
  }
@@ -76,7 +84,115 @@ function deltaUFixed(UFixed a, UFixed b) pure returns (UFixed) {
76
84
  return b - a;
77
85
  }
78
86
 
79
- library UFixedMathLib {
87
+ /// @dev copied from https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.9.3/contracts/utils/math/Math.sol
88
+ library MathLib {
89
+
90
+ enum Rounding {
91
+ Down, // Toward negative infinity
92
+ Up, // Toward infinity
93
+ Zero // Toward zero
94
+ }
95
+
96
+ /**
97
+ * @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
98
+ * @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv)
99
+ * with further edits by Uniswap Labs also under MIT license.
100
+ */
101
+ function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
102
+ unchecked {
103
+ // 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use
104
+ // use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
105
+ // variables such that product = prod1 * 2^256 + prod0.
106
+ uint256 prod0; // Least significant 256 bits of the product
107
+ uint256 prod1; // Most significant 256 bits of the product
108
+ assembly {
109
+ let mm := mulmod(x, y, not(0))
110
+ prod0 := mul(x, y)
111
+ prod1 := sub(sub(mm, prod0), lt(mm, prod0))
112
+ }
113
+
114
+ // Handle non-overflow cases, 256 by 256 division.
115
+ if (prod1 == 0) {
116
+ // Solidity will revert if denominator == 0, unlike the div opcode on its own.
117
+ // The surrounding unchecked block does not change this fact.
118
+ // See https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic.
119
+ return prod0 / denominator;
120
+ }
121
+
122
+ // Make sure the result is less than 2^256. Also prevents denominator == 0.
123
+ require(denominator > prod1, "Math: mulDiv overflow");
124
+
125
+ ///////////////////////////////////////////////
126
+ // 512 by 256 division.
127
+ ///////////////////////////////////////////////
128
+
129
+ // Make division exact by subtracting the remainder from [prod1 prod0].
130
+ uint256 remainder;
131
+ assembly {
132
+ // Compute remainder using mulmod.
133
+ remainder := mulmod(x, y, denominator)
134
+
135
+ // Subtract 256 bit number from 512 bit number.
136
+ prod1 := sub(prod1, gt(remainder, prod0))
137
+ prod0 := sub(prod0, remainder)
138
+ }
139
+
140
+ // Factor powers of two out of denominator and compute largest power of two divisor of denominator. Always >= 1.
141
+ // See https://cs.stackexchange.com/q/138556/92363.
142
+
143
+ // Does not overflow because the denominator cannot be zero at this stage in the function.
144
+ uint256 twos = denominator & (~denominator + 1);
145
+ assembly {
146
+ // Divide denominator by twos.
147
+ denominator := div(denominator, twos)
148
+
149
+ // Divide [prod1 prod0] by twos.
150
+ prod0 := div(prod0, twos)
151
+
152
+ // Flip twos such that it is 2^256 / twos. If twos is zero, then it becomes one.
153
+ twos := add(div(sub(0, twos), twos), 1)
154
+ }
155
+
156
+ // Shift in bits from prod1 into prod0.
157
+ prod0 |= prod1 * twos;
158
+
159
+ // Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such
160
+ // that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for
161
+ // four bits. That is, denominator * inv = 1 mod 2^4.
162
+ uint256 inverse = (3 * denominator) ^ 2;
163
+
164
+ // Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also works
165
+ // in modular arithmetic, doubling the correct bits in each step.
166
+ inverse *= 2 - denominator * inverse; // inverse mod 2^8
167
+ inverse *= 2 - denominator * inverse; // inverse mod 2^16
168
+ inverse *= 2 - denominator * inverse; // inverse mod 2^32
169
+ inverse *= 2 - denominator * inverse; // inverse mod 2^64
170
+ inverse *= 2 - denominator * inverse; // inverse mod 2^128
171
+ inverse *= 2 - denominator * inverse; // inverse mod 2^256
172
+
173
+ // Because the division is now exact we can divide by multiplying with the modular inverse of denominator.
174
+ // This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is
175
+ // less than 2^256, this is the final result. We don't need to compute the high bits of the result and prod1
176
+ // is no longer required.
177
+ result = prod0 * inverse;
178
+ return result;
179
+ }
180
+ }
181
+
182
+ /**
183
+ * @notice Calculates x * y / denominator with full precision, following the selected rounding direction.
184
+ */
185
+ function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
186
+ uint256 result = mulDiv(x, y, denominator);
187
+ if (rounding == Rounding.Up && mulmod(x, y, denominator) > 0) {
188
+ result += 1;
189
+ }
190
+ return result;
191
+ }
192
+
193
+ }
194
+
195
+ library UFixedLib {
80
196
  int8 public constant EXP = 18;
81
197
  uint256 public constant MULTIPLIER = 10 ** uint256(int256(EXP));
82
198
  uint256 public constant MULTIPLIER_HALF = MULTIPLIER / 2;
@@ -123,23 +239,23 @@ library UFixedMathLib {
123
239
  function toIntWithRounding(UFixed a, uint8 rounding) public pure returns (uint256) {
124
240
  if (rounding == ROUNDING_HALF_UP()) {
125
241
  return
126
- Math.mulDiv(
242
+ MathLib.mulDiv(
127
243
  UFixed.unwrap(a) + MULTIPLIER_HALF,
128
244
  1,
129
245
  MULTIPLIER,
130
- Math.Rounding.Down
246
+ MathLib.Rounding.Down
131
247
  );
132
248
  } else if (rounding == ROUNDING_DOWN()) {
133
249
  return
134
- Math.mulDiv(
250
+ MathLib.mulDiv(
135
251
  UFixed.unwrap(a),
136
252
  1,
137
253
  MULTIPLIER,
138
- Math.Rounding.Down
254
+ MathLib.Rounding.Down
139
255
  );
140
256
  } else {
141
257
  return
142
- Math.mulDiv(UFixed.unwrap(a), 1, MULTIPLIER, Math.Rounding.Up);
258
+ MathLib.mulDiv(UFixed.unwrap(a), 1, MULTIPLIER, MathLib.Rounding.Up);
143
259
  }
144
260
  }
145
261