@curvefi/api 2.49.4 → 2.49.5

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
@@ -1482,7 +1482,7 @@ For L2 networks `estimateGas` return array `[L2GasUsed, L1GasUsed]`, where `L2Ga
1482
1482
  Gas Price in L1 is required to calculate the fee.
1483
1483
  You can use `getGasPriceFromL1` for get Gas Price in L1.
1484
1484
  ````ts
1485
- const L1GasPrice = await curve.L1GasPrice()
1485
+ const L1GasPrice = await curve.getGasPriceFromL1()
1486
1486
  // 13161051
1487
1487
  ````
1488
1488
 
package/lib/index.d.ts CHANGED
@@ -203,7 +203,7 @@ declare const curve: {
203
203
  getSwappedAmount: (tx: ethers.ContractTransactionResponse, outputCoin: string) => Promise<string>;
204
204
  estimateGas: {
205
205
  approve: (inputCoin: string, amount: string | number) => Promise<number | number[]>;
206
- swap: (inputCoin: string, outputCoin: string, amount: string | number) => Promise<number>;
206
+ swap: (inputCoin: string, outputCoin: string, amount: string | number) => Promise<number | number[]>;
207
207
  };
208
208
  };
209
209
  };
package/lib/router.d.ts CHANGED
@@ -19,6 +19,6 @@ export declare const swapPriceImpact: (inputCoin: string, outputCoin: string, am
19
19
  export declare const swapIsApproved: (inputCoin: string, amount: number | string) => Promise<boolean>;
20
20
  export declare const swapApproveEstimateGas: (inputCoin: string, amount: number | string) => Promise<number | number[]>;
21
21
  export declare const swapApprove: (inputCoin: string, amount: number | string) => Promise<string[]>;
22
- export declare const swapEstimateGas: (inputCoin: string, outputCoin: string, amount: number | string) => Promise<number>;
22
+ export declare const swapEstimateGas: (inputCoin: string, outputCoin: string, amount: number | string) => Promise<number | number[]>;
23
23
  export declare const swap: (inputCoin: string, outputCoin: string, amount: number | string, slippage?: number) => Promise<ethers.ContractTransactionResponse>;
24
24
  export declare const getSwappedAmount: (tx: ethers.ContractTransactionResponse, outputCoin: string) => Promise<string>;
package/lib/router.js CHANGED
@@ -58,9 +58,10 @@ import axios from "axios";
58
58
  import memoize from "memoizee";
59
59
  import { ethers } from "ethers";
60
60
  import { curve } from "./curve.js";
61
- import { _getCoinAddresses, _getCoinDecimals, _getUsdRate, ensureAllowance, ensureAllowanceEstimateGas, fromBN, hasAllowance, isEth, toBN, BN, parseUnits, _cutZeros, ETH_ADDRESS, _get_small_x, _get_price_impact, DIGas, } from "./utils.js";
61
+ import { _getCoinAddresses, _getCoinDecimals, _getUsdRate, ensureAllowance, ensureAllowanceEstimateGas, fromBN, hasAllowance, isEth, toBN, BN, parseUnits, _cutZeros, ETH_ADDRESS, _get_small_x, _get_price_impact, DIGas, smartNumber, getTxCostsUsd, getGasPriceFromL1, } from "./utils.js";
62
62
  import { getPool } from "./pools/index.js";
63
63
  import { _getAmplificationCoefficientsFromApi } from "./pools/utils.js";
64
+ import { L2Networks } from "./constants/L2Networks.js";
64
65
  var MAX_STEPS = 5;
65
66
  var ROUTE_LENGTH = (MAX_STEPS * 2) + 1;
66
67
  var GRAPH_MAX_EDGES = 3;
@@ -568,7 +569,7 @@ var _estimateGasForDifferentRoutes = function (routes, inputCoinAddress, outputC
568
569
  var routeKey = _getRouteKey(route, inputCoinAddress, outputCoinAddress);
569
570
  _estimatedGasForDifferentRoutesCache[routeKey] = { 'gas': _gasAmounts_1[i], 'time': Date.now() };
570
571
  });
571
- return [2 /*return*/, _gasAmounts_1.map(function (_g) { return Number(curve.formatUnits(DIGas(_g), 0)); })];
572
+ return [2 /*return*/, _gasAmounts_1.map(function (_g) { return smartNumber(_g); })];
572
573
  case 3:
573
574
  err_1 = _c.sent();
574
575
  return [2 /*return*/, routes.map(function () { return 0; })];
@@ -577,9 +578,9 @@ var _estimateGasForDifferentRoutes = function (routes, inputCoinAddress, outputC
577
578
  });
578
579
  }); };
579
580
  var _getBestRoute = memoize(function (inputCoinAddress, outputCoinAddress, amount) { return __awaiter(void 0, void 0, void 0, function () {
580
- var _a, inputCoinDecimals, outputCoinDecimals, _amount, routesRaw, routes, calls, multicallContract, _i, routesRaw_1, r, _b, _route, _swapParams, _pools, _outputAmounts, i, err_2, contract, _outputs, _c, routesRaw_2, r, _d, _route, _swapParams, _pools, _e, _f, e_1, i, _h, gasAmounts, outputCoinUsdRate, gasData, ethUsdRate, gasPrice, expectedAmounts, expectedAmountsUsd, txCostsUsd;
581
- return __generator(this, function (_j) {
582
- switch (_j.label) {
581
+ var _a, inputCoinDecimals, outputCoinDecimals, _amount, routesRaw, routes, calls, multicallContract, _i, routesRaw_1, r, _b, _route, _swapParams, _pools, _outputAmounts, i, err_2, contract, _outputs, _c, routesRaw_2, r, _d, _route, _swapParams, _pools, _e, _f, e_1, i, _h, gasAmounts, outputCoinUsdRate, gasData, ethUsdRate, gasPrice, expectedAmounts, expectedAmountsUsd, L1GasPrice, _j, txCostsUsd;
582
+ return __generator(this, function (_k) {
583
+ switch (_k.label) {
583
584
  case 0:
584
585
  _a = _getCoinDecimals(inputCoinAddress, outputCoinAddress), inputCoinDecimals = _a[0], outputCoinDecimals = _a[1];
585
586
  _amount = parseUnits(amount, inputCoinDecimals);
@@ -587,11 +588,11 @@ var _getBestRoute = memoize(function (inputCoinAddress, outputCoinAddress, amoun
587
588
  return [2 /*return*/, []];
588
589
  return [4 /*yield*/, _findRoutes(inputCoinAddress, outputCoinAddress)];
589
590
  case 1:
590
- routesRaw = (_j.sent()).map(function (route) { return ({ route: route, _output: curve.parseUnits("0"), outputUsd: 0, txCostUsd: 0 }); });
591
+ routesRaw = (_k.sent()).map(function (route) { return ({ route: route, _output: curve.parseUnits("0"), outputUsd: 0, txCostUsd: 0 }); });
591
592
  routes = [];
592
- _j.label = 2;
593
+ _k.label = 2;
593
594
  case 2:
594
- _j.trys.push([2, 4, , 11]);
595
+ _k.trys.push([2, 4, , 11]);
595
596
  calls = [];
596
597
  multicallContract = curve.contracts[curve.constants.ALIASES.router].multicallContract;
597
598
  for (_i = 0, routesRaw_1 = routesRaw; _i < routesRaw_1.length; _i++) {
@@ -601,32 +602,32 @@ var _getBestRoute = memoize(function (inputCoinAddress, outputCoinAddress, amoun
601
602
  }
602
603
  return [4 /*yield*/, curve.multicallProvider.all(calls)];
603
604
  case 3:
604
- _outputAmounts = _j.sent();
605
+ _outputAmounts = _k.sent();
605
606
  for (i = 0; i < _outputAmounts.length; i++) {
606
607
  routesRaw[i]._output = _outputAmounts[i];
607
608
  routes.push(routesRaw[i]);
608
609
  }
609
610
  return [3 /*break*/, 11];
610
611
  case 4:
611
- err_2 = _j.sent();
612
+ err_2 = _k.sent();
612
613
  contract = curve.contracts[curve.constants.ALIASES.router].contract;
613
614
  _outputs = [];
614
615
  _c = 0, routesRaw_2 = routesRaw;
615
- _j.label = 5;
616
+ _k.label = 5;
616
617
  case 5:
617
618
  if (!(_c < routesRaw_2.length)) return [3 /*break*/, 10];
618
619
  r = routesRaw_2[_c];
619
620
  _d = _getExchangeArgs(r.route), _route = _d._route, _swapParams = _d._swapParams, _pools = _d._pools;
620
- _j.label = 6;
621
+ _k.label = 6;
621
622
  case 6:
622
- _j.trys.push([6, 8, , 9]);
623
+ _k.trys.push([6, 8, , 9]);
623
624
  _f = (_e = _outputs).push;
624
625
  return [4 /*yield*/, contract.get_dy(_route, _swapParams, _amount, _pools, curve.constantOptions)];
625
626
  case 7:
626
- _f.apply(_e, [_j.sent()]);
627
+ _f.apply(_e, [_k.sent()]);
627
628
  return [3 /*break*/, 9];
628
629
  case 8:
629
- e_1 = _j.sent();
630
+ e_1 = _k.sent();
630
631
  _outputs.push(curve.parseUnits('-1', 0));
631
632
  return [3 /*break*/, 9];
632
633
  case 9:
@@ -654,11 +655,21 @@ var _getBestRoute = memoize(function (inputCoinAddress, outputCoinAddress, amoun
654
655
  _getUsdRate(ETH_ADDRESS),
655
656
  ])];
656
657
  case 12:
657
- _h = _j.sent(), gasAmounts = _h[0], outputCoinUsdRate = _h[1], gasData = _h[2], ethUsdRate = _h[3];
658
+ _h = _k.sent(), gasAmounts = _h[0], outputCoinUsdRate = _h[1], gasData = _h[2], ethUsdRate = _h[3];
658
659
  gasPrice = gasData.data.data.gas.standard;
659
660
  expectedAmounts = (routes).map(function (route) { return Number(curve.formatUnits(route._output, outputCoinDecimals)); });
660
661
  expectedAmountsUsd = expectedAmounts.map(function (a) { return a * outputCoinUsdRate; });
661
- txCostsUsd = gasAmounts.map(function (a) { return ethUsdRate * a * gasPrice / 1e18; });
662
+ if (!L2Networks.includes(curve.chainId)) return [3 /*break*/, 14];
663
+ return [4 /*yield*/, getGasPriceFromL1()];
664
+ case 13:
665
+ _j = _k.sent();
666
+ return [3 /*break*/, 15];
667
+ case 14:
668
+ _j = 0;
669
+ _k.label = 15;
670
+ case 15:
671
+ L1GasPrice = _j;
672
+ txCostsUsd = gasAmounts.map(function (a) { return getTxCostsUsd(ethUsdRate, gasPrice, a, L1GasPrice); });
662
673
  routes.forEach(function (route, i) {
663
674
  route.outputUsd = expectedAmountsUsd[i];
664
675
  route.txCostUsd = txCostsUsd[i];
package/lib/utils.d.ts CHANGED
@@ -36,6 +36,7 @@ export declare const _getRewardsFromApi: () => Promise<IDict<IRewardFromApi[]>>;
36
36
  export declare const _getUsdRate: (assetId: string) => Promise<number>;
37
37
  export declare const getUsdRate: (coin: string) => Promise<number>;
38
38
  export declare const getGasPriceFromL1: () => Promise<number>;
39
+ export declare const getTxCostsUsd: (ethUsdRate: number, gasPrice: number, gas: number | number[], gasPriceL1?: number) => number;
39
40
  export declare const getTVL: (network?: INetworkName | IChainId) => Promise<number>;
40
41
  export declare const getVolume: (network?: INetworkName | IChainId) => Promise<{
41
42
  totalVolume: number;
package/lib/utils.js CHANGED
@@ -683,16 +683,28 @@ export var getUsdRate = function (coin) { return __awaiter(void 0, void 0, void
683
683
  });
684
684
  }); };
685
685
  export var getGasPriceFromL1 = function () { return __awaiter(void 0, void 0, void 0, function () {
686
+ var gasPrice;
686
687
  return __generator(this, function (_a) {
687
688
  switch (_a.label) {
688
689
  case 0:
689
690
  if (!L2Networks.includes(curve.chainId)) return [3 /*break*/, 2];
690
- return [4 /*yield*/, curve.contracts[curve.constants.ALIASES.gas_oracle].contract.gasPrice()];
691
- case 1: return [2 /*return*/, _a.sent()];
691
+ return [4 /*yield*/, curve.contracts[curve.constants.ALIASES.gas_oracle].contract.l1BaseFee()];
692
+ case 1:
693
+ gasPrice = _a.sent();
694
+ return [2 /*return*/, Number(gasPrice) + 1e9]; // + 1 gwei
692
695
  case 2: throw Error("This method exists only for L2 networks");
693
696
  }
694
697
  });
695
698
  }); };
699
+ export var getTxCostsUsd = function (ethUsdRate, gasPrice, gas, gasPriceL1) {
700
+ if (gasPriceL1 === void 0) { gasPriceL1 = 0; }
701
+ if (Array.isArray(gas)) {
702
+ return ethUsdRate * ((gas[0] * gasPrice / 1e18) + (gas[1] * gasPriceL1 / 1e18));
703
+ }
704
+ else {
705
+ return ethUsdRate * gas * gasPrice / 1e18;
706
+ }
707
+ };
696
708
  var _getNetworkName = function (network) {
697
709
  if (network === void 0) { network = curve.chainId; }
698
710
  if (typeof network === "number" && NETWORK_CONSTANTS[network]) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@curvefi/api",
3
- "version": "2.49.4",
3
+ "version": "2.49.5",
4
4
  "description": "JavaScript library for curve.fi",
5
5
  "main": "lib/index.js",
6
6
  "author": "Macket",