-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathAutoExit.sol
245 lines (214 loc) · 9.19 KB
/
AutoExit.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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
import "./Automator.sol";
/// @title AutoExit
/// @notice Lets a v3 position to be automatically removed (limit order) or swapped to the opposite token (stop loss order) when it reaches a certain tick.
/// A revert controlled bot (operator) is responsable for the execution of optimized swaps (using external swap router)
/// Positions need to be approved (approve or setApprovalForAll) for the contract and configured with configToken method
contract AutoExit is Automator {
event Executed(
uint256 indexed tokenId,
address account,
bool isSwap,
uint256 amountReturned0,
uint256 amountReturned1,
address token0,
address token1
);
event PositionConfigured(
uint256 indexed tokenId,
bool isActive,
bool token0Swap,
bool token1Swap,
int24 token0TriggerTick,
int24 token1TriggerTick,
uint64 token0SlippageX64,
uint64 token1SlippageX64,
bool onlyFees,
uint64 maxRewardX64
);
constructor(
INonfungiblePositionManager _npm,
address _operator,
address _withdrawer,
uint32 _TWAPSeconds,
uint16 _maxTWAPTickDifference,
address _zeroxRouter,
address _universalRouter
) Automator(_npm, _operator, _withdrawer, _TWAPSeconds, _maxTWAPTickDifference, _zeroxRouter, _universalRouter) {}
// define how stoploss / limit should be handled
struct PositionConfig {
bool isActive; // if position is active
// should swap token to other token when triggered
bool token0Swap;
bool token1Swap;
// when should action be triggered (when this tick is reached - allow execute)
int24 token0TriggerTick; // when tick is below this one
int24 token1TriggerTick; // when tick is equal or above this one
// max price difference from current pool price for swap / Q64
uint64 token0SlippageX64; // when token 0 is swapped to token 1
uint64 token1SlippageX64; // when token 1 is swapped to token 0
bool onlyFees; // if only fees maybe used for protocol reward
uint64 maxRewardX64; // max allowed reward percentage of fees or full position
}
// configured tokens
mapping(uint256 => PositionConfig) public positionConfigs;
/// @notice params for execute()
struct ExecuteParams {
uint256 tokenId; // tokenid to process
bytes swapData; // if its a swap order - must include swap data
uint128 liquidity; // liquidity the calculations are based on
uint256 amountRemoveMin0; // min amount to be removed from liquidity
uint256 amountRemoveMin1; // min amount to be removed from liquidity
uint256 deadline; // for uniswap operations - operator promises fair value
uint64 rewardX64; // which reward will be used for protocol, can be max configured amount (considering onlyFees)
}
struct ExecuteState {
address token0;
address token1;
uint24 fee;
int24 tickLower;
int24 tickUpper;
uint128 liquidity;
uint256 amount0;
uint256 amount1;
uint256 feeAmount0;
uint256 feeAmount1;
uint256 amountOutMin;
uint256 amountInDelta;
uint256 amountOutDelta;
IUniswapV3Pool pool;
uint256 swapAmount;
int24 tick;
bool isSwap;
bool isAbove;
address owner;
}
/**
* @notice Handle token (must be in correct state)
* Can only be called only from configured operator account
* Swap needs to be done with max price difference from current pool price - otherwise reverts
*/
function execute(ExecuteParams calldata params) external {
if (!operators[msg.sender]) {
revert Unauthorized();
}
ExecuteState memory state;
PositionConfig memory config = positionConfigs[params.tokenId];
if (!config.isActive) {
revert NotConfigured();
}
if (
config.onlyFees && params.rewardX64 > config.maxRewardX64
|| !config.onlyFees && params.rewardX64 > config.maxRewardX64
) {
revert ExceedsMaxReward();
}
// get position info
(,, state.token0, state.token1, state.fee, state.tickLower, state.tickUpper, state.liquidity,,,,) =
nonfungiblePositionManager.positions(params.tokenId);
// so can be executed only once
if (state.liquidity == 0) {
revert NoLiquidity();
}
if (state.liquidity != params.liquidity) {
revert LiquidityChanged();
}
state.pool = _getPool(state.token0, state.token1, state.fee);
(, state.tick,,,,,) = state.pool.slot0();
// not triggered
if (config.token0TriggerTick <= state.tick && state.tick < config.token1TriggerTick) {
revert NotReady();
}
state.isAbove = state.tick >= config.token1TriggerTick;
state.isSwap = !state.isAbove && config.token0Swap || state.isAbove && config.token1Swap;
// decrease full liquidity for given position - and return fees as well
(state.amount0, state.amount1, state.feeAmount0, state.feeAmount1) = _decreaseFullLiquidityAndCollect(
params.tokenId, state.liquidity, params.amountRemoveMin0, params.amountRemoveMin1, params.deadline
);
// swap to other token
if (state.isSwap) {
if (params.swapData.length == 0) {
revert MissingSwapData();
}
// reward is taken before swap - if from fees only
if (config.onlyFees) {
state.amount0 -= state.feeAmount0 * params.rewardX64 / Q64;
state.amount1 -= state.feeAmount1 * params.rewardX64 / Q64;
}
state.swapAmount = state.isAbove ? state.amount1 : state.amount0;
// checks if price in valid oracle range and calculates amountOutMin
(state.amountOutMin,,,) = _validateSwap(
!state.isAbove,
state.swapAmount,
state.pool,
TWAPSeconds,
maxTWAPTickDifference,
state.isAbove ? config.token1SlippageX64 : config.token0SlippageX64
);
(state.amountInDelta, state.amountOutDelta) = _routerSwap(
Swapper.RouterSwapParams(
state.isAbove ? IERC20(state.token1) : IERC20(state.token0),
state.isAbove ? IERC20(state.token0) : IERC20(state.token1),
state.swapAmount,
state.amountOutMin,
params.swapData
)
);
state.amount0 = state.isAbove ? state.amount0 + state.amountOutDelta : state.amount0 - state.amountInDelta;
state.amount1 = state.isAbove ? state.amount1 - state.amountInDelta : state.amount1 + state.amountOutDelta;
// when swap and !onlyFees - protocol reward is removed only from target token (to incentivize optimal swap done by operator)
if (!config.onlyFees) {
if (state.isAbove) {
state.amount0 -= state.amount0 * params.rewardX64 / Q64;
} else {
state.amount1 -= state.amount1 * params.rewardX64 / Q64;
}
}
} else {
// reward is taken as configured
state.amount0 -= (config.onlyFees ? state.feeAmount0 : state.amount0) * params.rewardX64 / Q64;
state.amount1 -= (config.onlyFees ? state.feeAmount1 : state.amount1) * params.rewardX64 / Q64;
}
state.owner = nonfungiblePositionManager.ownerOf(params.tokenId);
if (state.amount0 > 0) {
_transferToken(state.owner, IERC20(state.token0), state.amount0, true);
}
if (state.amount1 > 0) {
_transferToken(state.owner, IERC20(state.token1), state.amount1, true);
}
// delete config for position
delete positionConfigs[params.tokenId];
emit PositionConfigured(params.tokenId, false, false, false, 0, 0, 0, 0, false, 0);
// log event
emit Executed(
params.tokenId, msg.sender, state.isSwap, state.amount0, state.amount1, state.token0, state.token1
);
}
// function to configure a token to be used with this runner
// it needs to have approvals set for this contract beforehand
function configToken(uint256 tokenId, PositionConfig calldata config) external {
address owner = nonfungiblePositionManager.ownerOf(tokenId);
if (owner != msg.sender) {
revert Unauthorized();
}
if (config.isActive) {
if (config.token0TriggerTick >= config.token1TriggerTick) {
revert InvalidConfig();
}
}
positionConfigs[tokenId] = config;
emit PositionConfigured(
tokenId,
config.isActive,
config.token0Swap,
config.token1Swap,
config.token0TriggerTick,
config.token1TriggerTick,
config.token0SlippageX64,
config.token1SlippageX64,
config.onlyFees,
config.maxRewardX64
);
}
}