-
Notifications
You must be signed in to change notification settings - Fork 9
/
BaseTOFT.sol
574 lines (541 loc) · 18.2 KB
/
BaseTOFT.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
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.18;
import "./BaseTOFTStorage.sol";
//TOFT MODULES
import "./modules/BaseTOFTLeverageModule.sol";
import "./modules/BaseTOFTStrategyModule.sol";
import "./modules/BaseTOFTMarketModule.sol";
import "./modules/BaseTOFTOptionsModule.sol";
/// @title BaseTOFT contract
/// @notice Common tOFT capabilitites
/// @dev all LayerZero methods are defined here
contract BaseTOFT is BaseTOFTStorage, ERC20Permit {
using SafeERC20 for IERC20;
using BytesLib for bytes;
// ************ //
// *** VARS *** //
// ************ //
enum Module {
Leverage,
Strategy,
Market,
Options
}
/// @notice returns the leverage module
BaseTOFTLeverageModule public leverageModule;
/// @notice returns the Strategy module
BaseTOFTStrategyModule public strategyModule;
/// @notice returns the Market module
BaseTOFTMarketModule public marketModule;
/// @notice returns the Options module
BaseTOFTOptionsModule public optionsModule;
// ******************//
// *** MODIFIERS *** //
// ***************** //
/// @notice Require that the caller is on the host chain of the ERC20.
modifier onlyHostChain() {
require(block.chainid == hostChainID, "TOFT_host");
_;
}
constructor(
address _lzEndpoint,
address _erc20,
IYieldBoxBase _yieldBox,
string memory _name,
string memory _symbol,
uint8 _decimal,
uint256 _hostChainID,
address payable _leverageModule,
address payable _strategyModule,
address payable _marketModule,
address payable _optionsModule
)
BaseTOFTStorage(
_lzEndpoint,
_erc20,
_yieldBox,
_name,
_symbol,
_decimal,
_hostChainID
)
ERC20Permit(string(abi.encodePacked("TapiocaOFT-", _name)))
{
leverageModule = BaseTOFTLeverageModule(_leverageModule);
strategyModule = BaseTOFTStrategyModule(_strategyModule);
marketModule = BaseTOFTMarketModule(_marketModule);
optionsModule = BaseTOFTOptionsModule(_optionsModule);
}
// ********************** //
// *** VIEW FUNCTIONS *** //
// ********************** //
/// @notice decimal number of the ERC20
function decimals() public view override returns (uint8) {
if (_decimalCache == 0) return 18; //temporary fix for LZ _sharedDecimals check
return _decimalCache;
}
// ************************ //
// *** PUBLIC FUNCTIONS *** //
// ************************ //
/// @notice triggers a sendFrom to another layer from destination
/// @param lzDstChainId LZ destination id
/// @param airdropAdapterParams airdrop params
/// @param zroPaymentAddress ZRO payment address
/// @param amount amount to send back
/// @param sendFromData data needed to trigger sendFrom on destination
/// @param approvals approvals array
function triggerSendFrom(
uint16 lzDstChainId,
bytes calldata airdropAdapterParams,
address zroPaymentAddress,
uint256 amount,
ISendFrom.LzCallParams calldata sendFromData,
ICommonData.IApproval[] calldata approvals
) external payable {
_executeModule(
Module.Options,
abi.encodeWithSelector(
BaseTOFTOptionsModule.triggerSendFrom.selector,
lzDstChainId,
airdropAdapterParams,
zroPaymentAddress,
amount,
sendFromData,
approvals
),
false
);
}
/// @notice Exercise an oTAP position
/// @param optionsData oTap exerciseOptions data
/// @param lzData data needed for the cross chain transer
/// @param tapSendData needed for withdrawing Tap token
/// @param approvals array
function exerciseOption(
ITapiocaOptionsBrokerCrossChain.IExerciseOptionsData
calldata optionsData,
ITapiocaOptionsBrokerCrossChain.IExerciseLZData calldata lzData,
ITapiocaOptionsBrokerCrossChain.IExerciseLZSendTapData
calldata tapSendData,
ICommonData.IApproval[] calldata approvals
) external payable {
_executeModule(
Module.Options,
abi.encodeWithSelector(
BaseTOFTOptionsModule.exerciseOption.selector,
optionsData,
lzData,
tapSendData,
approvals
),
false
);
}
/// @notice inits a multiHopSellCollateral call
/// @param from The user who sells
/// @param share Collateral YieldBox-shares to sell
/// @param swapData Swap data used on destination chain for swapping USDO to the underlying TOFT token
/// @param lzData LayerZero specific data
/// @param externalData External contracts used for the cross chain operation
/// @param airdropAdapterParams default or airdrop adapter params
/// @param approvals array
function initMultiSell(
address from,
uint256 share,
IUSDOBase.ILeverageSwapData calldata swapData,
IUSDOBase.ILeverageLZData calldata lzData,
IUSDOBase.ILeverageExternalContractsData calldata externalData,
bytes calldata airdropAdapterParams,
ICommonData.IApproval[] memory approvals
) external payable {
_executeModule(
Module.Leverage,
abi.encodeWithSelector(
BaseTOFTLeverageModule.initMultiSell.selector,
from,
share,
swapData,
lzData,
externalData,
airdropAdapterParams,
approvals
),
false
);
}
/// @notice calls removeCollateral on another layer
/// @param from sending address
/// @param to receiver address
/// @param lzDstChainId LayerZero destination chain id
/// @param zroPaymentAddress LayerZero ZRO payment address
/// @param withdrawParams withdrawTo specific params
/// @param removeParams removeAsset specific params
/// @param approvals approvals specific params
/// @param adapterParams LZ adapter params
function removeCollateral(
address from,
address to,
uint16 lzDstChainId,
address zroPaymentAddress,
ICommonData.IWithdrawParams calldata withdrawParams,
ITapiocaOFT.IRemoveParams calldata removeParams,
ICommonData.IApproval[] calldata approvals,
bytes calldata adapterParams
) external payable {
_executeModule(
Module.Market,
abi.encodeWithSelector(
BaseTOFTMarketModule.removeCollateral.selector,
from,
to,
lzDstChainId,
zroPaymentAddress,
withdrawParams,
removeParams,
approvals,
adapterParams
),
false
);
}
/// @notice sends TOFT to a specific strategy available on another layer
/// @param from the sender address
/// @param to the receiver address
/// @param amount the transferred amount
/// @param assetId the destination YieldBox asset id
/// @param lzDstChainId the destination LayerZero id
/// @param options the operation data
function sendToStrategy(
address from,
address to,
uint256 amount,
uint256 share,
uint256 assetId,
uint16 lzDstChainId,
ICommonData.ISendOptions calldata options
) external payable {
_executeModule(
Module.Strategy,
abi.encodeWithSelector(
BaseTOFTStrategyModule.sendToStrategy.selector,
from,
to,
amount,
share,
assetId,
lzDstChainId,
options
),
false
);
}
/// @notice extracts TOFT from a specific strategy available on another layer
/// @param from the sender address
/// @param amount the transferred amount
/// @param assetId the destination YieldBox asset id
/// @param lzDstChainId the destination LayerZero id
/// @param zroPaymentAddress LayerZero ZRO payment address
/// @param airdropAdapterParam the LayerZero aidrop adapter params
function retrieveFromStrategy(
address from,
uint256 amount,
uint256 share,
uint256 assetId,
uint16 lzDstChainId,
address zroPaymentAddress,
bytes memory airdropAdapterParam
) external payable {
_executeModule(
Module.Strategy,
abi.encodeWithSelector(
BaseTOFTStrategyModule.retrieveFromStrategy.selector,
from,
amount,
share,
assetId,
lzDstChainId,
zroPaymentAddress,
airdropAdapterParam
),
false
);
}
/// @notice sends TOFT to a specific chain and performs a borrow operation
/// @param from the sender address
/// @param to the receiver address
/// @param lzDstChainId the destination LayerZero id
/// @param airdropAdapterParams the LayerZero aidrop adapter params
/// @param borrowParams the borrow operation data
/// @param withdrawParams the withdraw operation data
/// @param options the cross chain send operation data
/// @param approvals the cross chain approval operation data
function sendToYBAndBorrow(
address from,
address to,
uint16 lzDstChainId,
bytes calldata airdropAdapterParams,
ITapiocaOFT.IBorrowParams calldata borrowParams,
ICommonData.IWithdrawParams calldata withdrawParams,
ICommonData.ISendOptions calldata options,
ICommonData.IApproval[] calldata approvals
) external payable {
_executeModule(
Module.Market,
abi.encodeWithSelector(
BaseTOFTMarketModule.sendToYBAndBorrow.selector,
from,
to,
lzDstChainId,
airdropAdapterParams,
borrowParams,
withdrawParams,
options,
approvals
),
false
);
}
/// @notice sends TOFT to a specific chain and performs a leverage down operation
/// @param amount the amount to use
/// @param leverageFor the receiver address
/// @param lzData LZ specific data
/// @param swapData ISwapper specific data
/// @param externalData external contracts used for the flow
function sendForLeverage(
uint256 amount,
address leverageFor,
IUSDOBase.ILeverageLZData calldata lzData,
IUSDOBase.ILeverageSwapData calldata swapData,
IUSDOBase.ILeverageExternalContractsData calldata externalData
) external payable {
_executeModule(
Module.Leverage,
abi.encodeWithSelector(
BaseTOFTLeverageModule.sendForLeverage.selector,
amount,
leverageFor,
lzData,
swapData,
externalData
),
false
);
}
// ************************* //
// *** PRIVATE FUNCTIONS *** //
//---internal-
function _wrap(
address _fromAddress,
address _toAddress,
uint256 _amount
) internal virtual {
if (_fromAddress != msg.sender) {
require(
allowance(_fromAddress, msg.sender) >= _amount,
"TOFT_allowed"
);
}
IERC20(erc20).safeTransferFrom(_fromAddress, address(this), _amount);
_mint(_toAddress, _amount);
}
function _wrapNative(address _toAddress) internal virtual {
require(msg.value > 0, "TOFT_0");
_mint(_toAddress, msg.value);
}
function _unwrap(address _toAddress, uint256 _amount) internal virtual {
_burn(msg.sender, _amount);
if (erc20 == address(0)) {
_safeTransferETH(_toAddress, _amount);
} else {
IERC20(erc20).safeTransfer(_toAddress, _amount);
}
}
//---private---
function _safeTransferETH(address to, uint256 amount) internal {
(bool sent, ) = to.call{value: amount}("");
require(sent, "TOFT_failed");
}
function _extractModule(Module _module) private view returns (address) {
address module;
if (_module == Module.Leverage) {
module = address(leverageModule);
} else if (_module == Module.Strategy) {
module = address(strategyModule);
} else if (_module == Module.Market) {
module = address(marketModule);
} else if (_module == Module.Options) {
module = address(optionsModule);
}
if (module == address(0)) {
revert("TOFT_module");
}
return module;
}
function _executeModule(
Module _module,
bytes memory _data,
bool _forwardRevert
) private returns (bool success, bytes memory returnData) {
success = true;
address module = _extractModule(_module);
(success, returnData) = module.delegatecall(_data);
if (!success && !_forwardRevert) {
revert(_getRevertMsg(returnData));
}
}
function _executeOnDestination(
Module _module,
bytes memory _data,
uint16 _srcChainId,
bytes memory _srcAddress,
uint64 _nonce,
bytes memory _payload
) private {
(bool success, bytes memory returnData) = _executeModule(
_module,
_data,
true
);
if (!success) {
_storeFailedMessage(
_srcChainId,
_srcAddress,
_nonce,
_payload,
returnData
);
}
}
//---LZ---
function _nonblockingLzReceive(
uint16 _srcChainId,
bytes memory _srcAddress,
uint64 _nonce,
bytes memory _payload
) internal virtual override {
uint256 packetType = _payload.toUint256(0);
if (packetType == PT_YB_SEND_STRAT) {
_executeOnDestination(
Module.Strategy,
abi.encodeWithSelector(
BaseTOFTStrategyModule.strategyDeposit.selector,
strategyModule,
_srcChainId,
_srcAddress,
_nonce,
_payload,
IERC20(address(this))
),
_srcChainId,
_srcAddress,
_nonce,
_payload
);
} else if (packetType == PT_YB_RETRIEVE_STRAT) {
_executeOnDestination(
Module.Strategy,
abi.encodeWithSelector(
BaseTOFTStrategyModule.strategyWithdraw.selector,
_srcChainId,
_payload
),
_srcChainId,
_srcAddress,
_nonce,
_payload
);
} else if (packetType == PT_LEVERAGE_MARKET_DOWN) {
_executeOnDestination(
Module.Leverage,
abi.encodeWithSelector(
BaseTOFTLeverageModule.leverageDown.selector,
leverageModule,
_srcChainId,
_srcAddress,
_nonce,
_payload
),
_srcChainId,
_srcAddress,
_nonce,
_payload
);
} else if (packetType == PT_YB_SEND_SGL_BORROW) {
_executeOnDestination(
Module.Market,
abi.encodeWithSelector(
BaseTOFTMarketModule.borrow.selector,
marketModule,
_srcChainId,
_srcAddress,
_nonce,
_payload
),
_srcChainId,
_srcAddress,
_nonce,
_payload
);
} else if (packetType == PT_MARKET_REMOVE_COLLATERAL) {
_executeOnDestination(
Module.Market,
abi.encodeWithSelector(
BaseTOFTMarketModule.remove.selector,
_payload
),
_srcChainId,
_srcAddress,
_nonce,
_payload
);
} else if (packetType == PT_MARKET_MULTIHOP_SELL) {
_executeOnDestination(
Module.Leverage,
abi.encodeWithSelector(
BaseTOFTLeverageModule.multiHop.selector,
_payload
),
_srcChainId,
_srcAddress,
_nonce,
_payload
);
} else if (packetType == PT_TAP_EXERCISE) {
_executeOnDestination(
Module.Options,
abi.encodeWithSelector(
BaseTOFTOptionsModule.exercise.selector,
_srcChainId,
_srcAddress,
_nonce,
_payload
),
_srcChainId,
_srcAddress,
_nonce,
_payload
);
} else if (packetType == PT_SEND_FROM) {
_executeOnDestination(
Module.Options,
abi.encodeWithSelector(
BaseTOFTOptionsModule.sendFromDestination.selector,
_payload
),
_srcChainId,
_srcAddress,
_nonce,
_payload
);
} else {
packetType = _payload.toUint8(0);
if (packetType == PT_SEND) {
_sendAck(_srcChainId, _srcAddress, _nonce, _payload);
} else if (packetType == PT_SEND_AND_CALL) {
_sendAndCallAck(_srcChainId, _srcAddress, _nonce, _payload);
} else {
revert("TOFT_packet");
}
}
}
}