@curvefi/api 2.66.30 → 2.66.31

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.
Files changed (81) hide show
  1. package/lib/boosting.d.ts +33 -32
  2. package/lib/boosting.js +320 -252
  3. package/lib/cached.d.ts +4 -3
  4. package/lib/cached.js +63 -19
  5. package/lib/constants/utils.d.ts +14 -1
  6. package/lib/constants/utils.js +21 -6
  7. package/lib/curve.d.ts +5 -5
  8. package/lib/curve.js +21 -38
  9. package/lib/dao.d.ts +32 -31
  10. package/lib/dao.js +420 -350
  11. package/lib/external-api.d.ts +1 -1
  12. package/lib/external-api.js +2 -2
  13. package/lib/factory/common.d.ts +2 -1
  14. package/lib/factory/common.js +1 -1
  15. package/lib/factory/deploy.d.ts +46 -45
  16. package/lib/factory/deploy.js +630 -551
  17. package/lib/factory/factory-api.d.ts +3 -2
  18. package/lib/factory/factory-api.js +22 -23
  19. package/lib/factory/factory-crypto.d.ts +1 -1
  20. package/lib/factory/factory-crypto.js +12 -15
  21. package/lib/factory/factory-tricrypto.d.ts +1 -1
  22. package/lib/factory/factory-tricrypto.js +14 -15
  23. package/lib/factory/factory-twocrypto.d.ts +1 -1
  24. package/lib/factory/factory-twocrypto.js +12 -13
  25. package/lib/factory/factory.d.ts +4 -3
  26. package/lib/factory/factory.js +21 -24
  27. package/lib/index.d.ts +413 -104
  28. package/lib/index.js +253 -257
  29. package/lib/interfaces.d.ts +2 -0
  30. package/lib/pools/PoolTemplate.d.ts +13 -12
  31. package/lib/pools/PoolTemplate.js +279 -285
  32. package/lib/pools/mixins/common.js +2 -2
  33. package/lib/pools/mixins/depositBalancedAmountsMixins.d.ts +12 -4
  34. package/lib/pools/mixins/depositBalancedAmountsMixins.js +1 -15
  35. package/lib/pools/mixins/depositMixins.d.ts +25 -5
  36. package/lib/pools/mixins/depositMixins.js +38 -76
  37. package/lib/pools/mixins/depositWrappedMixins.d.ts +10 -2
  38. package/lib/pools/mixins/depositWrappedMixins.js +17 -33
  39. package/lib/pools/mixins/poolBalancesMixin.d.ts +6 -2
  40. package/lib/pools/mixins/poolBalancesMixin.js +3 -5
  41. package/lib/pools/mixins/swapMixins.d.ts +20 -4
  42. package/lib/pools/mixins/swapMixins.js +36 -70
  43. package/lib/pools/mixins/swapWrappedMixins.d.ts +19 -4
  44. package/lib/pools/mixins/swapWrappedMixins.js +32 -60
  45. package/lib/pools/mixins/withdrawExpectedMixins.d.ts +12 -4
  46. package/lib/pools/mixins/withdrawExpectedMixins.js +6 -11
  47. package/lib/pools/mixins/withdrawImbalanceMixins.d.ts +20 -4
  48. package/lib/pools/mixins/withdrawImbalanceMixins.js +26 -53
  49. package/lib/pools/mixins/withdrawImbalanceWrappedMixins.d.ts +10 -2
  50. package/lib/pools/mixins/withdrawImbalanceWrappedMixins.js +12 -27
  51. package/lib/pools/mixins/withdrawMixins.d.ts +25 -5
  52. package/lib/pools/mixins/withdrawMixins.js +33 -67
  53. package/lib/pools/mixins/withdrawOneCoinExpectedMixins.d.ts +12 -4
  54. package/lib/pools/mixins/withdrawOneCoinExpectedMixins.js +8 -13
  55. package/lib/pools/mixins/withdrawOneCoinMixins.d.ts +25 -5
  56. package/lib/pools/mixins/withdrawOneCoinMixins.js +32 -66
  57. package/lib/pools/mixins/withdrawOneCoinWrappedExpectedMixins.d.ts +6 -2
  58. package/lib/pools/mixins/withdrawOneCoinWrappedExpectedMixins.js +4 -7
  59. package/lib/pools/mixins/withdrawOneCoinWrappedMixins.d.ts +10 -2
  60. package/lib/pools/mixins/withdrawOneCoinWrappedMixins.js +13 -29
  61. package/lib/pools/mixins/withdrawWrappedMixins.d.ts +10 -2
  62. package/lib/pools/mixins/withdrawWrappedMixins.js +14 -28
  63. package/lib/pools/poolConstructor.d.ts +2 -1
  64. package/lib/pools/poolConstructor.js +27 -28
  65. package/lib/pools/subClasses/corePool.d.ts +4 -1
  66. package/lib/pools/subClasses/corePool.js +5 -7
  67. package/lib/pools/subClasses/gaugePool.d.ts +5 -3
  68. package/lib/pools/subClasses/gaugePool.js +19 -18
  69. package/lib/pools/subClasses/statsPool.d.ts +2 -0
  70. package/lib/pools/subClasses/statsPool.js +22 -10
  71. package/lib/pools/subClasses/walletPool.d.ts +2 -1
  72. package/lib/pools/subClasses/walletPool.js +6 -6
  73. package/lib/pools/utils.d.ts +7 -6
  74. package/lib/pools/utils.js +316 -297
  75. package/lib/route-graph.worker.d.ts +2 -2
  76. package/lib/route-graph.worker.js +4 -6
  77. package/lib/router.d.ts +12 -11
  78. package/lib/router.js +331 -295
  79. package/lib/utils.d.ts +34 -33
  80. package/lib/utils.js +481 -435
  81. package/package.json +1 -1
package/lib/utils.js CHANGED
@@ -7,10 +7,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import { Contract } from 'ethers';
10
+ import { Contract, ethers } from 'ethers';
11
11
  import { Contract as MulticallContract } from "@curvefi/ethcall";
12
12
  import BigNumber from 'bignumber.js';
13
- import { curve } from "./curve.js";
14
13
  import { _getCurveLiteNetworks, _getFactoryAPYs, _getLiteNetworksData, _getSubgraphData, _getVolumes, } from "./external-api.js";
15
14
  import { _getAllPoolsFromApi, _getUsdPricesFromApi } from "./cached.js";
16
15
  import ERC20Abi from './constants/abis/ERC20.json' with { type: 'json' };
@@ -18,6 +17,7 @@ import { L2Networks } from './constants/L2Networks.js';
18
17
  import { volumeNetworks } from "./constants/volumeNetworks.js";
19
18
  import { getPool } from "./pools/index.js";
20
19
  import { NETWORK_CONSTANTS } from "./constants/network_constants.js";
20
+ import { formatUnits } from "./constants/utils";
21
21
  export const ETH_ADDRESS = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
22
22
  // export const MAX_ALLOWANCE = curve.parseUnits(new BigNumber(2).pow(256).minus(1).toFixed(), 0);
23
23
  export const MAX_ALLOWANCE = BigInt("115792089237316195423570985008687907853269984665640564039457584007913129639935"); // 2**256 - 1
@@ -36,24 +36,24 @@ export const formatNumber = (n, decimals = 18) => {
36
36
  const [integer, fractional] = String(n).split(".");
37
37
  return !fractional ? integer : integer + "." + fractional.slice(0, decimals);
38
38
  };
39
- export const parseUnits = (n, decimals = 18) => {
40
- return curve.parseUnits(formatNumber(n, decimals), decimals);
41
- };
39
+ export function parseUnits(n, decimals = 18) {
40
+ return ethers.parseUnits(formatNumber(n, decimals), decimals);
41
+ }
42
42
  // bignumber.js
43
43
  export const BN = (val) => new BigNumber(checkNumber(val));
44
44
  export const toBN = (n, decimals = 18) => {
45
- return BN(curve.formatUnits(n, decimals));
45
+ return BN(formatUnits(n, decimals));
46
46
  };
47
47
  export const toStringFromBN = (bn, decimals = 18) => {
48
48
  return bn.toFixed(decimals);
49
49
  };
50
50
  export const fromBN = (bn, decimals = 18) => {
51
- return curve.parseUnits(toStringFromBN(bn, decimals), decimals);
51
+ return parseUnits(toStringFromBN(bn, decimals), decimals);
52
52
  };
53
53
  // -------------------
54
54
  export const isEth = (address) => address.toLowerCase() === ETH_ADDRESS.toLowerCase();
55
55
  export const getEthIndex = (addresses) => addresses.map((address) => address.toLowerCase()).indexOf(ETH_ADDRESS.toLowerCase());
56
- export const mulBy1_3 = (n) => n * curve.parseUnits("130", 0) / curve.parseUnits("100", 0);
56
+ export const mulBy1_3 = (n) => n * parseUnits("130", 0) / parseUnits("100", 0);
57
57
  export const smartNumber = (abstractNumber) => {
58
58
  if (Array.isArray(abstractNumber)) {
59
59
  return [Number(abstractNumber[0]), Number(abstractNumber[1])];
@@ -89,289 +89,318 @@ export const gasSum = (gas, currentGas) => {
89
89
  return gas;
90
90
  };
91
91
  // coins can be either addresses or symbols
92
- export const _getCoinAddressesNoCheck = (...coins) => {
92
+ export function _getCoinAddressesNoCheck(...coins) {
93
93
  if (coins.length == 1 && Array.isArray(coins[0]))
94
94
  coins = coins[0];
95
95
  coins = coins;
96
- return coins.map((c) => c.toLowerCase()).map((c) => curve.constants.COINS[c] || c);
97
- };
98
- export const _getCoinAddresses = (...coins) => {
99
- const coinAddresses = _getCoinAddressesNoCheck(...coins);
100
- const availableAddresses = [...Object.keys(curve.constants.DECIMALS), ...curve.constants.GAUGES];
96
+ return coins.map((c) => c.toLowerCase()).map((c) => this.constants.COINS[c] || c);
97
+ }
98
+ export function _getCoinAddresses(...coins) {
99
+ const coinAddresses = _getCoinAddressesNoCheck.call(this, ...coins);
100
+ const availableAddresses = [...Object.keys(this.constants.DECIMALS), ...this.constants.GAUGES];
101
101
  for (const coinAddr of coinAddresses) {
102
102
  if (!availableAddresses.includes(coinAddr))
103
103
  throw Error(`Coin with address '${coinAddr}' is not available`);
104
104
  }
105
105
  return coinAddresses;
106
- };
107
- export const _getCoinDecimals = (...coinAddresses) => {
106
+ }
107
+ export function _getCoinDecimals(...coinAddresses) {
108
108
  if (coinAddresses.length == 1 && Array.isArray(coinAddresses[0]))
109
109
  coinAddresses = coinAddresses[0];
110
110
  coinAddresses = coinAddresses;
111
- return coinAddresses.map((coinAddr) => { var _a; return (_a = curve.constants.DECIMALS[coinAddr.toLowerCase()]) !== null && _a !== void 0 ? _a : 18; }); // 18 for gauges
112
- };
113
- export const _getBalances = (coins, addresses) => __awaiter(void 0, void 0, void 0, function* () {
114
- const coinAddresses = _getCoinAddresses(coins);
115
- const decimals = _getCoinDecimals(coinAddresses);
116
- const ethIndex = getEthIndex(coinAddresses);
117
- if (ethIndex !== -1) {
118
- coinAddresses.splice(ethIndex, 1);
119
- }
120
- const contractCalls = [];
121
- for (const coinAddr of coinAddresses) {
122
- contractCalls.push(...addresses.map((address) => curve.contracts[coinAddr].multicallContract.balanceOf(address)));
123
- }
124
- const _response = yield curve.multicallProvider.all(contractCalls);
125
- if (ethIndex !== -1) {
126
- const ethBalances = [];
111
+ return coinAddresses.map((coinAddr) => { var _a; return (_a = this.constants.DECIMALS[coinAddr.toLowerCase()]) !== null && _a !== void 0 ? _a : 18; }); // 18 for gauges
112
+ }
113
+ export function _getBalances(coins, addresses) {
114
+ return __awaiter(this, void 0, void 0, function* () {
115
+ const coinAddresses = _getCoinAddresses.call(this, coins);
116
+ const decimals = _getCoinDecimals.call(this, coinAddresses);
117
+ const ethIndex = getEthIndex(coinAddresses);
118
+ if (ethIndex !== -1) {
119
+ coinAddresses.splice(ethIndex, 1);
120
+ }
121
+ const contractCalls = [];
122
+ for (const coinAddr of coinAddresses) {
123
+ contractCalls.push(...addresses.map((address) => this.contracts[coinAddr].multicallContract.balanceOf(address)));
124
+ }
125
+ const _response = yield this.multicallProvider.all(contractCalls);
126
+ if (ethIndex !== -1) {
127
+ const ethBalances = [];
128
+ for (const address of addresses) {
129
+ ethBalances.push(yield this.provider.getBalance(address));
130
+ }
131
+ _response.splice(ethIndex * addresses.length, 0, ...ethBalances);
132
+ }
133
+ const _balances = {};
134
+ addresses.forEach((address, i) => {
135
+ _balances[address] = coins.map((_, j) => _response[i + (j * addresses.length)]);
136
+ });
137
+ const balances = {};
127
138
  for (const address of addresses) {
128
- ethBalances.push(yield curve.provider.getBalance(address));
139
+ balances[address] = _balances[address].map((b, i) => this.formatUnits(b, decimals[i]));
129
140
  }
130
- _response.splice(ethIndex * addresses.length, 0, ...ethBalances);
131
- }
132
- const _balances = {};
133
- addresses.forEach((address, i) => {
134
- _balances[address] = coins.map((_, j) => _response[i + (j * addresses.length)]);
141
+ return balances;
135
142
  });
136
- const balances = {};
137
- for (const address of addresses) {
138
- balances[address] = _balances[address].map((b, i) => curve.formatUnits(b, decimals[i]));
139
- }
140
- return balances;
141
- });
142
- export const _prepareAddresses = (addresses) => {
143
+ }
144
+ export function _prepareAddresses(addresses) {
143
145
  if (addresses.length == 1 && Array.isArray(addresses[0]))
144
146
  addresses = addresses[0];
145
- if (addresses.length === 0 && curve.signerAddress !== '')
146
- addresses = [curve.signerAddress];
147
+ if (addresses.length === 0 && this.signerAddress !== '')
148
+ addresses = [this.signerAddress];
147
149
  addresses = addresses;
148
150
  return addresses.filter((val, idx, arr) => arr.indexOf(val) === idx);
149
- };
150
- export const _getAddress = (address) => {
151
- address = address || curve.signerAddress;
151
+ }
152
+ export function _getAddress(address) {
153
+ address = address || this.signerAddress;
152
154
  if (!address)
153
155
  throw Error("Need to connect wallet or pass address into args");
154
156
  return address;
155
- };
156
- export const getBalances = (coins, ...addresses) => __awaiter(void 0, void 0, void 0, function* () {
157
- addresses = _prepareAddresses(addresses);
158
- const balances = yield _getBalances(coins, addresses);
159
- return addresses.length === 1 ? balances[addresses[0]] : balances;
160
- });
161
- export const _getAllowance = (coins, address, spender) => __awaiter(void 0, void 0, void 0, function* () {
162
- const _coins = [...coins];
163
- const ethIndex = getEthIndex(_coins);
164
- if (ethIndex !== -1) {
165
- _coins.splice(ethIndex, 1);
166
- }
167
- let allowance;
168
- if (_coins.length === 1) {
169
- allowance = [yield curve.contracts[_coins[0]].contract.allowance(address, spender, curve.constantOptions)];
170
- }
171
- else {
172
- const contractCalls = _coins.map((coinAddr) => curve.contracts[coinAddr].multicallContract.allowance(address, spender));
173
- allowance = yield curve.multicallProvider.all(contractCalls);
174
- }
175
- if (ethIndex !== -1) {
176
- allowance.splice(ethIndex, 0, MAX_ALLOWANCE);
177
- }
178
- return allowance;
179
- });
157
+ }
158
+ export function getBalances(coins, ...addresses) {
159
+ return __awaiter(this, void 0, void 0, function* () {
160
+ addresses = _prepareAddresses.call(this, addresses);
161
+ const balances = yield _getBalances.call(this, coins, addresses);
162
+ return addresses.length === 1 ? balances[addresses[0]] : balances;
163
+ });
164
+ }
165
+ export function _getAllowance(coins, address, spender) {
166
+ return __awaiter(this, void 0, void 0, function* () {
167
+ const _coins = [...coins];
168
+ const ethIndex = getEthIndex(_coins);
169
+ if (ethIndex !== -1) {
170
+ _coins.splice(ethIndex, 1);
171
+ }
172
+ let allowance;
173
+ if (_coins.length === 1) {
174
+ allowance = [yield this.contracts[_coins[0]].contract.allowance(address, spender, this.constantOptions)];
175
+ }
176
+ else {
177
+ const contractCalls = _coins.map((coinAddr) => this.contracts[coinAddr].multicallContract.allowance(address, spender));
178
+ allowance = yield this.multicallProvider.all(contractCalls);
179
+ }
180
+ if (ethIndex !== -1) {
181
+ allowance.splice(ethIndex, 0, MAX_ALLOWANCE);
182
+ }
183
+ return allowance;
184
+ });
185
+ }
180
186
  // coins can be either addresses or symbols
181
- export const getAllowance = (coins, address, spender) => __awaiter(void 0, void 0, void 0, function* () {
182
- const coinAddresses = _getCoinAddresses(coins);
183
- const decimals = _getCoinDecimals(coinAddresses);
184
- const _allowance = yield _getAllowance(coinAddresses, address, spender);
185
- return _allowance.map((a, i) => curve.formatUnits(a, decimals[i]));
186
- });
187
+ export function getAllowance(coins, address, spender) {
188
+ return __awaiter(this, void 0, void 0, function* () {
189
+ const coinAddresses = _getCoinAddresses.call(this, coins);
190
+ const decimals = _getCoinDecimals.call(this, coinAddresses);
191
+ const _allowance = yield _getAllowance.call(this, coinAddresses, address, spender);
192
+ return _allowance.map((a, i) => this.formatUnits(a, decimals[i]));
193
+ });
194
+ }
187
195
  // coins can be either addresses or symbols
188
- export const hasAllowance = (coins, amounts, address, spender) => __awaiter(void 0, void 0, void 0, function* () {
189
- const coinAddresses = _getCoinAddresses(coins);
190
- const decimals = _getCoinDecimals(coinAddresses);
191
- const _allowance = yield _getAllowance(coinAddresses, address, spender);
192
- const _amounts = amounts.map((a, i) => parseUnits(a, decimals[i]));
193
- return _allowance.map((a, i) => a >= _amounts[i]).reduce((a, b) => a && b);
194
- });
195
- export const _ensureAllowance = (coins_1, amounts_1, spender_1, ...args_1) => __awaiter(void 0, [coins_1, amounts_1, spender_1, ...args_1], void 0, function* (coins, amounts, spender, isMax = true) {
196
- const address = curve.signerAddress;
197
- const allowance = yield _getAllowance(coins, address, spender);
198
- const txHashes = [];
199
- for (let i = 0; i < allowance.length; i++) {
200
- if (allowance[i] < amounts[i]) {
201
- const contract = curve.contracts[coins[i]].contract;
202
- const _approveAmount = isMax ? MAX_ALLOWANCE : amounts[i];
203
- yield curve.updateFeeData();
204
- if (allowance[i] > curve.parseUnits("0")) {
205
- const gasLimit = mulBy1_3(DIGas(yield contract.approve.estimateGas(spender, curve.parseUnits("0"), curve.constantOptions)));
206
- const resetTx = yield contract.approve(spender, curve.parseUnits("0"), Object.assign(Object.assign({}, curve.options), { gasLimit }));
207
- txHashes.push(resetTx.hash);
208
- yield resetTx.wait();
196
+ export function hasAllowance(coins, amounts, address, spender) {
197
+ return __awaiter(this, void 0, void 0, function* () {
198
+ const coinAddresses = _getCoinAddresses.call(this, coins);
199
+ const decimals = _getCoinDecimals.call(this, coinAddresses);
200
+ const _allowance = yield _getAllowance.call(this, coinAddresses, address, spender);
201
+ const _amounts = amounts.map((a, i) => parseUnits(a, decimals[i]));
202
+ return _allowance.map((a, i) => a >= _amounts[i]).reduce((a, b) => a && b);
203
+ });
204
+ }
205
+ export function _ensureAllowance(coins_1, amounts_1, spender_1) {
206
+ return __awaiter(this, arguments, void 0, function* (coins, amounts, spender, isMax = true) {
207
+ const address = this.signerAddress;
208
+ const allowance = yield _getAllowance.call(this, coins, address, spender);
209
+ const txHashes = [];
210
+ for (let i = 0; i < allowance.length; i++) {
211
+ if (allowance[i] < amounts[i]) {
212
+ const contract = this.contracts[coins[i]].contract;
213
+ const _approveAmount = isMax ? MAX_ALLOWANCE : amounts[i];
214
+ yield this.updateFeeData();
215
+ if (allowance[i] > parseUnits("0")) {
216
+ const gasLimit = mulBy1_3(DIGas(yield contract.approve.estimateGas(spender, parseUnits("0"), this.constantOptions)));
217
+ const resetTx = yield contract.approve(spender, parseUnits("0"), Object.assign(Object.assign({}, this.options), { gasLimit }));
218
+ txHashes.push(resetTx.hash);
219
+ yield resetTx.wait();
220
+ }
221
+ const gasLimit = mulBy1_3(DIGas(yield contract.approve.estimateGas(spender, _approveAmount, this.constantOptions)));
222
+ const approveTx = yield contract.approve(spender, _approveAmount, Object.assign(Object.assign({}, this.options), { gasLimit }));
223
+ txHashes.push(approveTx.hash);
224
+ yield approveTx.wait();
209
225
  }
210
- const gasLimit = mulBy1_3(DIGas(yield contract.approve.estimateGas(spender, _approveAmount, curve.constantOptions)));
211
- const approveTx = yield contract.approve(spender, _approveAmount, Object.assign(Object.assign({}, curve.options), { gasLimit }));
212
- txHashes.push(approveTx.hash);
213
- yield approveTx.wait();
214
226
  }
215
- }
216
- return txHashes;
217
- });
227
+ return txHashes;
228
+ });
229
+ }
218
230
  // coins can be either addresses or symbols
219
- export const ensureAllowanceEstimateGas = (coins_1, amounts_1, spender_1, ...args_1) => __awaiter(void 0, [coins_1, amounts_1, spender_1, ...args_1], void 0, function* (coins, amounts, spender, isMax = true) {
220
- const coinAddresses = _getCoinAddresses(coins);
221
- const decimals = _getCoinDecimals(coinAddresses);
222
- const _amounts = amounts.map((a, i) => parseUnits(a, decimals[i]));
223
- const address = curve.signerAddress;
224
- const _allowance = yield _getAllowance(coinAddresses, address, spender);
225
- let gas = [0, 0];
226
- for (let i = 0; i < _allowance.length; i++) {
227
- if (_allowance[i] < _amounts[i]) {
228
- const contract = curve.contracts[coinAddresses[i]].contract;
229
- const _approveAmount = isMax ? MAX_ALLOWANCE : _amounts[i];
230
- if (_allowance[i] > curve.parseUnits("0")) {
231
- let currentGas = smartNumber(yield contract.approve.estimateGas(spender, curve.parseUnits("0"), curve.constantOptions));
232
- // For some coins (crv for example ) we can't estimate the second tx gas (approve: 0 --> amount), so we assume it will cost the same amount of gas
233
- if (typeof currentGas === "number") {
234
- currentGas = currentGas * 2;
231
+ export function ensureAllowanceEstimateGas(coins_1, amounts_1, spender_1) {
232
+ return __awaiter(this, arguments, void 0, function* (coins, amounts, spender, isMax = true) {
233
+ const coinAddresses = _getCoinAddresses.call(this, coins);
234
+ const decimals = _getCoinDecimals.call(this, coinAddresses);
235
+ const _amounts = amounts.map((a, i) => parseUnits(a, decimals[i]));
236
+ const address = this.signerAddress;
237
+ const _allowance = yield _getAllowance.call(this, coinAddresses, address, spender);
238
+ let gas = [0, 0];
239
+ for (let i = 0; i < _allowance.length; i++) {
240
+ if (_allowance[i] < _amounts[i]) {
241
+ const contract = this.contracts[coinAddresses[i]].contract;
242
+ const _approveAmount = isMax ? MAX_ALLOWANCE : _amounts[i];
243
+ if (_allowance[i] > parseUnits("0")) {
244
+ let currentGas = smartNumber(yield contract.approve.estimateGas(spender, parseUnits("0"), this.constantOptions));
245
+ // For some coins (crv for example ) we can't estimate the second tx gas (approve: 0 --> amount), so we assume it will cost the same amount of gas
246
+ if (typeof currentGas === "number") {
247
+ currentGas = currentGas * 2;
248
+ }
249
+ else {
250
+ currentGas = currentGas.map((g) => g * 2);
251
+ }
252
+ gas = gasSum(gas, currentGas);
235
253
  }
236
254
  else {
237
- currentGas = currentGas.map((g) => g * 2);
255
+ const currentGas = smartNumber(yield contract.approve.estimateGas(spender, _approveAmount, this.constantOptions));
256
+ gas = gasSum(gas, currentGas);
238
257
  }
239
- gas = gasSum(gas, currentGas);
240
- }
241
- else {
242
- const currentGas = smartNumber(yield contract.approve.estimateGas(spender, _approveAmount, curve.constantOptions));
243
- gas = gasSum(gas, currentGas);
244
258
  }
245
259
  }
246
- }
247
- return getGasFromArray(gas);
248
- });
260
+ return getGasFromArray(gas);
261
+ });
262
+ }
249
263
  // coins can be either addresses or symbols
250
- export const ensureAllowance = (coins_1, amounts_1, spender_1, ...args_1) => __awaiter(void 0, [coins_1, amounts_1, spender_1, ...args_1], void 0, function* (coins, amounts, spender, isMax = true) {
251
- const coinAddresses = _getCoinAddresses(coins);
252
- const decimals = _getCoinDecimals(coinAddresses);
253
- const _amounts = amounts.map((a, i) => parseUnits(a, decimals[i]));
254
- return yield _ensureAllowance(coinAddresses, _amounts, spender, isMax);
255
- });
256
- export const getPoolIdBySwapAddress = (swapAddress) => {
257
- const poolsData = curve.getPoolsData();
264
+ export function ensureAllowance(coins_1, amounts_1, spender_1) {
265
+ return __awaiter(this, arguments, void 0, function* (coins, amounts, spender, isMax = true) {
266
+ const coinAddresses = _getCoinAddresses.call(this, coins);
267
+ const decimals = _getCoinDecimals.call(this, coinAddresses);
268
+ const _amounts = amounts.map((a, i) => parseUnits(a, decimals[i]));
269
+ return yield _ensureAllowance.call(this, coinAddresses, _amounts, spender, isMax);
270
+ });
271
+ }
272
+ export function getPoolIdBySwapAddress(swapAddress) {
273
+ const poolsData = this.getPoolsData();
258
274
  const poolIds = Object.entries(poolsData).filter(([, poolData]) => poolData.swap_address.toLowerCase() === swapAddress.toLowerCase());
259
275
  if (poolIds.length === 0)
260
276
  return "";
261
277
  return poolIds[0][0];
262
- };
263
- export const _getRewardsFromApi = () => __awaiter(void 0, void 0, void 0, function* () {
264
- var _a, _b;
265
- const network = curve.constants.NETWORK_NAME;
266
- const allTypesExtendedPoolData = yield _getAllPoolsFromApi(network, curve.isLiteChain);
267
- const rewardsDict = {};
268
- for (const extendedPoolData of allTypesExtendedPoolData) {
269
- for (const pool of extendedPoolData.poolData) {
270
- if (pool.gaugeAddress) {
271
- rewardsDict[pool.gaugeAddress.toLowerCase()] = ((_b = (_a = pool.gaugeRewards) !== null && _a !== void 0 ? _a : pool.gaugeExtraRewards) !== null && _b !== void 0 ? _b : [])
272
- .filter((r) => curve.chainId === 1 || r.tokenAddress.toLowerCase() !== curve.constants.COINS.crv);
278
+ }
279
+ export function _getRewardsFromApi() {
280
+ return __awaiter(this, void 0, void 0, function* () {
281
+ var _a, _b;
282
+ const network = this.constants.NETWORK_NAME;
283
+ const allTypesExtendedPoolData = yield _getAllPoolsFromApi(network, this.isLiteChain);
284
+ const rewardsDict = {};
285
+ for (const extendedPoolData of allTypesExtendedPoolData) {
286
+ for (const pool of extendedPoolData.poolData) {
287
+ if (pool.gaugeAddress) {
288
+ rewardsDict[pool.gaugeAddress.toLowerCase()] = ((_b = (_a = pool.gaugeRewards) !== null && _a !== void 0 ? _a : pool.gaugeExtraRewards) !== null && _b !== void 0 ? _b : [])
289
+ .filter((r) => this.chainId === 1 || r.tokenAddress.toLowerCase() !== this.constants.COINS.crv);
290
+ }
273
291
  }
274
292
  }
275
- }
276
- return rewardsDict;
277
- });
293
+ return rewardsDict;
294
+ });
295
+ }
278
296
  const _usdRatesCache = {};
279
- export const _getUsdRate = (assetId) => __awaiter(void 0, void 0, void 0, function* () {
280
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
281
- if (curve.chainId === 1 && assetId.toLowerCase() === '0x8762db106b2c2a0bccb3a80d1ed41273552616e8')
282
- return 0; // RSR
283
- const pricesFromApi = yield _getUsdPricesFromApi();
284
- if (assetId.toLowerCase() in pricesFromApi)
285
- return pricesFromApi[assetId.toLowerCase()];
286
- if (assetId === 'USD' || (curve.chainId === 137 && (assetId.toLowerCase() === curve.constants.COINS.am3crv.toLowerCase())))
287
- return 1;
288
- let chainName = curve.isLiteChain ? yield curve.constants.NETWORK_NAME : {
289
- 1: 'ethereum',
290
- 10: 'optimistic-ethereum',
291
- 56: "binance-smart-chain",
292
- 100: 'xdai',
293
- 137: 'polygon-pos',
294
- 146: 'sonic',
295
- 196: 'x-layer',
296
- 250: 'fantom',
297
- 252: 'fraxtal',
298
- 324: 'zksync',
299
- 1284: 'moonbeam',
300
- 2222: 'kava',
301
- 5000: 'mantle',
302
- 8453: 'base',
303
- 42220: 'celo',
304
- 43114: 'avalanche',
305
- 42161: 'arbitrum-one',
306
- 1313161554: 'aurora',
307
- }[curve.chainId];
308
- const nativeTokenName = curve.isLiteChain ? (_b = (_a = curve.constants) === null || _a === void 0 ? void 0 : _a.API_CONSTANTS) === null || _b === void 0 ? void 0 : _b.nativeTokenName : {
309
- 1: 'ethereum',
310
- 10: 'ethereum',
311
- 56: 'binancecoin',
312
- 100: 'xdai',
313
- 137: 'matic-network',
314
- 146: 'sonic-3',
315
- 196: 'okb',
316
- 250: 'fantom',
317
- 252: 'frax-share',
318
- 324: 'ethereum',
319
- 1284: 'moonbeam',
320
- 2222: 'kava',
321
- 5000: 'mantle',
322
- 8453: 'ethereum',
323
- 42220: 'celo',
324
- 43114: 'avalanche-2',
325
- 42161: 'ethereum',
326
- 1313161554: 'ethereum',
327
- }[curve.chainId];
328
- if (chainName === undefined) {
329
- throw Error('curve object is not initialized');
330
- }
331
- if (nativeTokenName === undefined) {
332
- if (curve.isLiteChain && ((_c = curve.constants.API_CONSTANTS) === null || _c === void 0 ? void 0 : _c.wrappedNativeTokenAddress.toLowerCase()) && ((_d = curve.constants.API_CONSTANTS) === null || _d === void 0 ? void 0 : _d.wrappedNativeTokenAddress.toLowerCase()) in pricesFromApi) {
333
- return pricesFromApi[(_e = curve.constants.API_CONSTANTS) === null || _e === void 0 ? void 0 : _e.wrappedNativeTokenAddress.toLowerCase()];
334
- }
335
- else {
336
- throw Error('nativeTokenName not found');
297
+ export function _getUsdRate(assetId) {
298
+ return __awaiter(this, void 0, void 0, function* () {
299
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
300
+ if (this.chainId === 1 && assetId.toLowerCase() === '0x8762db106b2c2a0bccb3a80d1ed41273552616e8')
301
+ return 0; // RSR
302
+ const pricesFromApi = yield _getUsdPricesFromApi(this.constants.NETWORK_NAME, this.isLiteChain);
303
+ if (assetId.toLowerCase() in pricesFromApi)
304
+ return pricesFromApi[assetId.toLowerCase()];
305
+ if (assetId === 'USD' || (this.chainId === 137 && (assetId.toLowerCase() === this.constants.COINS.am3crv.toLowerCase())))
306
+ return 1;
307
+ let chainName = this.isLiteChain ? yield this.constants.NETWORK_NAME : {
308
+ 1: 'ethereum',
309
+ 10: 'optimistic-ethereum',
310
+ 56: "binance-smart-chain",
311
+ 100: 'xdai',
312
+ 137: 'polygon-pos',
313
+ 146: 'sonic',
314
+ 196: 'x-layer',
315
+ 250: 'fantom',
316
+ 252: 'fraxtal',
317
+ 324: 'zksync',
318
+ 1284: 'moonbeam',
319
+ 2222: 'kava',
320
+ 5000: 'mantle',
321
+ 8453: 'base',
322
+ 42220: 'celo',
323
+ 43114: 'avalanche',
324
+ 42161: 'arbitrum-one',
325
+ 1313161554: 'aurora',
326
+ }[this.chainId];
327
+ const nativeTokenName = this.isLiteChain ? (_b = (_a = this.constants) === null || _a === void 0 ? void 0 : _a.API_CONSTANTS) === null || _b === void 0 ? void 0 : _b.nativeTokenName : {
328
+ 1: 'ethereum',
329
+ 10: 'ethereum',
330
+ 56: 'binancecoin',
331
+ 100: 'xdai',
332
+ 137: 'matic-network',
333
+ 146: 'sonic-3',
334
+ 196: 'okb',
335
+ 250: 'fantom',
336
+ 252: 'frax-share',
337
+ 324: 'ethereum',
338
+ 1284: 'moonbeam',
339
+ 2222: 'kava',
340
+ 5000: 'mantle',
341
+ 8453: 'ethereum',
342
+ 42220: 'celo',
343
+ 43114: 'avalanche-2',
344
+ 42161: 'ethereum',
345
+ 1313161554: 'ethereum',
346
+ }[this.chainId];
347
+ if (chainName === undefined) {
348
+ throw Error('curve object is not initialized');
337
349
  }
338
- }
339
- assetId = {
340
- 'CRV': 'curve-dao-token',
341
- 'EUR': 'stasis-eurs',
342
- 'BTC': 'bitcoin',
343
- 'ETH': 'ethereum',
344
- 'LINK': 'link',
345
- }[assetId.toUpperCase()] || assetId;
346
- assetId = isEth(assetId) ? nativeTokenName : assetId.toLowerCase();
347
- // No EURT on Coingecko Polygon
348
- if (curve.chainId === 137 && assetId.toLowerCase() === curve.constants.COINS.eurt) {
349
- chainName = 'ethereum';
350
- assetId = '0xC581b735A1688071A1746c968e0798D642EDE491'.toLowerCase(); // EURT Ethereum
351
- }
352
- // CRV
353
- if (assetId.toLowerCase() === curve.constants.ALIASES.crv) {
354
- assetId = 'curve-dao-token';
355
- }
356
- if (curve.isLiteChain && assetId.toLowerCase() === ((_f = curve.constants.API_CONSTANTS) === null || _f === void 0 ? void 0 : _f.wrappedNativeTokenAddress.toLowerCase())) {
357
- assetId = nativeTokenName;
358
- }
359
- if ((((_g = _usdRatesCache[assetId]) === null || _g === void 0 ? void 0 : _g.time) || 0) + 600000 < Date.now()) {
360
- const url = [nativeTokenName, 'ethereum', 'bitcoin', 'link', 'curve-dao-token', 'stasis-eurs'].includes(assetId.toLowerCase()) ?
361
- `https://api.coingecko.com/api/v3/simple/price?ids=${assetId}&vs_currencies=usd` :
362
- `https://api.coingecko.com/api/v3/simple/token_price/${chainName}?contract_addresses=${assetId}&vs_currencies=usd`;
363
- try {
364
- const response = yield fetch(url);
365
- const data = (_h = yield response.json()) !== null && _h !== void 0 ? _h : {};
366
- if (response.status === 200 && ((_j = data[assetId]) === null || _j === void 0 ? void 0 : _j.usd) !== undefined) {
367
- _usdRatesCache[assetId] = {
368
- 'rate': data[assetId].usd,
369
- 'time': Date.now(),
370
- };
350
+ if (nativeTokenName === undefined) {
351
+ if (this.isLiteChain && ((_c = this.constants.API_CONSTANTS) === null || _c === void 0 ? void 0 : _c.wrappedNativeTokenAddress.toLowerCase()) && ((_d = this.constants.API_CONSTANTS) === null || _d === void 0 ? void 0 : _d.wrappedNativeTokenAddress.toLowerCase()) in pricesFromApi) {
352
+ return pricesFromApi[(_e = this.constants.API_CONSTANTS) === null || _e === void 0 ? void 0 : _e.wrappedNativeTokenAddress.toLowerCase()];
371
353
  }
372
354
  else {
373
- if (!curve.isLiteChain) {
374
- console.warn(`Non-200 response for ${assetId}:`, response.status, data);
355
+ throw Error('nativeTokenName not found');
356
+ }
357
+ }
358
+ assetId = {
359
+ 'CRV': 'curve-dao-token',
360
+ 'EUR': 'stasis-eurs',
361
+ 'BTC': 'bitcoin',
362
+ 'ETH': 'ethereum',
363
+ 'LINK': 'link',
364
+ }[assetId.toUpperCase()] || assetId;
365
+ assetId = isEth(assetId) ? nativeTokenName : assetId.toLowerCase();
366
+ // No EURT on Coingecko Polygon
367
+ if (this.chainId === 137 && assetId.toLowerCase() === this.constants.COINS.eurt) {
368
+ chainName = 'ethereum';
369
+ assetId = '0xC581b735A1688071A1746c968e0798D642EDE491'.toLowerCase(); // EURT Ethereum
370
+ }
371
+ // CRV
372
+ if (assetId.toLowerCase() === this.constants.ALIASES.crv) {
373
+ assetId = 'curve-dao-token';
374
+ }
375
+ if (this.isLiteChain && assetId.toLowerCase() === ((_f = this.constants.API_CONSTANTS) === null || _f === void 0 ? void 0 : _f.wrappedNativeTokenAddress.toLowerCase())) {
376
+ assetId = nativeTokenName;
377
+ }
378
+ if ((((_g = _usdRatesCache[assetId]) === null || _g === void 0 ? void 0 : _g.time) || 0) + 600000 < Date.now()) {
379
+ const url = [nativeTokenName, 'ethereum', 'bitcoin', 'link', 'curve-dao-token', 'stasis-eurs'].includes(assetId.toLowerCase()) ?
380
+ `https://api.coingecko.com/api/v3/simple/price?ids=${assetId}&vs_currencies=usd` :
381
+ `https://api.coingecko.com/api/v3/simple/token_price/${chainName}?contract_addresses=${assetId}&vs_currencies=usd`;
382
+ try {
383
+ const response = yield fetch(url);
384
+ const data = (_h = yield response.json()) !== null && _h !== void 0 ? _h : {};
385
+ if (response.status === 200 && ((_j = data[assetId]) === null || _j === void 0 ? void 0 : _j.usd) !== undefined) {
386
+ _usdRatesCache[assetId] = {
387
+ 'rate': data[assetId].usd,
388
+ 'time': Date.now(),
389
+ };
390
+ }
391
+ else {
392
+ if (!this.isLiteChain) {
393
+ console.warn(`Non-200 response for ${assetId}:`, response.status, data);
394
+ }
395
+ _usdRatesCache[assetId] = {
396
+ 'rate': 0,
397
+ 'time': Date.now(),
398
+ };
399
+ }
400
+ }
401
+ catch (err) {
402
+ if (!this.isLiteChain) {
403
+ console.error(`Error fetching USD rate for ${assetId}:`, err.message);
375
404
  }
376
405
  _usdRatesCache[assetId] = {
377
406
  'rate': 0,
@@ -379,99 +408,102 @@ export const _getUsdRate = (assetId) => __awaiter(void 0, void 0, void 0, functi
379
408
  };
380
409
  }
381
410
  }
382
- catch (err) {
383
- if (!curve.isLiteChain) {
384
- console.error(`Error fetching USD rate for ${assetId}:`, err.message);
411
+ return _usdRatesCache[assetId]['rate'];
412
+ });
413
+ }
414
+ export function getUsdRate(coin) {
415
+ return __awaiter(this, void 0, void 0, function* () {
416
+ const [coinAddress] = _getCoinAddressesNoCheck.call(this, coin);
417
+ return yield _getUsdRate.call(this, coinAddress);
418
+ });
419
+ }
420
+ export function getBaseFeeByLastBlock() {
421
+ return __awaiter(this, void 0, void 0, function* () {
422
+ const provider = this.provider;
423
+ try {
424
+ const block = yield provider.getBlock('latest');
425
+ if (!block) {
426
+ return 0.01;
385
427
  }
386
- _usdRatesCache[assetId] = {
387
- 'rate': 0,
388
- 'time': Date.now(),
428
+ return Number(block.baseFeePerGas) / (Math.pow(10, 9));
429
+ }
430
+ catch (error) {
431
+ throw new Error(error);
432
+ }
433
+ });
434
+ }
435
+ export function getGasPrice() {
436
+ return __awaiter(this, void 0, void 0, function* () {
437
+ const provider = this.provider;
438
+ return Number((Number((yield provider.getFeeData()).gasPrice) / 1e9).toFixed(2));
439
+ });
440
+ }
441
+ export function getGasPriceFromL1() {
442
+ return __awaiter(this, void 0, void 0, function* () {
443
+ if (L2Networks.includes(this.chainId) && this.L1WeightedGasPrice) {
444
+ return this.L1WeightedGasPrice + 1e9; // + 1 gwei
445
+ }
446
+ else {
447
+ throw Error("This method exists only for L2 networks");
448
+ }
449
+ });
450
+ }
451
+ export function getGasPriceFromL2() {
452
+ return __awaiter(this, void 0, void 0, function* () {
453
+ if (this.chainId === 42161) {
454
+ return yield getBaseFeeByLastBlock.call(this);
455
+ }
456
+ if (this.chainId === 196) {
457
+ return yield getGasPrice.call(this); // gwei
458
+ }
459
+ if (this.chainId === 324) {
460
+ return yield getGasPrice.call(this); // gwei
461
+ }
462
+ if (this.chainId === 5000) {
463
+ return yield getGasPrice.call(this); // gwei
464
+ }
465
+ if (L2Networks.includes(this.chainId)) {
466
+ const gasPrice = yield this.contracts[this.constants.ALIASES.gas_oracle_blob].contract.gasPrice({ "gasPrice": "0x2000000" });
467
+ return Number(gasPrice);
468
+ }
469
+ else {
470
+ throw Error("This method exists only for L2 networks");
471
+ }
472
+ });
473
+ }
474
+ export function getGasInfoForL2() {
475
+ return __awaiter(this, void 0, void 0, function* () {
476
+ if (this.chainId === 42161) {
477
+ const baseFee = yield getBaseFeeByLastBlock.call(this);
478
+ return {
479
+ maxFeePerGas: Number(((baseFee * 1.1) + 0.01).toFixed(2)),
480
+ maxPriorityFeePerGas: 0.01,
389
481
  };
390
482
  }
391
- }
392
- return _usdRatesCache[assetId]['rate'];
393
- });
394
- export const getUsdRate = (coin) => __awaiter(void 0, void 0, void 0, function* () {
395
- const [coinAddress] = _getCoinAddressesNoCheck(coin);
396
- return yield _getUsdRate(coinAddress);
397
- });
398
- export const getBaseFeeByLastBlock = () => __awaiter(void 0, void 0, void 0, function* () {
399
- const provider = curve.provider;
400
- try {
401
- const block = yield provider.getBlock('latest');
402
- if (!block) {
403
- return 0.01;
404
- }
405
- return Number(block.baseFeePerGas) / (Math.pow(10, 9));
406
- }
407
- catch (error) {
408
- throw new Error(error);
409
- }
410
- });
411
- export const getGasPrice = () => __awaiter(void 0, void 0, void 0, function* () {
412
- const provider = curve.provider;
413
- return Number((Number((yield provider.getFeeData()).gasPrice) / 1e9).toFixed(2));
414
- });
415
- export const getGasPriceFromL1 = () => __awaiter(void 0, void 0, void 0, function* () {
416
- if (L2Networks.includes(curve.chainId) && curve.L1WeightedGasPrice) {
417
- return curve.L1WeightedGasPrice + 1e9; // + 1 gwei
418
- }
419
- else {
420
- throw Error("This method exists only for L2 networks");
421
- }
422
- });
423
- export const getGasPriceFromL2 = () => __awaiter(void 0, void 0, void 0, function* () {
424
- if (curve.chainId === 42161) {
425
- return yield getBaseFeeByLastBlock();
426
- }
427
- if (curve.chainId === 196) {
428
- return yield getGasPrice(); // gwei
429
- }
430
- if (curve.chainId === 324) {
431
- return yield getGasPrice(); // gwei
432
- }
433
- if (curve.chainId === 5000) {
434
- return yield getGasPrice(); // gwei
435
- }
436
- if (L2Networks.includes(curve.chainId)) {
437
- const gasPrice = yield curve.contracts[curve.constants.ALIASES.gas_oracle_blob].contract.gasPrice({ "gasPrice": "0x2000000" });
438
- return Number(gasPrice);
439
- }
440
- else {
441
- throw Error("This method exists only for L2 networks");
442
- }
443
- });
444
- export const getGasInfoForL2 = () => __awaiter(void 0, void 0, void 0, function* () {
445
- if (curve.chainId === 42161) {
446
- const baseFee = yield getBaseFeeByLastBlock();
447
- return {
448
- maxFeePerGas: Number(((baseFee * 1.1) + 0.01).toFixed(2)),
449
- maxPriorityFeePerGas: 0.01,
450
- };
451
- }
452
- else if (curve.chainId === 196) {
453
- const gasPrice = yield getGasPrice();
454
- return {
455
- gasPrice,
456
- };
457
- }
458
- else if (curve.chainId === 324) {
459
- const gasPrice = yield getGasPrice();
460
- return {
461
- gasPrice,
462
- };
463
- }
464
- else if (curve.chainId === 5000) {
465
- const baseFee = yield getBaseFeeByLastBlock();
466
- return {
467
- maxFeePerGas: Number(((baseFee * 1.1) + 0.01).toFixed(2)),
468
- maxPriorityFeePerGas: 0.01,
469
- };
470
- }
471
- else {
472
- throw Error("This method exists only for L2 networks");
473
- }
474
- });
483
+ else if (this.chainId === 196) {
484
+ const gasPrice = yield getGasPrice.call(this);
485
+ return {
486
+ gasPrice,
487
+ };
488
+ }
489
+ else if (this.chainId === 324) {
490
+ const gasPrice = yield getGasPrice.call(this);
491
+ return {
492
+ gasPrice,
493
+ };
494
+ }
495
+ else if (this.chainId === 5000) {
496
+ const baseFee = yield getBaseFeeByLastBlock.call(this);
497
+ return {
498
+ maxFeePerGas: Number(((baseFee * 1.1) + 0.01).toFixed(2)),
499
+ maxPriorityFeePerGas: 0.01,
500
+ };
501
+ }
502
+ else {
503
+ throw Error("This method exists only for L2 networks");
504
+ }
505
+ });
506
+ }
475
507
  export const getTxCostsUsd = (ethUsdRate, gasPrice, gas, gasPriceL1 = 0) => {
476
508
  if (Array.isArray(gas)) {
477
509
  return ethUsdRate * ((gas[0] * gasPrice / 1e18) + (gas[1] * gasPriceL1 / 1e18));
@@ -480,61 +512,71 @@ export const getTxCostsUsd = (ethUsdRate, gasPrice, gas, gasPriceL1 = 0) => {
480
512
  return ethUsdRate * gas * gasPrice / 1e18;
481
513
  }
482
514
  };
483
- export const getCurveLiteNetworks = () => __awaiter(void 0, void 0, void 0, function* () {
484
- return yield _getCurveLiteNetworks();
485
- });
515
+ export function getCurveLiteNetworks() {
516
+ return __awaiter(this, void 0, void 0, function* () {
517
+ return yield _getCurveLiteNetworks();
518
+ });
519
+ }
486
520
  export const getNetworkNameByChainId = (chainId, networks) => {
487
521
  const network = networks.find((network) => network.chainId === chainId);
488
522
  return network ? network.id : "Unknown Network";
489
523
  };
490
- export const getNetworkConstants = (chainId) => __awaiter(void 0, void 0, void 0, function* () {
491
- if (chainId in NETWORK_CONSTANTS) {
492
- return Object.assign(Object.assign({}, NETWORK_CONSTANTS[chainId]), { IS_LITE_CHAIN: false });
493
- }
494
- else {
495
- const NAME = getNetworkNameByChainId(chainId, yield _getCurveLiteNetworks());
496
- if (NAME === "Unknown Network")
497
- throw Error(`Wrong chain id: ${chainId}`);
498
- return Object.assign(Object.assign({}, yield _getLiteNetworksData(NAME)), { NAME, IS_LITE_CHAIN: true });
499
- }
500
- });
501
- export const getTVL = (...args_1) => __awaiter(void 0, [...args_1], void 0, function* (chainId = curve.chainId) {
502
- const networkConstants = yield getNetworkConstants(chainId);
503
- const allTypesExtendedPoolData = yield _getAllPoolsFromApi(networkConstants.NAME, networkConstants.IS_LITE_CHAIN);
504
- return allTypesExtendedPoolData.reduce((sum, data) => { var _a, _b; return sum + ((_b = (_a = data.tvl) !== null && _a !== void 0 ? _a : data.tvlAll) !== null && _b !== void 0 ? _b : 0); }, 0);
505
- });
506
- export const getVolumeApiController = (network) => __awaiter(void 0, void 0, void 0, function* () {
507
- if (curve.isLiteChain && curve.chainId !== 146) {
508
- throw Error('This method is not supported for the lite version');
509
- }
510
- if (volumeNetworks.getVolumes.includes(curve.chainId)) {
511
- return yield _getVolumes(network);
512
- }
513
- if (volumeNetworks.getFactoryAPYs.includes(curve.chainId)) {
514
- return yield _getFactoryAPYs(network);
515
- }
516
- if (volumeNetworks.getSubgraphData.includes(curve.chainId)) {
517
- return yield _getSubgraphData(network);
518
- }
519
- throw Error(`Can't get volume for network: ${network}`);
520
- });
521
- export const getVolume = (...args_1) => __awaiter(void 0, [...args_1], void 0, function* (chainId = curve.chainId) {
522
- if (curve.isLiteChain && curve.chainId !== 146) {
523
- throw Error('This method is not supported for the lite version');
524
- }
525
- const networkConstants = yield getNetworkConstants(chainId);
526
- const { totalVolume, cryptoVolume, cryptoShare } = yield getVolumeApiController(networkConstants.NAME);
527
- return { totalVolume, cryptoVolume, cryptoShare };
528
- });
529
- export const _setContracts = (address, abi) => {
524
+ export function getNetworkConstants(chainId) {
525
+ return __awaiter(this, void 0, void 0, function* () {
526
+ if (chainId in NETWORK_CONSTANTS) {
527
+ return Object.assign(Object.assign({}, NETWORK_CONSTANTS[chainId]), { IS_LITE_CHAIN: false });
528
+ }
529
+ else {
530
+ const NAME = getNetworkNameByChainId(chainId, yield _getCurveLiteNetworks());
531
+ if (NAME === "Unknown Network")
532
+ throw Error(`Wrong chain id: ${chainId}`);
533
+ return Object.assign(Object.assign({}, yield _getLiteNetworksData(NAME)), { NAME, IS_LITE_CHAIN: true });
534
+ }
535
+ });
536
+ }
537
+ export function getTVL() {
538
+ return __awaiter(this, arguments, void 0, function* (chainId = this.chainId) {
539
+ const networkConstants = yield getNetworkConstants.call(this, chainId);
540
+ const allTypesExtendedPoolData = yield _getAllPoolsFromApi(networkConstants.NAME, this.isLiteChain);
541
+ return allTypesExtendedPoolData.reduce((sum, data) => { var _a, _b; return sum + ((_b = (_a = data.tvl) !== null && _a !== void 0 ? _a : data.tvlAll) !== null && _b !== void 0 ? _b : 0); }, 0);
542
+ });
543
+ }
544
+ export function getVolumeApiController(network) {
545
+ return __awaiter(this, void 0, void 0, function* () {
546
+ if (this.isLiteChain && this.chainId !== 146) {
547
+ throw Error('This method is not supported for the lite version');
548
+ }
549
+ if (volumeNetworks.getVolumes.includes(this.chainId)) {
550
+ return yield _getVolumes(network);
551
+ }
552
+ if (volumeNetworks.getFactoryAPYs.includes(this.chainId)) {
553
+ return yield _getFactoryAPYs(network);
554
+ }
555
+ if (volumeNetworks.getSubgraphData.includes(this.chainId)) {
556
+ return yield _getSubgraphData(network);
557
+ }
558
+ throw Error(`Can't get volume for network: ${network}`);
559
+ });
560
+ }
561
+ export function getVolume() {
562
+ return __awaiter(this, arguments, void 0, function* (chainId = this.chainId) {
563
+ if (this.isLiteChain && this.chainId !== 146) {
564
+ throw Error('This method is not supported for the lite version');
565
+ }
566
+ const networkConstants = yield getNetworkConstants.call(this, chainId);
567
+ const { totalVolume, cryptoVolume, cryptoShare } = yield getVolumeApiController.call(this, networkConstants.NAME);
568
+ return { totalVolume, cryptoVolume, cryptoShare };
569
+ });
570
+ }
571
+ export function _setContracts(address, abi) {
530
572
  const contracts = {
531
573
  abi,
532
- contract: new Contract(address, abi, curve.signer || curve.provider),
574
+ contract: new Contract(address, abi, this.signer || this.provider),
533
575
  multicallContract: new MulticallContract(address, abi),
534
576
  };
535
- curve.contracts[address] = contracts;
577
+ this.contracts[address] = contracts;
536
578
  return contracts;
537
- };
579
+ }
538
580
  // Find k for which x * k = target_x or y * k = target_y
539
581
  // k = max(target_x / x, target_y / y)
540
582
  // small_x = x * k
@@ -557,37 +599,39 @@ export const _get_price_impact = (_x, _y, _small_x, _small_y, x_decimals, y_deci
557
599
  return BN(0);
558
600
  return BN(1).minus(rateBN.div(smallRateBN)).times(100);
559
601
  };
560
- export const getCoinsData = (...coins) => __awaiter(void 0, void 0, void 0, function* () {
561
- if (coins.length == 1 && Array.isArray(coins[0]))
562
- coins = coins[0];
563
- coins = coins;
564
- const coinAddresses = _getCoinAddressesNoCheck(coins);
565
- console.log(coinAddresses);
566
- const ethIndex = getEthIndex(coinAddresses);
567
- if (ethIndex !== -1) {
568
- coinAddresses.splice(ethIndex, 1);
569
- }
570
- const contractCalls = [];
571
- for (const coinAddr of coinAddresses) {
572
- const coinContract = new MulticallContract(coinAddr, ERC20Abi);
573
- contractCalls.push(coinContract.name(), coinContract.symbol(), coinContract.decimals());
574
- }
575
- const _response = yield curve.multicallProvider.all(contractCalls);
576
- if (ethIndex !== -1) {
577
- _response.splice(ethIndex * 2, 0, ...['Ethereum', 'ETH', 18]);
578
- }
579
- const res = [];
580
- coins.forEach(() => {
581
- res.push({
582
- name: _response.shift(),
583
- symbol: _response.shift(),
584
- decimals: Number(curve.formatUnits(_response.shift(), 0)),
602
+ export function getCoinsData(...coins) {
603
+ return __awaiter(this, void 0, void 0, function* () {
604
+ if (coins.length == 1 && Array.isArray(coins[0]))
605
+ coins = coins[0];
606
+ coins = coins;
607
+ const coinAddresses = _getCoinAddressesNoCheck.call(this, coins);
608
+ console.log(coinAddresses);
609
+ const ethIndex = getEthIndex(coinAddresses);
610
+ if (ethIndex !== -1) {
611
+ coinAddresses.splice(ethIndex, 1);
612
+ }
613
+ const contractCalls = [];
614
+ for (const coinAddr of coinAddresses) {
615
+ const coinContract = new MulticallContract(coinAddr, ERC20Abi);
616
+ contractCalls.push(coinContract.name(), coinContract.symbol(), coinContract.decimals());
617
+ }
618
+ const _response = yield this.multicallProvider.all(contractCalls);
619
+ if (ethIndex !== -1) {
620
+ _response.splice(ethIndex * 2, 0, ...['Ethereum', 'ETH', 18]);
621
+ }
622
+ const res = [];
623
+ coins.forEach(() => {
624
+ res.push({
625
+ name: _response.shift(),
626
+ symbol: _response.shift(),
627
+ decimals: Number(this.formatUnits(_response.shift(), 0)),
628
+ });
585
629
  });
630
+ return res;
586
631
  });
587
- return res;
588
- });
589
- export const hasDepositAndStake = () => "deposit_and_stake" in curve.constants.ALIASES;
590
- export const hasRouter = () => "router" in curve.constants.ALIASES;
632
+ }
633
+ export function hasDepositAndStake() { return "deposit_and_stake" in this.constants.ALIASES; }
634
+ export function hasRouter() { return "router" in this.constants.ALIASES; }
591
635
  export const findAbiFunction = (abi, methodName) => abi.filter((item) => item.type == 'function' && item.name === methodName);
592
636
  export const getCountArgsOfMethodByAbi = (abi, methodName) => { var _a, _b; return (_b = (_a = findAbiFunction(abi, methodName)[0]) === null || _a === void 0 ? void 0 : _a.inputs.length) !== null && _b !== void 0 ? _b : -1; };
593
637
  export const findAbiSignature = (abi, methodName, signature) => findAbiFunction(abi, methodName).find((func) => func.inputs.map((i) => `${i.type}`).join(',') == signature);
@@ -609,28 +653,30 @@ export const assetTypeNameHandler = (assetTypeName) => {
609
653
  return assetTypeName.toUpperCase();
610
654
  }
611
655
  };
612
- export const getBasePools = () => __awaiter(void 0, void 0, void 0, function* () {
613
- const factoryContract = curve.contracts[curve.constants.ALIASES['stable_ng_factory']].contract;
614
- const factoryMulticallContract = curve.contracts[curve.constants.ALIASES['stable_ng_factory']].multicallContract;
615
- const basePoolCount = Number(curve.formatUnits(yield factoryContract.base_pool_count(), 0));
616
- const calls = [];
617
- for (let i = 0; i < basePoolCount; i++) {
618
- calls.push(factoryMulticallContract.base_pool_list(i));
619
- }
620
- const basePoolList = (yield curve.multicallProvider.all(calls)).map((item) => item.toLowerCase());
621
- const pools = Object.assign(Object.assign(Object.assign({}, curve.constants.STABLE_NG_FACTORY_POOLS_DATA), curve.constants.FACTORY_POOLS_DATA), curve.constants.POOLS_DATA);
622
- const basePoolIds = Object.keys(pools).filter((item) => basePoolList.includes(pools[item].swap_address));
623
- return basePoolIds.map((poolId) => {
624
- const pool = getPool(poolId);
625
- return {
626
- id: poolId,
627
- name: pool.name,
628
- pool: pool.address,
629
- token: pool.lpToken,
630
- coins: pool.underlyingCoinAddresses,
631
- };
656
+ export function getBasePools() {
657
+ return __awaiter(this, void 0, void 0, function* () {
658
+ const factoryContract = this.contracts[this.constants.ALIASES['stable_ng_factory']].contract;
659
+ const factoryMulticallContract = this.contracts[this.constants.ALIASES['stable_ng_factory']].multicallContract;
660
+ const basePoolCount = Number(this.formatUnits(yield factoryContract.base_pool_count(), 0));
661
+ const calls = [];
662
+ for (let i = 0; i < basePoolCount; i++) {
663
+ calls.push(factoryMulticallContract.base_pool_list(i));
664
+ }
665
+ const basePoolList = (yield this.multicallProvider.all(calls)).map((item) => item.toLowerCase());
666
+ const pools = Object.assign(Object.assign(Object.assign({}, this.constants.STABLE_NG_FACTORY_POOLS_DATA), this.constants.FACTORY_POOLS_DATA), this.constants.POOLS_DATA);
667
+ const basePoolIds = Object.keys(pools).filter((item) => basePoolList.includes(pools[item].swap_address));
668
+ return basePoolIds.map((poolId) => {
669
+ const pool = getPool.call(this, poolId);
670
+ return {
671
+ id: poolId,
672
+ name: pool.name,
673
+ pool: pool.address,
674
+ token: pool.lpToken,
675
+ coins: pool.underlyingCoinAddresses,
676
+ };
677
+ });
632
678
  });
633
- });
679
+ }
634
680
  export function log(fnName, ...args) {
635
681
  if (process.env.NODE_ENV === 'development') {
636
682
  console.log(`curve-js@${new Date().toISOString()} -> ${fnName}:`, ...args);