@etherisc/gif-next 0.0.2-3ae3aa8 → 0.0.2-3e0ffc8-142

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 (141) hide show
  1. package/README.md +2 -1
  2. package/artifacts/contracts/components/Component.sol/Component.dbg.json +1 -1
  3. package/artifacts/contracts/components/Component.sol/Component.json +28 -2
  4. package/artifacts/contracts/components/Component.sol/InstanceLinked.dbg.json +1 -1
  5. package/artifacts/contracts/components/Component.sol/InstanceLinked.json +2 -2
  6. package/artifacts/contracts/components/IPool.sol/IPoolComponent.dbg.json +1 -1
  7. package/artifacts/contracts/components/IPool.sol/IPoolComponent.json +78 -2
  8. package/artifacts/contracts/components/IProduct.sol/IProductComponent.dbg.json +1 -1
  9. package/artifacts/contracts/components/IProduct.sol/IProductComponent.json +32 -150
  10. package/artifacts/contracts/components/Pool.sol/Pool.dbg.json +1 -1
  11. package/artifacts/contracts/components/Pool.sol/Pool.json +121 -6
  12. package/artifacts/contracts/components/Product.sol/Product.dbg.json +1 -1
  13. package/artifacts/contracts/components/Product.sol/Product.json +121 -6
  14. package/artifacts/contracts/experiment/errors/Require.sol/Require.dbg.json +1 -1
  15. package/artifacts/contracts/experiment/errors/Revert.sol/Revert.dbg.json +1 -1
  16. package/artifacts/contracts/experiment/inheritance/A.sol/A.dbg.json +1 -1
  17. package/artifacts/contracts/experiment/inheritance/A.sol/AShared.dbg.json +1 -1
  18. package/artifacts/contracts/experiment/inheritance/B.sol/B.dbg.json +1 -1
  19. package/artifacts/contracts/experiment/inheritance/C.sol/C.dbg.json +1 -1
  20. package/artifacts/contracts/experiment/inheritance/IA.sol/IA.dbg.json +1 -1
  21. package/artifacts/contracts/experiment/inheritance/IA.sol/ISharedA.dbg.json +1 -1
  22. package/artifacts/contracts/experiment/inheritance/IB.sol/IB.dbg.json +1 -1
  23. package/artifacts/contracts/experiment/inheritance/IC.sol/IC.dbg.json +1 -1
  24. package/artifacts/contracts/experiment/statemachine/Dummy.sol/LifeCycleModule.dbg.json +4 -0
  25. package/artifacts/contracts/experiment/statemachine/Dummy.sol/LifeCycleModule.json +59 -0
  26. package/artifacts/contracts/experiment/statemachine/ISM.sol/ISM.dbg.json +4 -0
  27. package/artifacts/contracts/experiment/statemachine/ISM.sol/ISM.json +124 -0
  28. package/artifacts/contracts/experiment/statemachine/ISM.sol/ISMEE.dbg.json +4 -0
  29. package/artifacts/contracts/experiment/statemachine/ISM.sol/ISMEE.json +74 -0
  30. package/artifacts/contracts/experiment/statemachine/SM.sol/SM.dbg.json +4 -0
  31. package/artifacts/contracts/experiment/statemachine/SM.sol/SM.json +124 -0
  32. package/artifacts/contracts/experiment/statemachine/SimpleStateMachine.sol/SimpleStateMachine.dbg.json +4 -0
  33. package/artifacts/contracts/experiment/statemachine/SimpleStateMachine.sol/SimpleStateMachine.json +207 -0
  34. package/artifacts/contracts/experiment/types/TypeA.sol/TypeALib.dbg.json +1 -1
  35. package/artifacts/contracts/experiment/types/TypeB.sol/TypeBLib.dbg.json +1 -1
  36. package/artifacts/contracts/instance/IInstance.sol/IInstance.dbg.json +1 -1
  37. package/artifacts/contracts/instance/IInstance.sol/IInstance.json +518 -47
  38. package/artifacts/contracts/instance/Instance.sol/Instance.dbg.json +1 -1
  39. package/artifacts/contracts/instance/Instance.sol/Instance.json +584 -66
  40. package/artifacts/contracts/instance/access/Access.sol/AccessModule.dbg.json +1 -1
  41. package/artifacts/contracts/instance/access/IAccess.sol/IAccess.dbg.json +1 -1
  42. package/artifacts/contracts/instance/access/IAccess.sol/IAccessCheckRole.dbg.json +1 -1
  43. package/artifacts/contracts/instance/access/IAccess.sol/IAccessComponentTypeRoles.dbg.json +1 -1
  44. package/artifacts/contracts/instance/access/IAccess.sol/IAccessModule.dbg.json +1 -1
  45. package/artifacts/contracts/instance/component/ComponentModule.sol/ComponentModule.dbg.json +1 -1
  46. package/artifacts/contracts/instance/component/ComponentModule.sol/ComponentModule.json +14 -23
  47. package/artifacts/contracts/instance/component/ComponentModule.sol/ComponentOwnerService.dbg.json +1 -1
  48. package/artifacts/contracts/instance/component/ComponentModule.sol/ComponentOwnerService.json +8 -8
  49. package/artifacts/contracts/instance/component/IComponent.sol/IComponent.dbg.json +1 -1
  50. package/artifacts/contracts/instance/component/IComponent.sol/IComponentContract.dbg.json +1 -1
  51. package/artifacts/contracts/instance/component/IComponent.sol/IComponentContract.json +28 -2
  52. package/artifacts/contracts/instance/component/IComponent.sol/IComponentModule.dbg.json +1 -1
  53. package/artifacts/contracts/instance/component/IComponent.sol/IComponentModule.json +12 -21
  54. package/artifacts/contracts/instance/component/IComponent.sol/IComponentOwnerService.dbg.json +1 -1
  55. package/artifacts/contracts/instance/component/IComponent.sol/IInstanceLinked.dbg.json +1 -1
  56. package/artifacts/contracts/instance/lifecycle/ILifecycle.sol/ILifecycle.dbg.json +4 -0
  57. package/artifacts/contracts/instance/lifecycle/ILifecycle.sol/ILifecycle.json +134 -0
  58. package/artifacts/contracts/instance/lifecycle/ILifecycle.sol/ILifecycleModule.dbg.json +4 -0
  59. package/artifacts/contracts/instance/lifecycle/ILifecycle.sol/ILifecycleModule.json +182 -0
  60. package/artifacts/contracts/instance/lifecycle/LifecycleModule.sol/LifecycleModule.dbg.json +4 -0
  61. package/artifacts/contracts/instance/lifecycle/LifecycleModule.sol/LifecycleModule.json +221 -0
  62. package/artifacts/contracts/instance/policy/IPolicy.sol/IPolicy.dbg.json +1 -1
  63. package/artifacts/contracts/instance/policy/IPolicy.sol/IPolicyModule.dbg.json +1 -1
  64. package/artifacts/contracts/instance/policy/IPolicy.sol/IPolicyModule.json +26 -3
  65. package/artifacts/contracts/instance/policy/PolicyModule.sol/PolicyModule.dbg.json +1 -1
  66. package/artifacts/contracts/instance/policy/PolicyModule.sol/PolicyModule.json +26 -3
  67. package/artifacts/contracts/instance/pool/IPoolModule.sol/IPool.dbg.json +1 -1
  68. package/artifacts/contracts/instance/pool/IPoolModule.sol/IPoolModule.dbg.json +1 -1
  69. package/artifacts/contracts/instance/pool/IPoolModule.sol/IPoolModule.json +15 -35
  70. package/artifacts/contracts/instance/pool/PoolModule.sol/PoolModule.dbg.json +1 -1
  71. package/artifacts/contracts/instance/pool/PoolModule.sol/PoolModule.json +22 -29
  72. package/artifacts/contracts/instance/product/IProductService.sol/IProductModule.dbg.json +1 -1
  73. package/artifacts/contracts/instance/product/IProductService.sol/IProductService.dbg.json +1 -1
  74. package/artifacts/contracts/instance/product/IProductService.sol/IProductService.json +13 -0
  75. package/artifacts/contracts/instance/product/ProductService.sol/ProductModule.dbg.json +1 -1
  76. package/artifacts/contracts/instance/product/ProductService.sol/ProductService.dbg.json +1 -1
  77. package/artifacts/contracts/instance/product/ProductService.sol/ProductService.json +41 -12
  78. package/artifacts/contracts/instance/treasury/ITreasury.sol/ITreasury.dbg.json +4 -0
  79. package/artifacts/contracts/instance/treasury/ITreasury.sol/ITreasury.json +10 -0
  80. package/artifacts/contracts/instance/treasury/ITreasury.sol/ITreasuryModule.dbg.json +4 -0
  81. package/artifacts/contracts/instance/treasury/ITreasury.sol/ITreasuryModule.json +355 -0
  82. package/artifacts/contracts/instance/treasury/TokenHandler.sol/TokenHandler.dbg.json +4 -0
  83. package/artifacts/contracts/instance/treasury/TokenHandler.sol/TokenHandler.json +45 -0
  84. package/artifacts/contracts/instance/treasury/TreasuryModule.sol/TreasuryModule.dbg.json +4 -0
  85. package/artifacts/contracts/instance/treasury/TreasuryModule.sol/TreasuryModule.json +355 -0
  86. package/artifacts/contracts/registry/ChainNft.sol/ChainNft.dbg.json +1 -1
  87. package/artifacts/contracts/registry/IChainNft.sol/IChainNft.dbg.json +1 -1
  88. package/artifacts/contracts/registry/IRegistry.sol/IOwnable.dbg.json +1 -1
  89. package/artifacts/contracts/registry/IRegistry.sol/IRegisterable.dbg.json +1 -1
  90. package/artifacts/contracts/registry/IRegistry.sol/IRegisterable.json +2 -2
  91. package/artifacts/contracts/registry/IRegistry.sol/IRegistry.dbg.json +1 -1
  92. package/artifacts/contracts/registry/IRegistry.sol/IRegistry.json +4 -95
  93. package/artifacts/contracts/registry/IRegistry.sol/IRegistryLinked.dbg.json +1 -1
  94. package/artifacts/contracts/registry/Registry.sol/Registerable.dbg.json +1 -1
  95. package/artifacts/contracts/registry/Registry.sol/Registerable.json +2 -2
  96. package/artifacts/contracts/registry/Registry.sol/Registry.dbg.json +1 -1
  97. package/artifacts/contracts/registry/Registry.sol/Registry.json +14 -105
  98. package/artifacts/contracts/registry/Registry.sol/RegistryLinked.dbg.json +1 -1
  99. package/artifacts/contracts/registry/Registry.sol/RegistryLinked.json +2 -2
  100. package/artifacts/contracts/types/Blocknumber.sol/BlocknumberLib.dbg.json +1 -1
  101. package/artifacts/contracts/types/ChainId.sol/ChainIdLib.dbg.json +1 -1
  102. package/artifacts/contracts/types/NftId.sol/NftIdLib.dbg.json +1 -1
  103. package/artifacts/contracts/types/ObjectType.sol/ObjectTypeLib.dbg.json +4 -0
  104. package/artifacts/contracts/types/ObjectType.sol/ObjectTypeLib.json +92 -0
  105. package/artifacts/contracts/types/StateId.sol/StateIdLib.dbg.json +4 -0
  106. package/artifacts/contracts/types/StateId.sol/StateIdLib.json +92 -0
  107. package/artifacts/contracts/types/Timestamp.sol/TimestampLib.dbg.json +1 -1
  108. package/artifacts/contracts/types/UFixed.sol/UFixedMathLib.dbg.json +1 -1
  109. package/artifacts/contracts/types/UFixed.sol/UFixedMathLib.json +2 -2
  110. package/contracts/components/Component.sol +25 -8
  111. package/contracts/components/IPool.sol +3 -1
  112. package/contracts/components/IProduct.sol +4 -2
  113. package/contracts/components/Pool.sol +27 -4
  114. package/contracts/components/Product.sol +30 -4
  115. package/contracts/experiment/statemachine/Dummy.sol +33 -0
  116. package/contracts/experiment/statemachine/ISM.sol +20 -0
  117. package/contracts/experiment/statemachine/README.md +112 -0
  118. package/contracts/experiment/statemachine/SM.sol +54 -0
  119. package/contracts/experiment/statemachine/SimpleStateMachine.sol +24 -0
  120. package/contracts/instance/IInstance.sol +5 -1
  121. package/contracts/instance/Instance.sol +11 -6
  122. package/contracts/instance/component/ComponentModule.sol +80 -39
  123. package/contracts/instance/component/IComponent.sol +15 -12
  124. package/contracts/instance/lifecycle/ILifecycle.sol +32 -0
  125. package/contracts/instance/lifecycle/LifecycleModule.sol +91 -0
  126. package/contracts/instance/policy/IPolicy.sol +9 -8
  127. package/contracts/instance/policy/PolicyModule.sol +28 -12
  128. package/contracts/instance/pool/IPoolModule.sol +4 -11
  129. package/contracts/instance/pool/PoolModule.sol +27 -18
  130. package/contracts/instance/product/IProductService.sol +1 -1
  131. package/contracts/instance/product/ProductService.sol +48 -42
  132. package/contracts/instance/treasury/ITreasury.sol +73 -0
  133. package/contracts/instance/treasury/TokenHandler.sol +27 -0
  134. package/contracts/instance/treasury/TreasuryModule.sol +131 -0
  135. package/contracts/registry/IRegistry.sol +6 -17
  136. package/contracts/registry/Registry.sol +6 -38
  137. package/contracts/types/Fee.sol +30 -0
  138. package/contracts/types/ObjectType.sol +50 -0
  139. package/contracts/types/StateId.sol +46 -0
  140. package/contracts/types/UFixed.sol +1 -0
  141. package/package.json +4 -3
@@ -1,15 +1,23 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
2
  pragma solidity ^0.8.19;
3
3
 
4
+ import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
5
+
4
6
  import {RegistryLinked} from "../../registry/Registry.sol";
5
7
  import {IRegistry, IRegistryLinked} from "../../registry/IRegistry.sol";
6
8
  import {IAccessComponentTypeRoles, IAccessCheckRole} from "../access/IAccess.sol";
7
9
  import {IInstance} from "../IInstance.sol";
8
10
 
11
+ import {LifecycleModule} from "../lifecycle/LifecycleModule.sol";
12
+ import {TreasuryModule} from "../treasury/TreasuryModule.sol";
9
13
  import {IComponent, IComponentContract, IComponentModule, IComponentOwnerService} from "./IComponent.sol";
10
14
  import {IProductComponent} from "../../components/IProduct.sol";
15
+ import {IPoolComponent} from "../../components/IPool.sol";
11
16
  import {IPoolModule} from "../pool/IPoolModule.sol";
12
- import {NftId, NftIdLib} from "../../types/NftId.sol";
17
+ import {ObjectType, PRODUCT, ORACLE, POOL} from "../../types/ObjectType.sol";
18
+ import {StateId, ACTIVE, PAUSED} from "../../types/StateId.sol";
19
+ import {NftId, NftIdLib, zeroNftId} from "../../types/NftId.sol";
20
+ import {Fee, zeroFee} from "../../types/Fee.sol";
13
21
 
14
22
  abstract contract ComponentModule is
15
23
  IRegistryLinked,
@@ -20,12 +28,15 @@ abstract contract ComponentModule is
20
28
  using NftIdLib for NftId;
21
29
 
22
30
  mapping(NftId nftId => ComponentInfo info) private _componentInfo;
23
- mapping(NftId nftId => NftId poolNftId) private _poolNftIdForProduct;
24
31
  mapping(address cAddress => NftId nftId) private _nftIdByAddress;
25
32
  NftId[] private _nftIds;
26
33
 
27
- mapping(uint256 cType => bytes32 role) private _componentOwnerRole;
34
+ mapping(ObjectType cType => bytes32 role) private _componentOwnerRole;
28
35
 
36
+ // TODO maybe move this to Instance contract as internal variable?
37
+ LifecycleModule private _lifecycleModule;
38
+ TreasuryModule private _treasuryModule;
39
+ IPoolModule private _poolModule;
29
40
  IComponentOwnerService private _componentOwnerService;
30
41
 
31
42
  modifier onlyComponentOwnerService() {
@@ -37,51 +48,74 @@ abstract contract ComponentModule is
37
48
  }
38
49
 
39
50
  constructor(address componentOwnerService) {
51
+ address componentAddress = address(this);
52
+ _lifecycleModule = LifecycleModule(componentAddress);
53
+ _treasuryModule = TreasuryModule(componentAddress);
54
+ _poolModule = IPoolModule(componentAddress);
40
55
  _componentOwnerService = ComponentOwnerService(componentOwnerService);
41
56
  }
42
57
 
43
- function registerComponent(
44
- IComponentContract component
45
- ) external override onlyComponentOwnerService returns (NftId nftId) {
46
- bytes32 typeRole = getRoleForType(component.getType());
58
+ function registerComponent(IComponentContract component)
59
+ external
60
+ override
61
+ onlyComponentOwnerService
62
+ returns(NftId nftId)
63
+ {
64
+ ObjectType objectType = component.getType();
65
+ bytes32 typeRole = getRoleForType(objectType);
47
66
  require(
48
67
  this.hasRole(typeRole, component.getInitialOwner()),
49
68
  "ERROR:CMP-004:TYPE_ROLE_MISSING"
50
69
  );
51
70
 
52
71
  nftId = this.getRegistry().register(address(component));
72
+ IERC20 token = component.getToken();
73
+ address wallet = component.getWallet();
53
74
 
54
- _componentInfo[nftId] = ComponentInfo(nftId, CState.Active);
75
+ // create component info
76
+ _componentInfo[nftId] = ComponentInfo(
77
+ nftId,
78
+ _lifecycleModule.getInitialState(objectType),
79
+ token);
55
80
 
56
- // special case product -> persist product - pool assignment
57
- if (component.getType() == this.getRegistry().PRODUCT()) {
81
+ // component type specific registration actions
82
+ if(component.getType() == PRODUCT()) {
58
83
  IProductComponent product = IProductComponent(address(component));
59
84
  NftId poolNftId = product.getPoolNftId();
60
85
  require(poolNftId.gtz(), "ERROR:CMP-005:POOL_UNKNOWN");
61
- // add more validation (type, token, ...)
62
-
63
- _poolNftIdForProduct[nftId] = poolNftId;
64
-
65
- // add creation of productInfo
66
- } else if (component.getType() == this.getRegistry().POOL()) {
67
- IPoolModule poolModule = IPoolModule(address(this));
68
- poolModule.createPoolInfo(
69
- nftId,
70
- address(component), // set pool as its wallet
71
- address(0) // don't deal with token yet
72
- );
86
+ // validate pool token and product token are same
87
+
88
+ // register with tresury
89
+ // implement and add validation
90
+ NftId distributorNftId = zeroNftId();
91
+ _treasuryModule.registerProduct(
92
+ nftId,
93
+ distributorNftId,
94
+ poolNftId,
95
+ token,
96
+ wallet,
97
+ product.getPolicyFee(),
98
+ product.getProcessingFee());
99
+ }
100
+ else if(component.getType() == POOL()) {
101
+ IPoolComponent pool = IPoolComponent(address(component));
102
+
103
+ // register with pool
104
+ _poolModule.registerPool(nftId);
105
+
106
+ // register with tresury
107
+ _treasuryModule.registerPool(
108
+ nftId,
109
+ wallet,
110
+ pool.getStakingFee(),
111
+ pool.getPerformanceFee());
73
112
  }
113
+ // TODO add distribution
74
114
 
75
115
  _nftIdByAddress[address(component)] = nftId;
76
116
  _nftIds.push(nftId);
77
117
 
78
- // add logging
79
- }
80
-
81
- function getPoolNftId(
82
- NftId productNftId
83
- ) external view override returns (NftId poolNftId) {
84
- poolNftId = _poolNftIdForProduct[productNftId];
118
+ // TODO add loggingx
85
119
  }
86
120
 
87
121
  function getComponentOwnerService()
@@ -102,9 +136,12 @@ abstract contract ComponentModule is
102
136
  "ERROR:CMP-006:COMPONENT_UNKNOWN"
103
137
  );
104
138
 
139
+ // TODO decide if state changes should have explicit functions and not
140
+ // just a generic setXYZInfo and implicit state transitions
141
+ // when in doubt go for the explicit approach ...
142
+ ObjectType objectType = this.getRegistry().getInfo(nftId).objectType;
143
+ _lifecycleModule.checkAndLogTransition(nftId, objectType, _componentInfo[nftId].state, info.state);
105
144
  _componentInfo[nftId] = info;
106
-
107
- // add logging
108
145
  }
109
146
 
110
147
  function getComponentInfo(
@@ -138,14 +175,18 @@ abstract contract ComponentModule is
138
175
  return _nftIds.length;
139
176
  }
140
177
 
141
- function getRoleForType(uint256 cType) public view returns (bytes32 role) {
142
- if (cType == this.getRegistry().PRODUCT()) {
178
+ function getRoleForType(ObjectType cType)
179
+ public
180
+ view
181
+ returns(bytes32 role)
182
+ {
183
+ if(cType == PRODUCT()) {
143
184
  return this.PRODUCT_OWNER_ROLE();
144
185
  }
145
- if (cType == this.getRegistry().POOL()) {
186
+ if(cType == POOL()) {
146
187
  return this.POOL_OWNER_ROLE();
147
188
  }
148
- if (cType == this.getRegistry().ORACLE()) {
189
+ if(cType == ORACLE()) {
149
190
  return this.ORACLE_OWNER_ROLE();
150
191
  }
151
192
  }
@@ -191,9 +232,9 @@ contract ComponentOwnerService is
191
232
  component.getNftId()
192
233
  );
193
234
  require(info.nftId.gtz(), "ERROR_COMPONENT_UNKNOWN");
194
- // TODO add state change validation
195
235
 
196
- info.state = CState.Locked;
236
+ info.state = PAUSED();
237
+ // setComponentInfo checks for valid state changes
197
238
  instance.setComponentInfo(info);
198
239
  }
199
240
 
@@ -205,9 +246,9 @@ contract ComponentOwnerService is
205
246
  component.getNftId()
206
247
  );
207
248
  require(info.nftId.gtz(), "ERROR_COMPONENT_UNKNOWN");
208
- // TODO state change validation
209
249
 
210
- info.state = CState.Active;
250
+ info.state = ACTIVE();
251
+ // setComponentInfo checks for valid state changes
211
252
  instance.setComponentInfo(info);
212
253
  }
213
254
  }
@@ -1,22 +1,22 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
2
  pragma solidity ^0.8.19;
3
3
 
4
+ import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
5
+
4
6
  import {IOwnable, IRegistryLinked, IRegisterable} from "../../registry/IRegistry.sol";
5
7
  import {IInstance} from "../IInstance.sol";
8
+ import {StateId} from "../../types/StateId.sol";
6
9
  import {NftId} from "../../types/NftId.sol";
10
+ import {Fee} from "../../types/Fee.sol";
11
+ import {UFixed} from "../../types/UFixed.sol";
7
12
 
8
13
  interface IComponent {
9
- // TODO decide if enum or uints with constants (as in IRegistry.PRODUCT())
10
- enum CState {
11
- Undefined,
12
- Active,
13
- Locked
14
- }
15
14
 
16
15
  // component dynamic info (static info kept in registry)
17
16
  struct ComponentInfo {
18
17
  NftId nftId;
19
- CState state;
18
+ StateId state;
19
+ IERC20 token;
20
20
  }
21
21
  }
22
22
 
@@ -25,7 +25,14 @@ interface IInstanceLinked {
25
25
  function getInstance() external view returns (IInstance instance);
26
26
  }
27
27
 
28
- interface IComponentContract is IRegisterable, IInstanceLinked, IComponent {}
28
+ interface IComponentContract is
29
+ IRegisterable,
30
+ IInstanceLinked,
31
+ IComponent
32
+ {
33
+ function getToken() external view returns(IERC20 token);
34
+ function getWallet() external view returns(address walletAddress);
35
+ }
29
36
 
30
37
  interface IComponentOwnerService is IRegistryLinked {
31
38
  function register(
@@ -60,10 +67,6 @@ interface IComponentModule is IOwnable, IRegistryLinked, IComponent {
60
67
 
61
68
  function getComponentId(uint256 idx) external view returns (NftId nftId);
62
69
 
63
- function getPoolNftId(
64
- NftId productNftId
65
- ) external view returns (NftId poolNftId);
66
-
67
70
  function components() external view returns (uint256 numberOfCompnents);
68
71
 
69
72
  function getComponentOwnerService()
@@ -0,0 +1,32 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.19;
3
+
4
+ import {ObjectType} from "../../types/ObjectType.sol";
5
+ import {StateId, toStateId, zeroStateId} from "../../types/StateId.sol";
6
+ import {NftId} from "../../types/NftId.sol";
7
+
8
+ interface ILifecycle {
9
+
10
+ event LogComponentStateChanged(NftId nftId, ObjectType objectType, StateId fromStateId, StateId toStateId);
11
+ event LogBundleStateChanged(NftId nftId, StateId fromStateId, StateId toStateId);
12
+ event LogPolicyStateChanged(NftId nftId, StateId fromStateId, StateId toStateId);
13
+ // event LogClaimStateChanged(NftId nftId, ClaimId claimId, StateId fromStateId, StateId toStateId);
14
+ // event LogPayoutStateChanged(NftId nftId, ClaimId claimId, PayoutId payoutId, StateId fromStateId, StateId toStateId);
15
+
16
+ error ErrorNoLifecycle(NftId nftId, ObjectType objectType);
17
+ error ErrorInvalidStateTransition(NftId nftId, ObjectType objectType, StateId fromStateId, StateId toStateId);
18
+ }
19
+
20
+ interface ILifecycleModule is ILifecycle {
21
+
22
+ function getInitialState(ObjectType objectType)
23
+ external
24
+ view
25
+ returns(StateId);
26
+
27
+
28
+ function isValidTransition(ObjectType objectType, StateId fromId, StateId toId)
29
+ external
30
+ view
31
+ returns(bool);
32
+ }
@@ -0,0 +1,91 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.19;
3
+
4
+ import {NftId} from "../../types/NftId.sol";
5
+ import {ObjectType, PRODUCT, ORACLE, POOL, BUNDLE, POLICY} from "../../types/ObjectType.sol";
6
+ import {StateId, ACTIVE, PAUSED, ARCHIVED, CLOSED, APPLIED, REVOKED, DECLINED} from "../../types/StateId.sol";
7
+ import {ILifecycleModule} from "./ILifecycle.sol";
8
+
9
+ contract LifecycleModule is ILifecycleModule {
10
+
11
+ mapping(ObjectType objectType => StateId initialState) private _initialState;
12
+
13
+ mapping(ObjectType objectType =>
14
+ mapping(StateId stateFrom =>
15
+ mapping(StateId stateTo => bool isValid))) private _isValidTransition;
16
+
17
+
18
+ constructor() {
19
+ _setupComponentLifecycle(PRODUCT());
20
+ _setupComponentLifecycle(ORACLE());
21
+ _setupComponentLifecycle(POOL());
22
+
23
+ _setupBundleLifecycle();
24
+ _setupPolicyLifecycle();
25
+ }
26
+
27
+
28
+ function checkAndLogTransition(NftId nftId, ObjectType objectType, StateId fromId, StateId toId)
29
+ public
30
+ returns(StateId)
31
+ // add only currentcontract? would that work?
32
+ {
33
+ if(!_isValidTransition[objectType][fromId][toId]) {
34
+ revert ErrorInvalidStateTransition(nftId, objectType, fromId, toId);
35
+ }
36
+
37
+ if(objectType == POLICY()) {
38
+ emit LogPolicyStateChanged(nftId, fromId, toId);
39
+ } else if(objectType == BUNDLE()) {
40
+ emit LogBundleStateChanged(nftId, fromId, toId);
41
+ } else if(objectType == PRODUCT() || objectType == ORACLE() || objectType == POOL()) {
42
+ emit LogComponentStateChanged(nftId, objectType, fromId, toId);
43
+ } else {
44
+ revert ErrorNoLifecycle(nftId, objectType);
45
+ }
46
+
47
+ return toId;
48
+ }
49
+
50
+ function getInitialState(ObjectType objectType)
51
+ public
52
+ view
53
+ returns(StateId)
54
+ {
55
+ return _initialState[objectType];
56
+ }
57
+
58
+
59
+ function isValidTransition(ObjectType objectType, StateId fromId, StateId toId)
60
+ public
61
+ view
62
+ returns(bool)
63
+ {
64
+ return _isValidTransition[objectType][fromId][toId];
65
+ }
66
+
67
+
68
+ function _setupComponentLifecycle(ObjectType objectType) internal {
69
+ _initialState[objectType] = ACTIVE();
70
+ _isValidTransition[objectType][ACTIVE()][PAUSED()] = true;
71
+ _isValidTransition[objectType][PAUSED()][ACTIVE()] = true;
72
+ _isValidTransition[objectType][PAUSED()][ARCHIVED()] = true;
73
+ }
74
+
75
+
76
+ function _setupBundleLifecycle() internal {
77
+ _initialState[BUNDLE()] = ACTIVE();
78
+ _isValidTransition[BUNDLE()][ACTIVE()][PAUSED()] = true;
79
+ _isValidTransition[BUNDLE()][PAUSED()][ACTIVE()] = true;
80
+ _isValidTransition[BUNDLE()][PAUSED()][CLOSED()] = true;
81
+ }
82
+
83
+
84
+ function _setupPolicyLifecycle() internal {
85
+ _initialState[POLICY()] = APPLIED();
86
+ _isValidTransition[POLICY()][APPLIED()][REVOKED()] = true;
87
+ _isValidTransition[POLICY()][APPLIED()][DECLINED()] = true;
88
+ _isValidTransition[POLICY()][APPLIED()][ACTIVE()] = true;
89
+ _isValidTransition[POLICY()][ACTIVE()][CLOSED()] = true;
90
+ }
91
+ }
@@ -5,24 +5,21 @@ import {IOwnable, IRegistryLinked, IRegisterable, IRegistry} from "../../registr
5
5
  import {IInstance} from "../IInstance.sol";
6
6
  import {IProductService} from "../product/IProductService.sol";
7
7
  import {NftId} from "../../types/NftId.sol";
8
+ import {StateId} from "../../types/StateId.sol";
8
9
 
9
10
  // TODO check if there is value to introuce IContract and let IPolicy derive from IContract
10
11
  interface IPolicy {
11
- enum PolicyState {
12
- Undefined,
13
- Applied,
14
- Rejected,
15
- Active,
16
- Closed
17
- }
18
12
 
19
13
  struct PolicyInfo {
20
14
  NftId nftId;
21
- PolicyState state; // applied, withdrawn, rejected, active, closed
15
+ StateId state; // applied, withdrawn, rejected, active, closed
16
+ // TODO add beneficiary address
22
17
  uint256 sumInsuredAmount;
23
18
  uint256 premiumAmount;
19
+ uint256 premiumPaidAmount;
24
20
  uint256 lifetime; // activatedAt + lifetime >= expiredAt
25
21
  uint256 createdAt;
22
+ uint256 updatedAt;
26
23
  uint256 activatedAt; // time of underwriting
27
24
  uint256 expiredAt; // no new claims
28
25
  uint256 closedAt; // no locked capital
@@ -30,6 +27,7 @@ interface IPolicy {
30
27
  }
31
28
 
32
29
  interface IPolicyModule is IOwnable, IRegistryLinked, IPolicy {
30
+
33
31
  function createApplication(
34
32
  IRegistry.RegistryInfo memory productInfo,
35
33
  address applicationOwner,
@@ -39,6 +37,9 @@ interface IPolicyModule is IOwnable, IRegistryLinked, IPolicy {
39
37
  NftId bundleNftId
40
38
  ) external returns (NftId nftId);
41
39
 
40
+ // process full premium
41
+ function processPremium(NftId nftId) external;
42
+
42
43
  function activate(NftId nftId) external;
43
44
 
44
45
  function getBundleNftForPolicy(
@@ -4,16 +4,25 @@ pragma solidity ^0.8.19;
4
4
  // import {IOwnable, IRegistryLinked, IRegisterable} from "../../registry/IRegistry.sol";
5
5
  import {IRegistry, IRegistryLinked} from "../../registry/IRegistry.sol";
6
6
 
7
+ import {LifecycleModule} from "../lifecycle/LifecycleModule.sol";
7
8
  import {IProductService} from "../product/IProductService.sol";
8
9
  import {IPolicy, IPolicyModule} from "./IPolicy.sol";
10
+ import {ObjectType, POLICY} from "../../types/ObjectType.sol";
11
+ import {ACTIVE} from "../../types/StateId.sol";
9
12
  import {NftId, NftIdLib} from "../../types/NftId.sol";
10
13
 
11
- abstract contract PolicyModule is IRegistryLinked, IPolicyModule {
14
+ import {LifecycleModule} from "../lifecycle/LifecycleModule.sol";
15
+
16
+ abstract contract PolicyModule is
17
+ IRegistryLinked,
18
+ IPolicyModule
19
+ {
12
20
  using NftIdLib for NftId;
13
21
 
14
22
  mapping(NftId nftId => PolicyInfo info) private _policyInfo;
15
23
  mapping(NftId nftId => NftId bundleNftId) private _bundleForPolicy;
16
24
 
25
+ LifecycleModule private _lifecycleModule;
17
26
  IProductService private _productService;
18
27
 
19
28
  // TODO find a better place to avoid dupliation
@@ -26,6 +35,7 @@ abstract contract PolicyModule is IRegistryLinked, IPolicyModule {
26
35
  }
27
36
 
28
37
  constructor(address productService) {
38
+ _lifecycleModule = LifecycleModule(address(this));
29
39
  _productService = IProductService(productService);
30
40
  }
31
41
 
@@ -39,25 +49,26 @@ abstract contract PolicyModule is IRegistryLinked, IPolicyModule {
39
49
  ) external override onlyProductService2 returns (NftId nftId) {
40
50
  // TODO add parameter validation
41
51
  if (bundleNftId.gtz()) {
42
- IRegistry.RegistryInfo memory bundleInfo = this
43
- .getRegistry()
44
- .getInfo(bundleNftId);
52
+ // IRegistry.RegistryInfo memory bundleInfo = this
53
+ // .getRegistry()
54
+ // .getInfo(bundleNftId);
45
55
  // IRegistry.RegistryInfo memory poolInfo = this.getRegistry().getInfo(bundleInfo.parentNftId);
46
56
  }
47
57
 
48
58
  nftId = this.getRegistry().registerObjectForInstance(
49
59
  productInfo.nftId,
50
- this.getRegistry().POLICY(),
51
- applicationOwner
52
- );
60
+ POLICY(),
61
+ applicationOwner);
53
62
 
54
63
  _policyInfo[nftId] = PolicyInfo(
55
64
  nftId,
56
- PolicyState.Applied,
65
+ _lifecycleModule.getInitialState(POLICY()),
57
66
  sumInsuredAmount,
58
67
  premiumAmount,
68
+ 0, // premium paid amount
59
69
  lifetime,
60
- block.timestamp,
70
+ block.timestamp, // createdAt
71
+ block.timestamp, // updatedAt
61
72
  0, // activatedAt
62
73
  0, // expiredAt
63
74
  0 // closedAt
@@ -68,13 +79,18 @@ abstract contract PolicyModule is IRegistryLinked, IPolicyModule {
68
79
  // add logging
69
80
  }
70
81
 
82
+ function processPremium(NftId nftId) external override onlyProductService2 {
83
+ PolicyInfo storage info = _policyInfo[nftId];
84
+ info.premiumPaidAmount = info.premiumAmount;
85
+ info.updatedAt = block.timestamp;
86
+ }
87
+
88
+
71
89
  function activate(NftId nftId) external override onlyProductService2 {
72
90
  PolicyInfo storage info = _policyInfo[nftId];
73
91
  info.activatedAt = block.timestamp;
74
92
  info.expiredAt = block.timestamp + info.lifetime;
75
- info.state = PolicyState.Active;
76
-
77
- // add logging
93
+ info.state = _lifecycleModule.checkAndLogTransition(nftId, POLICY(), info.state, ACTIVE());
78
94
  }
79
95
 
80
96
  function getBundleNftForPolicy(
@@ -7,23 +7,16 @@ import {NftId} from "../../types/NftId.sol";
7
7
  interface IPool {
8
8
  struct PoolInfo {
9
9
  NftId nftId;
10
- address wallet;
11
- address token;
12
10
  uint256 capital;
13
11
  uint256 lockedCapital;
14
12
  }
15
13
  }
16
14
 
17
15
  interface IPoolModule is IOwnable, IRegistryLinked, IPool {
18
- function underwrite(NftId poolNftId, NftId policyNftId) external;
19
16
 
20
- function createPoolInfo(
21
- NftId nftId,
22
- address wallet,
23
- address token
24
- ) external;
17
+ function underwrite(NftId policyNftId, NftId productNftId) external;
25
18
 
26
- function getPoolInfo(
27
- NftId nftId
28
- ) external view returns (PoolInfo memory info);
19
+ function registerPool(NftId nftId) external;
20
+
21
+ function getPoolInfo(NftId nftId) external view returns (PoolInfo memory info);
29
22
  }
@@ -4,6 +4,7 @@ pragma solidity ^0.8.19;
4
4
  import {IOwnable, IRegistry, IRegistryLinked} from "../../registry/IRegistry.sol";
5
5
  import {IProductService} from "../product/IProductService.sol";
6
6
  import {IPolicy, IPolicyModule} from "../policy/IPolicy.sol";
7
+ import {ITreasuryModule} from "../treasury/ITreasury.sol";
7
8
  import {IPoolModule} from "./IPoolModule.sol";
8
9
  import {NftId, NftIdLib} from "../../types/NftId.sol";
9
10
 
@@ -11,9 +12,12 @@ abstract contract PoolModule is IPoolModule {
11
12
  using NftIdLib for NftId;
12
13
 
13
14
  uint256 public constant INITIAL_CAPITAL = 10000 * 10 ** 6;
15
+ uint256 public constant INITIAL_LOCKED_CAPITAL = 0;
14
16
 
15
17
  mapping(NftId nftId => PoolInfo info) private _poolInfo;
16
18
 
19
+ IPolicyModule private _policyModule;
20
+ ITreasuryModule private _treasuryModule;
17
21
  IProductService private _productService;
18
22
 
19
23
  modifier onlyProductService() {
@@ -25,41 +29,46 @@ abstract contract PoolModule is IPoolModule {
25
29
  }
26
30
 
27
31
  constructor(address productService) {
32
+ _policyModule = IPolicyModule(address(this));
33
+ _treasuryModule = ITreasuryModule(address(this));
28
34
  _productService = IProductService(productService);
29
35
  }
30
36
 
31
- function createPoolInfo(
32
- NftId nftId,
33
- address wallet,
34
- address token
35
- ) public override {
37
+ function registerPool(NftId nftId)
38
+ public
39
+ override
40
+ {
36
41
  require(_poolInfo[nftId].nftId.eqz(), "ERROR:PL-001:ALREADY_CREATED");
37
42
 
38
43
  _poolInfo[nftId] = PoolInfo(
39
44
  nftId,
40
- wallet,
41
- token,
42
45
  INITIAL_CAPITAL,
43
- 0 // locked capital
46
+ INITIAL_LOCKED_CAPITAL
44
47
  );
45
48
  }
46
49
 
47
50
  function underwrite(
48
- NftId poolNftId,
49
- NftId policyNftId
50
- ) external override onlyProductService {
51
- PoolInfo storage poolInfo = _poolInfo[poolNftId];
52
- require(poolInfo.nftId == poolNftId, "ERROR:PL-002:POOL_UNKNOWN");
51
+ NftId policyNftId,
52
+ NftId productNftId
53
+ )
54
+ external
55
+ override
56
+ onlyProductService
57
+ {
58
+ IPolicy.PolicyInfo memory policyInfo = _policyModule.getPolicyInfo(policyNftId);
59
+ require(policyInfo.nftId == policyNftId, "ERROR:PL-002:POLICY_UNKNOWN");
53
60
 
54
- IPolicyModule policyModule = IPolicyModule(address(this));
55
- IPolicy.PolicyInfo memory policyInfo = policyModule.getPolicyInfo(
56
- policyNftId
57
- );
61
+ ITreasuryModule.ProductSetup memory product = _treasuryModule.getProductSetup(productNftId);
62
+ require(product.productNftId == productNftId, "ERROR:PL-003:PRODUCT_SETUP_MISSING");
63
+
64
+ NftId poolNftId = product.poolNftId;
65
+ PoolInfo storage poolInfo = _poolInfo[poolNftId];
66
+ require(poolInfo.nftId == poolNftId, "ERROR:PL-004:POOL_UNKNOWN");
58
67
 
59
68
  require(
60
69
  poolInfo.capital - poolInfo.lockedCapital >=
61
70
  policyInfo.sumInsuredAmount,
62
- "ERROR:PL-003:CAPACITY_TOO_LOW"
71
+ "ERROR:PL-005:CAPACITY_TOO_LOW"
63
72
  );
64
73
 
65
74
  poolInfo.lockedCapital += policyInfo.sumInsuredAmount;
@@ -23,7 +23,7 @@ interface IProductService is IRegistryLinked {
23
23
  // function expire(uint256 nftId) external;
24
24
  function close(NftId nftId) external;
25
25
 
26
- // function collectPremium(uint256 nftId, uint256 premiumAmount) external;
26
+ function collectPremium(NftId nftId) external;
27
27
 
28
28
  // function createClaim(uint256 nftId, uint256 claimAmount) external;
29
29
  // function confirmClaim(uint256 nftId, uint256 claimId, uint256 claimAmount) external;