@etherisc/gif-next 0.0.2-f30e0eb-805 → 0.0.2-f4f2d93-430

Sign up to get free protection for your applications and to get access to all the features.
Files changed (196) hide show
  1. package/README.md +6 -83
  2. package/artifacts/contracts/components/Component.sol/Component.dbg.json +1 -1
  3. package/artifacts/contracts/components/Component.sol/Component.json +10 -0
  4. package/artifacts/contracts/components/Distribution.sol/Distribution.dbg.json +1 -1
  5. package/artifacts/contracts/components/Distribution.sol/Distribution.json +10 -0
  6. package/artifacts/contracts/components/IComponent.sol/IComponent.dbg.json +1 -1
  7. package/artifacts/contracts/components/IComponent.sol/IComponent.json +100 -0
  8. package/artifacts/contracts/components/IDistributionComponent.sol/IDistributionComponent.dbg.json +1 -1
  9. package/artifacts/contracts/components/IDistributionComponent.sol/IDistributionComponent.json +100 -0
  10. package/artifacts/contracts/components/IPoolComponent.sol/IPoolComponent.dbg.json +1 -1
  11. package/artifacts/contracts/components/IPoolComponent.sol/IPoolComponent.json +110 -0
  12. package/artifacts/contracts/components/IProductComponent.sol/IProductComponent.dbg.json +1 -1
  13. package/artifacts/contracts/components/IProductComponent.sol/IProductComponent.json +100 -0
  14. package/artifacts/contracts/components/Pool.sol/Pool.dbg.json +1 -1
  15. package/artifacts/contracts/components/Pool.sol/Pool.json +20 -0
  16. package/artifacts/contracts/components/Product.sol/Product.dbg.json +1 -1
  17. package/artifacts/contracts/components/Product.sol/Product.json +10 -0
  18. package/artifacts/contracts/instance/AccessManagerUpgradeableInitializeable.sol/AccessManagerUpgradeableInitializeable.dbg.json +1 -1
  19. package/artifacts/contracts/instance/BundleManager.sol/BundleManager.dbg.json +1 -1
  20. package/artifacts/contracts/instance/BundleManager.sol/BundleManager.json +2 -2
  21. package/artifacts/contracts/instance/Cloneable.sol/Cloneable.dbg.json +1 -1
  22. package/artifacts/contracts/instance/IInstance.sol/IInstance.dbg.json +1 -1
  23. package/artifacts/contracts/instance/IInstance.sol/IInstance.json +34 -14
  24. package/artifacts/contracts/instance/IInstanceService.sol/IInstanceService.dbg.json +1 -1
  25. package/artifacts/contracts/instance/IInstanceService.sol/IInstanceService.json +80 -14
  26. package/artifacts/contracts/instance/Instance.sol/Instance.dbg.json +1 -1
  27. package/artifacts/contracts/instance/Instance.sol/Instance.json +90 -70
  28. package/artifacts/contracts/instance/InstanceAccessManager.sol/InstanceAccessManager.dbg.json +1 -1
  29. package/artifacts/contracts/instance/InstanceAccessManager.sol/InstanceAccessManager.json +76 -57
  30. package/artifacts/contracts/instance/InstanceReader.sol/InstanceReader.dbg.json +1 -1
  31. package/artifacts/contracts/instance/InstanceReader.sol/InstanceReader.json +21 -11
  32. package/artifacts/contracts/instance/InstanceService.sol/InstanceService.dbg.json +1 -1
  33. package/artifacts/contracts/instance/InstanceService.sol/InstanceService.json +208 -62
  34. package/artifacts/contracts/instance/InstanceServiceManager.sol/InstanceServiceManager.dbg.json +1 -1
  35. package/artifacts/contracts/instance/InstanceServiceManager.sol/InstanceServiceManager.json +62 -22
  36. package/artifacts/contracts/instance/ObjectManager.sol/ObjectManager.dbg.json +1 -1
  37. package/artifacts/contracts/instance/ObjectManager.sol/ObjectManager.json +2 -2
  38. package/artifacts/contracts/instance/base/ComponentService.sol/ComponentService.dbg.json +1 -1
  39. package/artifacts/contracts/instance/base/ComponentService.sol/ComponentService.json +85 -30
  40. package/artifacts/contracts/instance/base/IKeyValueStore.sol/IKeyValueStore.dbg.json +1 -1
  41. package/artifacts/contracts/instance/base/ILifecycle.sol/ILifecycle.dbg.json +1 -1
  42. package/artifacts/contracts/instance/base/KeyValueStore.sol/KeyValueStore.dbg.json +1 -1
  43. package/artifacts/contracts/instance/base/KeyValueStore.sol/KeyValueStore.json +2 -2
  44. package/artifacts/contracts/instance/base/Lifecycle.sol/Lifecycle.dbg.json +1 -1
  45. package/artifacts/contracts/instance/module/IAccess.sol/IAccess.dbg.json +1 -1
  46. package/artifacts/contracts/instance/module/IAccess.sol/IAccess.json +6 -6
  47. package/artifacts/contracts/instance/module/IBundle.sol/IBundle.dbg.json +1 -1
  48. package/artifacts/contracts/instance/module/IComponents.sol/IComponents.dbg.json +1 -1
  49. package/artifacts/contracts/instance/module/IDistribution.sol/IDistribution.dbg.json +1 -1
  50. package/artifacts/contracts/instance/module/IPolicy.sol/IPolicy.dbg.json +1 -1
  51. package/artifacts/contracts/instance/module/IRisk.sol/IRisk.dbg.json +1 -1
  52. package/artifacts/contracts/instance/module/ISetup.sol/ISetup.dbg.json +1 -1
  53. package/artifacts/contracts/instance/module/ITreasury.sol/ITreasury.dbg.json +1 -1
  54. package/artifacts/contracts/instance/service/ApplicationService.sol/ApplicationService.dbg.json +1 -1
  55. package/artifacts/contracts/instance/service/ApplicationService.sol/ApplicationService.json +124 -53
  56. package/artifacts/contracts/instance/service/ApplicationServiceManager.sol/ApplicationServiceManager.dbg.json +1 -1
  57. package/artifacts/contracts/instance/service/ApplicationServiceManager.sol/ApplicationServiceManager.json +21 -13
  58. package/artifacts/contracts/instance/service/BundleService.sol/BundleService.dbg.json +1 -1
  59. package/artifacts/contracts/instance/service/BundleService.sol/BundleService.json +298 -160
  60. package/artifacts/contracts/instance/service/BundleServiceManager.sol/BundleServiceManager.dbg.json +1 -1
  61. package/artifacts/contracts/instance/service/BundleServiceManager.sol/BundleServiceManager.json +74 -14
  62. package/artifacts/contracts/instance/service/ClaimService.sol/ClaimService.dbg.json +1 -1
  63. package/artifacts/contracts/instance/service/ClaimService.sol/ClaimService.json +104 -41
  64. package/artifacts/contracts/instance/service/ClaimServiceManager.sol/ClaimServiceManager.dbg.json +1 -1
  65. package/artifacts/contracts/instance/service/ClaimServiceManager.sol/ClaimServiceManager.json +11 -7
  66. package/artifacts/contracts/instance/service/DistributionService.sol/DistributionService.dbg.json +1 -1
  67. package/artifacts/contracts/instance/service/DistributionService.sol/DistributionService.json +189 -123
  68. package/artifacts/contracts/instance/service/DistributionServiceManager.sol/DistributionServiceManager.dbg.json +1 -1
  69. package/artifacts/contracts/instance/service/DistributionServiceManager.sol/DistributionServiceManager.json +48 -48
  70. package/artifacts/contracts/instance/service/IApplicationService.sol/IApplicationService.dbg.json +1 -1
  71. package/artifacts/contracts/instance/service/IApplicationService.sol/IApplicationService.json +80 -14
  72. package/artifacts/contracts/instance/service/IBundleService.sol/IBundleService.dbg.json +1 -1
  73. package/artifacts/contracts/instance/service/IBundleService.sol/IBundleService.json +142 -113
  74. package/artifacts/contracts/instance/service/IClaimService.sol/IClaimService.dbg.json +1 -1
  75. package/artifacts/contracts/instance/service/IClaimService.sol/IClaimService.json +80 -14
  76. package/artifacts/contracts/instance/service/IDistributionService.sol/IDistributionService.dbg.json +1 -1
  77. package/artifacts/contracts/instance/service/IDistributionService.sol/IDistributionService.json +101 -24
  78. package/artifacts/contracts/instance/service/IPolicyService.sol/IPolicyService.dbg.json +1 -1
  79. package/artifacts/contracts/instance/service/IPolicyService.sol/IPolicyService.json +90 -24
  80. package/artifacts/contracts/instance/service/IPoolService.sol/IPoolService.dbg.json +1 -1
  81. package/artifacts/contracts/instance/service/IPoolService.sol/IPoolService.json +430 -14
  82. package/artifacts/contracts/instance/service/IProductService.sol/IProductService.dbg.json +1 -1
  83. package/artifacts/contracts/instance/service/IProductService.sol/IProductService.json +80 -14
  84. package/artifacts/contracts/instance/service/PolicyService.sol/PolicyService.dbg.json +1 -1
  85. package/artifacts/contracts/instance/service/PolicyService.sol/PolicyService.json +136 -65
  86. package/artifacts/contracts/instance/service/PolicyServiceManager.sol/PolicyServiceManager.dbg.json +1 -1
  87. package/artifacts/contracts/instance/service/PolicyServiceManager.sol/PolicyServiceManager.json +27 -19
  88. package/artifacts/contracts/instance/service/PoolService.sol/PoolService.dbg.json +1 -1
  89. package/artifacts/contracts/instance/service/PoolService.sol/PoolService.json +468 -55
  90. package/artifacts/contracts/instance/service/PoolServiceManager.sol/PoolServiceManager.dbg.json +1 -1
  91. package/artifacts/contracts/instance/service/PoolServiceManager.sol/PoolServiceManager.json +18 -14
  92. package/artifacts/contracts/instance/service/ProductService.sol/ProductService.dbg.json +1 -1
  93. package/artifacts/contracts/instance/service/ProductService.sol/ProductService.json +114 -51
  94. package/artifacts/contracts/instance/service/ProductServiceManager.sol/ProductServiceManager.dbg.json +1 -1
  95. package/artifacts/contracts/instance/service/ProductServiceManager.sol/ProductServiceManager.json +16 -12
  96. package/artifacts/contracts/registry/ChainNft.sol/ChainNft.dbg.json +1 -1
  97. package/artifacts/contracts/registry/IRegistry.sol/IRegistry.dbg.json +1 -1
  98. package/artifacts/contracts/registry/IRegistryService.sol/IRegistryService.dbg.json +1 -1
  99. package/artifacts/contracts/registry/IRegistryService.sol/IRegistryService.json +0 -24
  100. package/artifacts/contracts/registry/ITransferInterceptor.sol/ITransferInterceptor.dbg.json +1 -1
  101. package/artifacts/contracts/registry/Registry.sol/Registry.dbg.json +1 -1
  102. package/artifacts/contracts/registry/Registry.sol/Registry.json +2 -2
  103. package/artifacts/contracts/registry/RegistryAccessManager.sol/RegistryAccessManager.dbg.json +1 -1
  104. package/artifacts/contracts/registry/RegistryAccessManager.sol/RegistryAccessManager.json +2 -2
  105. package/artifacts/contracts/registry/RegistryService.sol/RegistryService.dbg.json +1 -1
  106. package/artifacts/contracts/registry/RegistryService.sol/RegistryService.json +17 -36
  107. package/artifacts/contracts/registry/RegistryServiceManager.sol/RegistryServiceManager.dbg.json +1 -1
  108. package/artifacts/contracts/registry/RegistryServiceManager.sol/RegistryServiceManager.json +7 -7
  109. package/artifacts/contracts/registry/ReleaseManager.sol/ReleaseManager.dbg.json +1 -1
  110. package/artifacts/contracts/registry/ReleaseManager.sol/ReleaseManager.json +23 -11
  111. package/artifacts/contracts/registry/TokenRegistry.sol/TokenRegistry.dbg.json +1 -1
  112. package/artifacts/contracts/shared/ContractDeployerLib.sol/ContractDeployerLib.dbg.json +1 -1
  113. package/artifacts/contracts/shared/ERC165.sol/ERC165.dbg.json +1 -1
  114. package/artifacts/contracts/shared/INftOwnable.sol/INftOwnable.dbg.json +1 -1
  115. package/artifacts/contracts/shared/IPolicyHolder.sol/IPolicyHolder.dbg.json +1 -1
  116. package/artifacts/contracts/shared/IRegisterable.sol/IRegisterable.dbg.json +1 -1
  117. package/artifacts/contracts/shared/IRegistryLinked.sol/IRegistryLinked.dbg.json +1 -1
  118. package/artifacts/contracts/shared/IService.sol/IService.dbg.json +1 -1
  119. package/artifacts/contracts/shared/IService.sol/IService.json +80 -14
  120. package/artifacts/contracts/shared/IVersionable.sol/IVersionable.dbg.json +1 -1
  121. package/artifacts/contracts/shared/NftOwnable.sol/NftOwnable.dbg.json +1 -1
  122. package/artifacts/contracts/shared/PolicyHolder.sol/PolicyHolder.dbg.json +1 -1
  123. package/artifacts/contracts/shared/ProxyManager.sol/ProxyManager.dbg.json +1 -1
  124. package/artifacts/contracts/shared/Registerable.sol/Registerable.dbg.json +1 -1
  125. package/artifacts/contracts/shared/RegistryLinked.sol/RegistryLinked.dbg.json +1 -1
  126. package/artifacts/contracts/shared/Service.sol/Service.dbg.json +1 -1
  127. package/artifacts/contracts/shared/Service.sol/Service.json +86 -15
  128. package/artifacts/contracts/shared/TokenHandler.sol/TokenHandler.dbg.json +1 -1
  129. package/artifacts/contracts/shared/UpgradableProxyWithAdmin.sol/UpgradableProxyWithAdmin.dbg.json +1 -1
  130. package/artifacts/contracts/shared/Versionable.sol/Versionable.dbg.json +1 -1
  131. package/artifacts/contracts/test/TestFee.sol/TestFee.dbg.json +1 -1
  132. package/artifacts/contracts/test/TestFee.sol/TestFee.json +2 -2
  133. package/artifacts/contracts/test/TestRegisterable.sol/TestRegisterable.dbg.json +1 -1
  134. package/artifacts/contracts/test/TestRoleId.sol/TestRoleId.dbg.json +1 -1
  135. package/artifacts/contracts/test/TestService.sol/TestService.dbg.json +1 -1
  136. package/artifacts/contracts/test/TestService.sol/TestService.json +101 -26
  137. package/artifacts/contracts/test/TestToken.sol/TestUsdc.dbg.json +1 -1
  138. package/artifacts/contracts/test/TestVersion.sol/TestVersion.dbg.json +1 -1
  139. package/artifacts/contracts/test/TestVersionable.sol/TestVersionable.dbg.json +1 -1
  140. package/artifacts/contracts/test/Usdc.sol/USDC.dbg.json +1 -1
  141. package/artifacts/contracts/types/AddressSet.sol/LibAddressSet.dbg.json +1 -1
  142. package/artifacts/contracts/types/Amount.sol/AmountLib.dbg.json +4 -0
  143. package/artifacts/contracts/types/Amount.sol/AmountLib.json +161 -0
  144. package/artifacts/contracts/types/Blocknumber.sol/BlocknumberLib.dbg.json +1 -1
  145. package/artifacts/contracts/types/ChainId.sol/ChainIdLib.dbg.json +1 -1
  146. package/artifacts/contracts/types/ClaimId.sol/ClaimIdLib.dbg.json +1 -1
  147. package/artifacts/contracts/types/DistributorType.sol/DistributorTypeLib.dbg.json +1 -1
  148. package/artifacts/contracts/types/Fee.sol/FeeLib.dbg.json +1 -1
  149. package/artifacts/contracts/types/Fee.sol/FeeLib.json +40 -9
  150. package/artifacts/contracts/types/Key32.sol/Key32Lib.dbg.json +1 -1
  151. package/artifacts/contracts/types/NftId.sol/NftIdLib.dbg.json +1 -1
  152. package/artifacts/contracts/types/NftIdSet.sol/LibNftIdSet.dbg.json +1 -1
  153. package/artifacts/contracts/types/NumberId.sol/NumberIdLib.dbg.json +1 -1
  154. package/artifacts/contracts/types/ObjectType.sol/ObjectTypeLib.dbg.json +1 -1
  155. package/artifacts/contracts/types/PayoutId.sol/PayoutIdLib.dbg.json +1 -1
  156. package/artifacts/contracts/types/Referral.sol/ReferralLib.dbg.json +1 -1
  157. package/artifacts/contracts/types/RiskId.sol/RiskIdLib.dbg.json +1 -1
  158. package/artifacts/contracts/types/RoleId.sol/RoleIdLib.dbg.json +1 -1
  159. package/artifacts/contracts/types/Seconds.sol/SecondsLib.dbg.json +1 -1
  160. package/artifacts/contracts/types/StateId.sol/StateIdLib.dbg.json +1 -1
  161. package/artifacts/contracts/types/Timestamp.sol/TimestampLib.dbg.json +1 -1
  162. package/artifacts/contracts/types/UFixed.sol/MathLib.dbg.json +1 -1
  163. package/artifacts/contracts/types/UFixed.sol/UFixedLib.dbg.json +1 -1
  164. package/artifacts/contracts/types/Version.sol/VersionLib.dbg.json +1 -1
  165. package/artifacts/contracts/types/Version.sol/VersionPartLib.dbg.json +1 -1
  166. package/contracts/components/Component.sol +4 -2
  167. package/contracts/components/IComponent.sol +3 -1
  168. package/contracts/components/Pool.sol +8 -4
  169. package/contracts/instance/Instance.sol +1 -0
  170. package/contracts/instance/InstanceAccessManager.sol +16 -11
  171. package/contracts/instance/InstanceReader.sol +1 -0
  172. package/contracts/instance/InstanceService.sol +19 -19
  173. package/contracts/instance/base/ComponentService.sol +17 -30
  174. package/contracts/instance/base/KeyValueStore.sol +8 -3
  175. package/contracts/instance/module/IAccess.sol +2 -2
  176. package/contracts/instance/module/IBundle.sol +6 -4
  177. package/contracts/instance/module/IComponents.sol +6 -0
  178. package/contracts/instance/service/ApplicationService.sol +9 -9
  179. package/contracts/instance/service/BundleService.sol +155 -60
  180. package/contracts/instance/service/ClaimService.sol +3 -3
  181. package/contracts/instance/service/DistributionService.sol +24 -56
  182. package/contracts/instance/service/IBundleService.sol +53 -22
  183. package/contracts/instance/service/IDistributionService.sol +1 -0
  184. package/contracts/instance/service/IPoolService.sol +65 -1
  185. package/contracts/instance/service/PolicyService.sol +60 -101
  186. package/contracts/instance/service/PoolService.sol +132 -49
  187. package/contracts/instance/service/ProductService.sol +20 -51
  188. package/contracts/registry/IRegistryService.sol +4 -3
  189. package/contracts/registry/RegistryService.sol +10 -11
  190. package/contracts/registry/ReleaseManager.sol +20 -18
  191. package/contracts/shared/IService.sol +4 -6
  192. package/contracts/shared/Service.sol +21 -7
  193. package/contracts/test/TestService.sol +1 -1
  194. package/contracts/types/Amount.sol +60 -0
  195. package/contracts/types/Fee.sol +12 -5
  196. package/package.json +1 -1
@@ -16,6 +16,7 @@ import {ObjectType, POOL, BUNDLE} from "../../types/ObjectType.sol";
16
16
  import {POOL_OWNER_ROLE, RoleId} from "../../types/RoleId.sol";
17
17
  import {Pool} from "../../components/Pool.sol";
18
18
 
19
+ import {Amount, AmountLib} from "../../types/Amount.sol";
19
20
  import {Fee, FeeLib} from "../../types/Fee.sol";
20
21
  import {StateId, ACTIVE, PAUSED, CLOSED, KEEP_STATE} from "../../types/StateId.sol";
21
22
  import {Seconds} from "../../types/Seconds.sol";
@@ -57,38 +58,85 @@ contract BundleService is
57
58
  (registryAddress, initialOwner) = abi.decode(data, (address, address));
58
59
  // TODO while PoolService is not deployed in PoolServiceManager constructor
59
60
  // owner is PoolServiceManager deployer
60
- initializeService(registryAddress, owner);
61
+ initializeService(registryAddress, address(0), owner);
61
62
  registerInterface(type(IBundleService).interfaceId);
62
63
  }
63
64
 
64
- function getDomain() public pure override(Service, IService) returns(ObjectType) {
65
+ function getDomain() public pure override returns(ObjectType) {
65
66
  return BUNDLE();
66
67
  }
67
68
 
69
+ // TODO staking/unstaking as well as collateralize need to go to pool service
70
+ // it will also be the pool service that is updating the pool info data
71
+ // collateralize -> potentially accumulate pool fees
72
+ // collateralize: additional reason to move to pool, pool might has retential level < 1 ...
73
+ // staking -> potentially accumulate staking fees
74
+ // unstaking -> potentially accumulate performance fees
75
+ function _updatePoolWithStakes(
76
+ IInstance instance,
77
+ NftId poolNftId,
78
+ uint256 stakingAmount
79
+ )
80
+ internal
81
+ returns (
82
+ TokenHandler tokenHandler,
83
+ address wallet,
84
+ uint256 netStakingAmount
85
+ )
86
+ {
87
+ if(stakingAmount > 0) {
88
+ InstanceReader instanceReader = instance.getInstanceReader();
89
+ IComponents.ComponentInfo memory componentInfo = instanceReader.getComponentInfo(poolNftId);
90
+
91
+ tokenHandler = componentInfo.tokenHandler;
92
+ wallet = componentInfo.wallet;
93
+
94
+ IComponents.PoolInfo memory poolInfo = abi.decode(componentInfo.data, (IComponents.PoolInfo));
95
+ uint256 poolFeeAmount;
96
+
97
+ // calculate pool fee and net staking amount
98
+ (poolFeeAmount, netStakingAmount) = FeeLib.calculateFee(poolInfo.stakingFee, stakingAmount);
99
+
100
+ // update pool balance and fee amount
101
+ poolInfo.balanceAmount += netStakingAmount;
102
+
103
+ if(poolFeeAmount > 0) {
104
+ poolInfo.feeAmount += poolFeeAmount;
105
+ }
106
+
107
+ // save updated pool info
108
+ componentInfo.data = abi.encode(poolInfo);
109
+ instance.updatePoolSetup(poolNftId, componentInfo, KEEP_STATE());
110
+ }
111
+ }
112
+
113
+
68
114
  function create(
115
+ IInstance instance,
116
+ NftId poolNftId,
69
117
  address owner,
70
118
  Fee memory fee,
71
- uint256 stakingAmount,
119
+ Amount stakingAmount,
72
120
  Seconds lifetime,
73
121
  bytes calldata filter
74
122
  )
75
123
  external
76
124
  override
125
+ // TODO add restricted and add authz for pool service
77
126
  returns(NftId bundleNftId)
78
127
  {
79
- (IRegistry.ObjectInfo memory info, IInstance instance) = _getAndVerifyComponentInfoAndInstance(POOL());
80
128
  InstanceReader instanceReader = instance.getInstanceReader();
81
- NftId poolNftId = info.nftId;
82
129
 
130
+ // create initial bundle info
83
131
  IBundle.BundleInfo memory bundleInfo = IBundle.BundleInfo(
84
132
  poolNftId,
85
133
  fee,
86
134
  filter,
87
135
  stakingAmount,
88
- 0,
89
- stakingAmount,
136
+ AmountLib.zero(),
137
+ AmountLib.zero(),
90
138
  lifetime,
91
- zeroTimestamp(),
139
+ TimestampLib.blockTimestamp().addSeconds(lifetime),
92
140
  zeroTimestamp()
93
141
  );
94
142
 
@@ -101,21 +149,24 @@ contract BundleService is
101
149
  false, // intercepting property for bundles is defined on pool
102
150
  address(0),
103
151
  owner,
104
- abi.encode(bundleInfo)
152
+ "" // bundle data to be stored in registry
105
153
  )
106
154
  );
107
155
 
108
156
  // create bundle info in instance
109
157
  instance.createBundle(bundleNftId, bundleInfo);
110
158
 
159
+ // put bundle under bundle managemet
111
160
  BundleManager bundleManager = instance.getBundleManager();
112
161
  bundleManager.add(bundleNftId);
113
162
 
114
- _processStakingByTreasury(
115
- instanceReader,
116
- poolNftId,
117
- bundleNftId,
118
- stakingAmount);
163
+ // transfer full staking amount to pool wallet
164
+ IComponents.ComponentInfo memory componentInfo = instanceReader.getComponentInfo(poolNftId);
165
+ componentInfo.tokenHandler.transfer(
166
+ owner,
167
+ componentInfo.wallet,
168
+ stakingAmount.toInt()
169
+ );
119
170
 
120
171
  // TODO add logging
121
172
  }
@@ -127,9 +178,8 @@ contract BundleService is
127
178
  external
128
179
  override
129
180
  {
130
- (IRegistry.ObjectInfo memory info , IInstance instance) = _getAndVerifyComponentInfoAndInstance(POOL());
181
+ (NftId poolNftId, IRegistry.ObjectInfo memory info , IInstance instance) = _getAndVerifyComponentInfoAndInstance(POOL());
131
182
  InstanceReader instanceReader = instance.getInstanceReader();
132
- NftId poolNftId = info.nftId;
133
183
 
134
184
  IBundle.BundleInfo memory bundleInfo = instanceReader.getBundleInfo(bundleNftId);
135
185
  require(bundleInfo.poolNftId.gtz(), "ERROR:PLS-010:BUNDLE_UNKNOWN");
@@ -141,31 +191,72 @@ contract BundleService is
141
191
  }
142
192
 
143
193
 
194
+ // the bundle MUST be unlocked (active) for linking (underwriting) and registered with this instance
144
195
  function lockCollateral(
145
196
  IInstance instance,
146
197
  NftId policyNftId,
147
198
  NftId bundleNftId,
148
- uint256 collateralAmount,
149
- uint256 netPremiumAmount
199
+ uint256 collateralAmount, // required amount to collateralize policy
200
+ uint256 premiumAmount // premium part that reaches bundle for this policy
150
201
  )
151
202
  external
152
- onlyService
153
- returns (
154
- IBundle.BundleInfo memory bundleInfo
155
- )
203
+ onlyService // TODO replace with restricted + appropriate granting
156
204
  {
157
205
  InstanceReader instanceReader = instance.getInstanceReader();
158
- bundleInfo = instanceReader.getBundleInfo(bundleNftId);
206
+ StateId bundleState = instanceReader.getMetadata(bundleNftId.toKey32(BUNDLE())).state;
207
+ IBundle.BundleInfo memory bundleInfo = instanceReader.getBundleInfo(bundleNftId);
159
208
 
160
- // TODO add validation
209
+ // ensure bundle is active and not yet expired
210
+ if(bundleState != ACTIVE() || bundleInfo.expiredAt < TimestampLib.blockTimestamp()) {
211
+ revert ErrorBundleServiceBundleNotOpen(bundleNftId, bundleState, bundleInfo.expiredAt);
212
+ }
161
213
 
162
- // lock collateral
163
- bundleInfo.lockedAmount += collateralAmount;
164
- bundleInfo.balanceAmount += netPremiumAmount;
214
+ // ensure bundle capacity is sufficent to collateralize policy
215
+ uint capacity = bundleInfo.capitalAmount.toInt() + premiumAmount - bundleInfo.lockedAmount.toInt();
216
+ if(capacity < collateralAmount) {
217
+ revert ErrorBundleServiceCapacityInsufficient(bundleNftId, capacity, collateralAmount);
218
+ }
165
219
 
166
- instance.updateBundle(bundleNftId, bundleInfo, KEEP_STATE());
220
+ // TODO add more validation
167
221
 
168
- linkPolicy(instance, policyNftId);
222
+ // updated locked amount
223
+ bundleInfo.lockedAmount = AmountLib.toAmount(bundleInfo.lockedAmount.toInt() + collateralAmount);
224
+
225
+ // update capital and fees when premiums are involved
226
+ _updateBundleWithPremium(instance, bundleNftId, bundleInfo, premiumAmount);
227
+
228
+ // link policy to bundle in bundle manger
229
+ _linkPolicy(instance, policyNftId);
230
+ }
231
+
232
+
233
+ function _updateBundleWithPremium(
234
+ IInstance instance,
235
+ NftId bundleNftId,
236
+ IBundle.BundleInfo memory bundleInfo,
237
+ uint256 premiumAmount
238
+ )
239
+ internal
240
+ {
241
+ // update bundle capital and fee amounts
242
+ if(premiumAmount > 0) {
243
+ // calculate fees and net premium amounts
244
+ (
245
+ uint256 feeAmount,
246
+ uint256 netPremiumAmount
247
+ ) = FeeLib.calculateFee(bundleInfo.fee, premiumAmount);
248
+
249
+ // update bundle info with additional capital
250
+ bundleInfo.capitalAmount = AmountLib.toAmount(bundleInfo.capitalAmount.toInt() + netPremiumAmount);
251
+
252
+ // update bundle info with additional fees
253
+ if(feeAmount > 0) {
254
+ bundleInfo.feeAmount = AmountLib.toAmount(bundleInfo.feeAmount.toInt() + feeAmount);
255
+ }
256
+ }
257
+
258
+ // save updated bundle info
259
+ instance.updateBundle(bundleNftId, bundleInfo, KEEP_STATE());
169
260
  }
170
261
 
171
262
 
@@ -173,7 +264,7 @@ contract BundleService is
173
264
  external
174
265
  virtual
175
266
  {
176
- (, IInstance instance) = _getAndVerifyComponentInfoAndInstance(POOL());
267
+ (,, IInstance instance) = _getAndVerifyComponentInfoAndInstance(POOL());
177
268
 
178
269
  // udpate bundle state
179
270
  instance.updateBundleState(bundleNftId, PAUSED());
@@ -190,7 +281,7 @@ contract BundleService is
190
281
  external
191
282
  virtual
192
283
  {
193
- (, IInstance instance) = _getAndVerifyComponentInfoAndInstance(POOL());
284
+ (,, IInstance instance) = _getAndVerifyComponentInfoAndInstance(POOL());
194
285
 
195
286
  // udpate bundle state
196
287
  instance.updateBundleState(bundleNftId, ACTIVE());
@@ -203,12 +294,14 @@ contract BundleService is
203
294
  }
204
295
 
205
296
 
206
- function close(NftId bundleNftId)
297
+ function close(
298
+ IInstance instance,
299
+ NftId bundleNftId
300
+ )
207
301
  external
208
302
  virtual
303
+ // TODO add restricted and autz for pool service
209
304
  {
210
- (, IInstance instance) = _getAndVerifyComponentInfoAndInstance(POOL());
211
-
212
305
  // udpate bundle state
213
306
  instance.updateBundleState(bundleNftId, CLOSED());
214
307
 
@@ -221,14 +314,13 @@ contract BundleService is
221
314
 
222
315
  // update set of active bundles
223
316
  bundleManager.lock(bundleNftId);
224
-
225
- emit LogBundleServiceBundleClosed(bundleNftId);
226
317
  }
227
318
 
228
319
 
229
- function increaseBalance(IInstance instance,
320
+ function increaseBalance(
321
+ IInstance instance,
230
322
  NftId bundleNftId,
231
- uint256 amount
323
+ uint256 premiumAmount
232
324
  )
233
325
  external
234
326
  onlyService
@@ -236,12 +328,13 @@ contract BundleService is
236
328
  InstanceReader instanceReader = instance.getInstanceReader();
237
329
  IBundle.BundleInfo memory bundleInfo = instanceReader.getBundleInfo(bundleNftId);
238
330
 
239
- bundleInfo.balanceAmount += amount;
331
+ // update capital and fees when premiums are involved
332
+ _updateBundleWithPremium(instance, bundleNftId, bundleInfo, premiumAmount);
240
333
 
241
- instance.updateBundle(bundleNftId, bundleInfo, KEEP_STATE());
334
+ // TODO add logging (?)
242
335
  }
243
336
 
244
- function closePolicy(IInstance instance,
337
+ function releaseCollateral(IInstance instance,
245
338
  NftId policyNftId,
246
339
  NftId bundleNftId,
247
340
  uint256 collateralAmount
@@ -252,24 +345,23 @@ contract BundleService is
252
345
  InstanceReader instanceReader = instance.getInstanceReader();
253
346
  IBundle.BundleInfo memory bundleInfo = instanceReader.getBundleInfo(bundleNftId);
254
347
 
255
- // lock collateral
256
- bundleInfo.lockedAmount -= collateralAmount;
348
+ // reduce locked amount by released collateral amount
349
+ bundleInfo.lockedAmount = AmountLib.toAmount(bundleInfo.lockedAmount.toInt() - collateralAmount);
257
350
 
258
351
  instance.updateBundle(bundleNftId, bundleInfo, KEEP_STATE());
259
352
 
260
- unlinkPolicy(instance, policyNftId);
353
+ _unlinkPolicy(instance, policyNftId);
261
354
  }
262
355
 
263
356
  /// @dev links policy to bundle
264
- function linkPolicy(IInstance instance, NftId policyNftId)
357
+ function _linkPolicy(IInstance instance, NftId policyNftId)
265
358
  internal
266
- onlyService
267
359
  {
268
360
  InstanceReader instanceReader = instance.getInstanceReader();
269
361
  IPolicy.PolicyInfo memory policyInfo = instanceReader.getPolicyInfo(policyNftId);
270
362
 
271
- // ensure policy has not yet been activated
272
- if (policyInfo.activatedAt.gtz()) {
363
+ // ensure policy has not yet been activated in a previous tx already
364
+ if (policyInfo.activatedAt.gtz() && policyInfo.activatedAt < TimestampLib.blockTimestamp()) {
273
365
  revert BundleManager.ErrorBundleManagerPolicyAlreadyActivated(policyNftId);
274
366
  }
275
367
 
@@ -278,9 +370,8 @@ contract BundleService is
278
370
  }
279
371
 
280
372
  /// @dev unlinks policy from bundle
281
- function unlinkPolicy(IInstance instance, NftId policyNftId)
373
+ function _unlinkPolicy(IInstance instance, NftId policyNftId)
282
374
  internal
283
- onlyService
284
375
  {
285
376
  InstanceReader instanceReader = instance.getInstanceReader();
286
377
  IPolicy.PolicyInfo memory policyInfo = instanceReader.getPolicyInfo(policyNftId);
@@ -303,34 +394,38 @@ contract BundleService is
303
394
  bundleManager.unlinkPolicy(policyNftId);
304
395
  }
305
396
 
397
+ // TODO move this to pool service
306
398
  function _processStakingByTreasury(
307
399
  InstanceReader instanceReader,
308
400
  NftId poolNftId,
309
401
  NftId bundleNftId,
310
- uint256 stakingAmount
402
+ Amount stakingAmount
311
403
  )
312
404
  internal
313
405
  {
314
406
  // process token transfer(s)
315
- if(stakingAmount > 0) {
407
+ if(stakingAmount.gtz()) {
316
408
  IComponents.ComponentInfo memory componentInfo = instanceReader.getComponentInfo(poolNftId);
317
- IComponents.PoolInfo memory poolInfo = abi.decode(
318
- componentInfo.data, (IComponents.PoolInfo));
409
+ IComponents.PoolInfo memory poolInfo = abi.decode(componentInfo.data, (IComponents.PoolInfo));
319
410
 
320
411
  TokenHandler tokenHandler = componentInfo.tokenHandler;
321
412
  address bundleOwner = getRegistry().ownerOf(bundleNftId);
322
413
  Fee memory stakingFee = poolInfo.stakingFee;
323
414
 
415
+ // pool fee and bundle capital book keeping
416
+ if (FeeLib.gtz(stakingFee)) {
417
+ (uint256 stakingFeeAmount, uint256 netAmount) = FeeLib.calculateFee(stakingFee, stakingAmount.toInt());
418
+
419
+
420
+ // TODO: track staking fees in pool's state (issue #177)
421
+ }
422
+
423
+ // transfer full staking amount to pool wallet
324
424
  tokenHandler.transfer(
325
425
  bundleOwner,
326
426
  componentInfo.wallet,
327
- stakingAmount
427
+ stakingAmount.toInt()
328
428
  );
329
-
330
- if (! FeeLib.feeIsZero(stakingFee)) {
331
- (uint256 stakingFeeAmount, uint256 netAmount) = FeeLib.calculateFee(stakingFee, stakingAmount);
332
- // TODO: track staking fees in pool's state (issue #177)
333
- }
334
429
  }
335
430
  }
336
431
  }
@@ -64,12 +64,12 @@ contract ClaimService is
64
64
  address initialOwner;
65
65
  (registryAddress, initialOwner) = abi.decode(data, (address, address));
66
66
 
67
- initializeService(registryAddress, owner);
67
+ initializeService(registryAddress, address(0), owner);
68
68
  registerInterface(type(IClaimService).interfaceId);
69
69
  }
70
70
 
71
71
 
72
- function getDomain() public pure override(IService, Service) returns(ObjectType) {
72
+ function getDomain() public pure override returns(ObjectType) {
73
73
  return CLAIM();
74
74
  }
75
75
 
@@ -145,7 +145,7 @@ contract ClaimService is
145
145
 
146
146
  function _getAndVerifyInstanceAndProduct() internal view returns (Product product) {
147
147
  IRegistry.ObjectInfo memory productInfo;
148
- (productInfo,) = _getAndVerifyComponentInfoAndInstance(PRODUCT());
148
+ (, productInfo,) = _getAndVerifyComponentInfoAndInstance(PRODUCT());
149
149
  product = Product(productInfo.objectAddress);
150
150
  }
151
151
  }
@@ -60,11 +60,11 @@ contract DistributionService is
60
60
  (registryAddress, initialOwner) = abi.decode(data, (address, address));
61
61
  // TODO while DistributionService is not deployed in DistributionServiceManager constructor
62
62
  // owner is DistributionServiceManager deployer
63
- initializeService(registryAddress, owner);
63
+ initializeService(registryAddress, address(0), owner);
64
64
  registerInterface(type(IDistributionService).interfaceId);
65
65
  }
66
66
 
67
- function getDomain() public pure override(Service, IService) returns(ObjectType) {
67
+ function getDomain() public pure override returns(ObjectType) {
68
68
  return DISTRIBUTION();
69
69
  }
70
70
 
@@ -119,9 +119,8 @@ contract DistributionService is
119
119
  revert ErrorIDistributionServiceMinFeeTooHigh(minDistributionOwnerFee.fractionalFee.toInt(), distributionFee.fractionalFee.toInt());
120
120
  }
121
121
 
122
- (IRegistry.ObjectInfo memory info , IInstance instance) = _getAndVerifyComponentInfoAndInstance(DISTRIBUTION());
122
+ (NftId distributionNftId, IRegistry.ObjectInfo memory info , IInstance instance) = _getAndVerifyComponentInfoAndInstance(DISTRIBUTION());
123
123
  InstanceReader instanceReader = instance.getInstanceReader();
124
- NftId distributionNftId = info.nftId;
125
124
 
126
125
  ISetup.DistributionSetupInfo memory distSetupInfo = instanceReader.getDistributionSetupInfo(distributionNftId);
127
126
  distSetupInfo.minDistributionOwnerFee = minDistributionOwnerFee;
@@ -144,7 +143,7 @@ contract DistributionService is
144
143
  external
145
144
  returns (DistributorType distributorType)
146
145
  {
147
- (,NftId distributionNftId, IInstance instance) = _getAndVerifyCallingDistribution();
146
+ (NftId distributionNftId, IInstance instance) = _getAndVerifyCallingDistribution();
148
147
 
149
148
  {
150
149
  ISetup.DistributionSetupInfo memory setupInfo = instance.getInstanceReader().getDistributionSetupInfo(distributionNftId);
@@ -180,7 +179,7 @@ contract DistributionService is
180
179
  bytes memory data
181
180
  ) external returns (NftId distributorNftId)
182
181
  {
183
- (, NftId distributionNftId, IInstance instance) = _getAndVerifyCallingDistribution();
182
+ (NftId distributionNftId, IInstance instance) = _getAndVerifyCallingDistribution();
184
183
 
185
184
  distributorNftId = getRegistryService().registerDistributor(
186
185
  IRegistry.ObjectInfo(
@@ -209,7 +208,7 @@ contract DistributionService is
209
208
  bytes memory data
210
209
  ) external virtual
211
210
  {
212
- (,, IInstance instance) = _getAndVerifyCallingDistribution();
211
+ (, IInstance instance) = _getAndVerifyCallingDistribution();
213
212
  InstanceReader instanceReader = instance.getInstanceReader();
214
213
  IDistribution.DistributorInfo memory distributorInfo = instanceReader.getDistributorInfo(distributorNftId);
215
214
  distributorInfo.distributorType = distributorType;
@@ -230,7 +229,7 @@ contract DistributionService is
230
229
  virtual
231
230
  returns (ReferralId referralId)
232
231
  {
233
- (,NftId distributionNftId, IInstance instance) = _getAndVerifyCallingDistribution();
232
+ (NftId distributionNftId, IInstance instance) = _getAndVerifyCallingDistribution();
234
233
 
235
234
  if (bytes(code).length == 0) {
236
235
  revert ErrorIDistributionServiceInvalidReferral(code);
@@ -288,7 +287,7 @@ contract DistributionService is
288
287
  revert ErrorIDistributionServiceReferralInvalid(distributionNftId, referralId);
289
288
  }
290
289
 
291
- (, IInstance instance) = _getAndVerifyDistribution(distributionNftId);
290
+ IInstance instance = _getInstanceForDistribution(distributionNftId);
292
291
  InstanceReader reader = instance.getInstanceReader();
293
292
  IDistribution.ReferralInfo memory referralInfo = reader.getReferralInfo(referralId);
294
293
  IDistribution.DistributorInfo memory distributorInfo = reader.getDistributorInfo(referralInfo.distributorNftId);
@@ -329,7 +328,7 @@ contract DistributionService is
329
328
  view
330
329
  returns (IPolicy.Premium memory finalPremium)
331
330
  {
332
- (, IInstance instance) = _getAndVerifyDistribution(distributionNftId);
331
+ IInstance instance = _getInstanceForDistribution(distributionNftId);
333
332
  InstanceReader reader = instance.getInstanceReader();
334
333
 
335
334
  // first calculate all fixed and variable fees for the distribution - this will defined the fullPremium
@@ -390,7 +389,7 @@ contract DistributionService is
390
389
 
391
390
  // TODO: zero should return false
392
391
  function referralIsValid(NftId distributionNftId, ReferralId referralId) public view returns (bool isValid) {
393
- (address distributionAddress, IInstance instance) = _getAndVerifyDistribution(distributionNftId);
392
+ IInstance instance = _getInstanceForDistribution(distributionNftId);
394
393
  IDistribution.ReferralInfo memory info = instance.getInstanceReader().getReferralInfo(referralId);
395
394
 
396
395
  if (info.distributorNftId.eqz()) {
@@ -405,63 +404,32 @@ contract DistributionService is
405
404
  internal
406
405
  view
407
406
  returns(
408
- address distributionAddress,
409
407
  NftId distributionNftId,
410
408
  IInstance instance
411
409
  )
412
410
  {
413
- ObjectType objectType;
414
- (
415
- distributionAddress,
416
- distributionNftId,
417
- objectType,
418
- instance
419
- ) = _getAndVerifyCaller();
420
-
421
- if(objectType != DISTRIBUTION()) {
422
- revert ErrorIDistributionServiceCallerNotDistributor(msg.sender);
411
+ distributionNftId = getRegistry().getNftId(msg.sender);
412
+ if (distributionNftId.eqz()) {
413
+ revert ErrorDistributionServiceCallerNotRegistered(msg.sender);
423
414
  }
424
- }
425
415
 
426
- function _getAndVerifyDistribution(NftId distributionNftId)
427
- internal
428
- view
429
- returns(
430
- address distributionAddress,
431
- IInstance instance
432
- )
433
- {
434
416
  IRegistry.ObjectInfo memory info = getRegistry().getObjectInfo(distributionNftId);
435
- IRegistry.ObjectInfo memory parentInfo = getRegistry().getObjectInfo(info.parentNftId);
436
- if (parentInfo.objectType != INSTANCE()) {
437
- revert ErrorIDistributionServiceParentNftIdNotInstance(distributionNftId, info.parentNftId);
417
+ if(info.objectType != DISTRIBUTION()) {
418
+ revert ErrorIDistributionServiceCallerNotDistributor(msg.sender);
438
419
  }
439
- instance = IInstance(parentInfo.objectAddress);
420
+
421
+ address instanceAddress = getRegistry().getObjectInfo(info.parentNftId).objectAddress;
422
+ instance = IInstance(instanceAddress);
440
423
  }
441
424
 
442
- function _getAndVerifyCaller()
425
+
426
+ function _getInstanceForDistribution(NftId distributionNftId)
443
427
  internal
444
428
  view
445
- returns(
446
- address objectAddress,
447
- NftId objectNftId,
448
- ObjectType objectType,
449
- IInstance instance
450
- )
429
+ returns(IInstance instance)
451
430
  {
452
- objectAddress = msg.sender;
453
- objectNftId = getRegistry().getNftId(objectAddress);
454
- if ( objectNftId.eqz()) {
455
- revert ErrorIServiceCallerUnknown(objectAddress);
456
- }
457
- IRegistry.ObjectInfo memory info = getRegistry().getObjectInfo(objectNftId);
458
- objectType = info.objectType;
459
-
460
- IRegistry.ObjectInfo memory parentInfo = getRegistry().getObjectInfo(info.parentNftId);
461
- if (parentInfo.objectType != INSTANCE()) {
462
- revert ErrorIDistributionServiceParentNftIdNotInstance(objectNftId, info.parentNftId);
463
- }
464
- instance = IInstance(parentInfo.objectAddress);
431
+ NftId instanceNftId = getRegistry().getObjectInfo(distributionNftId).parentNftId;
432
+ address instanceAddress = getRegistry().getObjectInfo(instanceNftId).objectAddress;
433
+ return IInstance(instanceAddress);
465
434
  }
466
-
467
435
  }