@etherisc/gif-next 0.0.2-fe77319 → 0.0.2-ffba655

Sign up to get free protection for your applications and to get access to all the features.
Files changed (140) hide show
  1. package/artifacts/contracts/components/Component.sol/Component.dbg.json +1 -1
  2. package/artifacts/contracts/components/Component.sol/Component.json +7 -7
  3. package/artifacts/contracts/components/Component.sol/InstanceLinked.dbg.json +1 -1
  4. package/artifacts/contracts/components/Component.sol/InstanceLinked.json +2 -2
  5. package/artifacts/contracts/components/IPool.sol/IPoolComponent.dbg.json +1 -1
  6. package/artifacts/contracts/components/IPool.sol/IPoolComponent.json +6 -6
  7. package/artifacts/contracts/components/IProduct.sol/IProductComponent.dbg.json +1 -1
  8. package/artifacts/contracts/components/IProduct.sol/IProductComponent.json +8 -8
  9. package/artifacts/contracts/components/Pool.sol/Pool.dbg.json +1 -1
  10. package/artifacts/contracts/components/Pool.sol/Pool.json +29 -11
  11. package/artifacts/contracts/components/Product.sol/Product.dbg.json +1 -1
  12. package/artifacts/contracts/components/Product.sol/Product.json +31 -13
  13. package/artifacts/contracts/experiment/errors/Require.sol/Require.dbg.json +4 -0
  14. package/artifacts/contracts/experiment/errors/Require.sol/Require.json +105 -0
  15. package/artifacts/contracts/experiment/errors/Revert.sol/Revert.dbg.json +4 -0
  16. package/artifacts/contracts/experiment/errors/Revert.sol/Revert.json +105 -0
  17. package/artifacts/contracts/experiment/inheritance/A.sol/A.dbg.json +4 -0
  18. package/artifacts/contracts/experiment/{A.sol → inheritance/A.sol}/A.json +5 -5
  19. package/artifacts/contracts/experiment/inheritance/A.sol/AShared.dbg.json +4 -0
  20. package/artifacts/contracts/experiment/{A.sol → inheritance/A.sol}/AShared.json +3 -3
  21. package/artifacts/contracts/experiment/inheritance/B.sol/B.dbg.json +4 -0
  22. package/artifacts/contracts/experiment/{B.sol → inheritance/B.sol}/B.json +2 -2
  23. package/artifacts/contracts/experiment/inheritance/C.sol/C.dbg.json +4 -0
  24. package/artifacts/contracts/experiment/{C.sol → inheritance/C.sol}/C.json +2 -2
  25. package/artifacts/contracts/experiment/inheritance/IA.sol/IA.dbg.json +4 -0
  26. package/artifacts/contracts/experiment/{IA.sol → inheritance/IA.sol}/IA.json +1 -1
  27. package/artifacts/contracts/experiment/inheritance/IA.sol/ISharedA.dbg.json +4 -0
  28. package/artifacts/contracts/experiment/{IA.sol → inheritance/IA.sol}/ISharedA.json +1 -1
  29. package/artifacts/contracts/experiment/inheritance/IB.sol/IB.dbg.json +4 -0
  30. package/artifacts/contracts/experiment/{IB.sol → inheritance/IB.sol}/IB.json +1 -1
  31. package/artifacts/contracts/experiment/inheritance/IC.sol/IC.dbg.json +4 -0
  32. package/artifacts/contracts/experiment/{IC.sol → inheritance/IC.sol}/IC.json +1 -1
  33. package/artifacts/contracts/experiment/types/TypeA.sol/TypeALib.dbg.json +4 -0
  34. package/artifacts/contracts/experiment/types/TypeA.sol/TypeALib.json +10 -0
  35. package/artifacts/contracts/experiment/types/TypeB.sol/TypeBLib.dbg.json +4 -0
  36. package/artifacts/contracts/experiment/types/TypeB.sol/TypeBLib.json +10 -0
  37. package/artifacts/contracts/instance/IInstance.sol/IInstance.dbg.json +1 -1
  38. package/artifacts/contracts/instance/IInstance.sol/IInstance.json +60 -60
  39. package/artifacts/contracts/instance/Instance.sol/Instance.dbg.json +1 -1
  40. package/artifacts/contracts/instance/Instance.sol/Instance.json +122 -64
  41. package/artifacts/contracts/instance/access/Access.sol/AccessModule.dbg.json +1 -1
  42. package/artifacts/contracts/instance/access/IAccess.sol/IAccess.dbg.json +1 -1
  43. package/artifacts/contracts/instance/access/IAccess.sol/IAccessCheckRole.dbg.json +1 -1
  44. package/artifacts/contracts/instance/access/IAccess.sol/IAccessComponentTypeRoles.dbg.json +1 -1
  45. package/artifacts/contracts/instance/access/IAccess.sol/IAccessModule.dbg.json +1 -1
  46. package/artifacts/contracts/instance/component/ComponentModule.sol/ComponentModule.dbg.json +1 -1
  47. package/artifacts/contracts/instance/component/ComponentModule.sol/ComponentModule.json +24 -24
  48. package/artifacts/contracts/instance/component/ComponentModule.sol/ComponentOwnerService.dbg.json +1 -1
  49. package/artifacts/contracts/instance/component/ComponentModule.sol/ComponentOwnerService.json +48 -6
  50. package/artifacts/contracts/instance/component/IComponent.sol/IComponent.dbg.json +1 -1
  51. package/artifacts/contracts/instance/component/IComponent.sol/IComponentContract.dbg.json +1 -1
  52. package/artifacts/contracts/instance/component/IComponent.sol/IComponentContract.json +6 -6
  53. package/artifacts/contracts/instance/component/IComponent.sol/IComponentModule.dbg.json +1 -1
  54. package/artifacts/contracts/instance/component/IComponent.sol/IComponentModule.json +26 -26
  55. package/artifacts/contracts/instance/component/IComponent.sol/IComponentOwnerService.dbg.json +1 -1
  56. package/artifacts/contracts/instance/component/IComponent.sol/IComponentOwnerService.json +3 -3
  57. package/artifacts/contracts/instance/component/IComponent.sol/IInstanceLinked.dbg.json +1 -1
  58. package/artifacts/contracts/instance/policy/IPolicy.sol/IPolicy.dbg.json +1 -1
  59. package/artifacts/contracts/instance/policy/IPolicy.sol/IPolicyModule.dbg.json +1 -1
  60. package/artifacts/contracts/instance/policy/IPolicy.sol/IPolicyModule.json +18 -18
  61. package/artifacts/contracts/instance/policy/PolicyModule.sol/PolicyModule.dbg.json +1 -1
  62. package/artifacts/contracts/instance/policy/PolicyModule.sol/PolicyModule.json +18 -18
  63. package/artifacts/contracts/instance/pool/IPoolModule.sol/IPool.dbg.json +1 -1
  64. package/artifacts/contracts/instance/pool/IPoolModule.sol/IPoolModule.dbg.json +1 -1
  65. package/artifacts/contracts/instance/pool/IPoolModule.sol/IPoolModule.json +10 -10
  66. package/artifacts/contracts/instance/pool/PoolModule.sol/PoolModule.dbg.json +1 -1
  67. package/artifacts/contracts/instance/pool/PoolModule.sol/PoolModule.json +10 -10
  68. package/artifacts/contracts/instance/product/IProductService.sol/IProductModule.dbg.json +1 -1
  69. package/artifacts/contracts/instance/product/IProductService.sol/IProductService.dbg.json +1 -1
  70. package/artifacts/contracts/instance/product/IProductService.sol/IProductService.json +8 -8
  71. package/artifacts/contracts/instance/product/ProductService.sol/ProductModule.dbg.json +1 -1
  72. package/artifacts/contracts/instance/product/ProductService.sol/ProductService.dbg.json +1 -1
  73. package/artifacts/contracts/instance/product/ProductService.sol/ProductService.json +54 -12
  74. package/artifacts/contracts/registry/ChainNft.sol/ChainNft.dbg.json +4 -0
  75. package/artifacts/contracts/registry/ChainNft.sol/ChainNft.json +534 -0
  76. package/artifacts/contracts/registry/IChainNft.sol/IChainNft.dbg.json +4 -0
  77. package/artifacts/contracts/registry/IChainNft.sol/IChainNft.json +452 -0
  78. package/artifacts/contracts/registry/IRegistry.sol/IOwnable.dbg.json +1 -1
  79. package/artifacts/contracts/registry/IRegistry.sol/IRegisterable.dbg.json +1 -1
  80. package/artifacts/contracts/registry/IRegistry.sol/IRegisterable.json +6 -6
  81. package/artifacts/contracts/registry/IRegistry.sol/IRegistry.dbg.json +1 -1
  82. package/artifacts/contracts/registry/IRegistry.sol/IRegistry.json +41 -33
  83. package/artifacts/contracts/registry/IRegistry.sol/IRegistryLinked.dbg.json +1 -1
  84. package/artifacts/contracts/registry/Registry.sol/Registerable.dbg.json +1 -1
  85. package/artifacts/contracts/registry/Registry.sol/Registerable.json +7 -7
  86. package/artifacts/contracts/registry/Registry.sol/Registry.dbg.json +1 -1
  87. package/artifacts/contracts/registry/Registry.sol/Registry.json +100 -42
  88. package/artifacts/contracts/registry/Registry.sol/RegistryLinked.dbg.json +1 -1
  89. package/artifacts/contracts/registry/Registry.sol/RegistryLinked.json +2 -2
  90. package/artifacts/contracts/types/Blocknumber.sol/BlocknumberLib.dbg.json +4 -0
  91. package/artifacts/contracts/types/Blocknumber.sol/BlocknumberLib.json +174 -0
  92. package/artifacts/contracts/types/ChainId.sol/ChainIdLib.dbg.json +4 -0
  93. package/artifacts/contracts/types/ChainId.sol/ChainIdLib.json +10 -0
  94. package/artifacts/contracts/types/NftId.sol/NftIdLib.dbg.json +4 -0
  95. package/artifacts/contracts/types/NftId.sol/NftIdLib.json +92 -0
  96. package/artifacts/contracts/types/Timestamp.sol/TimestampLib.dbg.json +4 -0
  97. package/artifacts/contracts/types/Timestamp.sol/TimestampLib.json +174 -0
  98. package/artifacts/contracts/types/UFixed.sol/UFixedMathLib.dbg.json +4 -0
  99. package/artifacts/contracts/types/UFixed.sol/UFixedMathLib.json +453 -0
  100. package/contracts/components/Component.sol +3 -3
  101. package/contracts/components/IProduct.sol +2 -1
  102. package/contracts/components/Product.sol +5 -4
  103. package/contracts/experiment/errors/Require.sol +33 -0
  104. package/contracts/experiment/errors/Revert.sol +39 -0
  105. package/contracts/experiment/{B.sol → inheritance/B.sol} +1 -1
  106. package/contracts/experiment/{C.sol → inheritance/C.sol} +1 -1
  107. package/contracts/experiment/types/TypeA.sol +42 -0
  108. package/contracts/experiment/types/TypeB.sol +24 -0
  109. package/contracts/instance/IInstance.sol +3 -3
  110. package/contracts/instance/Instance.sol +4 -3
  111. package/contracts/instance/component/ComponentModule.sol +32 -43
  112. package/contracts/instance/component/IComponent.sol +11 -10
  113. package/contracts/instance/policy/IPolicy.sol +8 -8
  114. package/contracts/instance/policy/PolicyModule.sol +12 -11
  115. package/contracts/instance/pool/IPoolModule.sol +6 -5
  116. package/contracts/instance/pool/PoolModule.sol +9 -6
  117. package/contracts/instance/product/IProductService.sol +5 -4
  118. package/contracts/instance/product/ProductService.sol +14 -11
  119. package/contracts/registry/ChainNft.sol +173 -0
  120. package/contracts/registry/IChainNft.sol +18 -0
  121. package/contracts/registry/IRegistry.sol +16 -12
  122. package/contracts/registry/Registry.sol +43 -58
  123. package/contracts/types/Blocknumber.sol +60 -0
  124. package/contracts/types/ChainId.sol +30 -0
  125. package/contracts/types/NftId.sol +35 -0
  126. package/contracts/types/Timestamp.sol +55 -0
  127. package/contracts/types/UFixed.sol +211 -0
  128. package/package.json +1 -1
  129. package/artifacts/contracts/experiment/A.sol/A.dbg.json +0 -4
  130. package/artifacts/contracts/experiment/A.sol/AShared.dbg.json +0 -4
  131. package/artifacts/contracts/experiment/B.sol/B.dbg.json +0 -4
  132. package/artifacts/contracts/experiment/C.sol/C.dbg.json +0 -4
  133. package/artifacts/contracts/experiment/IA.sol/IA.dbg.json +0 -4
  134. package/artifacts/contracts/experiment/IA.sol/ISharedA.dbg.json +0 -4
  135. package/artifacts/contracts/experiment/IB.sol/IB.dbg.json +0 -4
  136. package/artifacts/contracts/experiment/IC.sol/IC.dbg.json +0 -4
  137. /package/contracts/experiment/{A.sol → inheritance/A.sol} +0 -0
  138. /package/contracts/experiment/{IA.sol → inheritance/IA.sol} +0 -0
  139. /package/contracts/experiment/{IB.sol → inheritance/IB.sol} +0 -0
  140. /package/contracts/experiment/{IC.sol → inheritance/IC.sol} +0 -0
@@ -11,12 +11,15 @@ import {RegistryLinked} from "../../registry/Registry.sol";
11
11
  import {IProductService, IProductModule} from "./IProductService.sol";
12
12
  import {IComponentModule} from "../../instance/component/IComponent.sol";
13
13
  import {IPoolModule} from "../../instance/pool/IPoolModule.sol";
14
+ import {NftId, NftIdLib} from "../../types/NftId.sol";
14
15
 
15
16
  // TODO or name this ProtectionService to have Product be something more generic (loan, savings account, ...)
16
17
  contract ProductService is
17
18
  RegistryLinked,
18
19
  IProductService
19
20
  {
21
+ using NftIdLib for NftId;
22
+
20
23
  constructor(address registry)
21
24
  RegistryLinked(registry)
22
25
  { }
@@ -27,20 +30,20 @@ contract ProductService is
27
30
  uint256 sumInsuredAmount,
28
31
  uint256 premiumAmount,
29
32
  uint256 lifetime,
30
- uint256 bundleNftId
33
+ NftId bundleNftId
31
34
  )
32
35
  external
33
36
  override
34
- returns(uint256 nftId)
37
+ returns(NftId nftId)
35
38
  {
36
39
  // same as only registered product
37
- uint256 productNftId = _registry.getNftId(msg.sender);
38
- require(productNftId > 0, "ERROR_PRODUCT_UNKNOWN");
40
+ NftId productNftId = _registry.getNftId(msg.sender);
41
+ require(productNftId.gtz(), "ERROR_PRODUCT_UNKNOWN");
39
42
  IRegistry.RegistryInfo memory productInfo = _registry.getInfo(productNftId);
40
43
  require(productInfo.objectType == _registry.PRODUCT(), "ERROR_NOT_PRODUCT");
41
44
 
42
45
  IRegistry.RegistryInfo memory instanceInfo = _registry.getInfo(productInfo.parentNftId);
43
- require(instanceInfo.nftId > 0, "ERROR_INSTANCE_UNKNOWN");
46
+ require(instanceInfo.nftId.gtz(), "ERROR_INSTANCE_UNKNOWN");
44
47
  require(instanceInfo.objectType == _registry.INSTANCE(), "ERROR_NOT_INSTANCE");
45
48
 
46
49
  IPolicyModule policyModule = IPolicyModule(instanceInfo.objectAddress);
@@ -55,23 +58,23 @@ contract ProductService is
55
58
  // add logging
56
59
  }
57
60
 
58
- function underwrite(uint256 nftId)
61
+ function underwrite(NftId nftId)
59
62
  external
60
63
  override
61
64
  {
62
65
  // same as only registered product
63
- uint256 productNftId = _registry.getNftId(msg.sender);
64
- require(productNftId > 0, "ERROR_PRODUCT_UNKNOWN");
66
+ NftId productNftId = _registry.getNftId(msg.sender);
67
+ require(productNftId.gtz(), "ERROR_PRODUCT_UNKNOWN");
65
68
  IRegistry.RegistryInfo memory productInfo = _registry.getInfo(productNftId);
66
69
  require(productInfo.objectType == _registry.PRODUCT(), "ERROR_NOT_PRODUCT");
67
70
 
68
71
  IRegistry.RegistryInfo memory instanceInfo = _registry.getInfo(productInfo.parentNftId);
69
- require(instanceInfo.nftId > 0, "ERROR_INSTANCE_UNKNOWN");
72
+ require(instanceInfo.nftId.gtz(), "ERROR_INSTANCE_UNKNOWN");
70
73
  require(instanceInfo.objectType == _registry.INSTANCE(), "ERROR_NOT_INSTANCE");
71
74
 
72
75
  // get responsible pool
73
76
  IComponentModule componentModule = IComponentModule(instanceInfo.objectAddress);
74
- uint256 poolNftId = componentModule.getPoolNftId(productNftId);
77
+ NftId poolNftId = componentModule.getPoolNftId(productNftId);
75
78
 
76
79
  // lock capital (and update pool accounting)
77
80
  IPoolModule poolModule = IPoolModule(instanceInfo.objectAddress);
@@ -86,7 +89,7 @@ contract ProductService is
86
89
  // add logging
87
90
  }
88
91
 
89
- function close(uint256 nftId) external override {}
92
+ function close(NftId nftId) external override {}
90
93
  }
91
94
 
92
95
  abstract contract ProductModule is
@@ -0,0 +1,173 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.19;
3
+
4
+ import {ERC721, ERC721Enumerable} from "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
5
+ import {IChainNft} from "./IChainNft.sol";
6
+
7
+ contract ChainNft is
8
+ ERC721Enumerable,
9
+ IChainNft
10
+ {
11
+ string public constant NAME = "Dezentralized Insurance Protocol Registry";
12
+ string public constant SYMBOL = "DIPR";
13
+
14
+ // remember token uri
15
+ mapping(uint256 tokenId => string uri) private _uri;
16
+
17
+ // remember registry
18
+ address private _registry;
19
+
20
+ // only used for _getNextTokenId
21
+ uint256 internal _chainIdInt;
22
+ uint256 internal _chainIdDigits;
23
+ uint256 internal _chainIdMultiplier;
24
+ uint256 internal _idNext;
25
+ uint256 internal _totalMinted;
26
+
27
+
28
+ modifier onlyRegistry() {
29
+ require(msg.sender == _registry, "ERROR:NFT-001:CALLER_NOT_REGISTRY");
30
+ _;
31
+ }
32
+
33
+
34
+ constructor(address registry)
35
+ ERC721(NAME, SYMBOL)
36
+ {
37
+ require(registry != address(0), "ERROR:NFT-010:REGISTRY_ZERO");
38
+
39
+ _registry = registry;
40
+
41
+ _chainIdInt = block.chainid;
42
+ _chainIdDigits = _countDigits(_chainIdInt);
43
+ _chainIdMultiplier = 10 ** _chainIdDigits;
44
+
45
+ // on mainnet/goerli start /1 (reserved for protocol nft) on other chains with 2
46
+ if(block.chainid == 1 || block.chainid == 5) {
47
+ _idNext = 1;
48
+ } else {
49
+ _idNext = 2;
50
+ }
51
+ }
52
+
53
+
54
+ function mint(
55
+ address to,
56
+ string memory uri
57
+ )
58
+ external
59
+ override
60
+ onlyRegistry
61
+ returns(uint256 tokenId)
62
+ {
63
+ tokenId = _getNextTokenId();
64
+ _totalMinted++;
65
+
66
+ _safeMint(to, tokenId);
67
+
68
+ if(bytes(uri).length > 0) {
69
+ _uri[tokenId] = uri;
70
+ }
71
+ }
72
+
73
+
74
+ function burn(uint256 tokenId)
75
+ external
76
+ override
77
+ onlyRegistry
78
+ {
79
+ _requireMinted(tokenId);
80
+ _burn(tokenId);
81
+ delete _uri[tokenId];
82
+ }
83
+
84
+
85
+ function setURI(uint256 tokenId, string memory uri)
86
+ external
87
+ override
88
+ onlyRegistry
89
+ {
90
+ require(bytes(uri).length > 0, "ERROR:CRG-011:URI_EMPTY");
91
+
92
+ _requireMinted(tokenId);
93
+ _uri[tokenId] = uri;
94
+ }
95
+
96
+
97
+ function exists(uint256 tokenId)
98
+ external
99
+ view
100
+ override
101
+ returns(bool)
102
+ {
103
+ return _exists(tokenId);
104
+ }
105
+
106
+
107
+ function tokenURI(uint256 tokenId)
108
+ public
109
+ view
110
+ override
111
+ returns(string memory)
112
+ {
113
+ _requireMinted(tokenId);
114
+ return _uri[tokenId];
115
+ }
116
+
117
+
118
+ function getRegistryAddress()
119
+ external
120
+ view
121
+ override
122
+ returns(address)
123
+ {
124
+ return _registry;
125
+ }
126
+
127
+ function totalMinted() external override view returns(uint256) {
128
+ return _totalMinted;
129
+ }
130
+
131
+ // requirement: each chain registry produces token ids that
132
+ // are guaranteed to not collide with any token id genereated
133
+ // on a different chain
134
+ //
135
+ // format concat(counter,chainid,2 digits for len-of-chain-id)
136
+ // restriction chainid up to 99 digits
137
+ // decode: from right to left:
138
+ // - 2 right most digits encode length of chainid
139
+ // - move number of digits to left as determined above (-> chainid)
140
+ // - the reminder to the left is the counter
141
+ // examples
142
+ // 1101
143
+ // ^^ ^
144
+ // || +- 1-digit chain id
145
+ // |+-- chain id = 1 (mainnet)
146
+ // +-- 1st token id on mainnet
147
+ // (1 * 10 ** 1 + 1) * 100 + 1
148
+ // 42987654321010
149
+ // ^ ^ ^
150
+ // | | +- 10-digit chain id
151
+ // | +-- chain id = 9876543210 (hypothetical chainid)
152
+ // +-- 42nd token id on this chain
153
+ // (42 * 10 ** 10 + 9876543210) * 100 + 10
154
+ // (index * 10 ** digits + chainid) * 100 + digits (1 < digits < 100)
155
+
156
+ function _getNextTokenId() private returns(uint256 id) {
157
+ id = (_idNext * _chainIdMultiplier + _chainIdInt) * 100 + _chainIdDigits;
158
+ _idNext++;
159
+ }
160
+
161
+
162
+ function _countDigits(uint256 num)
163
+ private
164
+ pure
165
+ returns (uint256 count)
166
+ {
167
+ count = 0;
168
+ while (num != 0) {
169
+ count++;
170
+ num /= 10;
171
+ }
172
+ }
173
+ }
@@ -0,0 +1,18 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.19;
3
+
4
+ import {IERC721Enumerable} from "@openzeppelin/contracts/token/ERC721/extensions/IERC721Enumerable.sol";
5
+
6
+ interface IChainNft is
7
+ IERC721Enumerable
8
+ {
9
+
10
+ function mint(address to, string memory uri) external returns(uint256 tokenId);
11
+ function burn(uint256 tokenId) external;
12
+ function setURI(uint256 tokenId, string memory uri) external;
13
+
14
+ function exists(uint256 tokenId) external view returns(bool);
15
+ function totalMinted() external view returns(uint256);
16
+
17
+ function getRegistryAddress() external view returns(address registry);
18
+ }
@@ -1,6 +1,8 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
2
  pragma solidity ^0.8.19;
3
3
 
4
+ import {NftId} from "../types/NftId.sol";
5
+
4
6
  interface IOwnable {
5
7
  function getOwner() external view returns(address owner);
6
8
  }
@@ -17,10 +19,10 @@ interface IRegisterable is
17
19
  IRegistryLinked
18
20
  {
19
21
 
20
- function register() external returns(uint256 nftId);
22
+ function register() external returns(NftId nftId);
21
23
 
22
- function getNftId() external view returns(uint256 nftId);
23
- function getParentNftId() external view returns(uint256 parentNftId);
24
+ function getNftId() external view returns(NftId nftId);
25
+ function getParentNftId() external view returns(NftId parentNftId);
24
26
  function getType() external view returns(uint256 objectType);
25
27
  function getData() external view returns(bytes memory data);
26
28
  function isRegisterable() external pure returns(bool);
@@ -33,8 +35,8 @@ interface IRegisterable is
33
35
  interface IRegistry {
34
36
 
35
37
  struct RegistryInfo {
36
- uint256 nftId;
37
- uint256 parentNftId;
38
+ NftId nftId;
39
+ NftId parentNftId;
38
40
  uint256 objectType;
39
41
  address objectAddress;
40
42
  address initialOwner;
@@ -48,19 +50,21 @@ interface IRegistry {
48
50
  function POLICY() external pure returns(uint256);
49
51
  function BUNDLE() external pure returns(uint256);
50
52
 
51
- function register(address objectAddress) external returns(uint256 nftId);
53
+ function register(address objectAddress) external returns(NftId nftId);
52
54
  function registerObjectForInstance(
53
- uint256 parentNftid,
55
+ NftId parentNftid,
54
56
  uint256 objectType,
55
57
  address initialOwner
56
58
  )
57
- external returns(uint256 nftId);
59
+ external returns(NftId nftId);
58
60
 
59
- function transfer(uint256 nftId, address newOwner) external;
61
+ function getObjectCount() external view returns(uint256);
60
62
 
61
- function getNftId(address objectAddress) external view returns(uint256 nftId);
62
- function getInfo(uint256 nftId) external view returns(RegistryInfo memory info);
63
- function getOwner(uint256 nftId) external view returns(address ownerAddress);
63
+ function getNftId(address objectAddress) external view returns(NftId nftId);
64
+ function getInfo(NftId nftId) external view returns(RegistryInfo memory info);
65
+ function getOwner(NftId nftId) external view returns(address ownerAddress);
64
66
 
65
67
  function isRegistered(address objectAddress) external view returns(bool);
68
+
69
+ function getNftAddress() external view returns(address nft);
66
70
  }
@@ -1,7 +1,10 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
2
  pragma solidity ^0.8.19;
3
3
 
4
+ import {IChainNft} from "./IChainNft.sol";
4
5
  import {IRegistry, IRegistryLinked, IRegisterable} from "./IRegistry.sol";
6
+ import {NftId, toNftId} from "../types/NftId.sol";
7
+ import {NftIdLib} from "../types/NftId.sol";
5
8
 
6
9
  contract RegistryLinked is IRegistryLinked {
7
10
 
@@ -11,11 +14,6 @@ contract RegistryLinked is IRegistryLinked {
11
14
  _registry = IRegistry(registry);
12
15
  }
13
16
 
14
- // function setRegistry(address registry) public override {
15
- // require(address(_registry) == address(0), "ERROR:RGL-001:REGISTRY_ALREADY_SET");
16
- // _registry = IRegistry(registry);
17
- // }
18
-
19
17
  function getRegistry() external view override returns(IRegistry registry) {
20
18
  return _registry;
21
19
  }
@@ -27,6 +25,7 @@ abstract contract Registerable is
27
25
  RegistryLinked,
28
26
  IRegisterable
29
27
  {
28
+ using NftIdLib for NftId;
30
29
 
31
30
  address private _initialOwner;
32
31
 
@@ -47,39 +46,35 @@ abstract contract Registerable is
47
46
  }
48
47
 
49
48
  function isRegistered() public view override returns(bool) {
50
- return _registry.getNftId(address(this)) > 0;
49
+ NftId nftId = _registry.getNftId(address(this));
50
+ return nftId.gtz();
51
51
  }
52
52
 
53
- function getNftId() public view override returns(uint256 id) {
53
+ function getNftId() public view override returns(NftId nftId) {
54
54
  return _registry.getNftId(address(this));
55
55
  }
56
56
 
57
57
  function getOwner() public view override returns(address owner) {
58
- uint256 id = _registry.getNftId(address(this));
58
+ NftId id = _registry.getNftId(address(this));
59
59
  owner = _registry.getOwner(id);
60
60
  return owner != address(0) ? owner : _initialOwner;
61
61
  }
62
62
 
63
63
  }
64
64
 
65
- // struct RegistryInfo {
66
- // bytes32 id;
67
- // uint256 objectType;
68
- // address objectAddress;
69
- // address initialOwner;
70
- // }
71
-
72
65
  contract Registry is IRegistry {
66
+ using NftIdLib for NftId;
67
+ string public constant EMPTY_URI = "";
73
68
 
74
- mapping(uint256 id => RegistryInfo info) private _info;
75
- mapping(uint256 id => address owner) private _owner;
76
- mapping(address object => uint256 id) private _idByAddress;
77
- uint256 [] private _ids;
78
- uint256 private _idNext;
69
+ mapping(NftId nftId => RegistryInfo info) private _info;
70
+ mapping(NftId nftId => address owner) private _owner;
71
+ mapping(address object => NftId nftId) private _nftIdByAddress;
79
72
 
73
+ IChainNft private _chainNft;
80
74
 
81
- constructor() {
82
- _idNext = 0;
75
+ function initialize(address chainNft) external {
76
+ require(address(_chainNft) == address(0), "ERROR:REG-001:ALREADY_INITIALIZED");
77
+ _chainNft = IChainNft(chainNft);
83
78
  }
84
79
 
85
80
  function TOKEN() public pure override returns(uint256) { return 30; }
@@ -90,21 +85,24 @@ contract Registry is IRegistry {
90
85
  function POLICY() public pure override returns(uint256) { return 80; }
91
86
  function BUNDLE() public pure override returns(uint256) { return 90; }
92
87
 
93
- function register(address objectAddress) external override returns(uint256 nftId) {
94
- require(_idByAddress[objectAddress] == 0, "ERROR:REG-001:ALREADY_REGISTERED");
88
+ function register(address objectAddress) external override returns(NftId nftId) {
89
+ require(_nftIdByAddress[objectAddress].eqz(), "ERROR:REG-002:ALREADY_REGISTERED");
95
90
 
96
91
  IRegisterable registerable = IRegisterable(objectAddress);
97
- require(registerable.isRegisterable(), "ERROR:REG-002:NOT_REGISTERABLE");
92
+ require(registerable.isRegisterable(), "ERROR:REG-003:NOT_REGISTERABLE");
98
93
 
99
94
  // check parent exists (for objects not instances)
100
95
  if(registerable.getType() != INSTANCE()) {
101
96
  RegistryInfo memory parentInfo = _info[registerable.getParentNftId()];
102
- require(parentInfo.nftId > 0, "ERROR:REG-003:PARENT_NOT_FOUND");
97
+ require(parentInfo.nftId.gtz(), "ERROR:REG-004:PARENT_NOT_FOUND");
103
98
  // check validity of parent relation, valid relations are
104
99
  // policy -> product, bundle -> pool, product -> instance, pool -> instance
105
100
  }
106
101
 
107
- nftId = _mint(registerable.getInitialOwner());
102
+ uint256 mintedTokenId = _chainNft.mint(
103
+ registerable.getInitialOwner(),
104
+ EMPTY_URI);
105
+ nftId = toNftId(mintedTokenId);
108
106
 
109
107
  RegistryInfo memory info = RegistryInfo(
110
108
  nftId,
@@ -115,28 +113,31 @@ contract Registry is IRegistry {
115
113
  );
116
114
 
117
115
  _info[nftId] = info;
118
- _idByAddress[objectAddress] = nftId;
116
+ _nftIdByAddress[objectAddress] = nftId;
119
117
 
120
118
  // add logging
121
119
  }
122
120
 
123
121
 
124
122
  function registerObjectForInstance(
125
- uint256 parentNftId,
123
+ NftId parentNftId,
126
124
  uint256 objectType,
127
125
  address initialOwner
128
126
  )
129
127
  external
130
128
  override
131
129
  // TODO add onlyRegisteredInstance
132
- returns(uint256 nftId)
130
+ returns(NftId nftId)
133
131
  {
134
132
  // TODO add more validation
135
133
  require(
136
134
  objectType == POLICY() || objectType == BUNDLE(),
137
135
  "ERROR:REG-005:TYPE_INVALID");
138
136
 
139
- nftId = _mint(initialOwner);
137
+ uint256 mintedTokenId = _chainNft.mint(
138
+ initialOwner,
139
+ EMPTY_URI);
140
+ nftId = toNftId(mintedTokenId);
140
141
 
141
142
  RegistryInfo memory info = RegistryInfo(
142
143
  nftId,
@@ -152,46 +153,30 @@ contract Registry is IRegistry {
152
153
  }
153
154
 
154
155
 
155
- function transfer(uint256 id, address newOwner) external {
156
- require(msg.sender == _owner[id], "ERROR:REG-010:NOT_OWNER");
157
- _owner[id] = newOwner;
158
-
159
- // TODO logging
156
+ function getObjectCount() external view override returns(uint256) {
157
+ return _chainNft.totalSupply();
160
158
  }
161
159
 
162
160
 
163
- function getObjectCount() external view returns(uint256) {
164
- return _ids.length;
165
- }
166
-
167
-
168
- function getNftId(address object) external view override returns(uint256 id) {
169
- return _idByAddress[object];
161
+ function getNftId(address object) external view override returns(NftId id) {
162
+ return _nftIdByAddress[object];
170
163
  }
171
164
 
172
165
 
173
166
  function isRegistered(address object) external view override returns(bool) {
174
- return _idByAddress[object] > 0;
167
+ return _nftIdByAddress[object].gtz();
175
168
  }
176
169
 
177
170
 
178
- function getInfo(uint256 id) external view override returns(RegistryInfo memory info) {
179
- return _info[id];
171
+ function getInfo(NftId nftId) external view override returns(RegistryInfo memory info) {
172
+ return _info[nftId];
180
173
  }
181
174
 
182
- function getOwner(uint256 id) external view override returns(address) {
183
- return _owner[id];
175
+ function getOwner(NftId nftId) external view override returns(address) {
176
+ return _chainNft.ownerOf(nftId.toInt());
184
177
  }
185
178
 
186
-
187
- function _mint(address initialOwner)
188
- internal
189
- returns(uint256 id)
190
- {
191
- _idNext++;
192
-
193
- id = _idNext;
194
- _owner[id] = initialOwner;
195
- _ids.push(id);
179
+ function getNftAddress() external view override returns(address nft) {
180
+ return address(_chainNft);
196
181
  }
197
182
  }
@@ -0,0 +1,60 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.19;
3
+
4
+ type Blocknumber is uint32;
5
+
6
+ using {
7
+ gtBlocknumber as >,
8
+ gteBlocknumber as >=,
9
+ ltBlocknumber as <,
10
+ lteBlocknumber as <=,
11
+ eqBlocknumber as ==,
12
+ neBlocknumber as !=,
13
+ BlocknumberLib.toInt
14
+ } for Blocknumber global;
15
+
16
+ /// @dev return true if Blocknumber a is greater than Blocknumber b
17
+ function gtBlocknumber(Blocknumber a, Blocknumber b) pure returns(bool) { return Blocknumber.unwrap(a) > Blocknumber.unwrap(b); }
18
+ /// @dev return true if Blocknumber a is greater than or equal to Blocknumber b
19
+ function gteBlocknumber(Blocknumber a, Blocknumber b) pure returns(bool) { return Blocknumber.unwrap(a) >= Blocknumber.unwrap(b); }
20
+
21
+ /// @dev return true if Blocknumber a is less than Blocknumber b
22
+ function ltBlocknumber(Blocknumber a, Blocknumber b) pure returns(bool) { return Blocknumber.unwrap(a) < Blocknumber.unwrap(b); }
23
+ /// @dev return true if Blocknumber a is less than or equal to Blocknumber b
24
+ function lteBlocknumber(Blocknumber a, Blocknumber b) pure returns(bool) { return Blocknumber.unwrap(a) <= Blocknumber.unwrap(b); }
25
+
26
+ /// @dev return true if Blocknumber a is equal to Blocknumber b
27
+ function eqBlocknumber(Blocknumber a, Blocknumber b) pure returns(bool) { return Blocknumber.unwrap(a) == Blocknumber.unwrap(b); }
28
+ /// @dev return true if Blocknumber a is not equal to Blocknumber b
29
+ function neBlocknumber(Blocknumber a, Blocknumber b) pure returns(bool) { return Blocknumber.unwrap(a) != Blocknumber.unwrap(b); }
30
+
31
+ /// @dev Converts the uint256 to a Blocknumber.
32
+ function toBlocknumber(uint256 blocknum) pure returns(Blocknumber) { return Blocknumber.wrap(uint32(blocknum)); }
33
+
34
+ function blockBlocknumber() view returns(Blocknumber) { return toBlocknumber(block.number); }
35
+
36
+ /// @dev Return the Blocknumber zero (0)
37
+ function zeroBlocknumber() pure returns(Blocknumber) { return toBlocknumber(0); }
38
+
39
+ /// @dev Return the current block number
40
+ function blockNumber() view returns(Blocknumber) { return toBlocknumber(block.number); }
41
+
42
+ library BlocknumberLib {
43
+ /// @dev return true if Blocknumber a is greater than Blocknumber b
44
+ function gt(Blocknumber a, Blocknumber b) public pure returns(bool isAfter) { return gtBlocknumber(a, b); }
45
+ /// @dev return true if Blocknumber a is greater than or equal to Blocknumber b
46
+ function gte(Blocknumber a, Blocknumber b) public pure returns(bool isAfterOrSame) { return gteBlocknumber(a, b); }
47
+
48
+ /// @dev return true if Blocknumber a is less than Blocknumber b
49
+ function lt(Blocknumber a, Blocknumber b) public pure returns(bool isBefore) { return ltBlocknumber(a, b); }
50
+ /// @dev return true if Blocknumber a is less than or equal to Blocknumber b
51
+ function lte(Blocknumber a, Blocknumber b) public pure returns(bool isBeforeOrSame) { return lteBlocknumber(a, b); }
52
+
53
+ /// @dev return true if Blocknumber a is equal to Blocknumber b
54
+ function eq(Blocknumber a, Blocknumber b) public pure returns(bool isSame) { return eqBlocknumber(a, b); }
55
+ /// @dev return true if Blocknumber a is not equal to Blocknumber b
56
+ function ne(Blocknumber a, Blocknumber b) public pure returns(bool isDifferent) { return neBlocknumber(a, b); }
57
+
58
+ /// @dev converts the Blocknumber to a uint256
59
+ function toInt(Blocknumber blocknumber) public pure returns(uint256) { return uint256(uint32(Blocknumber.unwrap(blocknumber))); }
60
+ }
@@ -0,0 +1,30 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.19;
3
+
4
+ /// @dev bytes5 (uint40) allows for chain ids up to 13 digits
5
+ type ChainId is bytes5;
6
+
7
+ // type bindings
8
+ using {
9
+ eqChainId as ==,
10
+ neChainId as !=,
11
+ ChainIdLib.toInt
12
+ } for ChainId global;
13
+
14
+ // general pure free functions
15
+ /// @dev Converts the uint256 to a ChainId.
16
+ function toChainId(uint256 chainId) pure returns(ChainId) { return ChainId.wrap(bytes5(uint40(chainId))); }
17
+ /// @dev Return the ChainId for the chain the contract is deployed to
18
+ function thisChainId() view returns(ChainId) { return toChainId(block.chainid); }
19
+
20
+ // pure free functions for operators
21
+ /// @dev Returns true if the values are equal (==).
22
+ function eqChainId(ChainId a, ChainId b) pure returns(bool isSame) { return ChainId.unwrap(a) == ChainId.unwrap(b); }
23
+ /// @dev Returns true if the values are not equal (!=).
24
+ function neChainId(ChainId a, ChainId b) pure returns(bool isDifferent) { return ChainId.unwrap(a) != ChainId.unwrap(b); }
25
+
26
+ // library functions that operate on user defined type
27
+ library ChainIdLib {
28
+ /// @dev Converts the ChainId to a uint256.
29
+ function toInt(ChainId chainId) internal pure returns(uint256) { return uint256(uint40(ChainId.unwrap(chainId))); }
30
+ }
@@ -0,0 +1,35 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.19;
3
+
4
+ // uint96 allows for chain ids up to 13 digits
5
+ type NftId is uint96;
6
+
7
+ // type bindings
8
+ using {
9
+ eqNftId as ==,
10
+ neNftId as !=,
11
+ NftIdLib.toInt
12
+ } for NftId global;
13
+
14
+ // general pure free functions
15
+ /// @dev Converts the uint256 to a NftId.
16
+ function toNftId(uint256 id) pure returns(NftId) { return NftId.wrap(uint96(id)); }
17
+
18
+ /// @dev Return the NftId zero (0)
19
+ function zeroNftId() pure returns(NftId) { return NftId.wrap(0); }
20
+
21
+ // pure free functions for operators
22
+ function eqNftId(NftId a, NftId b) pure returns(bool isSame) { return NftId.unwrap(a) == NftId.unwrap(b); }
23
+ function neNftId(NftId a, NftId b) pure returns(bool isDifferent) { return NftId.unwrap(a) != NftId.unwrap(b); }
24
+
25
+ // library functions that operate on user defined type
26
+ library NftIdLib {
27
+ /// @dev Converts the NftId to a uint256.
28
+ function toInt(NftId nftId) public pure returns(uint96) { return uint96(NftId.unwrap(nftId)); }
29
+ /// @dev Returns true if the value is non-zero (> 0).
30
+ function gtz(NftId a) public pure returns(bool) { return NftId.unwrap(a) > 0; }
31
+ /// @dev Returns true if the value is zero (== 0).
32
+ function eqz(NftId a) public pure returns(bool) { return NftId.unwrap(a) == 0; }
33
+ /// @dev Returns true if the values are equal (==).
34
+ function eq(NftId a, NftId b) public pure returns(bool isSame) { return eqNftId(a, b); }
35
+ }