@curvefi/api 2.0.0 → 2.1.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/README.md CHANGED
@@ -126,6 +126,13 @@ const WalletProvider: FunctionComponent = ({ children }) => {
126
126
  ...
127
127
  ```
128
128
 
129
+ ## Notes
130
+ - 1 Amounts can be passed in args either as numbers or strings.
131
+ - 2 depositOrWithdraw**Bonus** and swap**PriceImpact** methods return %, e. g. 0 < bonus/priceImpact <= 100
132
+ - 3 Slippage arg should be passed as %, e. g. 0 < slippage <= 100
133
+
134
+
135
+
129
136
  ## General methods
130
137
  ```ts
131
138
  import curve from "@curvefi/api";
@@ -814,6 +821,11 @@ import curve from "@curvefi/api";
814
821
  // OR const underlyingExpected = await pool.swapExpected(0, 1, '10');
815
822
  console.log(underlyingExpected);
816
823
  // 9.984619933234026875
824
+ const underlyingPriceImpact = await pool.swapPriceImpact('MIM','DAI', 10);
825
+ // OR const underlyingPriceImpact = await pool.swapPriceImpact('0x99d8a9c45b2eca8864373a26d1459e3dff1e17f3', '0x6B175474E89094C44Da98b954EedeAC495271d0F', '10');
826
+ // OR const underlyingPriceImpact = await pool.swapPriceImpact(0, 1, '10');
827
+ console.log(underlyingPriceImpact);
828
+ // 0.000026 (as %)
817
829
  await pool.swapIsApproved('MIM', 10);
818
830
  // true
819
831
  await pool.swapApprove('MIM', 10);
@@ -846,6 +858,11 @@ import curve from "@curvefi/api";
846
858
  // OR const wrappedExpected = await pool.swapWrappedExpected(1, 0, '10');
847
859
  console.log(wrappedExpected);
848
860
  // 10.217756467720521951
861
+ const wrappedPriceImpact = await pool.swapWrappedPriceImpact('3crv','MIM', 10);
862
+ // OR const wrappedPriceImpact = await pool.swapWrappedPriceImpact('0x6c3F90f043a72FA612cbac8115EE7e52BDe6E490', '0x99d8a9c45b2eca8864373a26d1459e3dff1e17f3', '10');
863
+ // OR const wrappedPriceImpact = await pool.swapWrappedPriceImpact(1, 0, '10');
864
+ console.log(wrappedPriceImpact);
865
+ // 0.000081 (as %)
849
866
  await pool.swapWrappedIsApproved('3crv', 10);
850
867
  // true
851
868
  await pool.swapWrappedApprove('3crv', 10);
@@ -868,7 +885,7 @@ import curve from "@curvefi/api";
868
885
  (async () => {
869
886
  await curve.init('JsonRpc', {}, { gasPrice: 0, maxFeePerGas: 0, maxPriorityFeePerGas: 0 });
870
887
 
871
- const pool = new curve.Pool('compound');
888
+ const pool = curve.getPool('compound');
872
889
  const amounts = [1000, 1000];
873
890
 
874
891
 
@@ -918,7 +935,7 @@ import curve from "@curvefi/api";
918
935
 
919
936
  await pool.depositAndStakeWrappedExpected(amounts);
920
937
  // 40.328408669183101673
921
- await pool.depositAndStakeWrappedPriceImpact(amounts);
938
+ await pool.depositAndStakeWrappedBonus(amounts);
922
939
  // 0.46040576921447873
923
940
  await pool.depositAndStakeWrappedIsApproved(amounts);
924
941
  // false
@@ -951,11 +968,13 @@ import curve from "@curvefi/api";
951
968
  // [ '9900.0', '100049.744832225238317557' ]
952
969
 
953
970
  const { route, output } = await curve.router.getBestRouteAndOutput('DAI', 'CRV', '1000');
954
- // OR await curve.router.getBestPoolAndOutput('0x6B175474E89094C44Da98b954EedeAC495271d0F', '0xD533a949740bb3306d119CC777fa900bA034cd52', '10000');
971
+ // OR await curve.router.getBestPoolAndOutput('0x6B175474E89094C44Da98b954EedeAC495271d0F', '0xD533a949740bb3306d119CC777fa900bA034cd52', '1000');
955
972
  const expected = await curve.router.expected('DAI', 'CRV', '1000');
956
- // OR await curve.router.expected('0x6B175474E89094C44Da98b954EedeAC495271d0F', '0xD533a949740bb3306d119CC777fa900bA034cd52', '10000');
973
+ // OR await curve.router.expected('0x6B175474E89094C44Da98b954EedeAC495271d0F', '0xD533a949740bb3306d119CC777fa900bA034cd52', '1000');
974
+ const priceImpact = await curve.router.priceImpact('DAI', 'CRV', '1000');
975
+ // OR await curve.router.priceImpact('0x6B175474E89094C44Da98b954EedeAC495271d0F', '0xD533a949740bb3306d119CC777fa900bA034cd52', '1000');
957
976
 
958
- route, output, expected;
977
+ console.log(route, output, expected, priceImpact);
959
978
  // route = [
960
979
  // {
961
980
  // poolId: '3pool',
@@ -987,7 +1006,9 @@ import curve from "@curvefi/api";
987
1006
  // ]
988
1007
  //
989
1008
  // output = expected = 378.881631202862354937
990
-
1009
+ //
1010
+ // priceImpact = 0.158012 %
1011
+
991
1012
  await curve.router.isApproved('DAI', 1000);
992
1013
  // false
993
1014
  await curve.router.approve('DAI', 1000);
@@ -995,7 +1016,7 @@ import curve from "@curvefi/api";
995
1016
  // '0xc111e471715ae6f5437e12d3b94868a5b6542cd7304efca18b5782d315760ae5'
996
1017
  // ]
997
1018
  const swapTx = await curve.router.swap('DAI', 'CRV', '1000');
998
- // OR const swapTx = await curve.router.swap('0x6B175474E89094C44Da98b954EedeAC495271d0F', '0xD533a949740bb3306d119CC777fa900bA034cd52', '10000');
1019
+ // OR const swapTx = await curve.router.swap('0x6B175474E89094C44Da98b954EedeAC495271d0F', '0xD533a949740bb3306d119CC777fa900bA034cd52', '1000');
999
1020
  console.log(swapTx);
1000
1021
  // 0xc7ba1d60871c0295ac5471bb602c37ec0f00a71543b3a041308ebd91833f26ba
1001
1022
 
package/lib/curve.js CHANGED
@@ -420,7 +420,7 @@ var Curve = /** @class */ (function () {
420
420
  _e.label = 4;
421
421
  case 4:
422
422
  this.constants.DECIMALS = __assign(__assign({}, this.constants.DECIMALS), (0, utils_1.extractDecimals)(this.constants.FACTORY_POOLS_DATA));
423
- this.constants.GAUGES = __assign(__assign({}, this.constants.GAUGES), (0, utils_1.extractGauges)(this.constants.FACTORY_POOLS_DATA));
423
+ this.constants.GAUGES = __spreadArray(__spreadArray([], this.constants.GAUGES, true), (0, utils_1.extractGauges)(this.constants.FACTORY_POOLS_DATA), true);
424
424
  return [2 /*return*/];
425
425
  }
426
426
  });
@@ -451,7 +451,7 @@ var Curve = /** @class */ (function () {
451
451
  _e.label = 4;
452
452
  case 4:
453
453
  this.constants.DECIMALS = __assign(__assign({}, this.constants.DECIMALS), (0, utils_1.extractDecimals)(this.constants.CRYPTO_FACTORY_POOLS_DATA));
454
- this.constants.GAUGES = __assign(__assign({}, this.constants.GAUGES), (0, utils_1.extractGauges)(this.constants.CRYPTO_FACTORY_POOLS_DATA));
454
+ this.constants.GAUGES = __spreadArray(__spreadArray([], this.constants.GAUGES, true), (0, utils_1.extractGauges)(this.constants.CRYPTO_FACTORY_POOLS_DATA), true);
455
455
  return [2 /*return*/];
456
456
  }
457
457
  });
package/lib/index.d.ts CHANGED
@@ -75,6 +75,7 @@ declare const curve: {
75
75
  output: string;
76
76
  }>;
77
77
  expected: (inputCoin: string, outputCoin: string, amount: string | number) => Promise<string>;
78
+ priceImpact: (inputCoin: string, outputCoin: string, amount: string | number) => Promise<string>;
78
79
  isApproved: (inputCoin: string, amount: string | number) => Promise<boolean>;
79
80
  approve: (inputCoin: string, amount: string | number) => Promise<string[]>;
80
81
  swap: (inputCoin: string, outputCoin: string, amount: string | number, slippage?: number) => Promise<string>;
package/lib/index.js CHANGED
@@ -132,6 +132,7 @@ var curve = {
132
132
  router: {
133
133
  getBestRouteAndOutput: router_1.getBestRouteAndOutput,
134
134
  expected: router_1.swapExpected,
135
+ priceImpact: router_1.swapPriceImpact,
135
136
  isApproved: router_1.swapIsApproved,
136
137
  approve: router_1.swapApprove,
137
138
  swap: router_1.swap,
@@ -184,6 +184,7 @@ export declare class PoolTemplate {
184
184
  private walletAllCoinBalances;
185
185
  private _swapExpected;
186
186
  swapExpected(inputCoin: string | number, outputCoin: string | number, amount: number | string): Promise<string>;
187
+ swapPriceImpact(inputCoin: string | number, outputCoin: string | number, amount: number | string): Promise<string>;
187
188
  private _swapContractAddress;
188
189
  swapIsApproved(inputCoin: string | number, amount: number | string): Promise<boolean>;
189
190
  private swapApproveEstimateGas;
@@ -192,6 +193,7 @@ export declare class PoolTemplate {
192
193
  swap(inputCoin: string | number, outputCoin: string | number, amount: number | string, slippage?: number): Promise<string>;
193
194
  private _swapWrappedExpected;
194
195
  swapWrappedExpected(inputCoin: string | number, outputCoin: string | number, amount: number | string): Promise<string>;
196
+ swapWrappedPriceImpact(inputCoin: string | number, outputCoin: string | number, amount: number | string): Promise<string>;
195
197
  swapWrappedIsApproved(inputCoin: string | number, amount: number | string): Promise<boolean>;
196
198
  private swapWrappedApproveEstimateGas;
197
199
  swapWrappedApprove(inputCoin: string | number, amount: number | string): Promise<string[]>;
@@ -55,9 +55,13 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
55
55
  }
56
56
  return to.concat(ar || Array.prototype.slice.call(from));
57
57
  };
58
+ var __importDefault = (this && this.__importDefault) || function (mod) {
59
+ return (mod && mod.__esModule) ? mod : { "default": mod };
60
+ };
58
61
  Object.defineProperty(exports, "__esModule", { value: true });
59
62
  exports.PoolTemplate = void 0;
60
63
  var ethers_1 = require("ethers");
64
+ var bignumber_js_1 = __importDefault(require("bignumber.js"));
61
65
  var external_api_1 = require("../external-api");
62
66
  var utils_1 = require("../utils");
63
67
  var curve_1 = require("../curve");
@@ -2165,6 +2169,42 @@ var PoolTemplate = /** @class */ (function () {
2165
2169
  });
2166
2170
  });
2167
2171
  };
2172
+ PoolTemplate.prototype.swapPriceImpact = function (inputCoin, outputCoin, amount) {
2173
+ return __awaiter(this, void 0, void 0, function () {
2174
+ var i, j, _a, inputCoinDecimals, outputCoinDecimals, _amount, _output, target, amountIntBN, outputIntBN, k, smallAmountIntBN, _smallAmount, _smallOutput, amountBN, outputBN, smallAmountBN, smallOutputBN, rateBN, smallRateBN, slippageBN;
2175
+ return __generator(this, function (_c) {
2176
+ switch (_c.label) {
2177
+ case 0:
2178
+ i = this._getCoinIdx(inputCoin);
2179
+ j = this._getCoinIdx(outputCoin);
2180
+ _a = [this.underlyingDecimals[i], this.underlyingDecimals[j]], inputCoinDecimals = _a[0], outputCoinDecimals = _a[1];
2181
+ _amount = (0, utils_1.parseUnits)(amount, inputCoinDecimals);
2182
+ return [4 /*yield*/, this._swapExpected(i, j, _amount)];
2183
+ case 1:
2184
+ _output = _c.sent();
2185
+ target = (0, utils_1.BN)(1000000);
2186
+ amountIntBN = (0, utils_1.BN)(amount).times(Math.pow(10, inputCoinDecimals));
2187
+ outputIntBN = (0, utils_1.toBN)(_output, 0);
2188
+ k = bignumber_js_1.default.min(bignumber_js_1.default.max(target.div(amountIntBN), target.div(outputIntBN)), 0.2);
2189
+ smallAmountIntBN = bignumber_js_1.default.min(amountIntBN.times(k), (0, utils_1.BN)(Math.pow(10, inputCoinDecimals)));
2190
+ if (smallAmountIntBN.toFixed(0) === '0')
2191
+ return [2 /*return*/, '0'];
2192
+ _smallAmount = (0, utils_1.fromBN)(smallAmountIntBN.div(Math.pow(10, inputCoinDecimals)), inputCoinDecimals);
2193
+ return [4 /*yield*/, this._swapExpected(i, j, _smallAmount)];
2194
+ case 2:
2195
+ _smallOutput = _c.sent();
2196
+ amountBN = (0, utils_1.BN)(amount);
2197
+ outputBN = (0, utils_1.toBN)(_output, outputCoinDecimals);
2198
+ smallAmountBN = (0, utils_1.toBN)(_smallAmount, inputCoinDecimals);
2199
+ smallOutputBN = (0, utils_1.toBN)(_smallOutput, outputCoinDecimals);
2200
+ rateBN = outputBN.div(amountBN);
2201
+ smallRateBN = smallOutputBN.div(smallAmountBN);
2202
+ slippageBN = (0, utils_1.BN)(1).minus(rateBN.div(smallRateBN)).times(100);
2203
+ return [2 /*return*/, (0, utils_1._cutZeros)(slippageBN.toFixed(6)).replace('-', '')];
2204
+ }
2205
+ });
2206
+ });
2207
+ };
2168
2208
  PoolTemplate.prototype._swapContractAddress = function () {
2169
2209
  return (this.isCrypto && this.isMeta) || (curve_1.curve.chainId === 137 && this.isMetaFactory) ? this.zap : this.address;
2170
2210
  };
@@ -2246,6 +2286,45 @@ var PoolTemplate = /** @class */ (function () {
2246
2286
  });
2247
2287
  });
2248
2288
  };
2289
+ PoolTemplate.prototype.swapWrappedPriceImpact = function (inputCoin, outputCoin, amount) {
2290
+ return __awaiter(this, void 0, void 0, function () {
2291
+ var i, j, _a, inputCoinDecimals, outputCoinDecimals, _amount, _output, target, amountIntBN, outputIntBN, k, smallAmountIntBN, _smallAmount, _smallOutput, amountBN, outputBN, smallAmountBN, smallOutputBN, rateBN, smallRateBN, slippageBN;
2292
+ return __generator(this, function (_c) {
2293
+ switch (_c.label) {
2294
+ case 0:
2295
+ if (this.isPlain || this.isFake) {
2296
+ throw Error("swapWrappedPriceImpact method doesn't exist for pool ".concat(this.name, " (id: ").concat(this.name, ")"));
2297
+ }
2298
+ i = this._getCoinIdx(inputCoin, false);
2299
+ j = this._getCoinIdx(outputCoin, false);
2300
+ _a = [this.wrappedDecimals[i], this.wrappedDecimals[j]], inputCoinDecimals = _a[0], outputCoinDecimals = _a[1];
2301
+ _amount = (0, utils_1.parseUnits)(amount, inputCoinDecimals);
2302
+ return [4 /*yield*/, this._swapWrappedExpected(i, j, _amount)];
2303
+ case 1:
2304
+ _output = _c.sent();
2305
+ target = (0, utils_1.BN)(1000000);
2306
+ amountIntBN = (0, utils_1.BN)(amount).times(Math.pow(10, inputCoinDecimals));
2307
+ outputIntBN = (0, utils_1.toBN)(_output, 0);
2308
+ k = bignumber_js_1.default.min(bignumber_js_1.default.max(target.div(amountIntBN), target.div(outputIntBN)), 0.2);
2309
+ smallAmountIntBN = bignumber_js_1.default.min(amountIntBN.times(k), (0, utils_1.BN)(Math.pow(10, inputCoinDecimals)));
2310
+ if (smallAmountIntBN.toFixed(0) === '0')
2311
+ return [2 /*return*/, '0'];
2312
+ _smallAmount = (0, utils_1.fromBN)(smallAmountIntBN.div(Math.pow(10, inputCoinDecimals)), inputCoinDecimals);
2313
+ return [4 /*yield*/, this._swapWrappedExpected(i, j, _smallAmount)];
2314
+ case 2:
2315
+ _smallOutput = _c.sent();
2316
+ amountBN = (0, utils_1.BN)(amount);
2317
+ outputBN = (0, utils_1.toBN)(_output, outputCoinDecimals);
2318
+ smallAmountBN = (0, utils_1.toBN)(_smallAmount, inputCoinDecimals);
2319
+ smallOutputBN = (0, utils_1.toBN)(_smallOutput, outputCoinDecimals);
2320
+ rateBN = outputBN.div(amountBN);
2321
+ smallRateBN = smallOutputBN.div(smallAmountBN);
2322
+ slippageBN = (0, utils_1.BN)(1).minus(rateBN.div(smallRateBN)).times(100);
2323
+ return [2 /*return*/, (0, utils_1._cutZeros)(slippageBN.toFixed(6)).replace('-', '')];
2324
+ }
2325
+ });
2326
+ });
2327
+ };
2249
2328
  // OVERRIDE
2250
2329
  PoolTemplate.prototype.swapWrappedIsApproved = function (inputCoin, amount) {
2251
2330
  return __awaiter(this, void 0, void 0, function () {
package/lib/router.d.ts CHANGED
@@ -5,6 +5,7 @@ export declare const getBestRouteAndOutput: (inputCoin: string, outputCoin: stri
5
5
  output: string;
6
6
  }>;
7
7
  export declare const swapExpected: (inputCoin: string, outputCoin: string, amount: number | string) => Promise<string>;
8
+ export declare const swapPriceImpact: (inputCoin: string, outputCoin: string, amount: number | string) => Promise<string>;
8
9
  export declare const swapIsApproved: (inputCoin: string, amount: number | string) => Promise<boolean>;
9
10
  export declare const swapApproveEstimateGas: (inputCoin: string, amount: number | string) => Promise<number>;
10
11
  export declare const swapApprove: (inputCoin: string, amount: number | string) => Promise<string[]>;
package/lib/router.js CHANGED
@@ -59,13 +59,15 @@ 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.swapExpected = exports.getBestRouteAndOutput = exports._findAllRoutes = void 0;
63
- var curve_1 = require("./curve");
62
+ exports.swap = exports.swapEstimateGas = exports.swapApprove = exports.swapApproveEstimateGas = exports.swapIsApproved = exports.swapPriceImpact = exports.swapExpected = exports.getBestRouteAndOutput = exports._findAllRoutes = void 0;
63
+ var axios_1 = __importDefault(require("axios"));
64
+ var memoizee_1 = __importDefault(require("memoizee"));
65
+ var bignumber_js_1 = __importDefault(require("bignumber.js"));
64
66
  var ethers_1 = require("ethers");
67
+ var curve_1 = require("./curve");
65
68
  var utils_1 = require("./utils");
66
- var memoizee_1 = __importDefault(require("memoizee"));
67
- var axios_1 = __importDefault(require("axios"));
68
69
  var pools_1 = require("./pools");
70
+ // TODO make working or remove
69
71
  var IMBALANCED_POOLS = [];
70
72
  // Inspired by Dijkstra's algorithm
71
73
  var _findAllRoutes = function (inputCoinAddress, outputCoinAddress) { return __awaiter(void 0, void 0, void 0, function () {
@@ -422,56 +424,46 @@ var _estimateGasForDifferentRoutes = function (routes, inputCoinAddress, outputC
422
424
  });
423
425
  }); };
424
426
  var _getBestRouteAndOutput = (0, memoizee_1.default)(function (inputCoinAddress, outputCoinAddress, amount) { return __awaiter(void 0, void 0, void 0, function () {
425
- var _a, inputCoinDecimals, outputCoinDecimals, _amount, routesRaw, routes, calls, promises, multicallContract, contract, _i, routesRaw_1, route, _b, _route, _swapParams, _factorySwapAddresses, calls_1, multicallContract_1, _c, routesRaw_2, route, _d, _route, _swapParams, _factorySwapAddresses, _outputAmounts, i, err_2, promises_1, contract_1, _e, routesRaw_3, route, _f, _route, _swapParams, _factorySwapAddresses, res, i, _h, gasAmounts, outputCoinUsdRate, gasData, ethUsdRate, gasPrice, expectedAmounts, expectedAmountsUsd, txCostsUsd;
426
- return __generator(this, function (_j) {
427
- switch (_j.label) {
427
+ var _a, inputCoinDecimals, outputCoinDecimals, _amount, routesRaw, routes, calls, multicallContract, _i, routesRaw_1, route, _b, _route, _swapParams, _factorySwapAddresses, _outputAmounts, i, err_2, promises, contract, _c, routesRaw_2, route, _d, _route, _swapParams, _factorySwapAddresses, res, i, _e, gasAmounts, outputCoinUsdRate, gasData, ethUsdRate, gasPrice, expectedAmounts, expectedAmountsUsd, txCostsUsd;
428
+ return __generator(this, function (_f) {
429
+ switch (_f.label) {
428
430
  case 0:
429
431
  _a = (0, utils_1._getCoinDecimals)(inputCoinAddress, outputCoinAddress), inputCoinDecimals = _a[0], outputCoinDecimals = _a[1];
430
432
  _amount = (0, utils_1.parseUnits)(amount, inputCoinDecimals);
431
433
  return [4 /*yield*/, (0, exports._findAllRoutes)(inputCoinAddress, outputCoinAddress)];
432
434
  case 1:
433
- routesRaw = (_j.sent()).map(function (steps) { return ({ steps: steps, _output: ethers_1.ethers.BigNumber.from(0), outputUsd: 0, txCostUsd: 0 }); });
435
+ routesRaw = (_f.sent()).map(function (steps) { return ({ steps: steps, _output: ethers_1.ethers.BigNumber.from(0), outputUsd: 0, txCostUsd: 0 }); });
434
436
  routes = [];
437
+ _f.label = 2;
438
+ case 2:
439
+ _f.trys.push([2, 4, , 6]);
435
440
  calls = [];
436
- promises = [];
437
441
  multicallContract = curve_1.curve.contracts[curve_1.curve.constants.ALIASES.registry_exchange].multicallContract;
438
- contract = curve_1.curve.contracts[curve_1.curve.constants.ALIASES.registry_exchange].contract;
439
442
  for (_i = 0, routesRaw_1 = routesRaw; _i < routesRaw_1.length; _i++) {
440
443
  route = routesRaw_1[_i];
441
444
  _b = _getExchangeMultipleArgs(inputCoinAddress, route), _route = _b._route, _swapParams = _b._swapParams, _factorySwapAddresses = _b._factorySwapAddresses;
442
445
  calls.push(multicallContract.get_exchange_multiple_amount(_route, _swapParams, _amount, _factorySwapAddresses));
443
- promises.push(contract.get_exchange_multiple_amount(_route, _swapParams, _amount, _factorySwapAddresses, curve_1.curve.constantOptions));
444
- }
445
- _j.label = 2;
446
- case 2:
447
- _j.trys.push([2, 4, , 6]);
448
- calls_1 = [];
449
- multicallContract_1 = curve_1.curve.contracts[curve_1.curve.constants.ALIASES.registry_exchange].multicallContract;
450
- for (_c = 0, routesRaw_2 = routesRaw; _c < routesRaw_2.length; _c++) {
451
- route = routesRaw_2[_c];
452
- _d = _getExchangeMultipleArgs(inputCoinAddress, route), _route = _d._route, _swapParams = _d._swapParams, _factorySwapAddresses = _d._factorySwapAddresses;
453
- calls_1.push(multicallContract_1.get_exchange_multiple_amount(_route, _swapParams, _amount, _factorySwapAddresses));
454
446
  }
455
- return [4 /*yield*/, curve_1.curve.multicallProvider.all(calls_1)];
447
+ return [4 /*yield*/, curve_1.curve.multicallProvider.all(calls)];
456
448
  case 3:
457
- _outputAmounts = _j.sent();
449
+ _outputAmounts = _f.sent();
458
450
  for (i = 0; i < _outputAmounts.length; i++) {
459
451
  routesRaw[i]._output = _outputAmounts[i];
460
452
  routes.push(routesRaw[i]);
461
453
  }
462
454
  return [3 /*break*/, 6];
463
455
  case 4:
464
- err_2 = _j.sent();
465
- promises_1 = [];
466
- contract_1 = curve_1.curve.contracts[curve_1.curve.constants.ALIASES.registry_exchange].contract;
467
- for (_e = 0, routesRaw_3 = routesRaw; _e < routesRaw_3.length; _e++) {
468
- route = routesRaw_3[_e];
469
- _f = _getExchangeMultipleArgs(inputCoinAddress, route), _route = _f._route, _swapParams = _f._swapParams, _factorySwapAddresses = _f._factorySwapAddresses;
470
- promises_1.push(contract_1.get_exchange_multiple_amount(_route, _swapParams, _amount, _factorySwapAddresses, curve_1.curve.constantOptions));
456
+ err_2 = _f.sent();
457
+ promises = [];
458
+ contract = curve_1.curve.contracts[curve_1.curve.constants.ALIASES.registry_exchange].contract;
459
+ for (_c = 0, routesRaw_2 = routesRaw; _c < routesRaw_2.length; _c++) {
460
+ route = routesRaw_2[_c];
461
+ _d = _getExchangeMultipleArgs(inputCoinAddress, route), _route = _d._route, _swapParams = _d._swapParams, _factorySwapAddresses = _d._factorySwapAddresses;
462
+ promises.push(contract.get_exchange_multiple_amount(_route, _swapParams, _amount, _factorySwapAddresses, curve_1.curve.constantOptions));
471
463
  }
472
- return [4 /*yield*/, Promise.allSettled(promises_1)];
464
+ return [4 /*yield*/, Promise.allSettled(promises)];
473
465
  case 5:
474
- res = _j.sent();
466
+ res = _f.sent();
475
467
  for (i = 0; i < res.length; i++) {
476
468
  if (res[i].status === 'rejected') {
477
469
  console.log("Route ".concat((routesRaw[i].steps.map(function (s) { return s.poolId; })).join(" --> "), " is anavailable"));
@@ -499,7 +491,7 @@ var _getBestRouteAndOutput = (0, memoizee_1.default)(function (inputCoinAddress,
499
491
  (0, utils_1._getUsdRate)(curve_1.curve.chainId === 137 ? curve_1.curve.constants.COINS.matic : curve_1.curve.constants.COINS.eth),
500
492
  ])];
501
493
  case 7:
502
- _h = _j.sent(), gasAmounts = _h[0], outputCoinUsdRate = _h[1], gasData = _h[2], ethUsdRate = _h[3];
494
+ _e = _f.sent(), gasAmounts = _e[0], outputCoinUsdRate = _e[1], gasData = _e[2], ethUsdRate = _e[3];
503
495
  gasPrice = gasData.data.data.gas.standard;
504
496
  expectedAmounts = (routes).map(function (route) { return Number(ethers_1.ethers.utils.formatUnits(route._output, outputCoinDecimals)); });
505
497
  expectedAmountsUsd = expectedAmounts.map(function (a) { return a * outputCoinUsdRate; });
@@ -539,6 +531,41 @@ var swapExpected = function (inputCoin, outputCoin, amount) { return __awaiter(v
539
531
  });
540
532
  }); };
541
533
  exports.swapExpected = swapExpected;
534
+ var swapPriceImpact = function (inputCoin, outputCoin, amount) { return __awaiter(void 0, void 0, void 0, function () {
535
+ 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;
536
+ return __generator(this, function (_d) {
537
+ switch (_d.label) {
538
+ case 0:
539
+ _a = (0, utils_1._getCoinAddresses)(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
540
+ _b = (0, utils_1._getCoinDecimals)(inputCoinAddress, outputCoinAddress), inputCoinDecimals = _b[0], outputCoinDecimals = _b[1];
541
+ return [4 /*yield*/, _getBestRouteAndOutput(inputCoinAddress, outputCoinAddress, amount)];
542
+ case 1:
543
+ route = _d.sent();
544
+ target = (0, utils_1.BN)(Math.pow(10, 12));
545
+ amountIntBN = (0, utils_1.BN)(amount).times(Math.pow(10, inputCoinDecimals));
546
+ outputIntBN = (0, utils_1.toBN)(route._output, 0);
547
+ k = bignumber_js_1.default.min(bignumber_js_1.default.max(target.div(amountIntBN), target.div(outputIntBN)), 0.2);
548
+ smallAmountIntBN = bignumber_js_1.default.min(amountIntBN.times(k), (0, utils_1.BN)(Math.pow(10, inputCoinDecimals)));
549
+ if (smallAmountIntBN.toFixed(0) === '0')
550
+ return [2 /*return*/, '0'];
551
+ contract = curve_1.curve.contracts[curve_1.curve.constants.ALIASES.registry_exchange].contract;
552
+ _smallAmount = (0, utils_1.fromBN)(smallAmountIntBN.div(Math.pow(10, inputCoinDecimals)), inputCoinDecimals);
553
+ _c = _getExchangeMultipleArgs(inputCoinAddress, route), _route = _c._route, _swapParams = _c._swapParams, _factorySwapAddresses = _c._factorySwapAddresses;
554
+ return [4 /*yield*/, contract.get_exchange_multiple_amount(_route, _swapParams, _smallAmount, _factorySwapAddresses, curve_1.curve.constantOptions)];
555
+ case 2:
556
+ _smallOutput = _d.sent();
557
+ amountBN = (0, utils_1.BN)(amount);
558
+ outputBN = (0, utils_1.toBN)(route._output, outputCoinDecimals);
559
+ smallAmountBN = (0, utils_1.toBN)(_smallAmount, inputCoinDecimals);
560
+ smallOutputBN = (0, utils_1.toBN)(_smallOutput, outputCoinDecimals);
561
+ rateBN = outputBN.div(amountBN);
562
+ smallRateBN = smallOutputBN.div(smallAmountBN);
563
+ slippageBN = (0, utils_1.BN)(1).minus(rateBN.div(smallRateBN)).times(100);
564
+ return [2 /*return*/, (0, utils_1._cutZeros)(slippageBN.toFixed(6)).replace('-', '')];
565
+ }
566
+ });
567
+ }); };
568
+ exports.swapPriceImpact = swapPriceImpact;
542
569
  var swapIsApproved = function (inputCoin, amount) { return __awaiter(void 0, void 0, void 0, function () {
543
570
  return __generator(this, function (_a) {
544
571
  switch (_a.label) {
package/lib/utils.d.ts CHANGED
@@ -6,6 +6,7 @@ export declare const BN: (val: number | string) => BigNumber;
6
6
  export declare const toBN: (n: ethers.BigNumber, decimals?: number) => BigNumber;
7
7
  export declare const toStringFromBN: (bn: BigNumber, decimals?: number) => string;
8
8
  export declare const fromBN: (bn: BigNumber, decimals?: number) => ethers.BigNumber;
9
+ export declare const _cutZeros: (strn: string) => string;
9
10
  export declare const checkNumber: (n: number | string) => number | string;
10
11
  export declare const formatNumber: (n: number | string, decimals?: number) => string;
11
12
  export declare const parseUnits: (n: number | string, decimals?: number) => ethers.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.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.getEthIndex = exports.isEth = exports.parseUnits = exports.formatNumber = exports.checkNumber = exports.fromBN = exports.toStringFromBN = exports.toBN = exports.BN = exports.MAX_ALLOWANCE = void 0;
62
+ 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.getEthIndex = exports.isEth = exports.parseUnits = exports.formatNumber = exports.checkNumber = exports._cutZeros = exports.fromBN = exports.toStringFromBN = exports.toBN = exports.BN = exports.MAX_ALLOWANCE = 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"));
@@ -86,6 +86,10 @@ var fromBN = function (bn, decimals) {
86
86
  };
87
87
  exports.fromBN = fromBN;
88
88
  // Formatting numbers
89
+ var _cutZeros = function (strn) {
90
+ return strn.replace(/0+$/gi, '').replace(/\.$/gi, '');
91
+ };
92
+ exports._cutZeros = _cutZeros;
89
93
  var checkNumber = function (n) {
90
94
  if (Number(n) !== Number(n))
91
95
  throw Error("".concat(n, " is not a number")); // NaN
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@curvefi/api",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "JavaScript library for curve.fi",
5
5
  "main": "lib/index.js",
6
6
  "author": "Macket",