forked from Uniswap/v3-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestUniswapV3Callee.sol
142 lines (118 loc) Β· 4.57 KB
/
TestUniswapV3Callee.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
// SPDX-License-Identifier: UNLICENSED
pragma solidity =0.7.6;
import '../interfaces/IERC20Minimal.sol';
import '../libraries/SafeCast.sol';
import '../libraries/TickMath.sol';
import '../interfaces/callback/IUniswapV3MintCallback.sol';
import '../interfaces/callback/IUniswapV3SwapCallback.sol';
import '../interfaces/callback/IUniswapV3FlashCallback.sol';
import '../interfaces/IUniswapV3Pool.sol';
contract TestUniswapV3Callee is IUniswapV3MintCallback, IUniswapV3SwapCallback, IUniswapV3FlashCallback {
using SafeCast for uint256;
function swapExact0For1(
address pool,
uint256 amount0In,
address recipient,
uint160 sqrtPriceLimitX96
) external {
IUniswapV3Pool(pool).swap(recipient, true, amount0In.toInt256(), sqrtPriceLimitX96, abi.encode(msg.sender));
}
function swap0ForExact1(
address pool,
uint256 amount1Out,
address recipient,
uint160 sqrtPriceLimitX96
) external {
IUniswapV3Pool(pool).swap(recipient, true, -amount1Out.toInt256(), sqrtPriceLimitX96, abi.encode(msg.sender));
}
function swapExact1For0(
address pool,
uint256 amount1In,
address recipient,
uint160 sqrtPriceLimitX96
) external {
IUniswapV3Pool(pool).swap(recipient, false, amount1In.toInt256(), sqrtPriceLimitX96, abi.encode(msg.sender));
}
function swap1ForExact0(
address pool,
uint256 amount0Out,
address recipient,
uint160 sqrtPriceLimitX96
) external {
IUniswapV3Pool(pool).swap(recipient, false, -amount0Out.toInt256(), sqrtPriceLimitX96, abi.encode(msg.sender));
}
function swapToLowerSqrtPrice(
address pool,
uint160 sqrtPriceX96,
address recipient
) external {
IUniswapV3Pool(pool).swap(recipient, true, type(int256).max, sqrtPriceX96, abi.encode(msg.sender));
}
function swapToHigherSqrtPrice(
address pool,
uint160 sqrtPriceX96,
address recipient
) external {
IUniswapV3Pool(pool).swap(recipient, false, type(int256).max, sqrtPriceX96, abi.encode(msg.sender));
}
event SwapCallback(int256 amount0Delta, int256 amount1Delta);
function uniswapV3SwapCallback(
int256 amount0Delta,
int256 amount1Delta,
bytes calldata data
) external override {
address sender = abi.decode(data, (address));
emit SwapCallback(amount0Delta, amount1Delta);
if (amount0Delta > 0) {
IERC20Minimal(IUniswapV3Pool(msg.sender).token0()).transferFrom(sender, msg.sender, uint256(amount0Delta));
} else if (amount1Delta > 0) {
IERC20Minimal(IUniswapV3Pool(msg.sender).token1()).transferFrom(sender, msg.sender, uint256(amount1Delta));
} else {
// if both are not gt 0, both must be 0.
assert(amount0Delta == 0 && amount1Delta == 0);
}
}
function mint(
address pool,
address recipient,
int24 tickLower,
int24 tickUpper,
uint128 amount
) external {
IUniswapV3Pool(pool).mint(recipient, tickLower, tickUpper, amount, abi.encode(msg.sender));
}
event MintCallback(uint256 amount0Owed, uint256 amount1Owed);
function uniswapV3MintCallback(
uint256 amount0Owed,
uint256 amount1Owed,
bytes calldata data
) external override {
address sender = abi.decode(data, (address));
emit MintCallback(amount0Owed, amount1Owed);
if (amount0Owed > 0)
IERC20Minimal(IUniswapV3Pool(msg.sender).token0()).transferFrom(sender, msg.sender, amount0Owed);
if (amount1Owed > 0)
IERC20Minimal(IUniswapV3Pool(msg.sender).token1()).transferFrom(sender, msg.sender, amount1Owed);
}
event FlashCallback(uint256 fee0, uint256 fee1);
function flash(
address pool,
address recipient,
uint256 amount0,
uint256 amount1,
uint256 pay0,
uint256 pay1
) external {
IUniswapV3Pool(pool).flash(recipient, amount0, amount1, abi.encode(msg.sender, pay0, pay1));
}
function uniswapV3FlashCallback(
uint256 fee0,
uint256 fee1,
bytes calldata data
) external override {
emit FlashCallback(fee0, fee1);
(address sender, uint256 pay0, uint256 pay1) = abi.decode(data, (address, uint256, uint256));
if (pay0 > 0) IERC20Minimal(IUniswapV3Pool(msg.sender).token0()).transferFrom(sender, msg.sender, pay0);
if (pay1 > 0) IERC20Minimal(IUniswapV3Pool(msg.sender).token1()).transferFrom(sender, msg.sender, pay1);
}
}