-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAvaxInstantSwap.sol
189 lines (168 loc) · 5.02 KB
/
AvaxInstantSwap.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
// SPDX-License-Identifier: MIT
pragma solidity =0.8.20;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
interface IWNATIVE is IERC20 {
function deposit() external payable;
function withdraw(uint256) external;
}
interface ILBRouter {
enum Version {
V1,
V2,
V2_1
}
struct Path {
uint256[] pairBinSteps;
Version[] versions;
IERC20[] tokenPath;
}
function swapExactTokensForTokensSupportingFeeOnTransferTokens(
uint256 amountIn,
uint256 amountOutMin,
Path memory path,
address to,
uint256 deadline
) external returns (uint256 amountOut);
function swapExactTokensForNATIVESupportingFeeOnTransferTokens(
uint256 amountIn,
uint256 amountOutMinNATIVE,
Path memory path,
address payable to,
uint256 deadline
) external returns (uint256 amountOut);
function getWNATIVE() external view returns (IWNATIVE);
}
interface IQuoter {
struct Quote {
address[] route;
address[] pairs;
uint256[] binSteps;
ILBRouter.Version[] versions;
uint128[] amounts;
uint128[] virtualAmountsWithoutSlippage;
uint128[] fees;
}
function findBestPathFromAmountIn(
address[] calldata route,
uint128 amountIn
) external view returns (Quote memory quote);
}
contract AvaxInstantSwap is Ownable, ReentrancyGuard {
using SafeERC20 for IERC20;
ILBRouter public lbRouter;
IQuoter public lbQuoter;
address public wethToken;
address public usdc;
address public executor;
//////////================= Events ====================================================
event SwapFromUSDC(
address indexed receiver,
address indexed token,
uint256 amountIn,
uint256 time
);
event ExecutorUpdated(address indexed oldExecutor, address indexed newExecutor);
modifier onlyExecutor() {
require(msg.sender == executor, "not executor");
_;
}
constructor(
address _lbRouter,
address _lbQuoter,
address _usdc,
address _executor,
address _owner
) Ownable(_owner) {
lbRouter = ILBRouter(_lbRouter);
lbQuoter = IQuoter(_lbQuoter);
wethToken = address(lbRouter.getWNATIVE());
usdc = _usdc;
executor = _executor;
}
// To get the estimated path for making a swap
function getQuote(
address[] memory _path,
uint256 _amountIn
) public view returns (ILBRouter.Path memory) {
// Use the quoter to find the best route for the swap
address[] memory path = new address[](_path.length);
IERC20[] memory pathToken = new IERC20[](_path.length);
for (uint256 i = 0; i < _path.length; i++) {
path[i] = _path[i];
pathToken[i] = IERC20(_path[i]);
}
IQuoter.Quote memory quote = lbQuoter.findBestPathFromAmountIn(
path,
uint128(_amountIn)
);
ILBRouter.Path memory myPath = ILBRouter.Path({
pairBinSteps: quote.binSteps,
versions: quote.versions,
tokenPath: pathToken
});
return myPath;
}
function swapFromUSDC(
address _tokenB,
uint256 _amountIn,
uint256 _minAmountOut,
address[] memory _path,
address _to,
bool _unwrappETH
) public nonReentrant onlyExecutor {
if (_tokenB == usdc) {
emit SwapFromUSDC(_to, _tokenB, _amountIn, block.timestamp);
IERC20(usdc).transfer(_to, _amountIn);
return;
}
if (IERC20(usdc).allowance(address(this), address(lbRouter)) < _amountIn) {
IERC20(usdc).approve(address(lbRouter), _amountIn);
}
ILBRouter.Path memory pathQuote = getQuote(_path, _amountIn);
// Make LBRouter swap
if (_unwrappETH) {
lbRouter.swapExactTokensForNATIVESupportingFeeOnTransferTokens(
_amountIn,
_minAmountOut, // Amount out min
pathQuote,
payable(_to),
block.timestamp + 1 hours
);
} else {
lbRouter.swapExactTokensForTokensSupportingFeeOnTransferTokens(
_amountIn,
_minAmountOut, // Amount out min
pathQuote,
_to,
block.timestamp + 1 hours
);
}
emit SwapFromUSDC(_to, _tokenB, _amountIn, block.timestamp);
}
function setExecutor(address _newExecutor) external onlyOwner {
emit ExecutorUpdated(executor, _newExecutor);
executor = _newExecutor;
}
function checkAndApproveAll(
address _token,
address _target,
uint256 _amountToCheck
) internal {
if (IERC20(_token).allowance(address(this), _target) < _amountToCheck) {
IERC20(_token).forceApprove(_target, 0);
IERC20(_token).forceApprove(_target, ~uint256(0));
}
}
function recoverStuckETH(address payable _beneficiary) public onlyOwner {
_beneficiary.transfer(address(this).balance);
}
function recoverStuckTokens(address _token) external onlyOwner {
uint256 amount = IERC20(_token).balanceOf(address(this));
IERC20(_token).transfer(owner(), amount);
}
receive() external payable {}
fallback() external payable {}
}