forked from andrecronje/anyswap-v1-core
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathAnyswapV6Router.sol
566 lines (488 loc) · 24.7 KB
/
AnyswapV6Router.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
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.2;
interface ISushiswapV2Pair {
function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external;
}
// a library for performing overflow-safe math, courtesy of DappHub (https://github.com/dapphub/ds-math)
library SafeMathSushiswap {
function add(uint x, uint y) internal pure returns (uint z) {
unchecked {
require((z = x + y) >= x, 'ds-math-add-overflow');
}
}
function sub(uint x, uint y) internal pure returns (uint z) {
unchecked {
require((z = x - y) <= x, 'ds-math-sub-underflow');
}
}
function mul(uint x, uint y) internal pure returns (uint z) {
unchecked {
require(y == 0 || (z = x * y) / y == x, 'ds-math-mul-overflow');
}
}
}
library SushiswapV2Library {
using SafeMathSushiswap for uint;
// returns sorted token addresses, used to handle return values from pairs sorted in this order
function sortTokens(address tokenA, address tokenB) internal pure returns (address token0, address token1) {
require(tokenA != tokenB, 'SushiswapV2Library: IDENTICAL_ADDRESSES');
(token0, token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
require(token0 != address(0), 'SushiswapV2Library: ZERO_ADDRESS');
}
// calculates the CREATE2 address for a pair without making any external calls
function pairFor(address factory, address tokenA, address tokenB) internal pure returns (address pair) {
(address token0, address token1) = sortTokens(tokenA, tokenB);
pair = address(uint160(uint256(keccak256(abi.encodePacked(
hex'ff',
factory,
keccak256(abi.encodePacked(token0, token1)),
hex'e18a34eb0e04b04f7a0ac29a6e80748dca96319b42c54d679cb821dca90c6303' // init code hash
)))));
}
// fetches and sorts the reserves for a pair
function getReserves(address factory, address tokenA, address tokenB) internal view returns (uint reserveA, uint reserveB) {
(address token0,) = sortTokens(tokenA, tokenB);
(uint reserve0, uint reserve1,) = ISushiswapV2Pair(pairFor(factory, tokenA, tokenB)).getReserves();
(reserveA, reserveB) = tokenA == token0 ? (reserve0, reserve1) : (reserve1, reserve0);
}
// given some amount of an asset and pair reserves, returns an equivalent amount of the other asset
function quote(uint amountA, uint reserveA, uint reserveB) internal pure returns (uint amountB) {
require(amountA > 0, 'SushiswapV2Library: INSUFFICIENT_AMOUNT');
require(reserveA > 0 && reserveB > 0, 'SushiswapV2Library: INSUFFICIENT_LIQUIDITY');
amountB = amountA.mul(reserveB) / reserveA;
}
// given an input amount of an asset and pair reserves, returns the maximum output amount of the other asset
function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) internal pure returns (uint amountOut) {
require(amountIn > 0, 'SushiswapV2Library: INSUFFICIENT_INPUT_AMOUNT');
require(reserveIn > 0 && reserveOut > 0, 'SushiswapV2Library: INSUFFICIENT_LIQUIDITY');
uint amountInWithFee = amountIn.mul(997);
uint numerator = amountInWithFee.mul(reserveOut);
uint denominator = reserveIn.mul(1000).add(amountInWithFee);
amountOut = numerator / denominator;
}
// given an output amount of an asset and pair reserves, returns a required input amount of the other asset
function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) internal pure returns (uint amountIn) {
require(amountOut > 0, 'SushiswapV2Library: INSUFFICIENT_OUTPUT_AMOUNT');
require(reserveIn > 0 && reserveOut > 0, 'SushiswapV2Library: INSUFFICIENT_LIQUIDITY');
uint numerator = reserveIn.mul(amountOut).mul(1000);
uint denominator = reserveOut.sub(amountOut).mul(997);
amountIn = (numerator / denominator).add(1);
}
// performs chained getAmountOut calculations on any number of pairs
function getAmountsOut(address factory, uint amountIn, address[] memory path) internal view returns (uint[] memory amounts) {
require(path.length >= 2, 'SushiswapV2Library: INVALID_PATH');
amounts = new uint[](path.length);
amounts[0] = amountIn;
for (uint i; i < path.length - 1; i++) {
(uint reserveIn, uint reserveOut) = getReserves(factory, path[i], path[i + 1]);
amounts[i + 1] = getAmountOut(amounts[i], reserveIn, reserveOut);
}
}
// performs chained getAmountIn calculations on any number of pairs
function getAmountsIn(address factory, uint amountOut, address[] memory path) internal view returns (uint[] memory amounts) {
require(path.length >= 2, 'SushiswapV2Library: INVALID_PATH');
amounts = new uint[](path.length);
amounts[amounts.length - 1] = amountOut;
for (uint i = path.length - 1; i > 0; i--) {
(uint reserveIn, uint reserveOut) = getReserves(factory, path[i - 1], path[i]);
amounts[i - 1] = getAmountIn(amounts[i], reserveIn, reserveOut);
}
}
}
// helper methods for interacting with ERC20 tokens and sending NATIVE that do not consistently return true/false
library TransferHelper {
function safeTransferNative(address to, uint value) internal {
(bool success,) = to.call{value:value}(new bytes(0));
require(success, 'TransferHelper: NATIVE_TRANSFER_FAILED');
}
}
interface IwNATIVE {
function deposit() external payable;
function transfer(address to, uint value) external returns (bool);
function withdraw(uint) external;
}
interface AnyswapV1ERC20 {
function mint(address to, uint256 amount) external returns (bool);
function burn(address from, uint256 amount) external returns (bool);
function setMinter(address _auth) external;
function applyMinter() external;
function revokeMinter(address _auth) external;
function changeVault(address newVault) external returns (bool);
function depositVault(uint amount, address to) external returns (uint);
function withdrawVault(address from, uint amount, address to) external returns (uint);
function underlying() external view returns (address);
function deposit(uint amount, address to) external returns (uint);
function withdraw(uint amount, address to) external returns (uint);
}
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
library Address {
function isContract(address account) internal view returns (bool) {
bytes32 codehash;
bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;
// solhint-disable-next-line no-inline-assembly
assembly { codehash := extcodehash(account) }
return (codehash != 0x0 && codehash != accountHash);
}
}
library SafeERC20 {
using Address for address;
function safeTransfer(IERC20 token, address to, uint value) internal {
callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
function safeTransferFrom(IERC20 token, address from, address to, uint value) internal {
callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
function safeApprove(IERC20 token, address spender, uint value) internal {
require((value == 0) || (token.allowance(address(this), spender) == 0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
function callOptionalReturn(IERC20 token, bytes memory data) private {
require(address(token).isContract(), "SafeERC20: call to non-contract");
// solhint-disable-next-line avoid-low-level-calls
(bool success, bytes memory returndata) = address(token).call(data);
require(success, "SafeERC20: low-level call failed");
if (returndata.length > 0) { // Return data is optional
// solhint-disable-next-line max-line-length
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
}
}
contract AnyswapV6Router {
using SafeERC20 for IERC20;
using SafeMathSushiswap for uint;
address public immutable factory;
address public immutable wNATIVE;
// delay for timelock functions
uint public constant DELAY = 2 days;
bool public enableSwapTrade;
modifier swapTradeEnabled() {
require(enableSwapTrade, 'AnyswapV6Router: SwapTrade disabled');
_;
}
modifier ensure(uint deadline) {
require(deadline >= block.timestamp, 'AnyswapV6Router: EXPIRED');
_;
}
constructor(address _factory, address _wNATIVE, address _mpc) {
_newMPC = _mpc;
_newMPCEffectiveTime = block.timestamp;
factory = _factory;
wNATIVE = _wNATIVE;
}
receive() external payable {
assert(msg.sender == wNATIVE); // only accept Native via fallback from the wNative contract
}
address private _oldMPC;
address private _newMPC;
uint256 private _newMPCEffectiveTime;
event LogChangeMPC(address indexed oldMPC, address indexed newMPC, uint indexed effectiveTime, uint chainID);
event LogAnySwapIn(bytes32 indexed txhash, address indexed token, address indexed to, uint amount, uint fromChainID, uint toChainID);
event LogAnySwapOut(address indexed token, address indexed from, address indexed to, uint amount, uint fromChainID, uint toChainID);
event LogAnySwapOut(address indexed token, address indexed from, string to, uint amount, uint fromChainID, uint toChainID);
event LogAnySwapTradeTokensForTokens(address[] path, address indexed from, address indexed to, uint amountIn, uint amountOutMin, uint fromChainID, uint toChainID);
event LogAnySwapTradeTokensForNative(address[] path, address indexed from, address indexed to, uint amountIn, uint amountOutMin, uint fromChainID, uint toChainID);
modifier onlyMPC() {
require(msg.sender == mpc(), "AnyswapV6Router: FORBIDDEN");
_;
}
function mpc() public view returns (address) {
if (block.timestamp >= _newMPCEffectiveTime) {
return _newMPC;
}
return _oldMPC;
}
function cID() public view returns (uint) {
return block.chainid;
}
function setEnableSwapTrade(bool enable) external onlyMPC {
enableSwapTrade = enable;
}
function changeMPC(address newMPC) external onlyMPC returns (bool) {
require(newMPC != address(0), "AnyswapV6Router: address(0)");
_oldMPC = mpc();
_newMPC = newMPC;
_newMPCEffectiveTime = block.timestamp + DELAY;
emit LogChangeMPC(_oldMPC, _newMPC, _newMPCEffectiveTime, cID());
return true;
}
function changeVault(address token, address newVault) external onlyMPC returns (bool) {
return AnyswapV1ERC20(token).changeVault(newVault);
}
function setMinter(address token, address _auth) external onlyMPC {
return AnyswapV1ERC20(token).setMinter(_auth);
}
function applyMinter(address token) external onlyMPC {
return AnyswapV1ERC20(token).applyMinter();
}
function revokeMinter(address token, address _auth) external onlyMPC {
return AnyswapV1ERC20(token).revokeMinter(_auth);
}
function _anySwapOut(address from, address token, address to, uint amount, uint toChainID) internal {
AnyswapV1ERC20(token).burn(from, amount);
emit LogAnySwapOut(token, from, to, amount, cID(), toChainID);
}
// Swaps `amount` `token` from this chain to `toChainID` chain with recipient `to`
function anySwapOut(address token, address to, uint amount, uint toChainID) external {
_anySwapOut(msg.sender, token, to, amount, toChainID);
}
function _anySwapOutUnderlying(address token, uint256 amount) internal returns (uint256) {
address _underlying = AnyswapV1ERC20(token).underlying();
require(_underlying != address(0), "AnyswapV6Router: no underlying");
uint256 old_balance = IERC20(_underlying).balanceOf(token);
IERC20(_underlying).safeTransferFrom(msg.sender, token, amount);
uint256 new_balance = IERC20(_underlying).balanceOf(token);
require(
new_balance >= old_balance && new_balance <= old_balance + amount
);
return new_balance - old_balance;
}
// Swaps `amount` `token` from this chain to `toChainID` chain with recipient `to` by minting with `underlying`
function anySwapOutUnderlying(address token, address to, uint amount, uint toChainID) external {
uint256 recvAmount = _anySwapOutUnderlying(token, amount);
emit LogAnySwapOut(token, msg.sender, to, recvAmount, cID(), toChainID);
}
function _anySwapOutNative(address token) internal returns (uint256) {
require(wNATIVE != address(0), "AnyswapV6Router: zero wNATIVE");
require(
AnyswapV1ERC20(token).underlying() == wNATIVE,
"AnyswapV6Router: underlying is not wNATIVE"
);
uint256 old_balance = IERC20(wNATIVE).balanceOf(token);
IwNATIVE(wNATIVE).deposit{value: msg.value}();
IERC20(wNATIVE).safeTransfer(token, msg.value);
uint256 new_balance = IERC20(wNATIVE).balanceOf(token);
require(
new_balance >= old_balance && new_balance <= old_balance + msg.value
);
return new_balance - old_balance;
}
function anySwapOutNative(address token, address to, uint toChainID) external payable {
uint256 recvAmount = _anySwapOutNative(token);
emit LogAnySwapOut(token, msg.sender, to, recvAmount, cID(), toChainID);
}
function anySwapOut(address[] calldata tokens, address[] calldata to, uint[] calldata amounts, uint[] calldata toChainIDs) external {
for (uint i = 0; i < tokens.length; i++) {
_anySwapOut(msg.sender, tokens[i], to[i], amounts[i], toChainIDs[i]);
}
}
function anySwapOut(address token, string memory to, uint amount, uint toChainID) external {
AnyswapV1ERC20(token).burn(msg.sender, amount);
emit LogAnySwapOut(token, msg.sender, to, amount, cID(), toChainID);
}
function anySwapOutUnderlying(address token, string memory to, uint amount, uint toChainID) external {
uint256 recvAmount = _anySwapOutUnderlying(token, amount);
emit LogAnySwapOut(token, msg.sender, to, recvAmount, cID(), toChainID);
}
function anySwapOutNative(address token, string memory to, uint toChainID) external payable {
uint256 recvAmount = _anySwapOutNative(token);
emit LogAnySwapOut(token, msg.sender, to, recvAmount, cID(), toChainID);
}
// swaps `amount` `token` in `fromChainID` to `to` on this chainID
function _anySwapIn(bytes32 txs, address token, address to, uint amount, uint fromChainID) internal {
AnyswapV1ERC20(token).mint(to, amount);
emit LogAnySwapIn(txs, token, to, amount, fromChainID, cID());
}
// swaps `amount` `token` in `fromChainID` to `to` on this chainID
// triggered by `anySwapOut`
function anySwapIn(bytes32 txs, address token, address to, uint amount, uint fromChainID) external onlyMPC {
_anySwapIn(txs, token, to, amount, fromChainID);
}
// swaps `amount` `token` in `fromChainID` to `to` on this chainID with `to` receiving `underlying`
function anySwapInUnderlying(bytes32 txs, address token, address to, uint amount, uint fromChainID) external onlyMPC {
_anySwapIn(txs, token, to, amount, fromChainID);
AnyswapV1ERC20(token).withdrawVault(to, amount, to);
}
// swaps `amount` `token` in `fromChainID` to `to` on this chainID with `to` receiving `underlying` if possible
function anySwapInAuto(bytes32 txs, address token, address to, uint amount, uint fromChainID) external onlyMPC {
_anySwapIn(txs, token, to, amount, fromChainID);
AnyswapV1ERC20 _anyToken = AnyswapV1ERC20(token);
address _underlying = _anyToken.underlying();
if (_underlying != address(0) && IERC20(_underlying).balanceOf(token) >= amount) {
if (_underlying == wNATIVE) {
_anyToken.withdrawVault(to, amount, address(this));
IwNATIVE(wNATIVE).withdraw(amount);
TransferHelper.safeTransferNative(to, amount);
} else {
_anyToken.withdrawVault(to, amount, to);
}
}
}
function depositNative(address token, address to) external payable returns (uint) {
require(wNATIVE != address(0), "AnyswapV6Router: zero wNATIVE");
require(AnyswapV1ERC20(token).underlying() == wNATIVE, "AnyswapV6Router: underlying is not wNATIVE");
IwNATIVE(wNATIVE).deposit{value: msg.value}();
assert(IwNATIVE(wNATIVE).transfer(token, msg.value));
AnyswapV1ERC20(token).depositVault(msg.value, to);
return msg.value;
}
function withdrawNative(address token, uint amount, address to) external returns (uint) {
require(wNATIVE != address(0), "AnyswapV6Router: zero wNATIVE");
require(AnyswapV1ERC20(token).underlying() == wNATIVE, "AnyswapV6Router: underlying is not wNATIVE");
uint256 old_balance = IERC20(wNATIVE).balanceOf(address(this));
AnyswapV1ERC20(token).withdrawVault(msg.sender, amount, address(this));
uint256 new_balance = IERC20(wNATIVE).balanceOf(address(this));
assert(new_balance == old_balance + amount);
IwNATIVE(wNATIVE).withdraw(amount);
TransferHelper.safeTransferNative(to, amount);
return amount;
}
// extracts mpc fee from bridge fees
function anySwapFeeTo(address token, uint amount) external onlyMPC {
address _mpc = mpc();
AnyswapV1ERC20(token).mint(_mpc, amount);
AnyswapV1ERC20(token).withdrawVault(_mpc, amount, _mpc);
}
function anySwapIn(bytes32[] calldata txs, address[] calldata tokens, address[] calldata to, uint256[] calldata amounts, uint[] calldata fromChainIDs) external onlyMPC {
for (uint i = 0; i < tokens.length; i++) {
_anySwapIn(txs[i], tokens[i], to[i], amounts[i], fromChainIDs[i]);
}
}
// **** SWAP ****
// requires the initial amount to have already been sent to the first pair
function _swap(uint[] memory amounts, address[] memory path, address _to) internal virtual {
for (uint i; i < path.length - 1; i++) {
(address input, address output) = (path[i], path[i + 1]);
(address token0,) = SushiswapV2Library.sortTokens(input, output);
uint amountOut = amounts[i + 1];
(uint amount0Out, uint amount1Out) = input == token0 ? (uint(0), amountOut) : (amountOut, uint(0));
address to = i < path.length - 2 ? SushiswapV2Library.pairFor(factory, output, path[i + 2]) : _to;
ISushiswapV2Pair(SushiswapV2Library.pairFor(factory, input, output)).swap(
amount0Out, amount1Out, to, new bytes(0)
);
}
}
// sets up a cross-chain trade from this chain to `toChainID` for `path` trades to `to`
function anySwapOutExactTokensForTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline,
uint toChainID
) external virtual swapTradeEnabled ensure(deadline) {
AnyswapV1ERC20(path[0]).burn(msg.sender, amountIn);
emit LogAnySwapTradeTokensForTokens(path, msg.sender, to, amountIn, amountOutMin, cID(), toChainID);
}
// sets up a cross-chain trade from this chain to `toChainID` for `path` trades to `to`
function anySwapOutExactTokensForTokensUnderlying(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline,
uint toChainID
) external virtual swapTradeEnabled ensure(deadline) {
IERC20(AnyswapV1ERC20(path[0]).underlying()).safeTransferFrom(msg.sender, path[0], amountIn);
emit LogAnySwapTradeTokensForTokens(path, msg.sender, to, amountIn, amountOutMin, cID(), toChainID);
}
// Swaps `amounts[path.length-1]` `path[path.length-1]` to `to` on this chain
// Triggered by `anySwapOutExactTokensForTokens`
function anySwapInExactTokensForTokens(
bytes32 txs,
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline,
uint fromChainID
) external onlyMPC virtual swapTradeEnabled ensure(deadline) returns (uint[] memory amounts) {
amounts = SushiswapV2Library.getAmountsOut(factory, amountIn, path);
require(amounts[amounts.length - 1] >= amountOutMin, 'SushiswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT');
_anySwapIn(txs, path[0], SushiswapV2Library.pairFor(factory, path[0], path[1]), amounts[0], fromChainID);
_swap(amounts, path, to);
}
// sets up a cross-chain trade from this chain to `toChainID` for `path` trades to `to`
function anySwapOutExactTokensForNative(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline,
uint toChainID
) external virtual swapTradeEnabled ensure(deadline) {
AnyswapV1ERC20(path[0]).burn(msg.sender, amountIn);
emit LogAnySwapTradeTokensForNative(path, msg.sender, to, amountIn, amountOutMin, cID(), toChainID);
}
// sets up a cross-chain trade from this chain to `toChainID` for `path` trades to `to`
function anySwapOutExactTokensForNativeUnderlying(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline,
uint toChainID
) external virtual swapTradeEnabled ensure(deadline) {
IERC20(AnyswapV1ERC20(path[0]).underlying()).safeTransferFrom(msg.sender, path[0], amountIn);
emit LogAnySwapTradeTokensForNative(path, msg.sender, to, amountIn, amountOutMin, cID(), toChainID);
}
// Swaps `amounts[path.length-1]` `path[path.length-1]` to `to` on this chain
// Triggered by `anySwapOutExactTokensForNative`
function anySwapInExactTokensForNative(
bytes32 txs,
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline,
uint fromChainID
) external onlyMPC virtual swapTradeEnabled ensure(deadline) returns (uint[] memory amounts) {
require(path[path.length - 1] == wNATIVE, 'AnyswapV6Router: INVALID_PATH');
amounts = SushiswapV2Library.getAmountsOut(factory, amountIn, path);
require(amounts[amounts.length - 1] >= amountOutMin, 'AnyswapV6Router: INSUFFICIENT_OUTPUT_AMOUNT');
_anySwapIn(txs, path[0], SushiswapV2Library.pairFor(factory, path[0], path[1]), amounts[0], fromChainID);
_swap(amounts, path, address(this));
IwNATIVE(wNATIVE).withdraw(amounts[amounts.length - 1]);
TransferHelper.safeTransferNative(to, amounts[amounts.length - 1]);
}
// **** LIBRARY FUNCTIONS ****
function quote(uint amountA, uint reserveA, uint reserveB) external pure virtual returns (uint amountB) {
return SushiswapV2Library.quote(amountA, reserveA, reserveB);
}
function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut)
external
pure
virtual
returns (uint amountOut)
{
return SushiswapV2Library.getAmountOut(amountIn, reserveIn, reserveOut);
}
function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut)
external
pure
virtual
returns (uint amountIn)
{
return SushiswapV2Library.getAmountIn(amountOut, reserveIn, reserveOut);
}
function getAmountsOut(uint amountIn, address[] memory path)
external
view
virtual
returns (uint[] memory amounts)
{
return SushiswapV2Library.getAmountsOut(factory, amountIn, path);
}
function getAmountsIn(uint amountOut, address[] memory path)
external
view
virtual
returns (uint[] memory amounts)
{
return SushiswapV2Library.getAmountsIn(factory, amountOut, path);
}
}