-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMessages.sol
979 lines (883 loc) · 31.9 KB
/
Messages.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.21;
import {BytesLib} from "src/util/BytesLib.sol";
/// @title Messages
/// @dev Library for encoding and decoding messages.
library Messages {
enum Call
/// 0 - An invalid message
{
Invalid,
/// 1 - Add a currency id -> EVM address mapping
AddCurrency,
/// 2 - Add Pool
AddPool,
/// 3 - Allow a registered currency to be used as a pool currency or as an investment currency
AllowPoolCurrency,
/// 4 - Add a Pool's Tranche Token
AddTranche,
/// 5 - Update the price of a Tranche Token
UpdateTrancheTokenPrice,
/// 6 - Update the member list of a tranche token with a new member
UpdateMember,
/// 7 - A transfer of Stable CoinsformatTransferTrancheTokens
Transfer,
/// 8 - A transfer of Tranche tokens
TransferTrancheTokens,
/// 9 - Increase an investment order by a given amount
IncreaseInvestOrder,
/// 10 - Decrease an investment order by a given amount
DecreaseInvestOrder,
/// 11 - Increase a Redeem order by a given amount
IncreaseRedeemOrder,
/// 12 - Decrease a Redeem order by a given amount
DecreaseRedeemOrder,
/// 13 - Collect investment
CollectInvest,
/// 14 - Collect Redeem
CollectRedeem,
/// 15 - Executed Decrease Invest Order
ExecutedDecreaseInvestOrder,
/// 16 - Executed Decrease Redeem Order
ExecutedDecreaseRedeemOrder,
/// 17 - Executed Collect Invest
ExecutedCollectInvest,
/// 18 - Executed Collect Redeem
ExecutedCollectRedeem,
/// 19 - Cancel an investment order
CancelInvestOrder,
/// 20 - Cancel a redeem order
CancelRedeemOrder,
/// 21 - Schedule an upgrade contract to be granted admin rights
ScheduleUpgrade,
/// 22 - Cancel a previously scheduled upgrade
CancelUpgrade,
/// 23 - Update tranche token metadata
UpdateTrancheTokenMetadata,
/// 24 - Update tranche investment limit
UpdateTrancheInvestmentLimit,
/// 25 - Freeze tranche tokens
Freeze,
/// 26 - Unfreeze tranche tokens
Unfreeze
}
enum Domain {
Centrifuge,
EVM
}
function messageType(bytes memory _msg) internal pure returns (Call _call) {
_call = Call(BytesLib.toUint8(_msg, 0));
}
/**
* Add Currency
*
* 0: call type (uint8 = 1 byte)
* 1-16: The Liquidity Pool's global currency id (uint128 = 16 bytes)
* 17-36: The EVM address of the currency (address = 20 bytes)
*/
function formatAddCurrency(uint128 currency, address currencyAddress) internal pure returns (bytes memory) {
return abi.encodePacked(uint8(Call.AddCurrency), currency, currencyAddress);
}
function isAddCurrency(bytes memory _msg) internal pure returns (bool) {
return messageType(_msg) == Call.AddCurrency;
}
function parseAddCurrency(bytes memory _msg) internal pure returns (uint128 currency, address currencyAddress) {
currency = BytesLib.toUint128(_msg, 1);
currencyAddress = BytesLib.toAddress(_msg, 17);
}
/**
* Add pool
*
* 0: call type (uint8 = 1 byte)
* 1-8: poolId (uint64 = 8 bytes)
*/
function formatAddPool(uint64 poolId) internal pure returns (bytes memory) {
return abi.encodePacked(uint8(Call.AddPool), poolId);
}
function isAddPool(bytes memory _msg) internal pure returns (bool) {
return messageType(_msg) == Call.AddPool;
}
function parseAddPool(bytes memory _msg) internal pure returns (uint64 poolId) {
poolId = BytesLib.toUint64(_msg, 1);
}
/**
* Allow Pool Currency
*
* 0: call type (uint8 = 1 byte)
* 1-8: poolId (uint64 = 8 bytes)
* 9-24: currency (uint128 = 16 bytes)
*/
function formatAllowPoolCurrency(uint64 poolId, uint128 currency) internal pure returns (bytes memory) {
return abi.encodePacked(uint8(Call.AllowPoolCurrency), poolId, currency);
}
function isAllowPoolCurrency(bytes memory _msg) internal pure returns (bool) {
return messageType(_msg) == Call.AllowPoolCurrency;
}
function parseAllowPoolCurrency(bytes memory _msg) internal pure returns (uint64 poolId, uint128 currency) {
poolId = BytesLib.toUint64(_msg, 1);
currency = BytesLib.toUint128(_msg, 9);
}
/**
* Add tranche
*
* 0: call type (uint8 = 1 byte)
* 1-8: poolId (uint64 = 8 bytes)
* 9-24: trancheId (16 bytes)
* 25-152: tokenName (string = 128 bytes)
* 153-184: tokenSymbol (string = 32 bytes)
* 185: decimals (uint8 = 1 byte)
* 186-202: price (uint128 = 16 bytes)
*/
function formatAddTranche(
uint64 poolId,
bytes16 trancheId,
string memory tokenName,
string memory tokenSymbol,
uint8 decimals,
uint128 price
) internal pure returns (bytes memory) {
return abi.encodePacked(
uint8(Call.AddTranche),
poolId,
trancheId,
_stringToBytes128(tokenName),
_stringToBytes32(tokenSymbol),
decimals,
price
);
}
function isAddTranche(bytes memory _msg) internal pure returns (bool) {
return messageType(_msg) == Call.AddTranche;
}
function parseAddTranche(bytes memory _msg)
internal
pure
returns (
uint64 poolId,
bytes16 trancheId,
string memory tokenName,
string memory tokenSymbol,
uint8 decimals,
uint128 price
)
{
poolId = BytesLib.toUint64(_msg, 1);
trancheId = BytesLib.toBytes16(_msg, 9);
tokenName = _bytes128ToString(BytesLib.slice(_msg, 25, 128));
tokenSymbol = _bytes32ToString(BytesLib.toBytes32(_msg, 153));
decimals = BytesLib.toUint8(_msg, 185);
price = BytesLib.toUint128(_msg, 186);
}
/**
* Update member
*
* 0: call type (uint8 = 1 byte)
* 1-8: poolId (uint64 = 8 bytes)
* 9-24: trancheId (16 bytes)
* 25-45: user (Ethereum address, 20 bytes - Skip 12 bytes from 32-byte addresses)
* 57-65: validUntil (uint64 = 8 bytes)
*
*/
function formatUpdateMember(uint64 poolId, bytes16 trancheId, address member, uint64 validUntil)
internal
pure
returns (bytes memory)
{
return formatUpdateMember(poolId, trancheId, bytes32(bytes20(member)), validUntil);
}
function formatUpdateMember(uint64 poolId, bytes16 trancheId, bytes32 member, uint64 validUntil)
internal
pure
returns (bytes memory)
{
return abi.encodePacked(uint8(Call.UpdateMember), poolId, trancheId, member, validUntil);
}
function isUpdateMember(bytes memory _msg) internal pure returns (bool) {
return messageType(_msg) == Call.UpdateMember;
}
function parseUpdateMember(bytes memory _msg)
internal
pure
returns (uint64 poolId, bytes16 trancheId, address user, uint64 validUntil)
{
poolId = BytesLib.toUint64(_msg, 1);
trancheId = BytesLib.toBytes16(_msg, 9);
user = BytesLib.toAddress(_msg, 25);
validUntil = BytesLib.toUint64(_msg, 57);
}
/**
* Update a Tranche token's price
*
* 0: call type (uint8 = 1 byte)
* 1-8: poolId (uint64 = 8 bytes)
* 9-24: trancheId (16 bytes)
* 25-40: currency (uint128 = 16 bytes)
* 41-56: price (uint128 = 16 bytes)
*/
function formatUpdateTrancheTokenPrice(uint64 poolId, bytes16 trancheId, uint128 currencyId, uint128 price)
internal
pure
returns (bytes memory)
{
return abi.encodePacked(uint8(Call.UpdateTrancheTokenPrice), poolId, trancheId, currencyId, price);
}
function isUpdateTrancheTokenPrice(bytes memory _msg) internal pure returns (bool) {
return messageType(_msg) == Call.UpdateTrancheTokenPrice;
}
function parseUpdateTrancheTokenPrice(bytes memory _msg)
internal
pure
returns (uint64 poolId, bytes16 trancheId, uint128 currencyId, uint128 price)
{
poolId = BytesLib.toUint64(_msg, 1);
trancheId = BytesLib.toBytes16(_msg, 9);
currencyId = BytesLib.toUint128(_msg, 25);
price = BytesLib.toUint128(_msg, 41);
}
/*
* Transfer Message - Transfer stable coins
*
* 0: call type (uint8 = 1 byte)
* 1-16: currency (uint128 = 16 bytes)
* 17-48: sender address (32 bytes)
* 49-80: receiver address (32 bytes)
* 81-96: amount (uint128 = 16 bytes)
*/
function formatTransfer(uint128 currency, bytes32 sender, bytes32 receiver, uint128 amount)
internal
pure
returns (bytes memory)
{
return abi.encodePacked(uint8(Call.Transfer), currency, sender, receiver, amount);
}
function isTransfer(bytes memory _msg) internal pure returns (bool) {
return messageType(_msg) == Call.Transfer;
}
function parseTransfer(bytes memory _msg)
internal
pure
returns (uint128 currency, bytes32 sender, bytes32 receiver, uint128 amount)
{
currency = BytesLib.toUint128(_msg, 1);
sender = BytesLib.toBytes32(_msg, 17);
receiver = BytesLib.toBytes32(_msg, 49);
amount = BytesLib.toUint128(_msg, 81);
}
// An optimised `parseTransfer` function that saves gas by ignoring the `sender` field and that
// parses and returns the `recipient` as an `address` instead of the `bytes32` the message holds.
function parseIncomingTransfer(bytes memory _msg)
internal
pure
returns (uint128 currency, address recipient, uint128 amount)
{
currency = BytesLib.toUint128(_msg, 1);
recipient = BytesLib.toAddress(_msg, 49);
amount = BytesLib.toUint128(_msg, 81);
}
/**
* TransferTrancheTokens
*
* 0: call type (uint8 = 1 byte)
* 1-8: poolId (uint64 = 8 bytes)
* 9-24: trancheId (16 bytes)
* 25-56: sender (bytes32)
* 57-65: destinationDomain ((Domain: u8, ChainId: u64) = 9 bytes total)
* 66-97: destinationAddress (32 bytes - Either a Centrifuge address or an EVM address followed by 12 zeros)
* 98-113: amount (uint128 = 16 bytes)
*/
function formatTransferTrancheTokens(
uint64 poolId,
bytes16 trancheId,
bytes32 sender,
bytes9 destinationDomain,
bytes32 destinationAddress,
uint128 amount
) internal pure returns (bytes memory) {
return abi.encodePacked(
uint8(Call.TransferTrancheTokens), poolId, trancheId, sender, destinationDomain, destinationAddress, amount
);
}
// Overload: Format a TransferTrancheTokens to an EVM domain
// Note: This is an overload function to dry the cast from `address` to `bytes32`
// for the `destinationAddress` field by using the default `formatTransferTrancheTokens` implementation
// by appending 12 zeros to the evm-based `destinationAddress`.
function formatTransferTrancheTokens(
uint64 poolId,
bytes16 trancheId,
bytes32 sender,
bytes9 destinationDomain,
address destinationAddress,
uint128 amount
) internal pure returns (bytes memory) {
return formatTransferTrancheTokens(
poolId, trancheId, sender, destinationDomain, bytes32(bytes20(destinationAddress)), amount
);
}
function isTransferTrancheTokens(bytes memory _msg) internal pure returns (bool) {
return messageType(_msg) == Call.TransferTrancheTokens;
}
// Parse a TransferTrancheTokens to an EVM-based `destinationAddress` (20-byte long).
// We ignore the `sender` and the `domain` since it's not relevant when parsing an incoming message.
function parseTransferTrancheTokens20(bytes memory _msg)
internal
pure
returns (uint64 poolId, bytes16 trancheId, address destinationAddress, uint128 amount)
{
poolId = BytesLib.toUint64(_msg, 1);
trancheId = BytesLib.toBytes16(_msg, 9);
// ignore: `sender` at bytes 25-56
// ignore: `domain` at bytes 57-65
destinationAddress = BytesLib.toAddress(_msg, 66);
amount = BytesLib.toUint128(_msg, 98);
}
/*
* IncreaseInvestOrder Message
*
* 0: call type (uint8 = 1 byte)
* 1-8: poolId (uint64 = 8 bytes)
* 9-24: trancheId (16 bytes)
* 25-56: investor address (32 bytes)
* 57-72: currency (uint128 = 16 bytes)
* 73-89: amount (uint128 = 16 bytes)
*/
function formatIncreaseInvestOrder(
uint64 poolId,
bytes16 trancheId,
bytes32 investor,
uint128 currency,
uint128 amount
) internal pure returns (bytes memory) {
return abi.encodePacked(uint8(Call.IncreaseInvestOrder), poolId, trancheId, investor, currency, amount);
}
function isIncreaseInvestOrder(bytes memory _msg) internal pure returns (bool) {
return messageType(_msg) == Call.IncreaseInvestOrder;
}
function parseIncreaseInvestOrder(bytes memory _msg)
internal
pure
returns (uint64 poolId, bytes16 trancheId, bytes32 investor, uint128 currency, uint128 amount)
{
poolId = BytesLib.toUint64(_msg, 1);
trancheId = BytesLib.toBytes16(_msg, 9);
investor = BytesLib.toBytes32(_msg, 25);
currency = BytesLib.toUint128(_msg, 57);
amount = BytesLib.toUint128(_msg, 73);
}
/*
* DecreaseInvestOrder Message
*
* 0: call type (uint8 = 1 byte)
* 1-8: poolId (uint64 = 8 bytes)
* 9-24: trancheId (16 bytes)
* 25-56: investor address (32 bytes)
* 57-72: currency (uint128 = 16 bytes)
* 73-89: amount (uint128 = 16 bytes)
*/
function formatDecreaseInvestOrder(
uint64 poolId,
bytes16 trancheId,
bytes32 investor,
uint128 currency,
uint128 amount
) internal pure returns (bytes memory) {
return abi.encodePacked(uint8(Call.DecreaseInvestOrder), poolId, trancheId, investor, currency, amount);
}
function isDecreaseInvestOrder(bytes memory _msg) internal pure returns (bool) {
return messageType(_msg) == Call.DecreaseInvestOrder;
}
function parseDecreaseInvestOrder(bytes memory _msg)
internal
pure
returns (uint64 poolId, bytes16 trancheId, bytes32 investor, uint128 currency, uint128 amount)
{
return parseIncreaseInvestOrder(_msg);
}
/*
* IncreaseRedeemOrder Message
*
* 0: call type (uint8 = 1 byte)
* 1-8: poolId (uint64 = 8 bytes)
* 9-24: trancheId (16 bytes)
* 25-56: investor address (32 bytes)
* 57-72: currency (uint128 = 16 bytes)
* 73-89: amount (uint128 = 16 bytes)
*/
function formatIncreaseRedeemOrder(
uint64 poolId,
bytes16 trancheId,
bytes32 investor,
uint128 currency,
uint128 amount
) internal pure returns (bytes memory) {
return abi.encodePacked(uint8(Call.IncreaseRedeemOrder), poolId, trancheId, investor, currency, amount);
}
function isIncreaseRedeemOrder(bytes memory _msg) internal pure returns (bool) {
return messageType(_msg) == Call.IncreaseRedeemOrder;
}
function parseIncreaseRedeemOrder(bytes memory _msg)
internal
pure
returns (uint64 poolId, bytes16 trancheId, bytes32 investor, uint128 currency, uint128 amount)
{
return parseIncreaseInvestOrder(_msg);
}
/*
* DecreaseRedeemOrder Message
*
* 0: call type (uint8 = 1 byte)
* 1-8: poolId (uint64 = 8 bytes)
* 9-24: trancheId (16 bytes)
* 25-56: investor address (32 bytes)
* 57-72: currency (uint128 = 16 bytes)
* 73-89: amount (uint128 = 16 bytes)
*/
function formatDecreaseRedeemOrder(
uint64 poolId,
bytes16 trancheId,
bytes32 investor,
uint128 currency,
uint128 amount
) internal pure returns (bytes memory) {
return abi.encodePacked(uint8(Call.DecreaseRedeemOrder), poolId, trancheId, investor, currency, amount);
}
function isDecreaseRedeemOrder(bytes memory _msg) internal pure returns (bool) {
return messageType(_msg) == Call.DecreaseRedeemOrder;
}
function parseDecreaseRedeemOrder(bytes memory _msg)
internal
pure
returns (uint64 poolId, bytes16 trancheId, bytes32 investor, uint128 currency, uint128 amount)
{
return parseDecreaseInvestOrder(_msg);
}
/*
* CollectInvest Message
*
* 0: call type (uint8 = 1 byte)
* 1-8: poolId (uint64 = 8 bytes)
* 9-24: trancheId (16 bytes)
* 25-56: investor address (32 bytes)
*/
function formatCollectInvest(uint64 poolId, bytes16 trancheId, bytes32 investor, uint128 currency)
internal
pure
returns (bytes memory)
{
return abi.encodePacked(uint8(Call.CollectInvest), poolId, trancheId, investor, currency);
}
function isCollectInvest(bytes memory _msg) internal pure returns (bool) {
return messageType(_msg) == Call.CollectInvest;
}
function parseCollectInvest(bytes memory _msg)
internal
pure
returns (uint64 poolId, bytes16 trancheId, bytes32 investor, uint128 currency)
{
poolId = BytesLib.toUint64(_msg, 1);
trancheId = BytesLib.toBytes16(_msg, 9);
investor = BytesLib.toBytes32(_msg, 25);
currency = BytesLib.toUint128(_msg, 57);
}
/*
* CollectRedeem Message
*
* 0: call type (uint8 = 1 byte)
* 1-8: poolId (uint64 = 8 bytes)
* 9-24: trancheId (16 bytes)
* 25-56: investor address (32 bytes)
*/
function formatCollectRedeem(uint64 poolId, bytes16 trancheId, bytes32 investor, uint128 currency)
internal
pure
returns (bytes memory)
{
return abi.encodePacked(uint8(Call.CollectRedeem), poolId, trancheId, investor, currency);
}
function isCollectRedeem(bytes memory _msg) internal pure returns (bool) {
return messageType(_msg) == Call.CollectRedeem;
}
function parseCollectRedeem(bytes memory _msg)
internal
pure
returns (uint64 poolId, bytes16 trancheId, bytes32 investor, uint128 currency)
{
poolId = BytesLib.toUint64(_msg, 1);
trancheId = BytesLib.toBytes16(_msg, 9);
investor = BytesLib.toBytes32(_msg, 25);
currency = BytesLib.toUint128(_msg, 57);
}
function formatExecutedDecreaseInvestOrder(
uint64 poolId,
bytes16 trancheId,
bytes32 investor,
uint128 currency,
uint128 currencyPayout,
uint128 remainingInvestOrder
) internal pure returns (bytes memory) {
return abi.encodePacked(
uint8(Call.ExecutedDecreaseInvestOrder),
poolId,
trancheId,
investor,
currency,
currencyPayout,
remainingInvestOrder
);
}
function isExecutedDecreaseInvestOrder(bytes memory _msg) internal pure returns (bool) {
return messageType(_msg) == Call.ExecutedDecreaseInvestOrder;
}
function parseExecutedDecreaseInvestOrder(bytes memory _msg)
internal
pure
returns (
uint64 poolId,
bytes16 trancheId,
address investor,
uint128 currency,
uint128 trancheTokenPayout,
uint128 remainingInvestOrder
)
{
poolId = BytesLib.toUint64(_msg, 1);
trancheId = BytesLib.toBytes16(_msg, 9);
investor = BytesLib.toAddress(_msg, 25);
currency = BytesLib.toUint128(_msg, 57);
trancheTokenPayout = BytesLib.toUint128(_msg, 73);
remainingInvestOrder = BytesLib.toUint128(_msg, 89);
}
function formatExecutedDecreaseRedeemOrder(
uint64 poolId,
bytes16 trancheId,
bytes32 investor,
uint128 currency,
uint128 trancheTokenPayout,
uint128 remainingRedeemOrder
) internal pure returns (bytes memory) {
return abi.encodePacked(
uint8(Call.ExecutedDecreaseRedeemOrder),
poolId,
trancheId,
investor,
currency,
trancheTokenPayout,
remainingRedeemOrder
);
}
function isExecutedDecreaseRedeemOrder(bytes memory _msg) internal pure returns (bool) {
return messageType(_msg) == Call.ExecutedDecreaseRedeemOrder;
}
function parseExecutedDecreaseRedeemOrder(bytes memory _msg)
internal
pure
returns (
uint64 poolId,
bytes16 trancheId,
address investor,
uint128 currency,
uint128 trancheTokensPayout,
uint128 remainingRedeemOrder
)
{
poolId = BytesLib.toUint64(_msg, 1);
trancheId = BytesLib.toBytes16(_msg, 9);
investor = BytesLib.toAddress(_msg, 25);
currency = BytesLib.toUint128(_msg, 57);
trancheTokensPayout = BytesLib.toUint128(_msg, 73);
remainingRedeemOrder = BytesLib.toUint128(_msg, 89);
}
function formatExecutedCollectInvest(
uint64 poolId,
bytes16 trancheId,
bytes32 investor,
uint128 currency,
uint128 currencyPayout,
uint128 trancheTokensPayout,
uint128 remainingInvestOrder
) internal pure returns (bytes memory) {
return abi.encodePacked(
uint8(Call.ExecutedCollectInvest),
poolId,
trancheId,
investor,
currency,
currencyPayout,
trancheTokensPayout,
remainingInvestOrder
);
}
function isExecutedCollectInvest(bytes memory _msg) internal pure returns (bool) {
return messageType(_msg) == Call.ExecutedCollectInvest;
}
function parseExecutedCollectInvest(bytes memory _msg)
internal
pure
returns (
uint64 poolId,
bytes16 trancheId,
address investor,
uint128 currency,
uint128 currencyPayout,
uint128 trancheTokensPayout,
uint128 remainingInvestOrder
)
{
poolId = BytesLib.toUint64(_msg, 1);
trancheId = BytesLib.toBytes16(_msg, 9);
investor = BytesLib.toAddress(_msg, 25);
currency = BytesLib.toUint128(_msg, 57);
currencyPayout = BytesLib.toUint128(_msg, 73);
trancheTokensPayout = BytesLib.toUint128(_msg, 89);
remainingInvestOrder = BytesLib.toUint128(_msg, 105);
}
function formatExecutedCollectRedeem(
uint64 poolId,
bytes16 trancheId,
bytes32 investor,
uint128 currency,
uint128 currencyPayout,
uint128 trancheTokensPayout,
uint128 remainingRedeemOrder
) internal pure returns (bytes memory) {
return abi.encodePacked(
uint8(Call.ExecutedCollectRedeem),
poolId,
trancheId,
investor,
currency,
currencyPayout,
trancheTokensPayout,
remainingRedeemOrder
);
}
function isExecutedCollectRedeem(bytes memory _msg) internal pure returns (bool) {
return messageType(_msg) == Call.ExecutedCollectRedeem;
}
function parseExecutedCollectRedeem(bytes memory _msg)
internal
pure
returns (
uint64 poolId,
bytes16 trancheId,
address investor,
uint128 currency,
uint128 currencyPayout,
uint128 trancheTokensPayout,
uint128 remainingRedeemOrder
)
{
poolId = BytesLib.toUint64(_msg, 1);
trancheId = BytesLib.toBytes16(_msg, 9);
investor = BytesLib.toAddress(_msg, 25);
currency = BytesLib.toUint128(_msg, 57);
currencyPayout = BytesLib.toUint128(_msg, 73);
trancheTokensPayout = BytesLib.toUint128(_msg, 89);
remainingRedeemOrder = BytesLib.toUint128(_msg, 105);
}
function formatScheduleUpgrade(address _contract) internal pure returns (bytes memory) {
return abi.encodePacked(uint8(Call.ScheduleUpgrade), _contract);
}
function isScheduleUpgrade(bytes memory _msg) internal pure returns (bool) {
return messageType(_msg) == Call.ScheduleUpgrade;
}
function parseScheduleUpgrade(bytes memory _msg) internal pure returns (address _contract) {
_contract = BytesLib.toAddress(_msg, 1);
}
function formatCancelUpgrade(address _contract) internal pure returns (bytes memory) {
return abi.encodePacked(uint8(Call.CancelUpgrade), _contract);
}
function isCancelUpgrade(bytes memory _msg) internal pure returns (bool) {
return messageType(_msg) == Call.CancelUpgrade;
}
function parseCancelUpgrade(bytes memory _msg) internal pure returns (address _contract) {
_contract = BytesLib.toAddress(_msg, 1);
}
/**
* Update tranche token metadata
*
* 0: call type (uint8 = 1 byte)
* 1-8: poolId (uint64 = 8 bytes)
* 9-24: trancheId (16 bytes)
* 25-152: tokenName (string = 128 bytes)
* 153-184: tokenSymbol (string = 32 bytes)
*/
function formatUpdateTrancheTokenMetadata(
uint64 poolId,
bytes16 trancheId,
string memory tokenName,
string memory tokenSymbol
) internal pure returns (bytes memory) {
return abi.encodePacked(
uint8(Call.UpdateTrancheTokenMetadata),
poolId,
trancheId,
_stringToBytes128(tokenName),
_stringToBytes32(tokenSymbol)
);
}
function isUpdateTrancheTokenMetadata(bytes memory _msg) internal pure returns (bool) {
return messageType(_msg) == Call.UpdateTrancheTokenMetadata;
}
function parseUpdateTrancheTokenMetadata(bytes memory _msg)
internal
pure
returns (uint64 poolId, bytes16 trancheId, string memory tokenName, string memory tokenSymbol)
{
poolId = BytesLib.toUint64(_msg, 1);
trancheId = BytesLib.toBytes16(_msg, 9);
tokenName = _bytes128ToString(BytesLib.slice(_msg, 25, 128));
tokenSymbol = _bytes32ToString(BytesLib.toBytes32(_msg, 153));
}
function formatCancelInvestOrder(uint64 poolId, bytes16 trancheId, bytes32 investor, uint128 currency)
internal
pure
returns (bytes memory)
{
return abi.encodePacked(uint8(Call.CancelInvestOrder), poolId, trancheId, investor, currency);
}
function parseCancelInvestOrder(bytes memory _msg)
internal
pure
returns (uint64 poolId, bytes16 trancheId, address investor, uint128 currency)
{
poolId = BytesLib.toUint64(_msg, 1);
trancheId = BytesLib.toBytes16(_msg, 9);
investor = BytesLib.toAddress(_msg, 25);
currency = BytesLib.toUint128(_msg, 57);
}
function formatCancelRedeemOrder(uint64 poolId, bytes16 trancheId, bytes32 investor, uint128 currency)
internal
pure
returns (bytes memory)
{
return abi.encodePacked(uint8(Call.CancelRedeemOrder), poolId, trancheId, investor, currency);
}
function parseCancelRedeemOrder(bytes memory _msg)
internal
pure
returns (uint64 poolId, bytes16 trancheId, address investor, uint128 currency)
{
poolId = BytesLib.toUint64(_msg, 1);
trancheId = BytesLib.toBytes16(_msg, 9);
investor = BytesLib.toAddress(_msg, 25);
currency = BytesLib.toUint128(_msg, 57);
}
/**
* Update a Tranche investment limit
*
* 0: call type (uint8 = 1 byte)
* 1-8: poolId (uint64 = 8 bytes)
* 9-24: trancheId (16 bytes)
* 25-40: investmentLimit (uint128 = 16 bytes)
*/
function formatUpdateTrancheInvestmentLimit(uint64 poolId, bytes16 trancheId, uint128 investmentLimit)
internal
pure
returns (bytes memory)
{
return abi.encodePacked(uint8(Call.UpdateTrancheInvestmentLimit), poolId, trancheId, investmentLimit);
}
function isUpdateTrancheInvestmentLimit(bytes memory _msg) internal pure returns (bool) {
return messageType(_msg) == Call.UpdateTrancheInvestmentLimit;
}
function parseUpdateTrancheInvestmentLimit(bytes memory _msg)
internal
pure
returns (uint64 poolId, bytes16 trancheId, uint128 investmentLimit)
{
poolId = BytesLib.toUint64(_msg, 1);
trancheId = BytesLib.toBytes16(_msg, 9);
investmentLimit = BytesLib.toUint128(_msg, 25);
}
/**
* Freeze
*
* 0: call type (uint8 = 1 byte)
* 1-8: poolId (uint64 = 8 bytes)
* 9-24: trancheId (16 bytes)
* 25-45: user (Ethereum address, 20 bytes - Skip 12 bytes from 32-byte addresses)
*
*/
function formatFreeze(uint64 poolId, bytes16 trancheId, address member) internal pure returns (bytes memory) {
return abi.encodePacked(uint8(Call.Freeze), poolId, trancheId, bytes32(bytes20(member)));
}
function isFreeze(bytes memory _msg) internal pure returns (bool) {
return messageType(_msg) == Call.Freeze;
}
function parseFreeze(bytes memory _msg) internal pure returns (uint64 poolId, bytes16 trancheId, address user) {
poolId = BytesLib.toUint64(_msg, 1);
trancheId = BytesLib.toBytes16(_msg, 9);
user = BytesLib.toAddress(_msg, 25);
}
/**
* Unfreeze
*
* 0: call type (uint8 = 1 byte)
* 1-8: poolId (uint64 = 8 bytes)
* 9-24: trancheId (16 bytes)
* 25-45: user (Ethereum address, 20 bytes - Skip 12 bytes from 32-byte addresses)
*
*/
function formatUnfreeze(uint64 poolId, bytes16 trancheId, address user) internal pure returns (bytes memory) {
return abi.encodePacked(uint8(Call.Unfreeze), poolId, trancheId, bytes32(bytes20(user)));
}
function isUnfreeze(bytes memory _msg) internal pure returns (bool) {
return messageType(_msg) == Call.Unfreeze;
}
function parseUnfreeze(bytes memory _msg) internal pure returns (uint64 poolId, bytes16 trancheId, address user) {
poolId = BytesLib.toUint64(_msg, 1);
trancheId = BytesLib.toBytes16(_msg, 9);
user = BytesLib.toAddress(_msg, 25);
}
// Utils
function formatDomain(Domain domain) public pure returns (bytes9) {
return bytes9(bytes1(uint8(domain)));
}
function formatDomain(Domain domain, uint64 chainId) public pure returns (bytes9) {
return bytes9(BytesLib.slice(abi.encodePacked(uint8(domain), chainId), 0, 9));
}
function _stringToBytes128(string memory source) internal pure returns (bytes memory) {
bytes memory temp = bytes(source);
bytes memory result = new bytes(128);
for (uint256 i = 0; i < 128; i++) {
if (i < temp.length) {
result[i] = temp[i];
} else {
result[i] = 0x00;
}
}
return result;
}
function _bytes128ToString(bytes memory _bytes128) internal pure returns (string memory) {
require(_bytes128.length == 128, "Input should be 128 bytes");
uint8 i = 0;
while (i < 128 && _bytes128[i] != 0) {
i++;
}
bytes memory bytesArray = new bytes(i);
for (uint8 j = 0; j < i; j++) {
bytesArray[j] = _bytes128[j];
}
return string(bytesArray);
}
function _stringToBytes32(string memory source) internal pure returns (bytes32 result) {
bytes memory tempEmptyStringTest = bytes(source);
if (tempEmptyStringTest.length == 0) {
return 0x0;
}
assembly {
result := mload(add(source, 32))
}
}
function _bytes32ToString(bytes32 _bytes32) internal pure returns (string memory) {
uint8 i = 0;
while (i < 32 && _bytes32[i] != 0) {
i++;
}
bytes memory bytesArray = new bytes(i);
for (i = 0; i < 32 && _bytes32[i] != 0; i++) {
bytesArray[i] = _bytes32[i];
}
return string(bytesArray);
}
}