-
Notifications
You must be signed in to change notification settings - Fork 76
/
SwapRouter.sol
251 lines (216 loc) · 8.8 KB
/
SwapRouter.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
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.24;
pragma abicoder v2;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "./interfaces/ISwapRouter.sol";
import "./interfaces/IPool.sol";
import "./interfaces/IPoolManager.sol";
contract SwapRouter is ISwapRouter {
IPoolManager public poolManager;
constructor(address _poolManager) {
poolManager = IPoolManager(_poolManager);
}
/// @dev Parses a revert reason that should contain the numeric quote
function parseRevertReason(
bytes memory reason
) private pure returns (uint256) {
if (reason.length != 32) {
if (reason.length < 68) revert("Unexpected error");
assembly {
reason := add(reason, 0x04)
}
revert(abi.decode(reason, (string)));
}
return abi.decode(reason, (uint256));
}
function exactInput(
ExactInputParams calldata params
) external payable override returns (uint256 amountOut) {
// 记录确定的输入 token 的 amount
uint256 amountIn = params.amountIn;
// 根据 tokenIn 和 tokenOut 的大小关系,确定是从 token0 到 token1 还是从 token1 到 token0
bool zeroForOne = params.tokenIn < params.tokenOut;
// 遍历指定的每一个 pool
for (uint256 i = 0; i < params.indexPath.length; i++) {
address poolAddress = poolManager.getPool(
params.tokenIn,
params.tokenOut,
params.indexPath[i]
);
// 如果 pool 不存在,则抛出错误
require(poolAddress != address(0), "Pool not found");
// 获取 pool 实例
IPool pool = IPool(poolAddress);
// 构造 swapCallback 函数需要的参数
bytes memory data = abi.encode(
params.tokenIn,
params.tokenOut,
params.indexPath[i],
params.recipient == address(0) ? address(0) : msg.sender,
true
);
// 调用 pool 的 swap 函数,进行交换,并拿到返回的 token0 和 token1 的数量
(int256 amount0, int256 amount1) = pool.swap(
params.recipient,
zeroForOne,
int256(amountIn),
params.sqrtPriceLimitX96,
data
);
// 更新 amountIn 和 amountOut
amountIn -= uint256(zeroForOne ? amount0 : amount1);
amountOut += uint256(zeroForOne ? -amount1 : -amount0);
// 如果 amountIn 为 0,表示交换完成,跳出循环
if (amountIn == 0) {
break;
}
}
// 如果交换到的 amountOut 小于指定的最少数量 amountOutMinimum,则抛出错误
require(amountOut >= params.amountOutMinimum, "Slippage exceeded");
// 发送 Swap 事件
emit Swap(msg.sender, zeroForOne, params.amountIn, amountIn, amountOut);
// 返回 amountOut
return amountOut;
}
function exactOutput(
ExactOutputParams calldata params
) external payable override returns (uint256 amountIn) {
// 记录确定的输出 token 的 amount
uint256 amountOut = params.amountOut;
// 根据 tokenIn 和 tokenOut 的大小关系,确定是从 token0 到 token1 还是从 token1 到 token0
bool zeroForOne = params.tokenIn < params.tokenOut;
// 遍历指定的每一个 pool
for (uint256 i = 0; i < params.indexPath.length; i++) {
address poolAddress = poolManager.getPool(
params.tokenIn,
params.tokenOut,
params.indexPath[i]
);
// 如果 pool 不存在,则抛出错误
require(poolAddress != address(0), "Pool not found");
// 获取 pool 实例
IPool pool = IPool(poolAddress);
// 构造 swapCallback 函数需要的参数
bytes memory data = abi.encode(
params.tokenIn,
params.tokenOut,
params.indexPath[i],
params.recipient == address(0) ? address(0) : msg.sender,
false
);
// 调用 pool 的 swap 函数,进行交换,并拿到返回的 token0 和 token1 的数量
(int256 amount0, int256 amount1) = pool.swap(
params.recipient,
zeroForOne,
-int256(amountOut),
params.sqrtPriceLimitX96,
data
);
// 更新 amountOut 和 amountIn
amountOut -= uint256(zeroForOne ? -amount1 : -amount0);
amountIn += uint256(zeroForOne ? amount0 : amount1);
// 如果 amountOut 为 0,表示交换完成,跳出循环
if (amountOut == 0) {
break;
}
}
// 如果交换到指定数量 tokenOut 消耗的 tokenIn 数量超过指定的最大值,报错
require(amountIn <= params.amountInMaximum, "Slippage exceeded");
// 发射 Swap 事件
emit Swap(
msg.sender,
zeroForOne,
params.amountOut,
amountOut,
amountIn
);
// 返回交换后的 amountIn
return amountIn;
}
// 报价,指定 tokenIn 的数量和 tokenOut 的最小值,返回 tokenOut 的实际数量
function quoteExactInput(
QuoteExactInputParams calldata params
) external override returns (uint256 amountOut) {
// 因为没有实际 approve,所以这里交易会报错,我们捕获错误信息,解析需要多少 token
try
this.exactInput(
ExactInputParams({
tokenIn: params.tokenIn,
tokenOut: params.tokenOut,
indexPath: params.indexPath,
recipient: address(0),
deadline: block.timestamp + 1 hours,
amountIn: params.amountIn,
amountOutMinimum: 0,
sqrtPriceLimitX96: params.sqrtPriceLimitX96
})
)
{} catch (bytes memory reason) {
return parseRevertReason(reason);
}
}
// 报价,指定 tokenOut 的数量和 tokenIn 的最大值,返回 tokenIn 的实际数量
function quoteExactOutput(
QuoteExactOutputParams calldata params
) external override returns (uint256 amountIn) {
try
this.exactOutput(
ExactOutputParams({
tokenIn: params.tokenIn,
tokenOut: params.tokenOut,
indexPath: params.indexPath,
recipient: address(0),
deadline: block.timestamp + 1 hours,
amountOut: params.amountOut,
amountInMaximum: type(uint256).max,
sqrtPriceLimitX96: params.sqrtPriceLimitX96
})
)
{} catch (bytes memory reason) {
return parseRevertReason(reason);
}
}
function swapCallback(
int256 amount0Delta,
int256 amount1Delta,
bytes calldata data
) external override {
// transfer token
(
address tokenIn,
address tokenOut,
uint32 index,
address payer,
bool isExactInput
) = abi.decode(data, (address, address, uint32, address, bool));
address _pool = poolManager.getPool(tokenIn, tokenOut, index);
// 检查 callback 的合约地址是否是 Pool
require(_pool == msg.sender, "Invalid callback caller");
(uint256 amountToPay, uint256 amountReceived) = amount0Delta > 0
? (uint256(amount0Delta), uint256(-amount1Delta))
: (uint256(amount1Delta), uint256(-amount0Delta));
// payer 是 address(0),这是一个用于预估 token 的请求(quoteExactInput or quoteExactOutput)
// 参考代码 https://github.com/Uniswap/v3-periphery/blob/main/contracts/lens/Quoter.sol#L38
if (payer == address(0)) {
if (isExactInput) {
// 指定输入情况下,抛出可以接收多少 token
assembly {
let ptr := mload(0x40)
mstore(ptr, amountReceived)
revert(ptr, 32)
}
} else {
// 指定输出情况下,抛出需要转入多少 token
assembly {
let ptr := mload(0x40)
mstore(ptr, amountToPay)
revert(ptr, 32)
}
}
}
// 正常交易,转账给交易池
if (amountToPay > 0) {
IERC20(tokenIn).transferFrom(payer, _pool, amountToPay);
}
}
}