@curvefi/api 2.19.2 → 2.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/interfaces.d.ts +1 -0
- package/lib/router.js +177 -277
- package/package.json +1 -1
package/lib/interfaces.d.ts
CHANGED
package/lib/router.js
CHANGED
|
@@ -67,10 +67,10 @@ var curve_1 = require("./curve");
|
|
|
67
67
|
var utils_1 = require("./utils");
|
|
68
68
|
var pools_1 = require("./pools");
|
|
69
69
|
var utils_2 = require("./pools/utils");
|
|
70
|
-
|
|
70
|
+
// --------------------------- LEGACY ROUTER ---------------------------
|
|
71
71
|
// Inspired by Dijkstra's algorithm
|
|
72
72
|
var _findAllRoutesTheShorterTheBetter = function (inputCoinAddress, outputCoinAddress) { return __awaiter(void 0, void 0, void 0, function () {
|
|
73
|
-
var ALL_POOLS, basePoolsSet, _i, ALL_POOLS_1, pool, basePoolIds, markedCoins, curCoins, nextCoins, routes, step, _a, curCoins_1, inCoin, _b, ALL_POOLS_2, _c, poolId, poolData, wrapped_coin_addresses, underlying_coin_addresses, base_pool, meta_coin_addresses, token_address, is_lending, inCoinIndexes, j, swapType, _d, _e, inCoinRoute, swapType, _f, _h, inCoinRoute, j, tvl, _j, swapType, _k, _l, inCoinRoute, poolAddress, j, tvl, _m, hasEth, swapType, _o, _p, inCoinRoute;
|
|
73
|
+
var ALL_POOLS, basePoolsSet, _i, ALL_POOLS_1, pool, basePoolIds, markedCoins, curCoins, nextCoins, routes, step, _a, curCoins_1, inCoin, _b, ALL_POOLS_2, _c, poolId, poolData, wrapped_coin_addresses, underlying_coin_addresses, base_pool, meta_coin_addresses, token_address, is_lending, inCoinIndexes, j, swapType, _d, _e, inCoinRoute, flag, swapType, _f, _h, inCoinRoute, j, tvl, _j, swapType, _k, _l, inCoinRoute, poolAddress, j, tvl, _m, hasEth, swapType, _o, _p, inCoinRoute;
|
|
74
74
|
var _q;
|
|
75
75
|
var _r, _s, _t, _u, _v, _w;
|
|
76
76
|
return __generator(this, function (_x) {
|
|
@@ -147,29 +147,32 @@ var _findAllRoutesTheShorterTheBetter = function (inputCoinAddress, outputCoinAd
|
|
|
147
147
|
}
|
|
148
148
|
// Wrapped coin -> LP "swaps" (actually add_liquidity)
|
|
149
149
|
if (basePoolIds.includes(poolId) && wrapped_coin_addresses.includes(inCoin)) {
|
|
150
|
+
flag = true;
|
|
150
151
|
// If this coin already marked or will be marked on the current step, no need to consider it on the next step
|
|
151
152
|
if (markedCoins.includes(token_address) || curCoins.includes(token_address))
|
|
152
|
-
|
|
153
|
+
flag = false;
|
|
153
154
|
// Looking for outputCoinAddress only on the final step
|
|
154
155
|
if (step === 3 && token_address !== outputCoinAddress)
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
156
|
+
flag = false;
|
|
157
|
+
if (flag) {
|
|
158
|
+
swapType = is_lending ? 9 : wrapped_coin_addresses.length === 2 ? 7 : 8;
|
|
159
|
+
for (_f = 0, _h = routes[inCoin]; _f < _h.length; _f++) {
|
|
160
|
+
inCoinRoute = _h[_f];
|
|
161
|
+
routes[token_address] = ((_t = routes[token_address]) !== null && _t !== void 0 ? _t : []).concat([__spreadArray(__spreadArray([], inCoinRoute, true), [
|
|
162
|
+
{
|
|
163
|
+
poolId: poolId,
|
|
164
|
+
poolAddress: poolData.swap_address,
|
|
165
|
+
inputCoinAddress: inCoin,
|
|
166
|
+
outputCoinAddress: token_address,
|
|
167
|
+
i: wrapped_coin_addresses.indexOf(inCoin),
|
|
168
|
+
j: 0,
|
|
169
|
+
swapType: swapType,
|
|
170
|
+
swapAddress: ethers_1.ethers.constants.AddressZero,
|
|
171
|
+
},
|
|
172
|
+
], false)]);
|
|
173
|
+
}
|
|
174
|
+
nextCoins.add(token_address);
|
|
171
175
|
}
|
|
172
|
-
nextCoins.add(token_address);
|
|
173
176
|
}
|
|
174
177
|
// No input coin in this pool --> skip
|
|
175
178
|
if (inCoinIndexes.wrapped_coin === -1 && inCoinIndexes.underlying_coin === -1 && inCoinIndexes.meta_coin === -1)
|
|
@@ -286,249 +289,169 @@ var _findAllRoutesTheShorterTheBetter = function (inputCoinAddress, outputCoinAd
|
|
|
286
289
|
});
|
|
287
290
|
}); };
|
|
288
291
|
exports._findAllRoutesTheShorterTheBetter = _findAllRoutesTheShorterTheBetter;
|
|
292
|
+
var getNewRoute = function (route, poolId, poolAddress, inputCoinAddress, outputCoinAddress, i, j, swapType, swapAddress, tvl) {
|
|
293
|
+
var routePoolIds = route.steps.map(function (s) { return s.poolId; });
|
|
294
|
+
// Steps <= 4
|
|
295
|
+
if (routePoolIds.length >= 4)
|
|
296
|
+
return { steps: [], minTvl: Infinity, totalTvl: 0 };
|
|
297
|
+
// Exclude such cases as cvxeth -> tricrypto2 -> tricrypto2 -> susd
|
|
298
|
+
if (routePoolIds.includes(poolId))
|
|
299
|
+
return { steps: [], minTvl: Infinity, totalTvl: 0 };
|
|
300
|
+
return {
|
|
301
|
+
steps: __spreadArray(__spreadArray([], route.steps, true), [{ poolId: poolId, poolAddress: poolAddress, inputCoinAddress: inputCoinAddress, outputCoinAddress: outputCoinAddress, i: i, j: j, swapType: swapType, swapAddress: swapAddress }], false),
|
|
302
|
+
minTvl: Math.min(tvl, route.minTvl),
|
|
303
|
+
totalTvl: route.totalTvl + tvl,
|
|
304
|
+
};
|
|
305
|
+
};
|
|
306
|
+
// --------------------------- MAIN ROUTER ---------------------------
|
|
307
|
+
var MAX_ROUTES_FOR_ONE_COIN = 3;
|
|
308
|
+
var filterRoutes = function (routes, inputCoinAddress, sortFn) {
|
|
309
|
+
var routesByPoolIds = routes.map(function (r) { return r.steps.map(function (s) { return s.poolId; }).toString(); });
|
|
310
|
+
return routes
|
|
311
|
+
.filter(function (r) { return r.steps.length > 0; })
|
|
312
|
+
.filter(function (r) { return r.steps[0].inputCoinAddress === inputCoinAddress; }) // Truncated routes
|
|
313
|
+
.filter(function (r, i) { return routesByPoolIds.indexOf(r.steps.map(function (s) { return s.poolId; }).toString()) === i; }) // Route duplications
|
|
314
|
+
.sort(sortFn).slice(0, MAX_ROUTES_FOR_ONE_COIN);
|
|
315
|
+
};
|
|
316
|
+
var sortByTvl = function (a, b) { return b.minTvl - a.minTvl || b.totalTvl - a.totalTvl || a.steps.length - b.steps.length; };
|
|
317
|
+
var sortByLength = function (a, b) { return a.steps.length - b.steps.length || b.minTvl - a.minTvl || b.totalTvl - a.totalTvl; };
|
|
289
318
|
// Inspired by Dijkstra's algorithm
|
|
290
319
|
var _findAllRoutesTvl = function (inputCoinAddress, outputCoinAddress) { return __awaiter(void 0, void 0, void 0, function () {
|
|
291
|
-
var ALL_POOLS, amplificationCoefficientDict, basePoolsSet, _i, ALL_POOLS_3, pool, basePoolIds, curCoins, nextCoins,
|
|
292
|
-
var _b;
|
|
293
|
-
var
|
|
294
|
-
return __generator(this, function (
|
|
295
|
-
switch (
|
|
320
|
+
var ALL_POOLS, amplificationCoefficientDict, basePoolsSet, _i, ALL_POOLS_3, pool, basePoolIds, curCoins, nextCoins, routesByTvl, routesByLength, step, _loop_1, _a, curCoins_2, inCoin, routes;
|
|
321
|
+
var _b, _c;
|
|
322
|
+
var _d, _e, _f, _h, _j, _k, _l, _m, _o, _p, _q, _r;
|
|
323
|
+
return __generator(this, function (_s) {
|
|
324
|
+
switch (_s.label) {
|
|
296
325
|
case 0:
|
|
297
326
|
inputCoinAddress = inputCoinAddress.toLowerCase();
|
|
298
327
|
outputCoinAddress = outputCoinAddress.toLowerCase();
|
|
299
328
|
ALL_POOLS = Object.entries(__assign(__assign(__assign({}, curve_1.curve.constants.POOLS_DATA), curve_1.curve.constants.FACTORY_POOLS_DATA), curve_1.curve.constants.CRYPTO_FACTORY_POOLS_DATA));
|
|
300
329
|
return [4 /*yield*/, (0, utils_2._getAmplificationCoefficientsFromApi)()];
|
|
301
330
|
case 1:
|
|
302
|
-
amplificationCoefficientDict =
|
|
331
|
+
amplificationCoefficientDict = _s.sent();
|
|
303
332
|
basePoolsSet = new Set();
|
|
304
333
|
for (_i = 0, ALL_POOLS_3 = ALL_POOLS; _i < ALL_POOLS_3.length; _i++) {
|
|
305
334
|
pool = ALL_POOLS_3[_i];
|
|
306
|
-
if (pool[1].
|
|
307
|
-
basePoolsSet.add(pool[
|
|
335
|
+
if (pool[1].wrapped_coin_addresses.length < 4)
|
|
336
|
+
basePoolsSet.add(pool[0]);
|
|
308
337
|
}
|
|
309
338
|
basePoolIds = Array.from(basePoolsSet);
|
|
310
339
|
curCoins = [inputCoinAddress];
|
|
311
340
|
nextCoins = new Set();
|
|
312
|
-
|
|
313
|
-
_b[inputCoinAddress] = [{ steps: [], minTvl: Infinity }],
|
|
341
|
+
routesByTvl = (_b = {},
|
|
342
|
+
_b[inputCoinAddress] = [{ steps: [], minTvl: Infinity, totalTvl: 0 }],
|
|
314
343
|
_b);
|
|
344
|
+
routesByLength = (_c = {},
|
|
345
|
+
_c[inputCoinAddress] = [{ steps: [], minTvl: Infinity, totalTvl: 0 }],
|
|
346
|
+
_c);
|
|
315
347
|
step = 0;
|
|
316
|
-
|
|
348
|
+
_s.label = 2;
|
|
317
349
|
case 2:
|
|
318
350
|
if (!(step < 4)) return [3 /*break*/, 8];
|
|
319
351
|
_loop_1 = function (inCoin) {
|
|
320
|
-
var _loop_2,
|
|
321
|
-
return __generator(this, function (
|
|
322
|
-
switch (
|
|
352
|
+
var _loop_2, _t, ALL_POOLS_4, _u, poolId, poolData;
|
|
353
|
+
return __generator(this, function (_v) {
|
|
354
|
+
switch (_v.label) {
|
|
323
355
|
case 0:
|
|
324
356
|
_loop_2 = function (poolId, poolData) {
|
|
325
|
-
var wrapped_coin_addresses, underlying_coin_addresses, base_pool, meta_coin_addresses, token_address, is_lending,
|
|
326
|
-
return __generator(this, function (
|
|
327
|
-
switch (
|
|
357
|
+
var wrapped_coin_addresses, underlying_coin_addresses, base_pool, meta_coin_addresses, token_address, is_lending, tvlMultiplier, inCoinIndexes, tvl, _w, _loop_3, j, swapType_1, newRoutesByTvl, newRoutesByLength, _loop_4, j, poolAddress, _loop_5, j;
|
|
358
|
+
return __generator(this, function (_x) {
|
|
359
|
+
switch (_x.label) {
|
|
328
360
|
case 0:
|
|
329
361
|
wrapped_coin_addresses = poolData.wrapped_coin_addresses.map(function (a) { return a.toLowerCase(); });
|
|
330
362
|
underlying_coin_addresses = poolData.underlying_coin_addresses.map(function (a) { return a.toLowerCase(); });
|
|
331
363
|
base_pool = poolData.is_meta ? curve_1.curve.constants.POOLS_DATA[poolData.base_pool] : null;
|
|
332
364
|
meta_coin_addresses = base_pool ? base_pool.underlying_coin_addresses.map(function (a) { return a.toLowerCase(); }) : [];
|
|
333
365
|
token_address = poolData.token_address.toLowerCase();
|
|
334
|
-
is_lending = (
|
|
335
|
-
|
|
366
|
+
is_lending = (_d = poolData.is_lending) !== null && _d !== void 0 ? _d : false;
|
|
367
|
+
tvlMultiplier = poolData.is_crypto ? 1 : ((_e = amplificationCoefficientDict[poolData.swap_address]) !== null && _e !== void 0 ? _e : 1);
|
|
336
368
|
inCoinIndexes = {
|
|
337
369
|
wrapped_coin: wrapped_coin_addresses.indexOf(inCoin),
|
|
338
370
|
underlying_coin: underlying_coin_addresses.indexOf(inCoin),
|
|
339
371
|
meta_coin: meta_coin_addresses ? meta_coin_addresses.indexOf(inCoin) : -1,
|
|
340
372
|
};
|
|
341
|
-
//
|
|
373
|
+
// Skip pools which don't contain inCoin
|
|
342
374
|
if (inCoinIndexes.wrapped_coin === -1 && inCoinIndexes.underlying_coin === -1 && inCoinIndexes.meta_coin === -1 && inCoin !== token_address)
|
|
343
375
|
return [2 /*return*/, "continue"];
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
var outputCoinIdx, tvl, _r, swapType, newRoutes, routesByPoolIds;
|
|
347
|
-
return __generator(this, function (_s) {
|
|
348
|
-
switch (_s.label) {
|
|
349
|
-
case 0:
|
|
350
|
-
// Looking for outputCoinAddress only on the final step
|
|
351
|
-
if (step === 3 && underlying_coin_addresses[j] !== outputCoinAddress)
|
|
352
|
-
return [2 /*return*/, "continue"];
|
|
353
|
-
outputCoinIdx = underlying_coin_addresses.indexOf(outputCoinAddress);
|
|
354
|
-
if (outputCoinIdx >= 0 && j !== outputCoinIdx)
|
|
355
|
-
return [2 /*return*/, "continue"];
|
|
356
|
-
_r = Number;
|
|
357
|
-
return [4 /*yield*/, ((0, pools_1.getPool)(poolId)).stats.totalLiquidity()];
|
|
358
|
-
case 1:
|
|
359
|
-
tvl = _r.apply(void 0, [_s.sent()]);
|
|
360
|
-
swapType = poolId === 'aave' ? 11 : 10;
|
|
361
|
-
newRoutes = routes[inCoin].map(function (route) {
|
|
362
|
-
var routePoolIds = route.steps.map(function (s) { return s.poolId; });
|
|
363
|
-
// Steps <= 4
|
|
364
|
-
if (routePoolIds.length >= 4)
|
|
365
|
-
return { steps: [], minTvl: -1 };
|
|
366
|
-
// Exclude such cases as cvxeth -> tricrypto2 -> tricrypto2 -> susd
|
|
367
|
-
if (routePoolIds.includes(poolId))
|
|
368
|
-
return { steps: [], minTvl: -1 };
|
|
369
|
-
return {
|
|
370
|
-
steps: __spreadArray(__spreadArray([], route.steps, true), [
|
|
371
|
-
{
|
|
372
|
-
poolId: poolId,
|
|
373
|
-
poolAddress: poolData.swap_address,
|
|
374
|
-
inputCoinAddress: inCoin,
|
|
375
|
-
outputCoinAddress: underlying_coin_addresses[j],
|
|
376
|
-
i: 0,
|
|
377
|
-
j: j,
|
|
378
|
-
swapType: swapType,
|
|
379
|
-
swapAddress: ethers_1.ethers.constants.AddressZero,
|
|
380
|
-
},
|
|
381
|
-
], false),
|
|
382
|
-
minTvl: Math.min(tvl, route.minTvl * minTvlMultiplier),
|
|
383
|
-
};
|
|
384
|
-
});
|
|
385
|
-
routes[underlying_coin_addresses[j]] = __spreadArray(__spreadArray([], ((_e = routes[underlying_coin_addresses[j]]) !== null && _e !== void 0 ? _e : []), true), newRoutes, true);
|
|
386
|
-
routesByPoolIds = routes[underlying_coin_addresses[j]].map(function (r) { return r.steps.map(function (s) { return s.poolId; }).toString(); });
|
|
387
|
-
routes[underlying_coin_addresses[j]] = routes[underlying_coin_addresses[j]]
|
|
388
|
-
.filter(function (r) { return r.steps.length > 0; })
|
|
389
|
-
.filter(function (r) { return r.steps[0].inputCoinAddress === inputCoinAddress; }) // Truncated routes
|
|
390
|
-
.filter(function (r, i) { return routesByPoolIds.indexOf(r.steps.map(function (s) { return s.poolId; }).toString()) === i; }) // Route duplications
|
|
391
|
-
.sort(function (a, b) { return b.minTvl - a.minTvl || a.steps.length - b.steps.length; }).slice(0, MAX_ROUTES_FOR_ONE_COIN);
|
|
392
|
-
nextCoins.add(underlying_coin_addresses[j]);
|
|
393
|
-
return [2 /*return*/];
|
|
394
|
-
}
|
|
395
|
-
});
|
|
396
|
-
};
|
|
397
|
-
j = 0;
|
|
398
|
-
_q.label = 1;
|
|
376
|
+
_w = Number;
|
|
377
|
+
return [4 /*yield*/, ((0, pools_1.getPool)(poolId)).stats.totalLiquidity()];
|
|
399
378
|
case 1:
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
_q.sent();
|
|
404
|
-
_q.label = 3;
|
|
405
|
-
case 3:
|
|
406
|
-
j++;
|
|
407
|
-
return [3 /*break*/, 1];
|
|
408
|
-
case 4:
|
|
409
|
-
if (!(basePoolIds.includes(poolId) && inCoinIndexes.underlying_coin >= 0)) return [3 /*break*/, 6];
|
|
410
|
-
// Looking for outputCoinAddress only on the final step
|
|
411
|
-
if (step === 3 && token_address !== outputCoinAddress)
|
|
379
|
+
tvl = _w.apply(void 0, [_x.sent()]) * tvlMultiplier;
|
|
380
|
+
// Skip empty pools
|
|
381
|
+
if (tvl === 0)
|
|
412
382
|
return [2 /*return*/, "continue"];
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
inputCoinAddress: inCoin,
|
|
432
|
-
outputCoinAddress: token_address,
|
|
433
|
-
i: underlying_coin_addresses.indexOf(inCoin),
|
|
434
|
-
j: 0,
|
|
435
|
-
swapType: swapType_1,
|
|
436
|
-
swapAddress: ethers_1.ethers.constants.AddressZero,
|
|
437
|
-
},
|
|
438
|
-
], false),
|
|
439
|
-
minTvl: Math.min(tvl_1, route.minTvl * minTvlMultiplier),
|
|
383
|
+
// LP -> wrapped coin "swaps" (actually remove_liquidity_one_coin)
|
|
384
|
+
if (basePoolIds.includes(poolId) && inCoin === token_address) {
|
|
385
|
+
_loop_3 = function (j) {
|
|
386
|
+
// Looking for outputCoinAddress only on the final step
|
|
387
|
+
if (step === 3 && wrapped_coin_addresses[j] !== outputCoinAddress)
|
|
388
|
+
return "continue";
|
|
389
|
+
// Exclude such cases as cvxeth -> tricrypto2 -> tusd -> susd or cvxeth -> tricrypto2 -> susd -> susd
|
|
390
|
+
var outputCoinIdx = wrapped_coin_addresses.indexOf(outputCoinAddress);
|
|
391
|
+
if (outputCoinIdx >= 0 && j !== outputCoinIdx)
|
|
392
|
+
return "continue";
|
|
393
|
+
var swapType = poolId === 'aave' ? 11 : 10;
|
|
394
|
+
var newRoutesByTvl = routesByTvl[inCoin].map(function (route) { return getNewRoute(route, poolId, poolData.swap_address, inCoin, wrapped_coin_addresses[j], 0, j, swapType, ethers_1.ethers.constants.AddressZero, tvl); });
|
|
395
|
+
var newRoutesByLength = routesByLength[inCoin].map(function (route) { return getNewRoute(route, poolId, poolData.swap_address, inCoin, wrapped_coin_addresses[j], 0, j, swapType, ethers_1.ethers.constants.AddressZero, tvl); });
|
|
396
|
+
routesByTvl[wrapped_coin_addresses[j]] = __spreadArray(__spreadArray([], ((_f = routesByTvl[wrapped_coin_addresses[j]]) !== null && _f !== void 0 ? _f : []), true), newRoutesByTvl, true);
|
|
397
|
+
routesByTvl[wrapped_coin_addresses[j]] = filterRoutes(routesByTvl[wrapped_coin_addresses[j]], inputCoinAddress, sortByTvl);
|
|
398
|
+
routesByLength[wrapped_coin_addresses[j]] = __spreadArray(__spreadArray([], ((_h = routesByLength[wrapped_coin_addresses[j]]) !== null && _h !== void 0 ? _h : []), true), newRoutesByLength, true);
|
|
399
|
+
routesByLength[wrapped_coin_addresses[j]] = filterRoutes(routesByLength[wrapped_coin_addresses[j]], inputCoinAddress, sortByLength);
|
|
400
|
+
nextCoins.add(wrapped_coin_addresses[j]);
|
|
440
401
|
};
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
{
|
|
488
|
-
poolId: poolId,
|
|
489
|
-
poolAddress: poolData.swap_address,
|
|
490
|
-
inputCoinAddress: inCoin,
|
|
491
|
-
outputCoinAddress: wrapped_coin_addresses[j],
|
|
492
|
-
i: inCoinIndexes.wrapped_coin,
|
|
493
|
-
j: j,
|
|
494
|
-
swapType: swapType,
|
|
495
|
-
swapAddress: ethers_1.ethers.constants.AddressZero,
|
|
496
|
-
},
|
|
497
|
-
], false),
|
|
498
|
-
minTvl: Math.min(tvl, route.minTvl * minTvlMultiplier),
|
|
499
|
-
};
|
|
500
|
-
});
|
|
501
|
-
routes[wrapped_coin_addresses[j]] = __spreadArray(__spreadArray([], ((_h = routes[wrapped_coin_addresses[j]]) !== null && _h !== void 0 ? _h : []), true), newRoutes, true);
|
|
502
|
-
routesByPoolIds = routes[wrapped_coin_addresses[j]].map(function (r) { return r.steps.map(function (s) { return s.poolId; }).toString(); });
|
|
503
|
-
routes[wrapped_coin_addresses[j]] = routes[wrapped_coin_addresses[j]]
|
|
504
|
-
.filter(function (r) { return r.steps.length > 0; })
|
|
505
|
-
.filter(function (r) { return r.steps[0].inputCoinAddress === inputCoinAddress; }) // Truncated routes
|
|
506
|
-
.filter(function (r, i) { return routesByPoolIds.indexOf(r.steps.map(function (s) { return s.poolId; }).toString()) === i; }) // Route duplications
|
|
507
|
-
.sort(function (a, b) { return b.minTvl - a.minTvl || a.steps.length - b.steps.length; }).slice(0, MAX_ROUTES_FOR_ONE_COIN);
|
|
508
|
-
nextCoins.add(wrapped_coin_addresses[j]);
|
|
509
|
-
return [2 /*return*/];
|
|
510
|
-
}
|
|
511
|
-
});
|
|
512
|
-
};
|
|
513
|
-
j = 0;
|
|
514
|
-
_q.label = 7;
|
|
515
|
-
case 7:
|
|
516
|
-
if (!(j < wrapped_coin_addresses.length)) return [3 /*break*/, 10];
|
|
517
|
-
return [5 /*yield**/, _loop_4(j)];
|
|
518
|
-
case 8:
|
|
519
|
-
_q.sent();
|
|
520
|
-
_q.label = 9;
|
|
521
|
-
case 9:
|
|
522
|
-
j++;
|
|
523
|
-
return [3 /*break*/, 7];
|
|
524
|
-
case 10:
|
|
402
|
+
for (j = 0; j < wrapped_coin_addresses.length; j++) {
|
|
403
|
+
_loop_3(j);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
// Wrapped coin -> LP "swaps" (actually add_liquidity)
|
|
407
|
+
if (basePoolIds.includes(poolId) && inCoinIndexes.wrapped_coin >= 0) {
|
|
408
|
+
// Looking for outputCoinAddress only on the final step
|
|
409
|
+
if (!(step === 3 && token_address !== outputCoinAddress)) {
|
|
410
|
+
swapType_1 = is_lending ? 9 : wrapped_coin_addresses.length === 2 ? 7 : 8;
|
|
411
|
+
newRoutesByTvl = routesByTvl[inCoin].map(function (route) { return getNewRoute(route, poolId, poolData.swap_address, inCoin, token_address, wrapped_coin_addresses.indexOf(inCoin), 0, swapType_1, ethers_1.ethers.constants.AddressZero, tvl); });
|
|
412
|
+
newRoutesByLength = routesByLength[inCoin].map(function (route) { return getNewRoute(route, poolId, poolData.swap_address, inCoin, token_address, wrapped_coin_addresses.indexOf(inCoin), 0, swapType_1, ethers_1.ethers.constants.AddressZero, tvl); });
|
|
413
|
+
routesByTvl[token_address] = __spreadArray(__spreadArray([], ((_j = routesByTvl[token_address]) !== null && _j !== void 0 ? _j : []), true), newRoutesByTvl, true);
|
|
414
|
+
routesByTvl[token_address] = filterRoutes(routesByTvl[token_address], inputCoinAddress, sortByTvl);
|
|
415
|
+
routesByLength[token_address] = __spreadArray(__spreadArray([], ((_k = routesByLength[token_address]) !== null && _k !== void 0 ? _k : []), true), newRoutesByLength, true);
|
|
416
|
+
routesByLength[token_address] = filterRoutes(routesByLength[token_address], inputCoinAddress, sortByLength);
|
|
417
|
+
nextCoins.add(token_address);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
// Wrapped swaps
|
|
421
|
+
if (inCoinIndexes.wrapped_coin >= 0 && !poolData.is_fake) {
|
|
422
|
+
_loop_4 = function (j) {
|
|
423
|
+
if (j === inCoinIndexes.wrapped_coin)
|
|
424
|
+
return "continue";
|
|
425
|
+
// Native swaps spend less gas
|
|
426
|
+
if (wrapped_coin_addresses[j] !== outputCoinAddress && wrapped_coin_addresses[j] === curve_1.curve.constants.NATIVE_TOKEN.wrappedAddress)
|
|
427
|
+
return "continue";
|
|
428
|
+
// Looking for outputCoinAddress only on the final step
|
|
429
|
+
if (step === 3 && wrapped_coin_addresses[j] !== outputCoinAddress)
|
|
430
|
+
return "continue";
|
|
431
|
+
// Exclude such cases as cvxeth -> tricrypto2 -> tusd -> susd or cvxeth -> tricrypto2 -> susd -> susd
|
|
432
|
+
var outputCoinIdx = wrapped_coin_addresses.indexOf(outputCoinAddress);
|
|
433
|
+
if (outputCoinIdx >= 0 && j !== outputCoinIdx)
|
|
434
|
+
return "continue";
|
|
435
|
+
var swapType = poolData.is_crypto ? 3 : 1;
|
|
436
|
+
var newRoutesByTvl = routesByTvl[inCoin].map(function (route) { return getNewRoute(route, poolId, poolData.swap_address, inCoin, wrapped_coin_addresses[j], inCoinIndexes.wrapped_coin, j, swapType, ethers_1.ethers.constants.AddressZero, tvl); });
|
|
437
|
+
var newRoutesByLength = routesByLength[inCoin].map(function (route) { return getNewRoute(route, poolId, poolData.swap_address, inCoin, wrapped_coin_addresses[j], inCoinIndexes.wrapped_coin, j, swapType, ethers_1.ethers.constants.AddressZero, tvl); });
|
|
438
|
+
routesByTvl[wrapped_coin_addresses[j]] = __spreadArray(__spreadArray([], ((_l = routesByTvl[wrapped_coin_addresses[j]]) !== null && _l !== void 0 ? _l : []), true), newRoutesByTvl, true);
|
|
439
|
+
routesByTvl[wrapped_coin_addresses[j]] = filterRoutes(routesByTvl[wrapped_coin_addresses[j]], inputCoinAddress, sortByTvl);
|
|
440
|
+
routesByLength[wrapped_coin_addresses[j]] = __spreadArray(__spreadArray([], ((_m = routesByLength[wrapped_coin_addresses[j]]) !== null && _m !== void 0 ? _m : []), true), newRoutesByLength, true);
|
|
441
|
+
routesByLength[wrapped_coin_addresses[j]] = filterRoutes(routesByLength[wrapped_coin_addresses[j]], inputCoinAddress, sortByLength);
|
|
442
|
+
nextCoins.add(wrapped_coin_addresses[j]);
|
|
443
|
+
};
|
|
444
|
+
for (j = 0; j < wrapped_coin_addresses.length; j++) {
|
|
445
|
+
_loop_4(j);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
525
448
|
poolAddress = (poolData.is_crypto && poolData.is_meta) || ((base_pool === null || base_pool === void 0 ? void 0 : base_pool.is_lending) && poolData.is_factory) ?
|
|
526
449
|
poolData.deposit_address : poolData.swap_address;
|
|
527
|
-
if (!(!poolData.is_plain && inCoinIndexes.underlying_coin >= 0)) return [3 /*break*/,
|
|
450
|
+
if (!(!poolData.is_plain && inCoinIndexes.underlying_coin >= 0)) return [3 /*break*/, 5];
|
|
528
451
|
_loop_5 = function (j) {
|
|
529
|
-
var outputCoinIdx,
|
|
530
|
-
return __generator(this, function (
|
|
531
|
-
switch (
|
|
452
|
+
var outputCoinIdx, tvl_1, _y, hasEth, swapType, newRoutesByTvl, newRoutesByLength;
|
|
453
|
+
return __generator(this, function (_z) {
|
|
454
|
+
switch (_z.label) {
|
|
532
455
|
case 0:
|
|
533
456
|
if (j === inCoinIndexes.underlying_coin)
|
|
534
457
|
return [2 /*return*/, "continue"];
|
|
@@ -541,11 +464,11 @@ var _findAllRoutesTvl = function (inputCoinAddress, outputCoinAddress) { return
|
|
|
541
464
|
outputCoinIdx = underlying_coin_addresses.indexOf(outputCoinAddress);
|
|
542
465
|
if (outputCoinIdx >= 0 && j !== outputCoinIdx)
|
|
543
466
|
return [2 /*return*/, "continue"];
|
|
544
|
-
|
|
467
|
+
_y = Number;
|
|
545
468
|
return [4 /*yield*/, ((0, pools_1.getPool)(poolId)).stats.totalLiquidity()];
|
|
546
469
|
case 1:
|
|
547
|
-
|
|
548
|
-
if (
|
|
470
|
+
tvl_1 = _y.apply(void 0, [_z.sent()]);
|
|
471
|
+
if (tvl_1 === 0)
|
|
549
472
|
return [2 /*return*/, "continue"];
|
|
550
473
|
hasEth = (inCoin === curve_1.curve.constants.NATIVE_TOKEN.address || underlying_coin_addresses[j] === curve_1.curve.constants.NATIVE_TOKEN.address);
|
|
551
474
|
swapType = (poolData.is_crypto && poolData.is_meta && poolData.is_factory) ? 6
|
|
@@ -553,93 +476,70 @@ var _findAllRoutesTvl = function (inputCoinAddress, outputCoinAddress) { return
|
|
|
553
476
|
: hasEth ? 3
|
|
554
477
|
: poolData.is_crypto ? 4
|
|
555
478
|
: 2;
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
if (routePoolIds.includes(poolId))
|
|
563
|
-
return { steps: [], minTvl: -1 };
|
|
564
|
-
return {
|
|
565
|
-
steps: __spreadArray(__spreadArray([], route.steps, true), [
|
|
566
|
-
{
|
|
567
|
-
poolId: poolId,
|
|
568
|
-
poolAddress: poolAddress,
|
|
569
|
-
inputCoinAddress: inCoin,
|
|
570
|
-
outputCoinAddress: underlying_coin_addresses[j],
|
|
571
|
-
i: inCoinIndexes.underlying_coin,
|
|
572
|
-
j: j,
|
|
573
|
-
swapType: swapType,
|
|
574
|
-
swapAddress: (swapType === 5 || swapType === 6) ? poolData.swap_address : ethers_1.ethers.constants.AddressZero,
|
|
575
|
-
},
|
|
576
|
-
], false),
|
|
577
|
-
minTvl: Math.min(tvl, route.minTvl * minTvlMultiplier),
|
|
578
|
-
};
|
|
579
|
-
});
|
|
580
|
-
routes[underlying_coin_addresses[j]] = __spreadArray(__spreadArray([], ((_j = routes[underlying_coin_addresses[j]]) !== null && _j !== void 0 ? _j : []), true), newRoutes, true);
|
|
581
|
-
routesByPoolIds = routes[underlying_coin_addresses[j]].map(function (r) { return r.steps.map(function (s) { return s.poolId; }).toString(); });
|
|
582
|
-
routes[underlying_coin_addresses[j]] = routes[underlying_coin_addresses[j]]
|
|
583
|
-
.filter(function (r) { return r.steps.length > 0; })
|
|
584
|
-
.filter(function (r) { return r.steps[0].inputCoinAddress === inputCoinAddress; }) // Truncated routes
|
|
585
|
-
.filter(function (r, i) { return routesByPoolIds.indexOf(r.steps.map(function (s) { return s.poolId; }).toString()) === i; }) // Route duplications
|
|
586
|
-
.sort(function (a, b) { return b.minTvl - a.minTvl || a.steps.length - b.steps.length; }).slice(0, MAX_ROUTES_FOR_ONE_COIN);
|
|
479
|
+
newRoutesByTvl = routesByTvl[inCoin].map(function (route) { return getNewRoute(route, poolId, poolAddress, inCoin, underlying_coin_addresses[j], inCoinIndexes.underlying_coin, j, swapType, (swapType === 5 || swapType === 6) ? poolData.swap_address : ethers_1.ethers.constants.AddressZero, tvl_1); });
|
|
480
|
+
newRoutesByLength = routesByLength[inCoin].map(function (route) { return getNewRoute(route, poolId, poolAddress, inCoin, underlying_coin_addresses[j], inCoinIndexes.underlying_coin, j, swapType, (swapType === 5 || swapType === 6) ? poolData.swap_address : ethers_1.ethers.constants.AddressZero, tvl_1); });
|
|
481
|
+
routesByTvl[underlying_coin_addresses[j]] = __spreadArray(__spreadArray([], ((_o = routesByTvl[underlying_coin_addresses[j]]) !== null && _o !== void 0 ? _o : []), true), newRoutesByTvl, true);
|
|
482
|
+
routesByTvl[underlying_coin_addresses[j]] = filterRoutes(routesByTvl[underlying_coin_addresses[j]], inputCoinAddress, sortByTvl);
|
|
483
|
+
routesByLength[underlying_coin_addresses[j]] = __spreadArray(__spreadArray([], ((_p = routesByLength[underlying_coin_addresses[j]]) !== null && _p !== void 0 ? _p : []), true), newRoutesByLength, true);
|
|
484
|
+
routesByLength[underlying_coin_addresses[j]] = filterRoutes(routesByLength[underlying_coin_addresses[j]], inputCoinAddress, sortByLength);
|
|
587
485
|
nextCoins.add(underlying_coin_addresses[j]);
|
|
588
486
|
return [2 /*return*/];
|
|
589
487
|
}
|
|
590
488
|
});
|
|
591
489
|
};
|
|
592
490
|
j = 0;
|
|
593
|
-
|
|
594
|
-
case
|
|
595
|
-
if (!(j < underlying_coin_addresses.length)) return [3 /*break*/,
|
|
491
|
+
_x.label = 2;
|
|
492
|
+
case 2:
|
|
493
|
+
if (!(j < underlying_coin_addresses.length)) return [3 /*break*/, 5];
|
|
596
494
|
return [5 /*yield**/, _loop_5(j)];
|
|
597
|
-
case
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
case
|
|
495
|
+
case 3:
|
|
496
|
+
_x.sent();
|
|
497
|
+
_x.label = 4;
|
|
498
|
+
case 4:
|
|
601
499
|
j++;
|
|
602
|
-
return [3 /*break*/,
|
|
603
|
-
case
|
|
500
|
+
return [3 /*break*/, 2];
|
|
501
|
+
case 5: return [2 /*return*/];
|
|
604
502
|
}
|
|
605
503
|
});
|
|
606
504
|
};
|
|
607
|
-
|
|
608
|
-
|
|
505
|
+
_t = 0, ALL_POOLS_4 = ALL_POOLS;
|
|
506
|
+
_v.label = 1;
|
|
609
507
|
case 1:
|
|
610
|
-
if (!(
|
|
611
|
-
|
|
508
|
+
if (!(_t < ALL_POOLS_4.length)) return [3 /*break*/, 4];
|
|
509
|
+
_u = ALL_POOLS_4[_t], poolId = _u[0], poolData = _u[1];
|
|
612
510
|
return [5 /*yield**/, _loop_2(poolId, poolData)];
|
|
613
511
|
case 2:
|
|
614
|
-
|
|
615
|
-
|
|
512
|
+
_v.sent();
|
|
513
|
+
_v.label = 3;
|
|
616
514
|
case 3:
|
|
617
|
-
|
|
515
|
+
_t++;
|
|
618
516
|
return [3 /*break*/, 1];
|
|
619
517
|
case 4: return [2 /*return*/];
|
|
620
518
|
}
|
|
621
519
|
});
|
|
622
520
|
};
|
|
623
521
|
_a = 0, curCoins_2 = curCoins;
|
|
624
|
-
|
|
522
|
+
_s.label = 3;
|
|
625
523
|
case 3:
|
|
626
524
|
if (!(_a < curCoins_2.length)) return [3 /*break*/, 6];
|
|
627
525
|
inCoin = curCoins_2[_a];
|
|
628
526
|
return [5 /*yield**/, _loop_1(inCoin)];
|
|
629
527
|
case 4:
|
|
630
|
-
|
|
631
|
-
|
|
528
|
+
_s.sent();
|
|
529
|
+
_s.label = 5;
|
|
632
530
|
case 5:
|
|
633
531
|
_a++;
|
|
634
532
|
return [3 /*break*/, 3];
|
|
635
533
|
case 6:
|
|
636
534
|
curCoins = Array.from(nextCoins);
|
|
637
535
|
nextCoins = new Set();
|
|
638
|
-
|
|
536
|
+
_s.label = 7;
|
|
639
537
|
case 7:
|
|
640
538
|
step++;
|
|
641
539
|
return [3 /*break*/, 2];
|
|
642
|
-
case 8:
|
|
540
|
+
case 8:
|
|
541
|
+
routes = __spreadArray(__spreadArray([], ((_q = routesByTvl[outputCoinAddress]) !== null && _q !== void 0 ? _q : []), true), ((_r = routesByLength[outputCoinAddress]) !== null && _r !== void 0 ? _r : []), true);
|
|
542
|
+
return [2 /*return*/, routes.map(function (r) { return r.steps; })];
|
|
643
543
|
}
|
|
644
544
|
});
|
|
645
545
|
}); };
|