This repository has been archived by the owner on Aug 6, 2021. It is now read-only.
/
ANNEncoder.sol
459 lines (423 loc) · 21.6 KB
/
ANNEncoder.sol
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// "SPDX-License-Identifier: Apache-2.0"
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
import "../Base/SharedTypes.sol";
import "../Base/AssetRegistry/BaseRegistryStorage.sol";
library ANNEncoder {
function storeInPackedTerms(Asset storage asset, bytes32 attributeKey, bytes32 value) private {
// skip if value did not change
if (asset.packedTerms[attributeKey] == value) return;
asset.packedTerms[attributeKey] = value;
}
function storeInPackedState(Asset storage asset, bytes32 attributeKey, bytes32 value) private {
// skip if value did not change
if (asset.packedState[attributeKey] == value) return;
asset.packedState[attributeKey] = value;
}
/**
* @dev Tightly pack and store only non-zero overwritten terms (LifecycleTerms)
* @notice All non zero values of the overwrittenTerms object are stored.
* It does not check if overwrittenAttributesMap actually marks attribute as overwritten.
*/
function encodeAndSetANNTerms(Asset storage asset, ANNTerms memory terms) external {
storeInPackedTerms(
asset,
"enums",
bytes32(uint256(uint8(terms.contractType))) << 248 |
bytes32(uint256(uint8(terms.calendar))) << 240 |
bytes32(uint256(uint8(terms.contractRole))) << 232 |
bytes32(uint256(uint8(terms.dayCountConvention))) << 224 |
bytes32(uint256(uint8(terms.businessDayConvention))) << 216 |
bytes32(uint256(uint8(terms.endOfMonthConvention))) << 208 |
bytes32(uint256(uint8(terms.scalingEffect))) << 200 |
bytes32(uint256(uint8(terms.feeBasis))) << 192
);
storeInPackedTerms(asset, "currency", bytes32(uint256(terms.currency) << 96));
storeInPackedTerms(asset, "settlementCurrency", bytes32(uint256(terms.settlementCurrency) << 96));
storeInPackedTerms(asset, "marketObjectCodeRateReset", bytes32(terms.marketObjectCodeRateReset));
storeInPackedTerms(asset, "statusDate", bytes32(terms.statusDate));
storeInPackedTerms(asset, "initialExchangeDate", bytes32(terms.initialExchangeDate));
storeInPackedTerms(asset, "maturityDate", bytes32(terms.maturityDate));
storeInPackedTerms(asset, "issueDate", bytes32(terms.issueDate));
storeInPackedTerms(asset, "purchaseDate", bytes32(terms.purchaseDate));
storeInPackedTerms(asset, "capitalizationEndDate", bytes32(terms.capitalizationEndDate));
storeInPackedTerms(asset, "cycleAnchorDateOfInterestPayment", bytes32(terms.cycleAnchorDateOfInterestPayment));
storeInPackedTerms(asset, "cycleAnchorDateOfRateReset", bytes32(terms.cycleAnchorDateOfRateReset));
storeInPackedTerms(asset, "cycleAnchorDateOfScalingIndex", bytes32(terms.cycleAnchorDateOfScalingIndex));
storeInPackedTerms(asset, "cycleAnchorDateOfFee", bytes32(terms.cycleAnchorDateOfFee));
storeInPackedTerms(asset, "cycleAnchorDateOfPrincipalRedemp", bytes32(terms.cycleAnchorDateOfPrincipalRedemption));
storeInPackedTerms(asset, "notionalPrincipal", bytes32(terms.notionalPrincipal));
storeInPackedTerms(asset, "nominalInterestRate", bytes32(terms.nominalInterestRate));
storeInPackedTerms(asset, "accruedInterest", bytes32(terms.accruedInterest));
storeInPackedTerms(asset, "rateMultiplier", bytes32(terms.rateMultiplier));
storeInPackedTerms(asset, "rateSpread", bytes32(terms.rateSpread));
storeInPackedTerms(asset, "nextResetRate", bytes32(terms.nextResetRate));
storeInPackedTerms(asset, "feeRate", bytes32(terms.feeRate));
storeInPackedTerms(asset, "feeAccrued", bytes32(terms.feeAccrued));
storeInPackedTerms(asset, "premiumDiscountAtIED", bytes32(terms.premiumDiscountAtIED));
storeInPackedTerms(asset, "priceAtPurchaseDate", bytes32(terms.priceAtPurchaseDate));
storeInPackedTerms(asset, "priceAtTerminationDate", bytes32(terms.priceAtTerminationDate));
storeInPackedTerms(asset, "nextPrincipalRedemptionPayment", bytes32(terms.nextPrincipalRedemptionPayment));
storeInPackedTerms(asset, "lifeCap", bytes32(terms.lifeCap));
storeInPackedTerms(asset, "lifeFloor", bytes32(terms.lifeFloor));
storeInPackedTerms(asset, "periodCap", bytes32(terms.periodCap));
storeInPackedTerms(asset, "periodFloor", bytes32(terms.periodFloor));
storeInPackedTerms(
asset,
"gracePeriod",
bytes32(uint256(terms.gracePeriod.i)) << 24 |
bytes32(uint256(terms.gracePeriod.p)) << 16 |
bytes32(uint256((terms.gracePeriod.isSet) ? 1 : 0)) << 8
);
storeInPackedTerms(
asset,
"delinquencyPeriod",
bytes32(uint256(terms.delinquencyPeriod.i)) << 24 |
bytes32(uint256(terms.delinquencyPeriod.p)) << 16 |
bytes32(uint256((terms.delinquencyPeriod.isSet) ? 1 : 0)) << 8
);
storeInPackedTerms(
asset,
"cycleOfInterestPayment",
bytes32(uint256(terms.cycleOfInterestPayment.i)) << 24 |
bytes32(uint256(terms.cycleOfInterestPayment.p)) << 16 |
bytes32(uint256(terms.cycleOfInterestPayment.s)) << 8 |
bytes32(uint256((terms.cycleOfInterestPayment.isSet) ? 1 : 0))
);
storeInPackedTerms(
asset,
"cycleOfRateReset",
bytes32(uint256(terms.cycleOfRateReset.i)) << 24 |
bytes32(uint256(terms.cycleOfRateReset.p)) << 16 |
bytes32(uint256(terms.cycleOfRateReset.s)) << 8 |
bytes32(uint256((terms.cycleOfRateReset.isSet) ? 1 : 0))
);
storeInPackedTerms(
asset,
"cycleOfScalingIndex",
bytes32(uint256(terms.cycleOfScalingIndex.i)) << 24 |
bytes32(uint256(terms.cycleOfScalingIndex.p)) << 16 |
bytes32(uint256(terms.cycleOfScalingIndex.s)) << 8 |
bytes32(uint256((terms.cycleOfScalingIndex.isSet) ? 1 : 0))
);
storeInPackedTerms(
asset,
"cycleOfFee",
bytes32(uint256(terms.cycleOfFee.i)) << 24 |
bytes32(uint256(terms.cycleOfFee.p)) << 16 |
bytes32(uint256(terms.cycleOfFee.s)) << 8 |
bytes32(uint256((terms.cycleOfFee.isSet) ? 1 : 0))
);
storeInPackedTerms(
asset,
"cycleOfPrincipalRedemption",
bytes32(uint256(terms.cycleOfPrincipalRedemption.i)) << 24 |
bytes32(uint256(terms.cycleOfPrincipalRedemption.p)) << 16 |
bytes32(uint256(terms.cycleOfPrincipalRedemption.s)) << 8 |
bytes32(uint256((terms.cycleOfPrincipalRedemption.isSet) ? 1 : 0))
);
}
/**
* @dev Decode and loads ANNTerms
*/
function decodeAndGetANNTerms(Asset storage asset) external view returns (ANNTerms memory) {
return ANNTerms(
ContractType(uint8(uint256(asset.packedTerms["enums"] >> 248))),
Calendar(uint8(uint256(asset.packedTerms["enums"] >> 240))),
ContractRole(uint8(uint256(asset.packedTerms["enums"] >> 232))),
DayCountConvention(uint8(uint256(asset.packedTerms["enums"] >> 224))),
BusinessDayConvention(uint8(uint256(asset.packedTerms["enums"] >> 216))),
EndOfMonthConvention(uint8(uint256(asset.packedTerms["enums"] >> 208))),
ScalingEffect(uint8(uint256(asset.packedTerms["enums"] >> 200))),
FeeBasis(uint8(uint256(asset.packedTerms["enums"] >> 192))),
address(uint160(uint256(asset.packedTerms["currency"]) >> 96)),
address(uint160(uint256(asset.packedTerms["settlementCurrency"]) >> 96)),
asset.packedTerms["marketObjectCodeRateReset"],
uint256(asset.packedTerms["statusDate"]),
uint256(asset.packedTerms["initialExchangeDate"]),
uint256(asset.packedTerms["maturityDate"]),
uint256(asset.packedTerms["issueDate"]),
uint256(asset.packedTerms["purchaseDate"]),
uint256(asset.packedTerms["capitalizationEndDate"]),
uint256(asset.packedTerms["cycleAnchorDateOfInterestPayment"]),
uint256(asset.packedTerms["cycleAnchorDateOfRateReset"]),
uint256(asset.packedTerms["cycleAnchorDateOfScalingIndex"]),
uint256(asset.packedTerms["cycleAnchorDateOfFee"]),
uint256(asset.packedTerms["cycleAnchorDateOfPrincipalRedemp"]),
int256(asset.packedTerms["notionalPrincipal"]),
int256(asset.packedTerms["nominalInterestRate"]),
int256(asset.packedTerms["accruedInterest"]),
int256(asset.packedTerms["rateMultiplier"]),
int256(asset.packedTerms["rateSpread"]),
int256(asset.packedTerms["nextResetRate"]),
int256(asset.packedTerms["feeRate"]),
int256(asset.packedTerms["feeAccrued"]),
int256(asset.packedTerms["premiumDiscountAtIED"]),
int256(asset.packedTerms["priceAtPurchaseDate"]),
int256(asset.packedTerms["priceAtTerminationDate"]),
int256(asset.packedTerms["nextPrincipalRedemptionPayment"]),
int256(asset.packedTerms["lifeCap"]),
int256(asset.packedTerms["lifeFloor"]),
int256(asset.packedTerms["periodCap"]),
int256(asset.packedTerms["periodFloor"]),
IP(
uint256(asset.packedTerms["gracePeriod"] >> 24),
P(uint8(uint256(asset.packedTerms["gracePeriod"] >> 16))),
(asset.packedTerms["gracePeriod"] >> 8 & bytes32(uint256(1)) == bytes32(uint256(1))) ? true : false
),
IP(
uint256(asset.packedTerms["delinquencyPeriod"] >> 24),
P(uint8(uint256(asset.packedTerms["delinquencyPeriod"] >> 16))),
(asset.packedTerms["delinquencyPeriod"] >> 8 & bytes32(uint256(1)) == bytes32(uint256(1))) ? true : false
),
IPS(
uint256(asset.packedTerms["cycleOfInterestPayment"] >> 24),
P(uint8(uint256(asset.packedTerms["cycleOfInterestPayment"] >> 16))),
S(uint8(uint256(asset.packedTerms["cycleOfInterestPayment"] >> 8))),
(asset.packedTerms["cycleOfInterestPayment"] & bytes32(uint256(1)) == bytes32(uint256(1))) ? true : false
),
IPS(
uint256(asset.packedTerms["cycleOfRateReset"] >> 24),
P(uint8(uint256(asset.packedTerms["cycleOfRateReset"] >> 16))),
S(uint8(uint256(asset.packedTerms["cycleOfRateReset"] >> 8))),
(asset.packedTerms["cycleOfRateReset"] & bytes32(uint256(1)) == bytes32(uint256(1))) ? true : false
),
IPS(
uint256(asset.packedTerms["cycleOfScalingIndex"] >> 24),
P(uint8(uint256(asset.packedTerms["cycleOfScalingIndex"] >> 16))),
S(uint8(uint256(asset.packedTerms["cycleOfScalingIndex"] >> 8))),
(asset.packedTerms["cycleOfScalingIndex"] & bytes32(uint256(1)) == bytes32(uint256(1))) ? true : false
),
IPS(
uint256(asset.packedTerms["cycleOfFee"] >> 24),
P(uint8(uint256(asset.packedTerms["cycleOfFee"] >> 16))),
S(uint8(uint256(asset.packedTerms["cycleOfFee"] >> 8))),
(asset.packedTerms["cycleOfFee"] & bytes32(uint256(1)) == bytes32(uint256(1))) ? true : false
),
IPS(
uint256(asset.packedTerms["cycleOfPrincipalRedemption"] >> 24),
P(uint8(uint256(asset.packedTerms["cycleOfPrincipalRedemption"] >> 16))),
S(uint8(uint256(asset.packedTerms["cycleOfPrincipalRedemption"] >> 8))),
(asset.packedTerms["cycleOfPrincipalRedemption"] & bytes32(uint256(1)) == bytes32(uint256(1))) ? true : false
)
);
}
function decodeAndGetEnumValueForANNTermsAttribute(Asset storage asset, bytes32 attributeKey)
external
view
returns (uint8)
{
if (attributeKey == "contractType") {
return uint8(uint256(asset.packedTerms["enums"] >> 248));
} else if (attributeKey == bytes32("calendar")) {
return uint8(uint256(asset.packedTerms["enums"] >> 240));
} else if (attributeKey == bytes32("contractRole")) {
return uint8(uint256(asset.packedTerms["enums"] >> 232));
} else if (attributeKey == bytes32("dayCountConvention")) {
return uint8(uint256(asset.packedTerms["enums"] >> 224));
} else if (attributeKey == bytes32("businessDayConvention")) {
return uint8(uint256(asset.packedTerms["enums"] >> 216));
} else if (attributeKey == bytes32("endOfMonthConvention")) {
return uint8(uint256(asset.packedTerms["enums"] >> 208));
} else if (attributeKey == bytes32("scalingEffect")) {
return uint8(uint256(asset.packedTerms["enums"] >> 200));
} else if (attributeKey == bytes32("feeBasis")) {
return uint8(uint256(asset.packedTerms["enums"] >> 192));
} else {
return uint8(0);
}
}
function decodeAndGetAddressValueForANNTermsAttribute(Asset storage asset, bytes32 attributeKey)
external
view
returns (address)
{
if (attributeKey == bytes32("currency")) {
return address(uint160(uint256(asset.packedTerms["currency"]) >> 96));
} else if (attributeKey == bytes32("settlementCurrency")) {
return address(uint160(uint256(asset.packedTerms["settlementCurrency"]) >> 96));
} else {
return address(0);
}
}
function decodeAndGetBytes32ValueForANNTermsAttribute(Asset storage asset, bytes32 attributeKey)
external
view
returns (bytes32)
{
return asset.packedTerms[attributeKey];
}
function decodeAndGetUIntValueForANNTermsAttribute(Asset storage asset, bytes32 attributeKey)
external
view
returns (uint256)
{
return uint256(asset.packedTerms[attributeKey]);
}
function decodeAndGetIntValueForANNTermsAttribute(Asset storage asset, bytes32 attributeKey)
external
view
returns (int256)
{
return int256(asset.packedTerms[attributeKey]);
}
function decodeAndGetPeriodValueForANNTermsAttribute(Asset storage asset, bytes32 attributeKey)
external
view
returns (IP memory)
{
if (
attributeKey == bytes32("gracePeriod")
|| attributeKey == bytes32("delinquencyPeriod")
) {
return IP(
uint256(asset.packedTerms[attributeKey] >> 24),
P(uint8(uint256(asset.packedTerms[attributeKey] >> 16))),
(asset.packedTerms[attributeKey] >> 8 & bytes32(uint256(1)) == bytes32(uint256(1))) ? true : false
);
} else {
return IP(0, P(0), false);
}
}
function decodeAndGetCycleValueForANNTermsAttribute(Asset storage asset, bytes32 attributeKey)
external
view
returns (IPS memory)
{
if (
attributeKey == bytes32("cycleOfInterestPayment")
|| attributeKey == bytes32("cycleRateReset")
|| attributeKey == bytes32("cycleScalingIndex")
|| attributeKey == bytes32("cycleFee")
|| attributeKey == bytes32("cycleOfPrincipalRedemption")
) {
return IPS(
uint256(asset.packedTerms[attributeKey] >> 24),
P(uint8(uint256(asset.packedTerms[attributeKey] >> 16))),
S(uint8(uint256(asset.packedTerms[attributeKey] >> 8))),
(asset.packedTerms[attributeKey] & bytes32(uint256(1)) == bytes32(uint256(1))) ? true : false
);
} else {
return IPS(0, P(0), S(0), false);
}
}
function decodeAndGetContractReferenceValueForANNTermsAttribute(Asset storage /* asset */, bytes32 /* attributeKey */)
external
pure
returns (ContractReference memory)
{
return ContractReference(
bytes32(0),
bytes32(0),
ContractReferenceType(0),
ContractReferenceRole(0)
);
}
/**
* @dev Tightly pack and store ANNState
*/
function encodeAndSetANNState(Asset storage asset, ANNState memory state) external {
storeInPackedState(asset, "contractPerformance", bytes32(uint256(uint8(state.contractPerformance))) << 248);
storeInPackedState(asset, "statusDate", bytes32(state.statusDate));
storeInPackedState(asset, "nonPerformingDate", bytes32(state.nonPerformingDate));
storeInPackedState(asset, "maturityDate", bytes32(state.maturityDate));
storeInPackedState(asset, "terminationDate", bytes32(state.terminationDate));
storeInPackedState(asset, "notionalPrincipal", bytes32(state.notionalPrincipal));
storeInPackedState(asset, "accruedInterest", bytes32(state.accruedInterest));
storeInPackedState(asset, "feeAccrued", bytes32(state.feeAccrued));
storeInPackedState(asset, "nominalInterestRate", bytes32(state.nominalInterestRate));
storeInPackedState(asset, "interestScalingMultiplier", bytes32(state.interestScalingMultiplier));
storeInPackedState(asset, "notionalScalingMultiplier", bytes32(state.notionalScalingMultiplier));
storeInPackedState(asset, "nextPrincipalRedemptionPayment", bytes32(state.nextPrincipalRedemptionPayment));
}
/**
* @dev Tightly pack and store finalized ANNState
*/
function encodeAndSetFinalizedANNState(Asset storage asset, ANNState memory state) external {
storeInPackedState(asset, "F_contractPerformance", bytes32(uint256(uint8(state.contractPerformance))) << 248);
storeInPackedState(asset, "F_statusDate", bytes32(state.statusDate));
storeInPackedState(asset, "F_nonPerformingDate", bytes32(state.nonPerformingDate));
storeInPackedState(asset, "F_maturityDate", bytes32(state.maturityDate));
storeInPackedState(asset, "F_terminationDate", bytes32(state.terminationDate));
storeInPackedState(asset, "F_notionalPrincipal", bytes32(state.notionalPrincipal));
storeInPackedState(asset, "F_accruedInterest", bytes32(state.accruedInterest));
storeInPackedState(asset, "F_feeAccrued", bytes32(state.feeAccrued));
storeInPackedState(asset, "F_nominalInterestRate", bytes32(state.nominalInterestRate));
storeInPackedState(asset, "F_interestScalingMultiplier", bytes32(state.interestScalingMultiplier));
storeInPackedState(asset, "F_notionalScalingMultiplier", bytes32(state.notionalScalingMultiplier));
storeInPackedState(asset, "F_nextPrincipalRedemptionPayment", bytes32(state.nextPrincipalRedemptionPayment));
}
/**
* @dev Decode and load the ANNState of the asset
*/
function decodeAndGetANNState(Asset storage asset)
external
view
returns (ANNState memory)
{
return ANNState(
ContractPerformance(uint8(uint256(asset.packedState["contractPerformance"] >> 248))),
uint256(asset.packedState["statusDate"]),
uint256(asset.packedState["nonPerformingDate"]),
uint256(asset.packedState["maturityDate"]),
uint256(asset.packedState["terminationDate"]),
int256(asset.packedState["notionalPrincipal"]),
int256(asset.packedState["accruedInterest"]),
int256(asset.packedState["feeAccrued"]),
int256(asset.packedState["nominalInterestRate"]),
int256(asset.packedState["interestScalingMultiplier"]),
int256(asset.packedState["notionalScalingMultiplier"]),
int256(asset.packedState["nextPrincipalRedemptionPayment"])
);
}
/**
* @dev Decode and load the finalized ANNState of the asset
*/
function decodeAndGetFinalizedANNState(Asset storage asset)
external
view
returns (ANNState memory)
{
return ANNState(
ContractPerformance(uint8(uint256(asset.packedState["F_contractPerformance"] >> 248))),
uint256(asset.packedState["F_statusDate"]),
uint256(asset.packedState["F_nonPerformingDate"]),
uint256(asset.packedState["F_maturityDate"]),
uint256(asset.packedState["F_terminationDate"]),
int256(asset.packedState["F_notionalPrincipal"]),
int256(asset.packedState["F_accruedInterest"]),
int256(asset.packedState["F_feeAccrued"]),
int256(asset.packedState["F_nominalInterestRate"]),
int256(asset.packedState["F_interestScalingMultiplier"]),
int256(asset.packedState["F_notionalScalingMultiplier"]),
int256(asset.packedState["F_nextPrincipalRedemptionPayment"])
);
}
function decodeAndGetEnumValueForANNStateAttribute(Asset storage asset, bytes32 attributeKey)
external
view
returns (uint8)
{
if (attributeKey == bytes32("contractPerformance")) {
return uint8(uint256(asset.packedState["contractPerformance"] >> 248));
} else if (attributeKey == bytes32("F_contractPerformance")) {
return uint8(uint256(asset.packedState["F_contractPerformance"] >> 248));
} else {
return uint8(0);
}
}
function decodeAndGetUIntValueForANNStateAttribute(Asset storage asset, bytes32 attributeKey)
external
view
returns (uint256)
{
return uint256(asset.packedState[attributeKey]);
}
function decodeAndGetIntValueForANNStateAttribute(Asset storage asset, bytes32 attributeKey)
external
view
returns (int256)
{
return int256(asset.packedState[attributeKey]);
}
}