This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JpycArbitrage.sol
373 lines (363 loc) · 14.7 KB
/
JpycArbitrage.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface IErc20 {
function decimals() external pure returns(uint8);
function balanceOf(address) external view returns(uint256);
function transfer(address, uint256) external returns(bool);
function approve(address, uint256) external returns(bool);
function transferFrom(address, address, uint256) external returns(bool);
}
interface IQuickSwapRouter {
function getAmountsOut(uint256, address[] calldata) external view returns(uint256[] memory);
function swapExactTokensForTokens(uint256, uint256, address[] calldata, address, uint256) external returns(uint256[] memory);
}
struct UniswapExactInputSingle {
address _0;
address _1;
uint24 _2;
address _3;
uint256 _4;
uint256 _5;
uint256 _6;
uint160 _7;
}
interface IUniswapQuoter {
function quoteExactInputSingle(address, address, uint24, uint256, uint160) external returns(uint256);
}
interface IUniswapRouter {
function exactInputSingle(UniswapExactInputSingle calldata) external returns(uint256);
}
interface ICurvePool {
function get_dy(int128, int128, uint256) external view returns(uint256);
function exchange(int128, int128, uint256, uint256) external returns(uint256);
}
struct JarvisMint {
address _0;
uint256 _1;
uint256 _2;
uint256 _3;
uint256 _4;
address _5;
}
interface IJarvisPool {
function mint(JarvisMint calldata) external returns(uint256, uint256);
function redeem(JarvisMint calldata) external returns(uint256, uint256);
function calculateFee(uint256) external view returns(uint256);
function getPriceFeedIdentifier() external view returns(bytes32);
}
interface IJarvisAggregator {
function latestRoundData() external view returns(uint80, int256, uint256, uint256, uint80);
function decimals() external view returns(uint8);
}
contract JpycArbitrage {
IErc20 internal constant jpyc = IErc20(0x6AE7Dfc73E0dDE2aa99ac063DcF7e8A63265108c);
IErc20 internal constant jjpy = IErc20(0x8343091F2499FD4b6174A46D067A920a3b851FF9);
IErc20 internal constant usdc = IErc20(0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174);
IQuickSwapRouter internal constant routerQuickSwap = IQuickSwapRouter(0xa5E0829CaCEd8fFDD4De3c43696c57F7D7A678ff);
IUniswapQuoter internal constant quoterUniswap = IUniswapQuoter(0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6);
IUniswapRouter internal constant routerUniswap = IUniswapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564);
ICurvePool internal constant poolCurve = ICurvePool(0xE8dCeA7Fb2Baf7a9F4d9af608F06d78a687F8d9A);
IJarvisPool internal constant poolJarvis = IJarvisPool(0x6cA82a7E54053B102e7eC452788cC19204e831de);
IJarvisAggregator internal constant aggregatorJarvis = IJarvisAggregator(0xD647a6fC9BC6402301583C91decC5989d8Bc382D);
address internal constant derivativeJarvis = 0x2076648e2D9d452D55f4252CBa9b162A1850Db48;
constructor() {
jpyc.approve(address(routerQuickSwap), type(uint256).max);
jpyc.approve(address(routerUniswap), type(uint256).max);
jpyc.approve(address(poolCurve), type(uint256).max);
jjpy.approve(address(routerUniswap), type(uint256).max);
jjpy.approve(address(poolCurve), type(uint256).max);
jjpy.approve(address(poolJarvis), type(uint256).max);
usdc.approve(address(routerQuickSwap), type(uint256).max);
usdc.approve(address(routerUniswap), type(uint256).max);
usdc.approve(address(poolJarvis), type(uint256).max);
}
function checkArbitrage(uint256 amount) public returns(uint256, uint256) {
uint256 route0;
uint256 amountOut0;
uint256 route1;
uint256 amountOut1;
uint256 amountIn;
uint256 amountOut;
uint256 i;
route0 = 0;
amountIn = amount;
amountOut0 = 0;
for(i = 0; i < 2; i++) {
amountOut = rateJpycToJjpy(i, amountIn);
if(amountOut > amountOut0) {
amountOut0 = amountOut;
route0 = (route0 & ~(uint256(1) << 1)) | (i << 1);
}
}
amountIn = amountOut0;
amountOut0 = 0;
for(i = 0; i < 2; i++) {
amountOut = rateJjpyToUsdc(i, amountIn);
if(amountOut > amountOut0) {
amountOut0 = amountOut;
route0 = (route0 & ~(uint256(1) << 2)) | (i << 2);
}
}
amountIn = amountOut0;
amountOut0 = 0;
for(i = 0; i < 2; i++) {
amountOut = rateUsdcToJpyc(i, amountIn);
if(amountOut > amountOut0) {
amountOut0 = amountOut;
route0 = (route0 & ~(uint256(1) << 3)) | (i << 3);
}
}
route1 = 1;
amountIn = amount;
amountOut1 = 0;
for(i = 0; i < 2; i++) {
amountOut = rateJpycToUsdc(i, amountIn);
if(amountOut > amountOut1) {
amountOut1 = amountOut;
route1 = (route1 & ~(uint256(1) << 1)) | (i << 1);
}
}
amountIn = amountOut1;
amountOut1 = 0;
for(i = 0; i < 2; i++) {
amountOut = rateUsdcToJjpy(i, amountIn);
if(amountOut > amountOut1) {
amountOut1 = amountOut;
route1 = (route1 & ~(uint256(1) << 2)) | (i << 2);
}
}
amountIn = amountOut1;
amountOut1 = 0;
for(i = 0; i < 2; i++) {
amountOut = rateJjpyToJpyc(i, amountIn);
if(amountOut > amountOut1) {
amountOut1 = amountOut;
route1 = (route1 & ~(uint256(1) << 3)) | (i << 3);
}
}
if(amountOut0 >= amountOut1) {
return (amountOut0, route0);
}
return (amountOut1, route1);
}
function checkArbitrageLimited(uint256 amount, uint256 enable0, uint256 enable1) internal returns(uint256, uint256) {
uint256 route0;
uint256 amountOut0;
uint256 route1;
uint256 amountOut1;
uint256 amountIn;
uint256 amountOut;
uint256 i;
route0 = 0;
amountOut0 = 0;
if(enable0 != 0) {
amountOut0 = rateJpycToJjpy(0, amount);
amountOut0 = rateJjpyToUsdc(0, amountOut0);
amountIn = amountOut0;
amountOut0 = 0;
for(i = 0; i < 2; i++) {
amountOut = rateUsdcToJpyc(i, amountIn);
if(amountOut > amountOut0) {
amountOut0 = amountOut;
route0 = (route0 & ~(uint256(1) << 3)) | (i << 3);
}
}
}
route1 = 1;
amountOut1 = 0;
if(enable1 != 0) {
amountIn = amount;
for(i = 0; i < 2; i++) {
amountOut = rateJpycToUsdc(i, amountIn);
if(amountOut > amountOut1) {
amountOut1 = amountOut;
route1 = (route1 & ~(uint256(1) << 1)) | (i << 1);
}
}
amountOut1 = rateUsdcToJjpy(0, amountOut1);
amountOut1 = rateJjpyToJpyc(0, amountOut1);
}
if(amountOut0 >= amountOut1) {
return (amountOut0, route0);
}
return (amountOut1, route1);
}
function arbitrage(uint256 amount, uint256 minimum, uint256 route, uint256 loop) public {
uint256 balance;
uint256 profitOld;
uint256 profit;
if((route & (1 << 4)) != 0) {
if((route & 1) == 0) {
(balance, route) = checkArbitrageLimited(amount, 1, 0);
}
else {
(balance, route) = checkArbitrageLimited(amount, 0, 1);
}
require(balance >= amount);
}
balance = jpyc.balanceOf(msg.sender);
jpyc.transferFrom(msg.sender, address(this), amount);
profitOld = 0;
while(loop > 0) {
try JpycArbitrage(this).exchange(amount, route) {
}
catch {
break;
}
profit = jpyc.balanceOf(address(this)) - amount;
amount += profit;
if(profit <= profitOld / 2) {
break;
}
profitOld = profit;
loop--;
}
require(amount >= minimum);
jpyc.transfer(msg.sender, amount);
require(jpyc.balanceOf(msg.sender) >= balance);
}
function exchange(uint256 amount, uint256 route) external {
if((route & 1) == 0) {
exchangeJpycToJjpy((route & (1 << 1)) >> 1);
exchangeJjpyToUsdc((route & (1 << 2)) >> 2);
exchangeUsdcToJpyc((route & (1 << 3)) >> 3);
}
else {
exchangeJpycToUsdc((route & (1 << 1)) >> 1);
exchangeUsdcToJjpy((route & (1 << 2)) >> 2);
exchangeJjpyToJpyc((route & (1 << 3)) >> 3);
}
require(jpyc.balanceOf(address(this)) >= amount);
}
function rateJpycToUsdc(uint256 route, uint256 amount) internal returns(uint256) {
if(route == 0) {
return routerQuickSwap.getAmountsOut(amount, addressArray(address(jpyc), address(usdc)))[1];
}
if(route == 1) {
return quoterUniswap.quoteExactInputSingle(address(jpyc), address(usdc), 500, amount, 0);
}
return 0;
}
function exchangeJpycToUsdc(uint256 route) internal {
if(route == 0) {
routerQuickSwap.swapExactTokensForTokens(jpyc.balanceOf(address(this)), 0, addressArray(address(jpyc), address(usdc)), address(this), block.timestamp);
return;
}
if(route == 1) {
routerUniswap.exactInputSingle(UniswapExactInputSingle(address(jpyc), address(usdc), 500, address(this), block.timestamp, jpyc.balanceOf(address(this)), 0, 0));
return;
}
revert();
}
function rateUsdcToJpyc(uint256 route, uint256 amount) internal returns(uint256) {
if(route == 0) {
return routerQuickSwap.getAmountsOut(amount, addressArray(address(usdc), address(jpyc)))[1];
}
if(route == 1) {
return quoterUniswap.quoteExactInputSingle(address(usdc), address(jpyc), 500, amount, 0);
}
return 0;
}
function exchangeUsdcToJpyc(uint256 route) internal {
if(route == 0) {
routerQuickSwap.swapExactTokensForTokens(usdc.balanceOf(address(this)), 0, addressArray(address(usdc), address(jpyc)), address(this), block.timestamp);
return;
}
if(route == 1) {
routerUniswap.exactInputSingle(UniswapExactInputSingle(address(usdc), address(jpyc), 500, address(this), block.timestamp, usdc.balanceOf(address(this)), 0, 0));
return;
}
revert();
}
function rateJjpyToJpyc(uint256 route, uint256 amount) internal returns(uint256) {
if(route == 0) {
return poolCurve.get_dy(0, 1, amount);
}
if(route == 1) {
return quoterUniswap.quoteExactInputSingle(address(jjpy), address(jpyc), 500, amount, 0);
}
return 0;
}
function exchangeJjpyToJpyc(uint256 route) internal {
if(route == 0) {
poolCurve.exchange(0, 1, jjpy.balanceOf(address(this)), 0);
return;
}
if(route == 1) {
routerUniswap.exactInputSingle(UniswapExactInputSingle(address(jjpy), address(jpyc), 500, address(this), block.timestamp, jjpy.balanceOf(address(this)), 0, 0));
return;
}
revert();
}
function rateJpycToJjpy(uint256 route, uint256 amount) internal returns(uint256) {
if(route == 0) {
return poolCurve.get_dy(1, 0, amount);
}
if(route == 1) {
return quoterUniswap.quoteExactInputSingle(address(jpyc), address(jjpy), 500, amount, 0);
}
return 0;
}
function exchangeJpycToJjpy(uint256 route) internal {
if(route == 0) {
poolCurve.exchange(1, 0, jpyc.balanceOf(address(this)), 0);
return;
}
if(route == 1) {
routerUniswap.exactInputSingle(UniswapExactInputSingle(address(jpyc), address(jjpy), 500, address(this), block.timestamp, jpyc.balanceOf(address(this)), 0, 0));
return;
}
revert();
}
function rateUsdcToJjpy(uint256 route, uint256 amount) internal returns(uint256) {
int256 a;
if(route == 0) {
(, a, , , ) = aggregatorJarvis.latestRoundData();
return ((amount * amount / (amount + poolJarvis.calculateFee(amount))) * (10 ** jjpy.decimals()) / (10 ** usdc.decimals())) * (10 ** aggregatorJarvis.decimals()) / uint256(a);
}
if(route == 1) {
return quoterUniswap.quoteExactInputSingle(address(usdc), address(jjpy), 500, amount, 0);
}
return 0;
}
function exchangeUsdcToJjpy(uint256 route) internal {
if(route == 0) {
poolJarvis.mint(JarvisMint(derivativeJarvis, 0, usdc.balanceOf(address(this)), 2000000000000000, block.timestamp, address(this)));
return;
}
if(route == 1) {
routerUniswap.exactInputSingle(UniswapExactInputSingle(address(usdc), address(jjpy), 500, address(this), block.timestamp, usdc.balanceOf(address(this)), 0, 0));
return;
}
revert();
}
function rateJjpyToUsdc(uint256 route, uint256 amount) internal returns(uint256) {
int256 a;
if(route == 0) {
(, a, , , ) = aggregatorJarvis.latestRoundData();
return ((amount - poolJarvis.calculateFee(amount)) * (10 ** usdc.decimals()) / (10 ** jjpy.decimals())) * uint256(a) / (10 ** aggregatorJarvis.decimals());
}
if(route == 1) {
return quoterUniswap.quoteExactInputSingle(address(jjpy), address(usdc), 500, amount, 0);
}
return 0;
}
function exchangeJjpyToUsdc(uint256 route) internal {
if(route == 0) {
poolJarvis.redeem(JarvisMint(derivativeJarvis, jjpy.balanceOf(address(this)), 0, 2000000000000000, block.timestamp, address(this)));
return;
}
if(route == 1) {
routerUniswap.exactInputSingle(UniswapExactInputSingle(address(jjpy), address(usdc), 500, address(this), block.timestamp, jjpy.balanceOf(address(this)), 0, 0));
return;
}
revert();
}
function addressArray(address _0, address _1) internal pure returns(address[] memory) {
address[] memory a;
a = new address[](2);
a[0] = _0;
a[1] = _1;
return a;
}
}