@curvefi/api 2.22.0 → 2.24.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/router.js CHANGED
@@ -59,7 +59,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
59
59
  return (mod && mod.__esModule) ? mod : { "default": mod };
60
60
  };
61
61
  Object.defineProperty(exports, "__esModule", { value: true });
62
- exports.swap = exports.swapEstimateGas = exports.swapApprove = exports.swapApproveEstimateGas = exports.swapIsApproved = exports.swapPriceImpact = exports.swapExpected = exports.getBestRouteAndOutput = exports._findAllRoutes = exports._findAllRoutesTvl = exports._findAllRoutesTheShorterTheBetter = void 0;
62
+ exports.getSwappedAmount = exports.swap = exports.swapEstimateGas = exports.swapApprove = exports.swapApproveEstimateGas = exports.swapIsApproved = exports.swapPriceImpact = exports.swapExpected = exports.getBestRouteAndOutput = exports._findAllRoutes = exports._findAllRoutesTvl = exports._findAllRoutesTheShorterTheBetter = void 0;
63
63
  var axios_1 = __importDefault(require("axios"));
64
64
  var memoizee_1 = __importDefault(require("memoizee"));
65
65
  var ethers_1 = require("ethers");
@@ -82,7 +82,7 @@ var _findAllRoutesTheShorterTheBetter = function (inputCoinAddress, outputCoinAd
82
82
  basePoolsSet = new Set();
83
83
  for (_i = 0, ALL_POOLS_1 = ALL_POOLS; _i < ALL_POOLS_1.length; _i++) {
84
84
  pool = ALL_POOLS_1[_i];
85
- if (pool[1].wrapped_coin_addresses.length < 4)
85
+ if (pool[1].wrapped_coin_addresses.length < 6)
86
86
  basePoolsSet.add(pool[0]);
87
87
  }
88
88
  basePoolIds = Array.from(basePoolsSet);
@@ -317,9 +317,9 @@ var sortByTvl = function (a, b) { return b.minTvl - a.minTvl || b.totalTvl - a.t
317
317
  var sortByLength = function (a, b) { return a.steps.length - b.steps.length || b.minTvl - a.minTvl || b.totalTvl - a.totalTvl; };
318
318
  // Inspired by Dijkstra's algorithm
319
319
  var _findAllRoutesTvl = function (inputCoinAddress, outputCoinAddress) { return __awaiter(void 0, void 0, void 0, function () {
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;
320
+ var ALL_POOLS, amplificationCoefficientDict, curCoins, nextCoins, routesByTvl, routesByLength, step, _loop_1, _i, curCoins_2, inCoin, routes;
321
+ var _a, _b;
322
+ var _c, _d, _e, _f, _h, _j, _k, _l, _m, _o, _p, _q, _r;
323
323
  return __generator(this, function (_s) {
324
324
  switch (_s.label) {
325
325
  case 0:
@@ -329,32 +329,35 @@ var _findAllRoutesTvl = function (inputCoinAddress, outputCoinAddress) { return
329
329
  return [4 /*yield*/, (0, utils_2._getAmplificationCoefficientsFromApi)()];
330
330
  case 1:
331
331
  amplificationCoefficientDict = _s.sent();
332
- basePoolsSet = new Set();
333
- for (_i = 0, ALL_POOLS_3 = ALL_POOLS; _i < ALL_POOLS_3.length; _i++) {
334
- pool = ALL_POOLS_3[_i];
335
- if (pool[1].wrapped_coin_addresses.length < 4)
336
- basePoolsSet.add(pool[0]);
337
- }
338
- basePoolIds = Array.from(basePoolsSet);
339
332
  curCoins = [inputCoinAddress];
340
333
  nextCoins = new Set();
341
- routesByTvl = (_b = {},
334
+ routesByTvl = (_a = {},
335
+ _a[inputCoinAddress] = [{ steps: [], minTvl: Infinity, totalTvl: 0 }],
336
+ _a);
337
+ routesByLength = (_b = {},
342
338
  _b[inputCoinAddress] = [{ steps: [], minTvl: Infinity, totalTvl: 0 }],
343
339
  _b);
344
- routesByLength = (_c = {},
345
- _c[inputCoinAddress] = [{ steps: [], minTvl: Infinity, totalTvl: 0 }],
346
- _c);
347
340
  step = 0;
348
341
  _s.label = 2;
349
342
  case 2:
350
343
  if (!(step < 4)) return [3 /*break*/, 8];
351
344
  _loop_1 = function (inCoin) {
352
- var _loop_2, _t, ALL_POOLS_4, _u, poolId, poolData;
345
+ var outCoin_1, newRoutesByTvl, newRoutesByLength, _loop_2, _t, ALL_POOLS_3, _u, poolId, poolData;
353
346
  return __generator(this, function (_v) {
354
347
  switch (_v.label) {
355
348
  case 0:
349
+ if (curve_1.curve.chainId !== 42220 && [curve_1.curve.constants.NATIVE_TOKEN.address, curve_1.curve.constants.NATIVE_TOKEN.wrappedAddress].includes(inCoin)) { // Exclude Celo
350
+ outCoin_1 = inCoin === curve_1.curve.constants.NATIVE_TOKEN.address ? curve_1.curve.constants.NATIVE_TOKEN.wrappedAddress : curve_1.curve.constants.NATIVE_TOKEN.address;
351
+ newRoutesByTvl = routesByTvl[inCoin].map(function (route) { return getNewRoute(route, "wrapper", curve_1.curve.constants.NATIVE_TOKEN.wrappedAddress, inCoin, outCoin_1, 0, 0, 15, ethers_1.ethers.constants.AddressZero, Infinity); });
352
+ newRoutesByLength = routesByLength[inCoin].map(function (route) { return getNewRoute(route, "wrapper", curve_1.curve.constants.NATIVE_TOKEN.wrappedAddress, inCoin, outCoin_1, 0, 0, 15, ethers_1.ethers.constants.AddressZero, Infinity); });
353
+ routesByTvl[outCoin_1] = __spreadArray(__spreadArray([], ((_c = routesByTvl[outCoin_1]) !== null && _c !== void 0 ? _c : []), true), newRoutesByTvl, true);
354
+ routesByTvl[outCoin_1] = filterRoutes(routesByTvl[outCoin_1], inputCoinAddress, sortByTvl);
355
+ routesByLength[outCoin_1] = __spreadArray(__spreadArray([], ((_d = routesByLength[outCoin_1]) !== null && _d !== void 0 ? _d : []), true), newRoutesByLength, true);
356
+ routesByLength[outCoin_1] = filterRoutes(routesByLength[outCoin_1], inputCoinAddress, sortByLength);
357
+ nextCoins.add(outCoin_1);
358
+ }
356
359
  _loop_2 = function (poolId, poolData) {
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;
360
+ var wrapped_coin_addresses, underlying_coin_addresses, base_pool, meta_coin_addresses, token_address, is_aave_like_lending, tvlMultiplier, inCoinIndexes, tvl, _w, poolAddress, coin_addresses, _loop_3, j, inCoinIndex, swapType_1, newRoutesByTvl, newRoutesByLength, _loop_4, j, _loop_5, j;
358
361
  return __generator(this, function (_x) {
359
362
  switch (_x.label) {
360
363
  case 0:
@@ -363,7 +366,7 @@ var _findAllRoutesTvl = function (inputCoinAddress, outputCoinAddress) { return
363
366
  base_pool = poolData.is_meta ? curve_1.curve.constants.POOLS_DATA[poolData.base_pool] : null;
364
367
  meta_coin_addresses = base_pool ? base_pool.underlying_coin_addresses.map(function (a) { return a.toLowerCase(); }) : [];
365
368
  token_address = poolData.token_address.toLowerCase();
366
- is_lending = (_d = poolData.is_lending) !== null && _d !== void 0 ? _d : false;
369
+ is_aave_like_lending = poolData.is_lending && wrapped_coin_addresses.length === 3 && !poolData.deposit_address;
367
370
  tvlMultiplier = poolData.is_crypto ? 1 : ((_e = amplificationCoefficientDict[poolData.swap_address]) !== null && _e !== void 0 ? _e : 1);
368
371
  inCoinIndexes = {
369
372
  wrapped_coin: wrapped_coin_addresses.indexOf(inCoin),
@@ -380,36 +383,41 @@ var _findAllRoutesTvl = function (inputCoinAddress, outputCoinAddress) { return
380
383
  // Skip empty pools
381
384
  if (tvl === 0)
382
385
  return [2 /*return*/, "continue"];
383
- // LP -> wrapped coin "swaps" (actually remove_liquidity_one_coin)
384
- if (basePoolIds.includes(poolId) && inCoin === token_address) {
386
+ poolAddress = poolData.is_fake ? poolData.deposit_address : poolData.swap_address;
387
+ coin_addresses = (is_aave_like_lending || poolData.is_fake) ? underlying_coin_addresses : wrapped_coin_addresses;
388
+ // LP -> wrapped coin (underlying for lending or fake pool) "swaps" (actually remove_liquidity_one_coin)
389
+ if (coin_addresses.length < 6 && inCoin === token_address) {
385
390
  _loop_3 = function (j) {
386
391
  // Looking for outputCoinAddress only on the final step
387
- if (step === 3 && wrapped_coin_addresses[j] !== outputCoinAddress)
392
+ if (step === 3 && coin_addresses[j] !== outputCoinAddress)
388
393
  return "continue";
389
394
  // Exclude such cases as cvxeth -> tricrypto2 -> tusd -> susd or cvxeth -> tricrypto2 -> susd -> susd
390
- var outputCoinIdx = wrapped_coin_addresses.indexOf(outputCoinAddress);
395
+ var outputCoinIdx = coin_addresses.indexOf(outputCoinAddress);
391
396
  if (outputCoinIdx >= 0 && j !== outputCoinIdx)
392
397
  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]);
398
+ var swapType = poolData.is_crypto ? 14 : is_aave_like_lending ? 13 : 12;
399
+ var newRoutesByTvl = routesByTvl[inCoin].map(function (route) { return getNewRoute(route, poolId, poolAddress, inCoin, coin_addresses[j], 0, j, swapType, ethers_1.ethers.constants.AddressZero, tvl); });
400
+ var newRoutesByLength = routesByLength[inCoin].map(function (route) { return getNewRoute(route, poolId, poolAddress, inCoin, coin_addresses[j], 0, j, swapType, ethers_1.ethers.constants.AddressZero, tvl); });
401
+ routesByTvl[coin_addresses[j]] = __spreadArray(__spreadArray([], ((_f = routesByTvl[coin_addresses[j]]) !== null && _f !== void 0 ? _f : []), true), newRoutesByTvl, true);
402
+ routesByTvl[coin_addresses[j]] = filterRoutes(routesByTvl[coin_addresses[j]], inputCoinAddress, sortByTvl);
403
+ routesByLength[coin_addresses[j]] = __spreadArray(__spreadArray([], ((_h = routesByLength[coin_addresses[j]]) !== null && _h !== void 0 ? _h : []), true), newRoutesByLength, true);
404
+ routesByLength[coin_addresses[j]] = filterRoutes(routesByLength[coin_addresses[j]], inputCoinAddress, sortByLength);
405
+ nextCoins.add(coin_addresses[j]);
401
406
  };
402
- for (j = 0; j < wrapped_coin_addresses.length; j++) {
407
+ for (j = 0; j < coin_addresses.length; j++) {
403
408
  _loop_3(j);
404
409
  }
405
410
  }
406
- // Wrapped coin -> LP "swaps" (actually add_liquidity)
407
- if (basePoolIds.includes(poolId) && inCoinIndexes.wrapped_coin >= 0) {
411
+ inCoinIndex = (is_aave_like_lending || poolData.is_fake) ? inCoinIndexes.underlying_coin : inCoinIndexes.wrapped_coin;
412
+ if (coin_addresses.length < 6 && inCoinIndex >= 0) {
408
413
  // Looking for outputCoinAddress only on the final step
409
414
  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); });
415
+ swapType_1 = is_aave_like_lending ? 9
416
+ : coin_addresses.length === 2 ? 7
417
+ : coin_addresses.length === 3 ? 8
418
+ : coin_addresses.length === 4 ? 10 : 11;
419
+ newRoutesByTvl = routesByTvl[inCoin].map(function (route) { return getNewRoute(route, poolId, poolAddress, inCoin, token_address, coin_addresses.indexOf(inCoin), 0, swapType_1, ethers_1.ethers.constants.AddressZero, tvl); });
420
+ newRoutesByLength = routesByLength[inCoin].map(function (route) { return getNewRoute(route, poolId, poolAddress, inCoin, token_address, coin_addresses.indexOf(inCoin), 0, swapType_1, ethers_1.ethers.constants.AddressZero, tvl); });
413
421
  routesByTvl[token_address] = __spreadArray(__spreadArray([], ((_j = routesByTvl[token_address]) !== null && _j !== void 0 ? _j : []), true), newRoutesByTvl, true);
414
422
  routesByTvl[token_address] = filterRoutes(routesByTvl[token_address], inputCoinAddress, sortByTvl);
415
423
  routesByLength[token_address] = __spreadArray(__spreadArray([], ((_k = routesByLength[token_address]) !== null && _k !== void 0 ? _k : []), true), newRoutesByLength, true);
@@ -445,6 +453,7 @@ var _findAllRoutesTvl = function (inputCoinAddress, outputCoinAddress) { return
445
453
  _loop_4(j);
446
454
  }
447
455
  }
456
+ // Only for underlying swaps
448
457
  poolAddress = (poolData.is_crypto && poolData.is_meta) || ((base_pool === null || base_pool === void 0 ? void 0 : base_pool.is_lending) && poolData.is_factory) ?
449
458
  poolData.deposit_address : poolData.swap_address;
450
459
  if (!(!poolData.is_plain && inCoinIndexes.underlying_coin >= 0)) return [3 /*break*/, 5];
@@ -502,11 +511,11 @@ var _findAllRoutesTvl = function (inputCoinAddress, outputCoinAddress) { return
502
511
  }
503
512
  });
504
513
  };
505
- _t = 0, ALL_POOLS_4 = ALL_POOLS;
514
+ _t = 0, ALL_POOLS_3 = ALL_POOLS;
506
515
  _v.label = 1;
507
516
  case 1:
508
- if (!(_t < ALL_POOLS_4.length)) return [3 /*break*/, 4];
509
- _u = ALL_POOLS_4[_t], poolId = _u[0], poolData = _u[1];
517
+ if (!(_t < ALL_POOLS_3.length)) return [3 /*break*/, 4];
518
+ _u = ALL_POOLS_3[_t], poolId = _u[0], poolData = _u[1];
510
519
  return [5 /*yield**/, _loop_2(poolId, poolData)];
511
520
  case 2:
512
521
  _v.sent();
@@ -518,17 +527,17 @@ var _findAllRoutesTvl = function (inputCoinAddress, outputCoinAddress) { return
518
527
  }
519
528
  });
520
529
  };
521
- _a = 0, curCoins_2 = curCoins;
530
+ _i = 0, curCoins_2 = curCoins;
522
531
  _s.label = 3;
523
532
  case 3:
524
- if (!(_a < curCoins_2.length)) return [3 /*break*/, 6];
525
- inCoin = curCoins_2[_a];
533
+ if (!(_i < curCoins_2.length)) return [3 /*break*/, 6];
534
+ inCoin = curCoins_2[_i];
526
535
  return [5 /*yield**/, _loop_1(inCoin)];
527
536
  case 4:
528
537
  _s.sent();
529
538
  _s.label = 5;
530
539
  case 5:
531
- _a++;
540
+ _i++;
532
541
  return [3 /*break*/, 3];
533
542
  case 6:
534
543
  curCoins = Array.from(nextCoins);
@@ -545,16 +554,10 @@ var _findAllRoutesTvl = function (inputCoinAddress, outputCoinAddress) { return
545
554
  }); };
546
555
  exports._findAllRoutesTvl = _findAllRoutesTvl;
547
556
  var _findAllRoutes = function (inputCoinAddress, outputCoinAddress) { return __awaiter(void 0, void 0, void 0, function () {
548
- var routes;
549
557
  return __generator(this, function (_a) {
550
558
  switch (_a.label) {
551
559
  case 0: return [4 /*yield*/, (0, exports._findAllRoutesTvl)(inputCoinAddress, outputCoinAddress)];
552
- case 1:
553
- routes = _a.sent();
554
- if (routes.length > 0)
555
- return [2 /*return*/, routes];
556
- return [4 /*yield*/, (0, exports._findAllRoutesTheShorterTheBetter)(inputCoinAddress, outputCoinAddress)];
557
- case 2: return [2 /*return*/, _a.sent()];
560
+ case 1: return [2 /*return*/, _a.sent()];
558
561
  }
559
562
  });
560
563
  }); };
@@ -711,7 +714,14 @@ var _getBestRouteAndOutput = (0, memoizee_1.default)(function (inputCoinAddress,
711
714
  route.outputUsd = expectedAmountsUsd[i];
712
715
  route.txCostUsd = txCostsUsd[i];
713
716
  });
714
- return [2 /*return*/, routes.reduce(function (route1, route2) { return (route1.outputUsd - route1.txCostUsd) - (route2.outputUsd - route2.txCostUsd) >= 0 ? route1 : route2; })];
717
+ return [2 /*return*/, routes.reduce(function (route1, route2) {
718
+ var diff = (route1.outputUsd - route1.txCostUsd) - (route2.outputUsd - route2.txCostUsd);
719
+ if (diff > 0)
720
+ return route1;
721
+ if (diff === 0 && route1.steps.length < route2.steps.length)
722
+ return route1;
723
+ return route2;
724
+ })];
715
725
  }
716
726
  });
717
727
  }); }, {
@@ -849,9 +859,25 @@ var swap = function (inputCoin, outputCoin, amount, slippage) {
849
859
  case 4:
850
860
  gasLimit = (_d.sent()).mul(curve_1.curve.chainId === 1 ? 130 : 160).div(100);
851
861
  return [4 /*yield*/, contract.exchange_multiple(_route, _swapParams, _amount, _minRecvAmount, _factorySwapAddresses, __assign(__assign({}, curve_1.curve.options), { value: value, gasLimit: gasLimit }))];
852
- case 5: return [2 /*return*/, (_d.sent()).hash];
862
+ case 5: return [2 /*return*/, _d.sent()];
853
863
  }
854
864
  });
855
865
  });
856
866
  };
857
867
  exports.swap = swap;
868
+ var getSwappedAmount = function (tx, outputCoin) { return __awaiter(void 0, void 0, void 0, function () {
869
+ var outputCoinAddress, outputCoinDecimals, txInfo, res;
870
+ return __generator(this, function (_a) {
871
+ switch (_a.label) {
872
+ case 0:
873
+ outputCoinAddress = (0, utils_1._getCoinAddresses)(outputCoin)[0];
874
+ outputCoinDecimals = (0, utils_1._getCoinDecimals)(outputCoinAddress)[0];
875
+ return [4 /*yield*/, tx.wait()];
876
+ case 1:
877
+ txInfo = _a.sent();
878
+ res = ethers_1.ethers.utils.defaultAbiCoder.decode(['address[9]', 'uint256[3][4]', 'address[4]', 'uint256', 'uint256'], ethers_1.ethers.utils.hexDataSlice(txInfo.logs[txInfo.logs.length - 1].data, 0));
879
+ return [2 /*return*/, ethers_1.ethers.utils.formatUnits(res[res.length - 1], outputCoinDecimals)];
880
+ }
881
+ });
882
+ }); };
883
+ exports.getSwappedAmount = getSwappedAmount;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@curvefi/api",
3
- "version": "2.22.0",
3
+ "version": "2.24.0",
4
4
  "description": "JavaScript library for curve.fi",
5
5
  "main": "lib/index.js",
6
6
  "author": "Macket",