forked from Uniswap/v3-periphery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestUniswapV3Callee.sol
62 lines (54 loc) · 2.11 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
// SPDX-License-Identifier: UNLICENSED
pragma solidity =0.7.6;
import '@birthdayresearchforks/uniswap-v3-core/contracts/interfaces/callback/IUniswapV3SwapCallback.sol';
import '@birthdayresearchforks/uniswap-v3-core/contracts/libraries/SafeCast.sol';
import '@birthdayresearchforks/uniswap-v3-core/contracts/interfaces/IUniswapV3Pool.sol';
import '@openzeppelin3.4.2/contracts/token/ERC20/IERC20.sol';
contract TestUniswapV3Callee is IUniswapV3SwapCallback {
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 uniswapV3SwapCallback(
int256 amount0Delta,
int256 amount1Delta,
bytes calldata data
) external override {
address sender = abi.decode(data, (address));
if (amount0Delta > 0) {
IERC20(IUniswapV3Pool(msg.sender).token0()).transferFrom(sender, msg.sender, uint256(amount0Delta));
} else {
assert(amount1Delta > 0);
IERC20(IUniswapV3Pool(msg.sender).token1()).transferFrom(sender, msg.sender, uint256(amount1Delta));
}
}
}