-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathInstanceReader.sol
More file actions
367 lines (303 loc) · 10.9 KB
/
InstanceReader.sol
File metadata and controls
367 lines (303 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;
import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import {Amount} from "../type/Amount.sol";
import {ClaimId} from "../type/ClaimId.sol";
import {DistributorType} from "../type/DistributorType.sol";
import {Fee, FeeLib} from "../type/Fee.sol";
import {Key32} from "../type/Key32.sol";
import {NftId} from "../type/NftId.sol";
import {ObjectType, COMPONENT, DISTRIBUTOR, DISTRIBUTION, INSTANCE, PRODUCT, POLICY, POOL, FEE, BUNDLE} from "../type/ObjectType.sol";
import {PayoutId} from "../type/PayoutId.sol";
import {ReferralId, ReferralStatus, ReferralLib, REFERRAL_OK, REFERRAL_ERROR_UNKNOWN, REFERRAL_ERROR_EXPIRED, REFERRAL_ERROR_EXHAUSTED} from "../type/Referral.sol";
import {RiskId} from "../type/RiskId.sol";
import {UFixed, MathLib, UFixedLib} from "../type/UFixed.sol";
import {Version} from "../type/Version.sol";
import {StateId} from "../type/StateId.sol";
import {TimestampLib} from "../type/Timestamp.sol";
import {IKeyValueStore} from "../shared/IKeyValueStore.sol";
import {IRisk} from "../instance/module/IRisk.sol";
import {IPolicy} from "../instance/module/IPolicy.sol";
import {IRegistry} from "../registry/IRegistry.sol";
import {IBundle} from "../instance/module/IBundle.sol";
import {IComponents} from "../instance/module/IComponents.sol";
import {IDistribution} from "../instance/module/IDistribution.sol";
import {IInstance} from "./IInstance.sol";
import {InstanceStore} from "./InstanceStore.sol";
contract InstanceReader {
error ErrorInstanceReaderAlreadyInitialized();
error ErrorInstanceReaderInstanceAddressZero();
bool private _initialized;
IInstance internal _instance;
InstanceStore internal _store;
function initialize(address instance) public {
if(_initialized) {
revert ErrorInstanceReaderAlreadyInitialized();
}
if(instance == address(0)) {
revert ErrorInstanceReaderInstanceAddressZero();
}
_instance = IInstance(instance);
_store = _instance.getInstanceStore();
_initialized = true;
}
// module specific functions
function getPolicyInfo(NftId policyNftId)
public
view
returns (IPolicy.PolicyInfo memory info)
{
bytes memory data = _store.getData(toPolicyKey(policyNftId));
if (data.length > 0) {
return abi.decode(data, (IPolicy.PolicyInfo));
}
}
function getPolicyState(NftId policyNftId)
public
view
returns (StateId state)
{
return _store.getState(toPolicyKey(policyNftId));
}
/// @dev returns true iff policy may be closed
/// a policy can be closed all conditions below are met
/// - policy exists
/// - has been activated
/// - is not yet closed
/// - has no open claims
/// - claim amount matches sum insured amount or is expired
function policyIsCloseable(NftId policyNftId)
public
view
returns (bool isCloseable)
{
IPolicy.PolicyInfo memory info = getPolicyInfo(policyNftId);
if (info.productNftId.eqz()) { return false; } // not closeable: policy does not exist (or does not belong to this instance)
if (info.activatedAt.eqz()) { return false; } // not closeable: not yet activated
if (info.closedAt.gtz()) { return false; } // not closeable: already closed
if (info.openClaimsCount > 0) { return false; } // not closeable: has open claims
// closeable: if sum of claims matches sum insured a policy may be closed prior to the expiry date
if (info.claimAmount == info.sumInsuredAmount) { return true; }
// not closeable: not yet expired
if (TimestampLib.blockTimestamp() < info.expiredAt) { return false; }
// all conditionsl to close the policy are met
return true;
}
function getClaimInfo(NftId policyNftId, ClaimId claimId)
public
view
returns (IPolicy.ClaimInfo memory info)
{
bytes memory data = _store.getData(claimId.toKey32(policyNftId));
if (data.length > 0) {
return abi.decode(data, (IPolicy.ClaimInfo));
}
}
function getClaimState(NftId policyNftId, ClaimId claimId)
public
view
returns (StateId state)
{
return _store.getState(claimId.toKey32(policyNftId));
}
function getPayoutInfo(NftId policyNftId, PayoutId payoutId)
public
view
returns (IPolicy.PayoutInfo memory info)
{
bytes memory data = _store.getData(payoutId.toKey32(policyNftId));
if (data.length > 0) {
return abi.decode(data, (IPolicy.PayoutInfo));
}
}
function getPayoutState(NftId policyNftId, PayoutId payoutId)
public
view
returns (StateId state)
{
return _store.getState(payoutId.toKey32(policyNftId));
}
function getRiskInfo(RiskId riskId)
public
view
returns (IRisk.RiskInfo memory info)
{
bytes memory data = _store.getData(riskId.toKey32());
if (data.length > 0) {
return abi.decode(data, (IRisk.RiskInfo));
}
}
function getTokenHandler(NftId componentNftId)
public
view
returns (address tokenHandler)
{
bytes memory data = _store.getData(toComponentKey(componentNftId));
if (data.length > 0) {
IComponents.ComponentInfo memory info = abi.decode(data, (IComponents.ComponentInfo));
return address(info.tokenHandler);
}
}
function getBundleInfo(NftId bundleNftId)
public
view
returns (IBundle.BundleInfo memory info)
{
bytes memory data = _store.getData(toBundleKey(bundleNftId));
if (data.length > 0) {
return abi.decode(data, (IBundle.BundleInfo));
}
}
function getDistributorTypeInfo(DistributorType distributorType)
public
view
returns (IDistribution.DistributorTypeInfo memory info)
{
bytes memory data = _store.getData(distributorType.toKey32());
if (data.length > 0) {
return abi.decode(data, (IDistribution.DistributorTypeInfo));
}
}
function getDistributorInfo(NftId distributorNftId)
public
view
returns (IDistribution.DistributorInfo memory info)
{
bytes memory data = _store.getData(toDistributorKey(distributorNftId));
if (data.length > 0) {
return abi.decode(data, (IDistribution.DistributorInfo));
}
}
function getBalanceAmount(NftId targetNftId) external view returns (Amount) {
return _store.getBalanceAmount(targetNftId);
}
function getLockedAmount(NftId targetNftId) external view returns (Amount) {
return _store.getLockedAmount(targetNftId);
}
function getFeeAmount(NftId targetNftId) external view returns (Amount) {
return _store.getFeeAmount(targetNftId);
}
function getComponentInfo(NftId componentNftId)
public
view
returns (IComponents.ComponentInfo memory info)
{
bytes memory data = _store.getData(toComponentKey(componentNftId));
if (data.length > 0) {
return abi.decode(data, (IComponents.ComponentInfo));
}
}
function getProductInfo(NftId productNftId)
public
view
returns (IComponents.ProductInfo memory info)
{
bytes memory data = _store.getData(toProductKey(productNftId));
if (data.length > 0) {
return abi.decode(data, (IComponents.ProductInfo));
}
}
function getPoolInfo(NftId poolNftId)
public
view
returns (IComponents.PoolInfo memory info)
{
bytes memory data = _store.getData(toPoolKey(poolNftId));
if (data.length > 0) {
return abi.decode(data, (IComponents.PoolInfo));
}
}
function getReferralInfo(ReferralId referralId)
public
view
returns (IDistribution.ReferralInfo memory info)
{
bytes memory data = _store.getData(referralId.toKey32());
if (data.length > 0) {
return abi.decode(data, (IDistribution.ReferralInfo));
}
}
function getMetadata(Key32 key)
public
view
returns (IKeyValueStore.Metadata memory metadata)
{
return _store.getMetadata(key);
}
function toReferralId(
NftId distributionNftId,
string memory referralCode
)
public
pure
returns (ReferralId referralId)
{
return ReferralLib.toReferralId(
distributionNftId,
referralCode);
}
function getDiscountPercentage(ReferralId referralId)
public
view
returns (
UFixed discountPercentage,
ReferralStatus status
)
{
IDistribution.ReferralInfo memory info = getReferralInfo(
referralId);
if (info.expiryAt.eqz()) {
return (
UFixedLib.zero(),
REFERRAL_ERROR_UNKNOWN());
}
if (info.expiryAt < TimestampLib.blockTimestamp()) {
return (
UFixedLib.zero(),
REFERRAL_ERROR_EXPIRED());
}
if (info.usedReferrals >= info.maxReferrals) {
return (
UFixedLib.zero(),
REFERRAL_ERROR_EXHAUSTED());
}
return (
info.discountPercentage,
REFERRAL_OK()
);
}
function toPolicyKey(NftId policyNftId) public pure returns (Key32) {
return policyNftId.toKey32(POLICY());
}
function toDistributorKey(NftId distributorNftId) public pure returns (Key32) {
return distributorNftId.toKey32(DISTRIBUTOR());
}
function toBundleKey(NftId poolNftId) public pure returns (Key32) {
return poolNftId.toKey32(BUNDLE());
}
function toComponentKey(NftId componentNftId) public pure returns (Key32) {
return componentNftId.toKey32(COMPONENT());
}
function toDistributionKey(NftId distributionNftId) public pure returns (Key32) {
return distributionNftId.toKey32(DISTRIBUTION());
}
function toPoolKey(NftId poolNftId) public pure returns (Key32) {
return poolNftId.toKey32(POOL());
}
function toProductKey(NftId productNftId) public pure returns (Key32) {
return productNftId.toKey32(PRODUCT());
}
// low level function
function getInstance() external view returns (IInstance instance) {
return _instance;
}
function getInstanceStore() external view returns (IKeyValueStore store) {
return _store;
}
function toUFixed(uint256 value, int8 exp) public pure returns (UFixed) {
return UFixedLib.toUFixed(value, exp);
}
function toInt(UFixed value) public pure returns (uint256) {
return UFixedLib.toInt(value);
}
}