forked from ArrakisFinance/v2-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwapTest.sol
92 lines (85 loc) · 2.58 KB
/
SwapTest.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {
IUniswapV3SwapCallback
} from "@uniswap/v3-core/contracts/interfaces/callback/IUniswapV3SwapCallback.sol";
import {
IUniswapV3Pool
} from "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
contract SwapTest is IUniswapV3SwapCallback {
function swap(
address pool,
bool zeroForOne,
int256 amountSpecified
) external {
(uint160 sqrtRatio, , , , , , ) = IUniswapV3Pool(pool).slot0();
IUniswapV3Pool(pool).swap(
address(msg.sender),
zeroForOne,
amountSpecified,
zeroForOne ? sqrtRatio - 1000 : sqrtRatio + 1000,
abi.encode(msg.sender)
);
}
function washTrade(
address pool,
int256 amountSpecified,
uint256 numTrades,
uint256 ratio
) external {
for (uint256 i = 0; i < numTrades; i++) {
bool zeroForOne = i % ratio > 0;
(uint160 sqrtRatio, , , , , , ) = IUniswapV3Pool(pool).slot0();
IUniswapV3Pool(pool).swap(
address(msg.sender),
zeroForOne,
amountSpecified,
zeroForOne ? sqrtRatio - 1000 : sqrtRatio + 1000,
abi.encode(msg.sender)
);
}
}
function getSwapResult(
address pool,
bool zeroForOne,
int256 amountSpecified,
uint160 sqrtPriceLimitX96
)
external
returns (
int256 amount0Delta,
int256 amount1Delta,
uint160 nextSqrtRatio
)
{
(amount0Delta, amount1Delta) = IUniswapV3Pool(pool).swap(
address(msg.sender),
zeroForOne,
amountSpecified,
sqrtPriceLimitX96,
abi.encode(msg.sender)
);
(nextSqrtRatio, , , , , , ) = IUniswapV3Pool(pool).slot0();
}
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 if (amount1Delta > 0) {
IERC20(IUniswapV3Pool(msg.sender).token1()).transferFrom(
sender,
msg.sender,
uint256(amount1Delta)
);
}
}
}