forked from syncswap/core-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.sol
executable file
·1066 lines (845 loc) · 30.2 KB
/
router.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
//
// __| __|
// \__ \ | | \ _| \__ \ \ \ \ / _` | _ \
// ____/ \_, | _| _| \__| ____/ \_/\_/ \__,_| .__/
// ___/ _|
//
// SyncSwap is a decentralized exchange on Ethereum L2 Rollups.
//
// API Documentation
// https://syncswap.gitbook.io/api-documentation/
// SPDX-License-Identifier: AGPL-3.0-or-later
// File contracts/interfaces/token/IERC20Base.sol
pragma solidity >=0.5.0;
interface IERC20Base {
function totalSupply() external view returns (uint);
function balanceOf(address owner) external view returns (uint);
function allowance(address owner, address spender) external view returns (uint);
function approve(address spender, uint amount) external returns (bool);
function transfer(address to, uint amount) external returns (bool);
function transferFrom(address from, address to, uint amount) external returns (bool);
event Approval(address indexed owner, address indexed spender, uint amount);
event Transfer(address indexed from, address indexed to, uint amount);
}
// File contracts/interfaces/token/IERC20.sol
pragma solidity >=0.5.0;
interface IERC20 is IERC20Base {
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function decimals() external view returns (uint8);
}
// File contracts/interfaces/token/IERC20Permit.sol
pragma solidity >=0.5.0;
interface IERC20Permit is IERC20 {
function permit(address owner, address spender, uint value, uint deadline, uint8 v, bytes32 r, bytes32 s) external;
function nonces(address owner) external view returns (uint);
function DOMAIN_SEPARATOR() external view returns (bytes32);
}
// File contracts/interfaces/token/IERC20Permit2.sol
pragma solidity >=0.5.0;
interface IERC20Permit2 is IERC20Permit {
function permit2(address owner, address spender, uint amount, uint deadline, bytes calldata signature) external;
}
// File contracts/interfaces/token/IERC20PermitAllowed.sol
pragma solidity >=0.5.0;
/// @title Interface for permit
/// @notice Interface used by DAI/CHAI for permit
interface IERC20PermitAllowed {
/// @notice Approve the spender to spend some tokens via the holder signature
/// @dev This is the permit interface used by DAI and CHAI
/// @param holder The address of the token holder, the token owner
/// @param spender The address of the token spender
/// @param nonce The holder's nonce, increases at each call to permit
/// @param expiry The timestamp at which the permit is no longer valid
/// @param allowed Boolean that sets approval amount, true for type(uint256).max and false for 0
/// @param v Must produce valid secp256k1 signature from the holder along with `r` and `s`
/// @param r Must produce valid secp256k1 signature from the holder along with `v` and `s`
/// @param s Must produce valid secp256k1 signature from the holder along with `r` and `v`
function permit(
address holder,
address spender,
uint256 nonce,
uint256 expiry,
bool allowed,
uint8 v,
bytes32 r,
bytes32 s
) external;
}
// File contracts/abstract/SelfPermit.sol
pragma solidity >=0.8.0;
abstract contract SelfPermit {
function selfPermit(
address token,
uint value,
uint deadline,
uint8 v,
bytes32 r,
bytes32 s
) public payable {
IERC20Permit(token).permit(msg.sender, address(this), value, deadline, v, r, s);
}
function selfPermitIfNecessary(
address token,
uint value,
uint deadline,
uint8 v,
bytes32 r,
bytes32 s
) external payable {
if (IERC20(token).allowance(msg.sender, address(this)) < value) {
selfPermit(token, value, deadline, v, r, s);
}
}
function selfPermitAllowed(
address token,
uint256 nonce,
uint256 expiry,
uint8 v,
bytes32 r,
bytes32 s
) public payable {
IERC20PermitAllowed(token).permit(msg.sender, address(this), nonce, expiry, true, v, r, s);
}
function selfPermitAllowedIfNecessary(
address token,
uint256 nonce,
uint256 expiry,
uint8 v,
bytes32 r,
bytes32 s
) external payable {
if (IERC20(token).allowance(msg.sender, address(this)) < type(uint256).max) {
selfPermitAllowed(token, nonce, expiry, v, r, s);
}
}
function selfPermit2(
address token,
uint value,
uint deadline,
bytes calldata signature
) public payable {
IERC20Permit2(token).permit2(msg.sender, address(this), value, deadline, signature);
}
function selfPermit2IfNecessary(
address token,
uint value,
uint deadline,
bytes calldata signature
) external payable {
if (IERC20(token).allowance(msg.sender, address(this)) < value) {
selfPermit2(token, value, deadline, signature);
}
}
}
// File contracts/interfaces/pool/IPool.sol
pragma solidity >=0.5.0;
interface IPool {
struct TokenAmount {
address token;
uint amount;
}
/// @dev Returns the address of pool master.
function master() external view returns (address);
/// @dev Returns the vault.
function vault() external view returns (address);
/// @dev Returns the pool type.
function poolType() external view returns (uint16);
/// @dev Returns the assets of the pool.
function getAssets() external view returns (address[] memory assets);
/// @dev Returns the swap fee of the pool.
function getSwapFee(address sender, address tokenIn, address tokenOut, bytes calldata data) external view returns (uint24 swapFee);
/// @dev Returns the protocol fee of the pool.
function getProtocolFee() external view returns (uint24 protocolFee);
/// @dev Mints liquidity.
function mint(
bytes calldata data,
address sender,
address callback,
bytes calldata callbackData
) external returns (uint liquidity);
/// @dev Burns liquidity.
function burn(
bytes calldata data,
address sender,
address callback,
bytes calldata callbackData
) external returns (TokenAmount[] memory tokenAmounts);
/// @dev Burns liquidity with single output token.
function burnSingle(
bytes calldata data,
address sender,
address callback,
bytes calldata callbackData
) external returns (TokenAmount memory tokenAmount);
/// @dev Swaps between tokens.
function swap(
bytes calldata data,
address sender,
address callback,
bytes calldata callbackData
) external returns (TokenAmount memory tokenAmount);
}
// File contracts/interfaces/pool/IBasePool.sol
pragma solidity >=0.5.0;
interface IBasePool is IPool, IERC20Permit2 {
function token0() external view returns (address);
function token1() external view returns (address);
function reserve0() external view returns (uint);
function reserve1() external view returns (uint);
function invariantLast() external view returns (uint);
function getReserves() external view returns (uint, uint);
function getAmountOut(address tokenIn, uint amountIn, address sender) external view returns (uint amountOut);
function getAmountIn(address tokenOut, uint amountOut, address sender) external view returns (uint amountIn);
event Mint(
address indexed sender,
uint amount0,
uint amount1,
uint liquidity,
address indexed to
);
event Burn(
address indexed sender,
uint amount0,
uint amount1,
uint liquidity,
address indexed to
);
event Swap(
address indexed sender,
uint amount0In,
uint amount1In,
uint amount0Out,
uint amount1Out,
address indexed to
);
event Sync(
uint reserve0,
uint reserve1
);
}
// File contracts/interfaces/vault/IERC3156FlashBorrower.sol
pragma solidity >=0.5.0;
interface IERC3156FlashBorrower {
/**
* @dev Receive a flash loan.
* @param initiator The initiator of the loan.
* @param token The loan currency.
* @param amount The amount of tokens lent.
* @param fee The additional amount of tokens to repay.
* @param data Arbitrary data structure, intended to contain user-defined parameters.
* @return The keccak256 hash of "ERC3156FlashBorrower.onFlashLoan"
*/
function onFlashLoan(
address initiator,
address token,
uint256 amount,
uint256 fee,
bytes calldata data
) external returns (bytes32);
}
// File contracts/interfaces/vault/IERC3156FlashLender.sol
pragma solidity >=0.5.0;
interface IERC3156FlashLender {
/**
* @dev The amount of currency available to be lent.
* @param token The loan currency.
* @return The amount of `token` that can be borrowed.
*/
function maxFlashLoan(
address token
) external view returns (uint256);
/**
* @dev The fee to be charged for a given loan.
* @param token The loan currency.
* @param amount The amount of tokens lent.
* @return The amount of `token` to be charged for the loan, on top of the returned principal.
*/
function flashFee(
address token,
uint256 amount
) external view returns (uint256);
/**
* @dev Initiate a flash loan.
* @param receiver The receiver of the tokens in the loan, and the receiver of the callback.
* @param token The loan currency.
* @param amount The amount of tokens lent.
* @param data Arbitrary data structure, intended to contain user-defined parameters.
*/
function flashLoan(
IERC3156FlashBorrower receiver,
address token,
uint256 amount,
bytes calldata data
) external returns (bool);
}
// File contracts/interfaces/vault/IFlashLoanRecipient.sol
pragma solidity >=0.7.0 <0.9.0;
// Inspired by Aave Protocol's IFlashLoanReceiver.
interface IFlashLoanRecipient {
/**
* @dev When `flashLoan` is called on the Vault, it invokes the `receiveFlashLoan` hook on the recipient.
*
* At the time of the call, the Vault will have transferred `amounts` for `tokens` to the recipient. Before this
* call returns, the recipient must have transferred `amounts` plus `feeAmounts` for each token back to the
* Vault, or else the entire flash loan will revert.
*
* `userData` is the same value passed in the `IVault.flashLoan` call.
*/
function receiveFlashLoan(
address[] memory tokens,
uint[] memory amounts,
uint[] memory feeAmounts,
bytes memory userData
) external;
}
// File contracts/interfaces/vault/IFlashLoan.sol
pragma solidity >=0.5.0;
interface IFlashLoan is IERC3156FlashLender {
function flashLoanFeePercentage() external view returns (uint);
/**
* @dev Performs a 'flash loan', sending tokens to `recipient`, executing the `receiveFlashLoan` hook on it,
* and then reverting unless the tokens plus a proportional protocol fee have been returned.
*
* The `tokens` and `amounts` arrays must have the same length, and each entry in these indicates the loan amount
* for each token contract. `tokens` must be sorted in ascending order.
*
* The 'userData' field is ignored by the Vault, and forwarded as-is to `recipient` as part of the
* `receiveFlashLoan` call.
*
* Emits `FlashLoan` events.
*/
function flashLoanMultiple(
IFlashLoanRecipient recipient,
address[] memory tokens,
uint[] memory amounts,
bytes memory userData
) external;
/**
* @dev Emitted for each individual flash loan performed by `flashLoan`.
*/
event FlashLoan(address indexed recipient, address indexed token, uint amount, uint feeAmount);
}
// File contracts/interfaces/vault/IVault.sol
pragma solidity >=0.5.0;
interface IVault is IFlashLoan {
function wETH() external view returns (address);
function reserves(address token) external view returns (uint reserve);
function balanceOf(address token, address owner) external view returns (uint balance);
function deposit(address token, address to) external payable returns (uint amount);
function depositETH(address to) external payable returns (uint amount);
function transferAndDeposit(address token, address to, uint amount) external payable returns (uint);
function transfer(address token, address to, uint amount) external;
function withdraw(address token, address to, uint amount) external;
function withdrawAlternative(address token, address to, uint amount, uint8 mode) external;
function withdrawETH(address to, uint amount) external;
}
// File contracts/abstract/Multicall.sol
pragma solidity >=0.8.0;
/// @notice Helper utility that enables calling multiple local methods in a single call.
/// @author Modified from Uniswap (https://github.com/Uniswap/v3-periphery/blob/main/contracts/base/Multicall.sol)
/// License-Identifier: GPL-2.0-or-later
abstract contract Multicall {
function multicall(bytes[] calldata data) public payable returns (bytes[] memory results) {
results = new bytes[](data.length);
for (uint i; i < data.length;) {
(bool success, bytes memory result) = address(this).delegatecall(data[i]);
if (!success) {
// Next 5 lines from https://ethereum.stackexchange.com/a/83577
if (result.length < 68) revert();
assembly {
result := add(result, 0x04)
}
revert(abi.decode(result, (string)));
}
results[i] = result;
// cannot realistically overflow on human timescales
unchecked {
++i;
}
}
}
}
// File contracts/interfaces/factory/IPoolFactory.sol
pragma solidity >=0.5.0;
interface IPoolFactory {
function master() external view returns (address);
function getDeployData() external view returns (bytes memory);
function createPool(bytes calldata data) external returns (address pool);
}
// File contracts/interfaces/IRouter.sol
pragma solidity >=0.5.0;
interface IRouter {
struct SwapStep {
address pool;
bytes data;
address callback;
bytes callbackData;
}
struct SwapPath {
SwapStep[] steps;
address tokenIn;
uint amountIn;
}
struct SplitPermitParams {
address token;
uint approveAmount;
uint deadline;
uint8 v;
bytes32 r;
bytes32 s;
}
struct ArrayPermitParams {
uint approveAmount;
uint deadline;
bytes signature;
}
}
// File contracts/interfaces/IStakingPool.sol
pragma solidity >=0.5.0;
interface IStakingPool {
function stake(uint amount, address onBehalf) external;
}
// File contracts/interfaces/IWETH.sol
pragma solidity >=0.5.0;
interface IWETH {
function deposit() external payable;
function transfer(address to, uint value) external returns (bool);
function transferFrom(address from, address to, uint value) external returns (bool);
function withdraw(uint) external;
}
// File contracts/libraries/TransferHelper.sol
pragma solidity ^0.8.0;
/// @dev The ETH transfer has failed.
error ETHTransferFailed();
/// @dev The ERC20 `transferFrom` has failed.
error TransferFromFailed();
/// @dev The ERC20 `transfer` has failed.
error TransferFailed();
/// @dev The ERC20 `approve` has failed.
error ApproveFailed();
/// @dev Helper methods for interacting with ERC20 tokens and sending ETH that do not consistently return true / false.
library TransferHelper {
function safeApprove(
address token,
address to,
uint value
) internal {
// bytes4(keccak256(bytes("approve(address,uint256)")));
// solhint-disable-next-line avoid-low-level-calls
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x095ea7b3, to, value));
if (!success || (data.length != 0 && !abi.decode(data, (bool)))) {
revert ApproveFailed();
}
}
function safeTransfer(
address token,
address to,
uint value
) internal {
// bytes4(keccak256(bytes("transfer(address,uint256)")));
// solhint-disable-next-line avoid-low-level-calls
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0xa9059cbb, to, value));
if (!success || (data.length != 0 && !abi.decode(data, (bool)))) {
revert TransferFailed();
}
}
function safeTransferFrom(
address token,
address from,
address to,
uint value
) internal {
// bytes4(keccak256(bytes("transferFrom(address,address,uint256)")));
// solhint-disable-next-line avoid-low-level-calls
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x23b872dd, from, to, value));
if (!success || (data.length != 0 && !abi.decode(data, (bool)))) {
revert TransferFromFailed();
}
}
function safeTransferETH(address to, uint value) internal {
// solhint-disable-next-line avoid-low-level-calls
(bool success, ) = to.call{value: value}("");
if (!success) {
revert ETHTransferFailed();
}
}
}
// File contracts/SyncSwapRouter.sol
pragma solidity ^0.8.0;
error NotEnoughLiquidityMinted();
error TooLittleReceived();
error Expired();
/// @notice The router is a universal interface for users to access
/// functions across different protocol parts in one place.
///
/// It handles the allowances and transfers of tokens, and
/// allows chained swaps/operations across multiple pools, with
/// additional features like slippage protection and permit support.
///
contract SyncSwapRouter is IRouter, SelfPermit, Multicall {
struct TokenInput {
address token;
uint amount;
}
address public immutable vault;
address public immutable wETH;
address private constant NATIVE_ETH = address(0);
mapping(address => mapping(address => bool)) public isPoolEntered;
mapping(address => address[]) public enteredPools;
modifier ensure(uint deadline) {
// solhint-disable-next-line not-rely-on-time
if (block.timestamp > deadline) {
revert Expired();
}
_;
}
constructor(address _vault, address _wETH) {
vault = _vault;
wETH = _wETH;
}
function enteredPoolsLength(address account) external view returns (uint) {
return enteredPools[account].length;
}
// Add Liquidity
function _transferFromSender(address token, address to, uint amount) private {
if (token == NATIVE_ETH) {
// Deposit ETH to the vault.
IVault(vault).deposit{value: amount}(token, to);
} else {
// Transfer tokens to the vault.
TransferHelper.safeTransferFrom(token, msg.sender, vault, amount);
// Notify the vault to deposit.
IVault(vault).deposit(token, to);
}
}
function _transferAndAddLiquidity(
address pool,
TokenInput[] calldata inputs,
bytes calldata data,
uint minLiquidity,
address callback,
bytes calldata callbackData
) private returns (uint liquidity) {
// Send all input tokens to the pool.
uint n = inputs.length;
TokenInput memory input;
for (uint i; i < n; ) {
input = inputs[i];
_transferFromSender(input.token, pool, input.amount);
unchecked {
++i;
}
}
liquidity = IPool(pool).mint(data, msg.sender, callback, callbackData);
if (liquidity < minLiquidity) {
revert NotEnoughLiquidityMinted();
}
}
function _markPoolEntered(address pool) private {
if (!isPoolEntered[pool][msg.sender]) {
isPoolEntered[pool][msg.sender] = true;
enteredPools[msg.sender].push(pool);
}
}
function addLiquidity(
address pool,
TokenInput[] calldata inputs,
bytes calldata data,
uint minLiquidity,
address callback,
bytes calldata callbackData
) external payable returns (uint liquidity) {
liquidity = _transferAndAddLiquidity(
pool,
inputs,
data,
minLiquidity,
callback,
callbackData
);
}
function addLiquidity2(
address pool,
TokenInput[] calldata inputs,
bytes calldata data,
uint minLiquidity,
address callback,
bytes calldata callbackData
) external payable returns (uint liquidity) {
liquidity = _transferAndAddLiquidity(
pool,
inputs,
data,
minLiquidity,
callback,
callbackData
);
_markPoolEntered(pool);
}
function addLiquidityWithPermit(
address pool,
TokenInput[] calldata inputs,
bytes calldata data,
uint minLiquidity,
address callback,
bytes calldata callbackData,
SplitPermitParams[] memory permits
) public payable returns (uint liquidity) {
// Approve all tokens via permit.
uint n = permits.length;
SplitPermitParams memory params;
for (uint i; i < n; ) {
params = permits[i];
IERC20Permit(params.token).permit(
msg.sender,
address(this),
params.approveAmount,
params.deadline,
params.v,
params.r,
params.s
);
unchecked {
++i;
}
}
liquidity = _transferAndAddLiquidity(
pool,
inputs,
data,
minLiquidity,
callback,
callbackData
);
}
function addLiquidityWithPermit2(
address pool,
TokenInput[] calldata inputs,
bytes calldata data,
uint minLiquidity,
address callback,
bytes calldata callbackData,
SplitPermitParams[] memory permits
) public payable returns (uint liquidity) {
liquidity = addLiquidityWithPermit(
pool,
inputs,
data,
minLiquidity,
callback,
callbackData,
permits
);
_markPoolEntered(pool);
}
// Burn Liquidity
function _transferAndBurnLiquidity(
address pool,
uint liquidity,
bytes memory data,
uint[] memory minAmounts,
address callback,
bytes calldata callbackData
) private returns (IPool.TokenAmount[] memory amounts) {
IBasePool(pool).transferFrom(msg.sender, pool, liquidity);
amounts = IPool(pool).burn(data, msg.sender, callback, callbackData);
uint n = amounts.length;
for (uint i; i < n; ) {
if (amounts[i].amount < minAmounts[i]) {
revert TooLittleReceived();
}
unchecked {
++i;
}
}
}
function burnLiquidity(
address pool,
uint liquidity,
bytes calldata data,
uint[] calldata minAmounts,
address callback,
bytes calldata callbackData
) external returns (IPool.TokenAmount[] memory amounts) {
amounts = _transferAndBurnLiquidity(
pool,
liquidity,
data,
minAmounts,
callback,
callbackData
);
}
function burnLiquidityWithPermit(
address pool,
uint liquidity,
bytes calldata data,
uint[] calldata minAmounts,
address callback,
bytes calldata callbackData,
ArrayPermitParams memory permit
) external returns (IPool.TokenAmount[] memory amounts) {
// Approve liquidity via permit.
IBasePool(pool).permit2(
msg.sender,
address(this),
permit.approveAmount,
permit.deadline,
permit.signature
);
amounts = _transferAndBurnLiquidity(
pool,
liquidity,
data,
minAmounts,
callback,
callbackData
);
}
// Burn Liquidity Single
function _transferAndBurnLiquiditySingle(
address pool,
uint liquidity,
bytes memory data,
uint minAmount,
address callback,
bytes memory callbackData
) private returns (IPool.TokenAmount memory amountOut) {
IBasePool(pool).transferFrom(msg.sender, pool, liquidity);
amountOut = IPool(pool).burnSingle(data, msg.sender, callback, callbackData);
if (amountOut.amount < minAmount) {
revert TooLittleReceived();
}
}
function burnLiquiditySingle(
address pool,
uint liquidity,
bytes memory data,
uint minAmount,
address callback,
bytes memory callbackData
) external returns (IPool.TokenAmount memory amountOut) {
amountOut = _transferAndBurnLiquiditySingle(
pool,
liquidity,
data,
minAmount,
callback,
callbackData
);
}
function burnLiquiditySingleWithPermit(
address pool,
uint liquidity,
bytes memory data,
uint minAmount,
address callback,
bytes memory callbackData,
ArrayPermitParams calldata permit
) external returns (IPool.TokenAmount memory amountOut) {
// Approve liquidity via permit.
IBasePool(pool).permit2(
msg.sender,
address(this),
permit.approveAmount,
permit.deadline,
permit.signature
);
amountOut = _transferAndBurnLiquiditySingle(
pool,
liquidity,
data,
minAmount,
callback,
callbackData
);
}
// Swap
function _swap(
SwapPath[] memory paths,
uint amountOutMin
) private returns (IPool.TokenAmount memory amountOut) {
uint pathsLength = paths.length;
SwapPath memory path;
SwapStep memory step;
IPool.TokenAmount memory tokenAmount;
uint stepsLength;
uint j;
for (uint i; i < pathsLength; ) {
path = paths[i];
// Prefund the first step.
step = path.steps[0];
_transferFromSender(path.tokenIn, step.pool, path.amountIn);
// Cache steps length.
stepsLength = path.steps.length;
for (j = 0; j < stepsLength; ) {
if (j == stepsLength - 1) {
// Accumulate output amount at the last step.
tokenAmount = IBasePool(step.pool).swap(
step.data, msg.sender, step.callback, step.callbackData
);
amountOut.token = tokenAmount.token;
amountOut.amount += tokenAmount.amount;
break;
} else {
// Swap and send tokens to the next step.
IBasePool(step.pool).swap(step.data, msg.sender, step.callback, step.callbackData);
// Cache the next step.
step = path.steps[j + 1];