This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
MagnetarV2.sol
1088 lines (1002 loc) · 41.2 KB
/
MagnetarV2.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
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.18;
//OZ
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
//TAPIOCA
import "./MagnetarV2Storage.sol";
import "./modules/MagnetarMarketModule.sol";
/*
__/\\\\\\\\\\\\\\\_____/\\\\\\\\\_____/\\\\\\\\\\\\\____/\\\\\\\\\\\_______/\\\\\_____________/\\\\\\\\\_____/\\\\\\\\\____
_\///////\\\/////____/\\\\\\\\\\\\\__\/\\\/////////\\\_\/////\\\///______/\\\///\\\________/\\\////////____/\\\\\\\\\\\\\__
_______\/\\\________/\\\/////////\\\_\/\\\_______\/\\\_____\/\\\_______/\\\/__\///\\\____/\\\/____________/\\\/////////\\\_
_______\/\\\_______\/\\\_______\/\\\_\/\\\\\\\\\\\\\/______\/\\\______/\\\______\//\\\__/\\\_____________\/\\\_______\/\\\_
_______\/\\\_______\/\\\\\\\\\\\\\\\_\/\\\/////////________\/\\\_____\/\\\_______\/\\\_\/\\\_____________\/\\\\\\\\\\\\\\\_
_______\/\\\_______\/\\\/////////\\\_\/\\\_________________\/\\\_____\//\\\______/\\\__\//\\\____________\/\\\/////////\\\_
_______\/\\\_______\/\\\_______\/\\\_\/\\\_________________\/\\\______\///\\\__/\\\_____\///\\\__________\/\\\_______\/\\\_
_______\/\\\_______\/\\\_______\/\\\_\/\\\______________/\\\\\\\\\\\____\///\\\\\/________\////\\\\\\\\\_\/\\\_______\/\\\_
_______\///________\///________\///__\///______________\///////////_______\/////_____________\/////////__\///________\///__
*/
/// @title Magnetar contract
/// @notice Generic helper contract
/// @dev can execute individual or combined actions on BigBang/Market/tOFT and USDO
/// - the `burst` method allows combining multiple calls into 1 transaction
contract MagnetarV2 is Ownable, MagnetarV2Storage {
using SafeERC20 for IERC20;
using RebaseLibrary for Rebase;
// ************ //
// *** VARS *** //
// ************ //
enum Module {
Market
}
/// @notice returns the Market module
MagnetarMarketModule public marketModule;
constructor(address _owner, address payable _marketModule) {
transferOwnership(_owner);
marketModule = MagnetarMarketModule(_marketModule);
}
// ******************** //
// *** VIEW METHODS *** //
// ******************** //
/// @notice returns Singularity markets' information
/// @param who user to return for
/// @param markets the list of Singularity markets to query for
function singularityMarketInfo(
address who,
ISingularity[] calldata markets
) external view returns (SingularityInfo[] memory) {
return _singularityMarketInfo(who, markets);
}
/// @notice returns BigBang markets' information
/// @param who user to return for
/// @param markets the list of BigBang markets to query for
function bigBangMarketInfo(
address who,
IBigBang[] calldata markets
) external view returns (BigBangInfo[] memory) {
return _bigBangMarketInfo(who, markets);
}
/// @notice Calculate the collateral amount off the shares.
/// @param market the Singularity or BigBang address
/// @param share The shares.
/// @return amount The amount.
function getCollateralAmountForShare(
IMarket market,
uint256 share
) public view returns (uint256 amount) {
IYieldBoxBase yieldBox = IYieldBoxBase(market.yieldBox());
return yieldBox.toAmount(market.collateralId(), share, false);
}
/// @notice Calculate the collateral shares that are needed for `borrowPart`,
/// taking the current exchange rate into account.
/// @param market the Singularity or BigBang address
/// @param borrowPart The borrow part.
/// @return collateralShares The collateral shares.
function getCollateralSharesForBorrowPart(
IMarket market,
uint256 borrowPart,
uint256 liquidationMultiplierPrecision,
uint256 exchangeRatePrecision
) public view returns (uint256 collateralShares) {
Rebase memory _totalBorrowed;
(uint128 totalBorrowElastic, uint128 totalBorrowBase) = market
.totalBorrow();
_totalBorrowed = Rebase(totalBorrowElastic, totalBorrowBase);
IYieldBoxBase yieldBox = IYieldBoxBase(market.yieldBox());
uint256 borrowAmount = _totalBorrowed.toElastic(borrowPart, false);
return
yieldBox.toShare(
market.collateralId(),
(borrowAmount *
market.liquidationMultiplier() *
market.exchangeRate()) /
(liquidationMultiplierPrecision * exchangeRatePrecision),
false
);
}
/// @notice Return the equivalent of borrow part in asset amount.
/// @param market the Singularity or BigBang address
/// @param borrowPart The amount of borrow part to convert.
/// @return amount The equivalent of borrow part in asset amount.
function getAmountForBorrowPart(
IMarket market,
uint256 borrowPart
) public view returns (uint256 amount) {
Rebase memory _totalBorrowed;
(uint128 totalBorrowElastic, uint128 totalBorrowBase) = market
.totalBorrow();
_totalBorrowed = Rebase(totalBorrowElastic, totalBorrowBase);
return _totalBorrowed.toElastic(borrowPart, false);
}
/// @notice Return the equivalent of amount in borrow part.
/// @param market the Singularity or BigBang address
/// @param amount The amount to convert.
/// @return part The equivalent of amount in borrow part.
function getBorrowPartForAmount(
IMarket market,
uint256 amount
) public view returns (uint256 part) {
Rebase memory _totalBorrowed;
(uint128 totalBorrowElastic, uint128 totalBorrowBase) = market
.totalBorrow();
_totalBorrowed = Rebase(totalBorrowElastic, totalBorrowBase);
return _totalBorrowed.toBase(amount, false);
}
/// @notice Compute the amount of `singularity.assetId` from `fraction`
/// `fraction` can be `singularity.accrueInfo.feeFraction` or `singularity.balanceOf`
/// @param singularity the singularity address
/// @param fraction The fraction.
/// @return amount The amount.
function getAmountForAssetFraction(
ISingularity singularity,
uint256 fraction
) public view returns (uint256 amount) {
(uint128 totalAssetElastic, uint128 totalAssetBase) = singularity
.totalAsset();
IYieldBoxBase yieldBox = IYieldBoxBase(singularity.yieldBox());
return
yieldBox.toAmount(
singularity.assetId(),
(fraction * totalAssetElastic) / totalAssetBase,
false
);
}
/// @notice Compute the fraction of `singularity.assetId` from `amount`
/// `fraction` can be `singularity.accrueInfo.feeFraction` or `singularity.balanceOf`
/// @param singularity the singularity address
/// @param amount The amount.
/// @return fraction The fraction.
function getFractionForAmount(
ISingularity singularity,
uint256 amount
) public view returns (uint256 fraction) {
(uint128 totalAssetShare, uint128 totalAssetBase) = singularity
.totalAsset();
(uint128 totalBorrowElastic, ) = singularity.totalBorrow();
uint256 assetId = singularity.assetId();
IYieldBoxBase yieldBox = IYieldBoxBase(singularity.yieldBox());
uint256 share = yieldBox.toShare(assetId, amount, false);
uint256 allShare = totalAssetShare +
yieldBox.toShare(assetId, totalBorrowElastic, true);
fraction = allShare == 0 ? share : (share * totalAssetBase) / allShare;
}
// ********************** //
// *** PUBLIC METHODS *** //
// ********************** //
/// @notice Batch multiple calls together
/// @param calls The list of actions to perform
function burst(
Call[] calldata calls
) external payable returns (Result[] memory returnData) {
uint256 valAccumulator;
uint256 length = calls.length;
returnData = new Result[](length);
for (uint256 i = 0; i < length; i++) {
Call calldata _action = calls[i];
if (!_action.allowFailure) {
require(
_action.call.length > 0,
string.concat(
"MagnetarV2: Missing call for action with index",
string(abi.encode(i))
)
);
}
unchecked {
valAccumulator += _action.value;
}
if (_action.id == PERMIT_ALL) {
_permit(
_action.target,
_action.call,
true,
_action.allowFailure
);
} else if (_action.id == PERMIT) {
_permit(
_action.target,
_action.call,
false,
_action.allowFailure
);
} else if (_action.id == TOFT_WRAP) {
WrapData memory data = abi.decode(_action.call[4:], (WrapData));
_checkSender(data.from);
if (_action.value > 0) {
unchecked {
valAccumulator += _action.value;
}
ITapiocaOFT(_action.target).wrapNative{
value: _action.value
}(data.to);
} else {
ITapiocaOFT(_action.target).wrap(
msg.sender,
data.to,
data.amount
);
}
} else if (_action.id == TOFT_SEND_FROM) {
(
address from,
uint16 dstChainId,
bytes32 to,
uint256 amount,
ISendFrom.LzCallParams memory lzCallParams
) = abi.decode(
_action.call[4:],
(
address,
uint16,
bytes32,
uint256,
(ISendFrom.LzCallParams)
)
);
_checkSender(from);
ISendFrom(_action.target).sendFrom{value: _action.value}(
msg.sender,
dstChainId,
to,
amount,
lzCallParams
);
} else if (_action.id == YB_DEPOSIT_ASSET) {
YieldBoxDepositData memory data = abi.decode(
_action.call[4:],
(YieldBoxDepositData)
);
_checkSender(data.from);
(uint256 amountOut, uint256 shareOut) = IYieldBoxBase(
_action.target
).depositAsset(
data.assetId,
msg.sender,
data.to,
data.amount,
data.share
);
returnData[i] = Result({
success: true,
returnData: abi.encode(amountOut, shareOut)
});
} else if (_action.id == MARKET_ADD_COLLATERAL) {
SGLAddCollateralData memory data = abi.decode(
_action.call[4:],
(SGLAddCollateralData)
);
_checkSender(data.from);
IMarket(_action.target).addCollateral(
msg.sender,
data.to,
data.skim,
data.amount,
data.share
);
} else if (_action.id == MARKET_BORROW) {
SGLBorrowData memory data = abi.decode(
_action.call[4:],
(SGLBorrowData)
);
_checkSender(data.from);
(uint256 part, uint256 share) = IMarket(_action.target).borrow(
msg.sender,
data.to,
data.amount
);
returnData[i] = Result({
success: true,
returnData: abi.encode(part, share)
});
} else if (_action.id == YB_WITHDRAW_TO) {
(
address yieldBox,
address from,
uint256 assetId,
uint16 dstChainId,
bytes32 receiver,
uint256 amount,
uint256 share,
bytes memory adapterParams,
address payable refundAddress
) = abi.decode(
_action.call[4:],
(
address,
address,
uint256,
uint16,
bytes32,
uint256,
uint256,
bytes,
address
)
);
_executeModule(
Module.Market,
abi.encodeWithSelector(
MagnetarMarketModule.withdrawToChain.selector,
yieldBox,
from,
assetId,
dstChainId,
receiver,
amount,
share,
adapterParams,
refundAddress,
_action.value
)
);
} else if (_action.id == MARKET_LEND) {
SGLLendData memory data = abi.decode(
_action.call[4:],
(SGLLendData)
);
_checkSender(data.from);
uint256 fraction = IMarket(_action.target).addAsset(
msg.sender,
data.to,
data.skim,
data.share
);
returnData[i] = Result({
success: true,
returnData: abi.encode(fraction)
});
} else if (_action.id == MARKET_REPAY) {
SGLRepayData memory data = abi.decode(
_action.call[4:],
(SGLRepayData)
);
_checkSender(data.from);
uint256 amount = IMarket(_action.target).repay(
msg.sender,
data.to,
data.skim,
data.part
);
returnData[i] = Result({
success: true,
returnData: abi.encode(amount)
});
} else if (_action.id == TOFT_SEND_AND_BORROW) {
(
address from,
address to,
uint16 lzDstChainId,
bytes memory airdropAdapterParams,
ITapiocaOFT.IBorrowParams memory borrowParams,
ICommonData.IWithdrawParams memory withdrawParams,
ICommonData.ISendOptions memory options,
ICommonData.IApproval[] memory approvals
) = abi.decode(
_action.call[4:],
(
address,
address,
uint16,
bytes,
ITapiocaOFT.IBorrowParams,
ICommonData.IWithdrawParams,
ICommonData.ISendOptions,
ICommonData.IApproval[]
)
);
_checkSender(from);
ITapiocaOFT(_action.target).sendToYBAndBorrow{
value: _action.value
}(
msg.sender,
to,
lzDstChainId,
airdropAdapterParams,
borrowParams,
withdrawParams,
options,
approvals
);
} else if (_action.id == TOFT_SEND_AND_LEND) {
(
address from,
address to,
uint16 dstChainId,
address zroPaymentAddress,
IUSDOBase.ILendOrRepayParams memory lendParams,
ICommonData.IApproval[] memory approvals,
ICommonData.IWithdrawParams memory withdrawParams,
bytes memory adapterParams
) = abi.decode(
_action.call[4:],
(
address,
address,
uint16,
address,
(IUSDOBase.ILendOrRepayParams),
(ICommonData.IApproval[]),
(ICommonData.IWithdrawParams),
bytes
)
);
_checkSender(from);
IUSDOBase(_action.target).sendAndLendOrRepay{
value: _action.value
}(
msg.sender,
to,
dstChainId,
zroPaymentAddress,
lendParams,
approvals,
withdrawParams,
adapterParams
);
} else if (_action.id == TOFT_DEPOSIT_TO_STRATEGY) {
TOFTSendToStrategyData memory data = abi.decode(
_action.call[4:],
(TOFTSendToStrategyData)
);
_checkSender(data.from);
ITapiocaOFT(_action.target).sendToStrategy{
value: _action.value
}(
msg.sender,
data.to,
data.amount,
data.share,
data.assetId,
data.lzDstChainId,
data.options
);
} else if (_action.id == TOFT_RETRIEVE_FROM_STRATEGY) {
(
address from,
uint256 amount,
uint256 share,
uint256 assetId,
uint16 lzDstChainId,
address zroPaymentAddress,
bytes memory airdropAdapterParam
) = abi.decode(
_action.call[4:],
(
address,
uint256,
uint256,
uint256,
uint16,
address,
bytes
)
);
_checkSender(from);
ITapiocaOFT(_action.target).retrieveFromStrategy{
value: _action.value
}(
msg.sender,
amount,
share,
assetId,
lzDstChainId,
zroPaymentAddress,
airdropAdapterParam
);
} else if (_action.id == MARKET_YBDEPOSIT_AND_LEND) {
HelperLendData memory data = abi.decode(
_action.call[4:],
(HelperLendData)
);
_executeModule(
Module.Market,
abi.encodeWithSelector(
MagnetarMarketModule.mintFromBBAndLendOnSGL.selector,
data.user,
data.lendAmount,
data.mintData,
data.depositData,
data.lockData,
data.participateData,
data.externalContracts
)
);
} else if (_action.id == MARKET_YBDEPOSIT_COLLATERAL_AND_BORROW) {
(
address market,
address user,
uint256 collateralAmount,
uint256 borrowAmount,
,
bool deposit,
ICommonData.IWithdrawParams memory withdrawParams
) = abi.decode(
_action.call[4:],
(
address,
address,
uint256,
uint256,
bool,
bool,
ICommonData.IWithdrawParams
)
);
_executeModule(
Module.Market,
abi.encodeWithSelector(
MagnetarMarketModule
.depositAddCollateralAndBorrowFromMarket
.selector,
market,
user,
collateralAmount,
borrowAmount,
false,
deposit,
withdrawParams
)
);
} else if (_action.id == MARKET_REMOVE_ASSET) {
HelperMarketRemoveAndRepayAsset memory data = abi.decode(
_action.call[4:],
(HelperMarketRemoveAndRepayAsset)
);
_executeModule(
Module.Market,
abi.encodeWithSelector(
MagnetarMarketModule
.exitPositionAndRemoveCollateral
.selector,
data.user,
data.externalData,
data.removeAndRepayData
)
);
} else if (_action.id == MARKET_DEPOSIT_REPAY_REMOVE_COLLATERAL) {
HelperDepositRepayRemoveCollateral memory data = abi.decode(
_action.call[4:],
(HelperDepositRepayRemoveCollateral)
);
_executeModule(
Module.Market,
abi.encodeWithSelector(
MagnetarMarketModule
.depositRepayAndRemoveCollateralFromMarket
.selector,
data.market,
data.user,
data.depositAmount,
data.repayAmount,
data.collateralAmount,
data.extractFromSender,
data.withdrawCollateralParams
)
);
} else if (_action.id == MARKET_BUY_COLLATERAL) {
HelperBuyCollateral memory data = abi.decode(
_action.call[4:],
(HelperBuyCollateral)
);
IMarket(data.market).buyCollateral(
data.from,
data.borrowAmount,
data.supplyAmount,
data.minAmountOut,
address(data.swapper),
data.dexData
);
} else if (_action.id == MARKET_SELL_COLLATERAL) {
HelperSellCollateral memory data = abi.decode(
_action.call[4:],
(HelperSellCollateral)
);
IMarket(data.market).sellCollateral(
data.from,
data.share,
data.minAmountOut,
address(data.swapper),
data.dexData
);
} else if (_action.id == TAP_EXERCISE_OPTION) {
HelperExerciseOption memory data = abi.decode(
_action.call[4:],
(HelperExerciseOption)
);
ITapiocaOptionsBrokerCrossChain(_action.target).exerciseOption(
data.optionsData,
data.lzData,
data.tapSendData,
data.approvals
);
} else if (_action.id == MARKET_MULTIHOP_BUY) {
HelperMultiHopBuy memory data = abi.decode(
_action.call[4:],
(HelperMultiHopBuy)
);
IUSDOBase(_action.target).initMultiHopBuy(
data.from,
data.collateralAmount,
data.borrowAmount,
data.swapData,
data.lzData,
data.externalData,
data.airdropAdapterParams,
data.approvals
);
} else if (_action.id == MARKET_MULTIHOP_BUY) {
HelperMultiHopBuy memory data = abi.decode(
_action.call[4:],
(HelperMultiHopBuy)
);
IUSDOBase(_action.target).initMultiHopBuy(
data.from,
data.collateralAmount,
data.borrowAmount,
data.swapData,
data.lzData,
data.externalData,
data.airdropAdapterParams,
data.approvals
);
} else if (_action.id == TOFT_REMOVE_AND_REPAY) {
HelperTOFTRemoveAndRepayAsset memory data = abi.decode(
_action.call[4:],
(HelperTOFTRemoveAndRepayAsset)
);
IUSDOBase(_action.target).removeAsset(
data.from,
data.to,
data.lzDstChainId,
data.zroPaymentAddress,
data.adapterParams,
data.externalData,
data.removeAndRepayData,
data.approvals
);
} else {
revert("MagnetarV2: action not valid");
}
}
require(msg.value == valAccumulator, "MagnetarV2: value mismatch");
}
/// @notice performs a withdraw operation
/// @dev it can withdraw on the current chain or it can send it to another one
/// - if `dstChainId` is 0 performs a same-chain withdrawal
/// - all parameters except `yieldBox`, `from`, `assetId` and `amount` or `share` are ignored
/// - if `dstChainId` is NOT 0, the method requires gas for the `sendFrom` operation
/// @param yieldBox the YieldBox address
/// @param from user to withdraw from
/// @param assetId the YieldBox asset id to withdraw
/// @param dstChainId LZ chain id to withdraw to
/// @param receiver the receiver on the destination chain
/// @param amount the amount to withdraw
/// @param share the share to withdraw
/// @param adapterParams LZ adapter params
/// @param refundAddress the LZ refund address which receives the gas not used in the process
/// @param gas the amount of gas to use for sending the asset to another layer
function withdrawToChain(
IYieldBoxBase yieldBox,
address from,
uint256 assetId,
uint16 dstChainId,
bytes32 receiver,
uint256 amount,
uint256 share,
bytes memory adapterParams,
address payable refundAddress,
uint256 gas
) external payable {
_executeModule(
Module.Market,
abi.encodeWithSelector(
MagnetarMarketModule.withdrawToChain.selector,
yieldBox,
from,
assetId,
dstChainId,
receiver,
amount,
share,
adapterParams,
refundAddress,
gas
)
);
}
/// @notice helper for deposit to YieldBox, add collateral to a market, borrom from the same market and withdraw
/// @dev all operations are optional:
/// - if `deposit` is false it will skip the deposit to YieldBox step
/// - if `withdraw` is false it will skip the withdraw step
/// - if `collateralAmount == 0` it will skip the add collateral step
/// - if `borrowAmount == 0` it will skip the borrow step
/// - the amount deposited to YieldBox is `collateralAmount`
/// @param market the SGL/BigBang market
/// @param user the user to perform the action for
/// @param collateralAmount the collateral amount to add
/// @param borrowAmount the borrow amount
/// @param extractFromSender extracts collateral tokens from sender or from the user
/// @param deposit true/false flag for the deposit to YieldBox step
/// @param withdrawParams necessary data for the same chain or the cross-chain withdrawal
function depositAddCollateralAndBorrowFromMarket(
IMarket market,
address user,
uint256 collateralAmount,
uint256 borrowAmount,
bool extractFromSender,
bool deposit,
ICommonData.IWithdrawParams calldata withdrawParams
) external payable {
_executeModule(
Module.Market,
abi.encodeWithSelector(
MagnetarMarketModule
.depositAddCollateralAndBorrowFromMarket
.selector,
market,
user,
collateralAmount,
borrowAmount,
extractFromSender,
deposit,
withdrawParams
)
);
}
/// @notice helper for deposit asset to YieldBox, repay on a market, remove collateral and withdraw
/// @dev all steps are optional:
/// - if `depositAmount` is 0, the deposit to YieldBox step is skipped
/// - if `repayAmount` is 0, the repay step is skipped
/// - if `collateralAmount` is 0, the add collateral step is skipped
/// @param market the SGL/BigBang market
/// @param user the user to perform the action for
/// @param depositAmount the amount to deposit to YieldBox
/// @param repayAmount the amount to repay to the market
/// @param collateralAmount the amount to withdraw from the market
/// @param extractFromSender extracts collateral tokens from sender or from the user
/// @param withdrawCollateralParams withdraw specific params
function depositRepayAndRemoveCollateralFromMarket(
address market,
address user,
uint256 depositAmount,
uint256 repayAmount,
uint256 collateralAmount,
bool extractFromSender,
ICommonData.IWithdrawParams calldata withdrawCollateralParams
) external payable {
_executeModule(
Module.Market,
abi.encodeWithSelector(
MagnetarMarketModule
.depositRepayAndRemoveCollateralFromMarket
.selector,
market,
user,
depositAmount,
repayAmount,
collateralAmount,
extractFromSender,
withdrawCollateralParams
)
);
}
/// @notice helper to deposit mint from BB, lend on SGL, lock on tOLP and participate on tOB
/// @dev all steps are optional:
/// - if `mintData.mint` is false, the mint operation on BB is skipped
/// - add BB collateral to YB, add collateral on BB and borrow from BB are part of the mint operation
/// - if `depositData.deposit` is false, the asset deposit to YB is skipped
/// - if `lendAmount == 0` the addAsset operation on SGL is skipped
/// - if `mintData.mint` is true, `lendAmount` will be automatically filled with the minted value
/// - if `lockData.lock` is false, the tOLP lock operation is skipped
/// - if `participateData.participate` is false, the tOB participate operation is skipped
/// @param user the user to perform the operation for
/// @param lendAmount the amount to lend on SGL
/// @param mintData the data needed to mint on BB
/// @param depositData the data needed for asset deposit on YieldBox
/// @param lockData the data needed to lock on TapiocaOptionLiquidityProvision
/// @param participateData the data needed to perform a participate operation on TapiocaOptionsBroker
/// @param externalContracts the contracts' addresses used in all the operations performed by the helper
function mintFromBBAndLendOnSGL(
address user,
uint256 lendAmount,
IUSDOBase.IMintData calldata mintData,
ICommonData.IDepositData calldata depositData,
ITapiocaOptionLiquidityProvision.IOptionsLockData calldata lockData,
ITapiocaOptionsBroker.IOptionsParticipateData calldata participateData,
ICommonData.ICommonExternalContracts calldata externalContracts
) external payable {
_executeModule(
Module.Market,
abi.encodeWithSelector(
MagnetarMarketModule.mintFromBBAndLendOnSGL.selector,
user,
lendAmount,
mintData,
depositData,
lockData,
participateData,
externalContracts
)
);
}
/// @notice helper to exit from tOB, unlock from tOLP, remove from SGL, repay on BB, remove collateral from BB and withdraw
/// @dev all steps are optional:
/// - if `removeAndRepayData.exitData.exit` is false, the exit operation is skipped
/// - if `removeAndRepayData.unlockData.unlock` is false, the unlock operation is skipped
/// - if `removeAndRepayData.removeAssetFromSGL` is false, the removeAsset operation is skipped
/// - if `!removeAndRepayData.assetWithdrawData.withdraw && removeAndRepayData.repayAssetOnBB`, the repay operation is performed
/// - if `removeAndRepayData.removeCollateralFromBB` is false, the rmeove collateral is skipped
/// - the helper can either stop at the remove asset from SGL step or it can continue until is removes & withdraws collateral from BB
/// - removed asset can be withdrawn by providing `removeAndRepayData.assetWithdrawData`
/// - BB collateral can be removed by providing `removeAndRepayData.collateralWithdrawData`
function exitPositionAndRemoveCollateral(
address user,
ICommonData.ICommonExternalContracts calldata externalData,
IUSDOBase.IRemoveAndRepay calldata removeAndRepayData
) external payable {
_executeModule(
Module.Market,
abi.encodeWithSelector(
MagnetarMarketModule.exitPositionAndRemoveCollateral.selector,
user,
externalData,
removeAndRepayData
)
);
}
// ********************** //
// *** PRIVATE METHODS *** //
// *********************** //
function _commonInfo(
address who,
IMarket market
) private view returns (MarketInfo memory) {
Rebase memory _totalBorrowed;
MarketInfo memory info;
info.collateral = market.collateral();
info.asset = market.asset();
info.oracle = IOracle(market.oracle());
info.oracleData = market.oracleData();
info.totalCollateralShare = market.totalCollateralShare();
info.userCollateralShare = market.userCollateralShare(who);
(uint128 totalBorrowElastic, uint128 totalBorrowBase) = market
.totalBorrow();
_totalBorrowed = Rebase(totalBorrowElastic, totalBorrowBase);
info.totalBorrow = _totalBorrowed;
info.userBorrowPart = market.userBorrowPart(who);
info.currentExchangeRate = market.exchangeRate();
(, info.oracleExchangeRate) = IOracle(market.oracle()).peek(
market.oracleData()
);
info.spotExchangeRate = IOracle(market.oracle()).peekSpot(
market.oracleData()
);
info.totalBorrowCap = market.totalBorrowCap();
info.assetId = market.assetId();
info.collateralId = market.collateralId();
IYieldBoxBase yieldBox = IYieldBoxBase(market.yieldBox());
(
info.totalYieldBoxCollateralShare,
info.totalYieldBoxCollateralAmount
) = yieldBox.assetTotals(info.collateralId);
(info.totalYieldBoxAssetShare, info.totalYieldBoxAssetAmount) = yieldBox
.assetTotals(info.assetId);
(
info.yieldBoxCollateralTokenType,
info.yieldBoxCollateralContractAddress,
info.yieldBoxCollateralStrategyAddress,
info.yieldBoxCollateralTokenId
) = yieldBox.assets(info.collateralId);
(
info.yieldBoxAssetTokenType,
info.yieldBoxAssetContractAddress,
info.yieldBoxAssetStrategyAddress,
info.yieldBoxAssetTokenId
) = yieldBox.assets(info.assetId);
return info;
}
function _singularityMarketInfo(
address who,
ISingularity[] memory markets
) private view returns (SingularityInfo[] memory) {
uint256 len = markets.length;
SingularityInfo[] memory result = new SingularityInfo[](len);
Rebase memory _totalAsset;
for (uint256 i = 0; i < len; i++) {
ISingularity sgl = markets[i];
result[i].market = _commonInfo(who, IMarket(address(sgl)));
(uint128 totalAssetElastic, uint128 totalAssetBase) = sgl //
.totalAsset(); //
_totalAsset = Rebase(totalAssetElastic, totalAssetBase); //
result[i].totalAsset = _totalAsset; //
result[i].userAssetFraction = sgl.balanceOf(who); //
(
ISingularity.AccrueInfo memory _accrueInfo,
uint256 _utilization
) = sgl.getInterestDetails();
result[i].accrueInfo = _accrueInfo;
result[i].utilization = _utilization;
}
return result;
}
function _bigBangMarketInfo(
address who,
IBigBang[] memory markets
) private view returns (BigBangInfo[] memory) {
uint256 len = markets.length;