@curvefi/api 2.12.1 → 2.13.1

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
@@ -62,7 +62,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
62
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;
63
63
  var axios_1 = __importDefault(require("axios"));
64
64
  var memoizee_1 = __importDefault(require("memoizee"));
65
- var bignumber_js_1 = __importDefault(require("bignumber.js"));
66
65
  var ethers_1 = require("ethers");
67
66
  var curve_1 = require("./curve");
68
67
  var utils_1 = require("./utils");
@@ -126,7 +125,7 @@ var _findAllRoutesTheShorterTheBetter = function (inputCoinAddress, outputCoinAd
126
125
  // Looking for outputCoinAddress only on the final step
127
126
  if (step === 3 && underlying_coin_addresses[j] !== outputCoinAddress)
128
127
  continue;
129
- swapType = poolId === 'aave' ? 10 : 9;
128
+ swapType = poolId === 'aave' ? 11 : 10;
130
129
  for (_d = 0, _e = routes[inCoin]; _d < _e.length; _d++) {
131
130
  inCoinRoute = _e[_d];
132
131
  routes[underlying_coin_addresses[j]] = ((_s = routes[underlying_coin_addresses[j]]) !== null && _s !== void 0 ? _s : []).concat([__spreadArray(__spreadArray([], inCoinRoute, true), [
@@ -153,7 +152,7 @@ var _findAllRoutesTheShorterTheBetter = function (inputCoinAddress, outputCoinAd
153
152
  // Looking for outputCoinAddress only on the final step
154
153
  if (step === 3 && token_address !== outputCoinAddress)
155
154
  return [3 /*break*/, 11];
156
- swapType = is_lending ? 8 : underlying_coin_addresses.length === 2 ? 6 : 7;
155
+ swapType = is_lending ? 9 : underlying_coin_addresses.length === 2 ? 7 : 8;
157
156
  for (_f = 0, _h = routes[inCoin]; _f < _h.length; _f++) {
158
157
  inCoinRoute = _h[_f];
159
158
  routes[token_address] = ((_t = routes[token_address]) !== null && _t !== void 0 ? _t : []).concat([__spreadArray(__spreadArray([], inCoinRoute, true), [
@@ -239,7 +238,11 @@ var _findAllRoutesTheShorterTheBetter = function (inputCoinAddress, outputCoinAd
239
238
  if (tvl === 0)
240
239
  return [3 /*break*/, 10];
241
240
  hasEth = (inCoin === curve_1.curve.constants.NATIVE_TOKEN.address || underlying_coin_addresses[j] === curve_1.curve.constants.NATIVE_TOKEN.address);
242
- swapType = ((base_pool === null || base_pool === void 0 ? void 0 : base_pool.is_lending) && poolData.is_factory) ? 5 : hasEth ? 3 : poolData.is_crypto ? 4 : 2;
241
+ swapType = (poolData.is_crypto && poolData.is_meta && poolData.is_factory) ? 6
242
+ : ((base_pool === null || base_pool === void 0 ? void 0 : base_pool.is_lending) && poolData.is_factory) ? 5
243
+ : hasEth ? 3
244
+ : poolData.is_crypto ? 4
245
+ : 2;
243
246
  for (_o = 0, _p = routes[inCoin]; _o < _p.length; _o++) {
244
247
  inCoinRoute = _p[_o];
245
248
  routes[underlying_coin_addresses[j]] = ((_v = routes[underlying_coin_addresses[j]]) !== null && _v !== void 0 ? _v : []).concat([__spreadArray(__spreadArray([], inCoinRoute, true), [
@@ -251,7 +254,7 @@ var _findAllRoutesTheShorterTheBetter = function (inputCoinAddress, outputCoinAd
251
254
  i: inCoinIndexes.underlying_coin,
252
255
  j: j,
253
256
  swapType: swapType,
254
- swapAddress: ethers_1.ethers.constants.AddressZero,
257
+ swapAddress: (swapType === 5 || swapType === 6) ? poolData.swap_address : ethers_1.ethers.constants.AddressZero,
255
258
  },
256
259
  ], false)]);
257
260
  }
@@ -349,7 +352,7 @@ var _findAllRoutesTvl = function (inputCoinAddress, outputCoinAddress) { return
349
352
  return [4 /*yield*/, ((0, pools_1.getPool)(poolId)).stats.totalLiquidity()];
350
353
  case 1:
351
354
  tvl = _q.apply(void 0, [_r.sent()]);
352
- swapType = poolId === 'aave' ? 10 : 9;
355
+ swapType = poolId === 'aave' ? 11 : 10;
353
356
  newRoutes = routes[inCoin].map(function (route) {
354
357
  var routePoolIds = route.steps.map(function (s) { return s.poolId; });
355
358
  // Steps <= 4
@@ -406,7 +409,7 @@ var _findAllRoutesTvl = function (inputCoinAddress, outputCoinAddress) { return
406
409
  return [4 /*yield*/, ((0, pools_1.getPool)(poolId)).stats.totalLiquidity()];
407
410
  case 5:
408
411
  tvl_1 = _o.apply(void 0, [_p.sent()]);
409
- swapType_1 = is_lending ? 8 : underlying_coin_addresses.length === 2 ? 6 : 7;
412
+ swapType_1 = is_lending ? 9 : underlying_coin_addresses.length === 2 ? 7 : 8;
410
413
  newRoutes = routes[inCoin].map(function (route) {
411
414
  var routePoolIds = route.steps.map(function (s) { return s.poolId; });
412
415
  // Steps <= 4
@@ -540,7 +543,11 @@ var _findAllRoutesTvl = function (inputCoinAddress, outputCoinAddress) { return
540
543
  if (tvl === 0)
541
544
  return [2 /*return*/, "continue"];
542
545
  hasEth = (inCoin === curve_1.curve.constants.NATIVE_TOKEN.address || underlying_coin_addresses[j] === curve_1.curve.constants.NATIVE_TOKEN.address);
543
- swapType = ((base_pool === null || base_pool === void 0 ? void 0 : base_pool.is_lending) && poolData.is_factory) ? 5 : hasEth ? 3 : poolData.is_crypto ? 4 : 2;
546
+ swapType = (poolData.is_crypto && poolData.is_meta && poolData.is_factory) ? 6
547
+ : ((base_pool === null || base_pool === void 0 ? void 0 : base_pool.is_lending) && poolData.is_factory) ? 5
548
+ : hasEth ? 3
549
+ : poolData.is_crypto ? 4
550
+ : 2;
544
551
  newRoutes = routes[inCoin].map(function (route) {
545
552
  var routePoolIds = route.steps.map(function (s) { return s.poolId; });
546
553
  // Steps <= 4
@@ -559,7 +566,7 @@ var _findAllRoutesTvl = function (inputCoinAddress, outputCoinAddress) { return
559
566
  i: inCoinIndexes.underlying_coin,
560
567
  j: j,
561
568
  swapType: swapType,
562
- swapAddress: swapType === 5 ? poolData.swap_address : ethers_1.ethers.constants.AddressZero,
569
+ swapAddress: (swapType === 5 || swapType === 6) ? poolData.swap_address : ethers_1.ethers.constants.AddressZero,
563
570
  },
564
571
  ], false),
565
572
  minTvl: Math.min(tvl, route.minTvl),
@@ -831,7 +838,7 @@ var swapExpected = function (inputCoin, outputCoin, amount) { return __awaiter(v
831
838
  }); };
832
839
  exports.swapExpected = swapExpected;
833
840
  var swapPriceImpact = function (inputCoin, outputCoin, amount) { return __awaiter(void 0, void 0, void 0, function () {
834
- var _a, inputCoinAddress, outputCoinAddress, _b, inputCoinDecimals, outputCoinDecimals, route, target, amountIntBN, outputIntBN, k, smallAmountIntBN, contract, _smallAmount, _c, _route, _swapParams, _factorySwapAddresses, _smallOutput, amountBN, outputBN, smallAmountBN, smallOutputBN, rateBN, smallRateBN, slippageBN;
841
+ var _a, inputCoinAddress, outputCoinAddress, _b, inputCoinDecimals, outputCoinDecimals, route, _amount, _output, smallAmountIntBN, amountIntBN, contract, _smallAmount, _c, _route, _swapParams, _factorySwapAddresses, _smallOutput, priceImpactBN;
835
842
  return __generator(this, function (_d) {
836
843
  switch (_d.label) {
837
844
  case 0:
@@ -840,27 +847,20 @@ var swapPriceImpact = function (inputCoin, outputCoin, amount) { return __awaite
840
847
  return [4 /*yield*/, _getBestRouteAndOutput(inputCoinAddress, outputCoinAddress, amount)];
841
848
  case 1:
842
849
  route = _d.sent();
843
- target = (0, utils_1.BN)(Math.pow(10, 15));
844
- amountIntBN = (0, utils_1.BN)(amount).times(Math.pow(10, inputCoinDecimals));
845
- outputIntBN = (0, utils_1.toBN)(route._output, 0);
846
- k = bignumber_js_1.default.min(bignumber_js_1.default.max(target.div(amountIntBN), target.div(outputIntBN)), 0.2);
847
- smallAmountIntBN = bignumber_js_1.default.min(amountIntBN.times(k), (0, utils_1.BN)(Math.pow(10, inputCoinDecimals)));
848
- if (smallAmountIntBN.toFixed(0) === '0')
849
- return [2 /*return*/, '0'];
850
+ _amount = (0, utils_1.parseUnits)(amount, inputCoinDecimals);
851
+ _output = route._output;
852
+ smallAmountIntBN = (0, utils_1._get_small_x)(_amount, _output, inputCoinDecimals, outputCoinDecimals);
853
+ amountIntBN = (0, utils_1.toBN)(_amount, 0);
854
+ if (smallAmountIntBN.gte(amountIntBN))
855
+ return [2 /*return*/, 0];
850
856
  contract = curve_1.curve.contracts[curve_1.curve.constants.ALIASES.registry_exchange].contract;
851
857
  _smallAmount = (0, utils_1.fromBN)(smallAmountIntBN.div(Math.pow(10, inputCoinDecimals)), inputCoinDecimals);
852
858
  _c = _getExchangeMultipleArgs(inputCoinAddress, route), _route = _c._route, _swapParams = _c._swapParams, _factorySwapAddresses = _c._factorySwapAddresses;
853
859
  return [4 /*yield*/, contract.get_exchange_multiple_amount(_route, _swapParams, _smallAmount, _factorySwapAddresses, curve_1.curve.constantOptions)];
854
860
  case 2:
855
861
  _smallOutput = _d.sent();
856
- amountBN = (0, utils_1.BN)(amount);
857
- outputBN = (0, utils_1.toBN)(route._output, outputCoinDecimals);
858
- smallAmountBN = (0, utils_1.toBN)(_smallAmount, inputCoinDecimals);
859
- smallOutputBN = (0, utils_1.toBN)(_smallOutput, outputCoinDecimals);
860
- rateBN = outputBN.div(amountBN);
861
- smallRateBN = smallOutputBN.div(smallAmountBN);
862
- slippageBN = (0, utils_1.BN)(1).minus(rateBN.div(smallRateBN)).times(100);
863
- return [2 /*return*/, (0, utils_1._cutZeros)(slippageBN.toFixed(6)).replace('-', '')];
862
+ priceImpactBN = (0, utils_1._get_price_impact)(_amount, _output, _smallAmount, _smallOutput, inputCoinDecimals, outputCoinDecimals);
863
+ return [2 /*return*/, Number((0, utils_1._cutZeros)(priceImpactBN.toFixed(4)).replace('-', ''))];
864
864
  }
865
865
  });
866
866
  }); };
package/lib/utils.d.ts CHANGED
@@ -31,3 +31,5 @@ export declare const _getUsdRate: (assetId: string) => Promise<number>;
31
31
  export declare const getUsdRate: (coin: string) => Promise<number>;
32
32
  export declare const getTVL: (chainId?: number) => Promise<number>;
33
33
  export declare const _setContracts: (address: string, abi: any) => void;
34
+ export declare const _get_small_x: (_x: ethers.BigNumber, _y: ethers.BigNumber, x_decimals: number, y_decimals: number) => BigNumber;
35
+ export declare const _get_price_impact: (_x: ethers.BigNumber, _y: ethers.BigNumber, _small_x: ethers.BigNumber, _small_y: ethers.BigNumber, x_decimals: number, y_decimals: number) => BigNumber;
package/lib/utils.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._setContracts = exports.getTVL = exports.getUsdRate = exports._getUsdRate = exports._getUsdPricesFromApi = exports.getPoolNameBySwapAddress = exports.ensureAllowance = exports.ensureAllowanceEstimateGas = exports._ensureAllowance = exports.hasAllowance = exports.getAllowance = exports._getAllowance = exports.getBalances = exports._prepareAddresses = exports._getBalances = exports._getCoinDecimals = exports._getCoinAddresses = exports._getCoinAddressesNoCheck = exports.getEthIndex = exports.isEth = exports.parseUnits = exports.formatNumber = exports.checkNumber = exports._cutZeros = exports.fromBN = exports.toStringFromBN = exports.toBN = exports.BN = exports.MAX_ALLOWANCE = exports.ETH_ADDRESS = void 0;
62
+ exports._get_price_impact = exports._get_small_x = exports._setContracts = exports.getTVL = exports.getUsdRate = exports._getUsdRate = exports._getUsdPricesFromApi = exports.getPoolNameBySwapAddress = exports.ensureAllowance = exports.ensureAllowanceEstimateGas = exports._ensureAllowance = exports.hasAllowance = exports.getAllowance = exports._getAllowance = exports.getBalances = exports._prepareAddresses = exports._getBalances = exports._getCoinDecimals = exports._getCoinAddresses = exports._getCoinAddressesNoCheck = exports.getEthIndex = exports.isEth = exports.parseUnits = exports.formatNumber = exports.checkNumber = exports._cutZeros = exports.fromBN = exports.toStringFromBN = exports.toBN = exports.BN = exports.MAX_ALLOWANCE = exports.ETH_ADDRESS = void 0;
63
63
  var axios_1 = __importDefault(require("axios"));
64
64
  var ethers_1 = require("ethers");
65
65
  var bignumber_js_1 = __importDefault(require("bignumber.js"));
@@ -578,3 +578,25 @@ var _setContracts = function (address, abi) {
578
578
  };
579
579
  };
580
580
  exports._setContracts = _setContracts;
581
+ // Find k for which x * k = target_x or y * k = target_y
582
+ // k = max(target_x / x, target_y / y)
583
+ // small_x = x * k
584
+ var _get_small_x = function (_x, _y, x_decimals, y_decimals) {
585
+ var target_x = (0, exports.BN)(Math.pow(10, (x_decimals > 5 ? x_decimals - 3 : x_decimals)));
586
+ var target_y = (0, exports.BN)(Math.pow(10, (y_decimals > 5 ? y_decimals - 3 : y_decimals)));
587
+ var x_int_BN = (0, exports.toBN)(_x, 0);
588
+ var y_int_BN = (0, exports.toBN)(_y, 0);
589
+ var k = bignumber_js_1.default.max(target_x.div(x_int_BN), target_y.div(y_int_BN));
590
+ return bignumber_js_1.default.min(x_int_BN.times(k), (0, exports.BN)(Math.pow(10, x_decimals)));
591
+ };
592
+ exports._get_small_x = _get_small_x;
593
+ var _get_price_impact = function (_x, _y, _small_x, _small_y, x_decimals, y_decimals) {
594
+ var x_BN = (0, exports.toBN)(_x, x_decimals);
595
+ var y_BN = (0, exports.toBN)(_y, y_decimals);
596
+ var small_x_BN = (0, exports.toBN)(_small_x, x_decimals);
597
+ var small_y_BN = (0, exports.toBN)(_small_y, y_decimals);
598
+ var rateBN = y_BN.div(x_BN);
599
+ var smallRateBN = small_y_BN.div(small_x_BN);
600
+ return (0, exports.BN)(1).minus(rateBN.div(smallRateBN)).times(100);
601
+ };
602
+ exports._get_price_impact = _get_price_impact;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@curvefi/api",
3
- "version": "2.12.1",
3
+ "version": "2.13.1",
4
4
  "description": "JavaScript library for curve.fi",
5
5
  "main": "lib/index.js",
6
6
  "author": "Macket",