@curvefi/api 2.66.30 → 2.67.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/boosting.d.ts +33 -32
- package/lib/boosting.js +320 -252
- package/lib/cached.d.ts +4 -3
- package/lib/cached.js +63 -19
- package/lib/constants/coins/hyperliquid.d.ts +2 -0
- package/lib/constants/coins/hyperliquid.js +5 -0
- package/lib/constants/coins/index.d.ts +2 -1
- package/lib/constants/coins/index.js +2 -1
- package/lib/constants/factory/crypto.d.ts +5 -0
- package/lib/constants/factory/crypto.js +8 -0
- package/lib/constants/factory/stable.js +3 -0
- package/lib/constants/network_constants.js +37 -11
- package/lib/constants/pools/hyperliquid.d.ts +2 -0
- package/lib/constants/pools/hyperliquid.js +2 -0
- package/lib/constants/pools/index.d.ts +2 -1
- package/lib/constants/pools/index.js +2 -1
- package/lib/constants/utils.d.ts +14 -1
- package/lib/constants/utils.js +21 -6
- package/lib/constants/volumeNetworks.js +1 -1
- package/lib/curve.d.ts +5 -5
- package/lib/curve.js +21 -38
- package/lib/dao.d.ts +32 -31
- package/lib/dao.js +420 -350
- package/lib/external-api.d.ts +1 -1
- package/lib/external-api.js +2 -2
- package/lib/factory/common.d.ts +2 -1
- package/lib/factory/common.js +1 -1
- package/lib/factory/deploy.d.ts +46 -45
- package/lib/factory/deploy.js +630 -551
- package/lib/factory/factory-api.d.ts +3 -2
- package/lib/factory/factory-api.js +22 -23
- package/lib/factory/factory-crypto.d.ts +1 -1
- package/lib/factory/factory-crypto.js +12 -15
- package/lib/factory/factory-tricrypto.d.ts +1 -1
- package/lib/factory/factory-tricrypto.js +14 -15
- package/lib/factory/factory-twocrypto.d.ts +1 -1
- package/lib/factory/factory-twocrypto.js +12 -13
- package/lib/factory/factory.d.ts +4 -3
- package/lib/factory/factory.js +21 -24
- package/lib/index.d.ts +413 -104
- package/lib/index.js +253 -257
- package/lib/interfaces.d.ts +2 -0
- package/lib/pools/PoolTemplate.d.ts +13 -12
- package/lib/pools/PoolTemplate.js +279 -285
- package/lib/pools/mixins/common.js +2 -2
- package/lib/pools/mixins/depositBalancedAmountsMixins.d.ts +12 -4
- package/lib/pools/mixins/depositBalancedAmountsMixins.js +1 -15
- package/lib/pools/mixins/depositMixins.d.ts +25 -5
- package/lib/pools/mixins/depositMixins.js +38 -76
- package/lib/pools/mixins/depositWrappedMixins.d.ts +10 -2
- package/lib/pools/mixins/depositWrappedMixins.js +17 -33
- package/lib/pools/mixins/poolBalancesMixin.d.ts +6 -2
- package/lib/pools/mixins/poolBalancesMixin.js +3 -5
- package/lib/pools/mixins/swapMixins.d.ts +20 -4
- package/lib/pools/mixins/swapMixins.js +36 -70
- package/lib/pools/mixins/swapWrappedMixins.d.ts +19 -4
- package/lib/pools/mixins/swapWrappedMixins.js +32 -60
- package/lib/pools/mixins/withdrawExpectedMixins.d.ts +12 -4
- package/lib/pools/mixins/withdrawExpectedMixins.js +6 -11
- package/lib/pools/mixins/withdrawImbalanceMixins.d.ts +20 -4
- package/lib/pools/mixins/withdrawImbalanceMixins.js +26 -53
- package/lib/pools/mixins/withdrawImbalanceWrappedMixins.d.ts +10 -2
- package/lib/pools/mixins/withdrawImbalanceWrappedMixins.js +12 -27
- package/lib/pools/mixins/withdrawMixins.d.ts +25 -5
- package/lib/pools/mixins/withdrawMixins.js +33 -67
- package/lib/pools/mixins/withdrawOneCoinExpectedMixins.d.ts +12 -4
- package/lib/pools/mixins/withdrawOneCoinExpectedMixins.js +8 -13
- package/lib/pools/mixins/withdrawOneCoinMixins.d.ts +25 -5
- package/lib/pools/mixins/withdrawOneCoinMixins.js +32 -66
- package/lib/pools/mixins/withdrawOneCoinWrappedExpectedMixins.d.ts +6 -2
- package/lib/pools/mixins/withdrawOneCoinWrappedExpectedMixins.js +4 -7
- package/lib/pools/mixins/withdrawOneCoinWrappedMixins.d.ts +10 -2
- package/lib/pools/mixins/withdrawOneCoinWrappedMixins.js +13 -29
- package/lib/pools/mixins/withdrawWrappedMixins.d.ts +10 -2
- package/lib/pools/mixins/withdrawWrappedMixins.js +14 -28
- package/lib/pools/poolConstructor.d.ts +2 -1
- package/lib/pools/poolConstructor.js +27 -28
- package/lib/pools/subClasses/corePool.d.ts +4 -1
- package/lib/pools/subClasses/corePool.js +5 -7
- package/lib/pools/subClasses/gaugePool.d.ts +5 -3
- package/lib/pools/subClasses/gaugePool.js +19 -18
- package/lib/pools/subClasses/statsPool.d.ts +2 -0
- package/lib/pools/subClasses/statsPool.js +22 -10
- package/lib/pools/subClasses/walletPool.d.ts +2 -1
- package/lib/pools/subClasses/walletPool.js +6 -6
- package/lib/pools/utils.d.ts +7 -6
- package/lib/pools/utils.js +316 -297
- package/lib/route-graph.worker.d.ts +2 -2
- package/lib/route-graph.worker.js +4 -6
- package/lib/router.d.ts +12 -11
- package/lib/router.js +331 -295
- package/lib/utils.d.ts +34 -33
- package/lib/utils.js +483 -435
- package/package.json +2 -2
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
|
|
40
|
-
return
|
|
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(
|
|
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
|
|
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 *
|
|
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,320 @@ export const gasSum = (gas, currentGas) => {
|
|
|
89
89
|
return gas;
|
|
90
90
|
};
|
|
91
91
|
// coins can be either addresses or symbols
|
|
92
|
-
export
|
|
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) =>
|
|
97
|
-
}
|
|
98
|
-
export
|
|
99
|
-
const coinAddresses = _getCoinAddressesNoCheck(...coins);
|
|
100
|
-
const availableAddresses = [...Object.keys(
|
|
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
|
|
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 =
|
|
112
|
-
}
|
|
113
|
-
export
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
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
|
-
|
|
139
|
+
balances[address] = _balances[address].map((b, i) => this.formatUnits(b, decimals[i]));
|
|
129
140
|
}
|
|
130
|
-
|
|
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
|
-
|
|
137
|
-
|
|
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 &&
|
|
146
|
-
addresses = [
|
|
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
|
|
151
|
-
address = address ||
|
|
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
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
_coins
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
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
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
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
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
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
|
-
|
|
217
|
-
}
|
|
227
|
+
return txHashes;
|
|
228
|
+
});
|
|
229
|
+
}
|
|
218
230
|
// coins can be either addresses or symbols
|
|
219
|
-
export
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
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 =
|
|
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
|
-
|
|
248
|
-
}
|
|
260
|
+
return getGasFromArray(gas);
|
|
261
|
+
});
|
|
262
|
+
}
|
|
249
263
|
// coins can be either addresses or symbols
|
|
250
|
-
export
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
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
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
for (const
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
.
|
|
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
|
-
|
|
277
|
-
}
|
|
293
|
+
return rewardsDict;
|
|
294
|
+
});
|
|
295
|
+
}
|
|
278
296
|
const _usdRatesCache = {};
|
|
279
|
-
export
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
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
|
+
999: 'hyperliquid',
|
|
319
|
+
1284: 'moonbeam',
|
|
320
|
+
2222: 'kava',
|
|
321
|
+
5000: 'mantle',
|
|
322
|
+
8453: 'base',
|
|
323
|
+
42220: 'celo',
|
|
324
|
+
43114: 'avalanche',
|
|
325
|
+
42161: 'arbitrum-one',
|
|
326
|
+
1313161554: 'aurora',
|
|
327
|
+
}[this.chainId];
|
|
328
|
+
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 : {
|
|
329
|
+
1: 'ethereum',
|
|
330
|
+
10: 'ethereum',
|
|
331
|
+
56: 'binancecoin',
|
|
332
|
+
100: 'xdai',
|
|
333
|
+
137: 'matic-network',
|
|
334
|
+
146: 'sonic-3',
|
|
335
|
+
196: 'okb',
|
|
336
|
+
250: 'fantom',
|
|
337
|
+
252: 'frax-share',
|
|
338
|
+
324: 'ethereum',
|
|
339
|
+
1284: 'moonbeam',
|
|
340
|
+
999: 'hyperliquid',
|
|
341
|
+
2222: 'kava',
|
|
342
|
+
5000: 'mantle',
|
|
343
|
+
8453: 'ethereum',
|
|
344
|
+
42220: 'celo',
|
|
345
|
+
43114: 'avalanche-2',
|
|
346
|
+
42161: 'ethereum',
|
|
347
|
+
1313161554: 'ethereum',
|
|
348
|
+
}[this.chainId];
|
|
349
|
+
if (chainName === undefined) {
|
|
350
|
+
throw Error('curve object is not initialized');
|
|
337
351
|
}
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
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
|
-
};
|
|
352
|
+
if (nativeTokenName === undefined) {
|
|
353
|
+
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) {
|
|
354
|
+
return pricesFromApi[(_e = this.constants.API_CONSTANTS) === null || _e === void 0 ? void 0 : _e.wrappedNativeTokenAddress.toLowerCase()];
|
|
371
355
|
}
|
|
372
356
|
else {
|
|
373
|
-
|
|
374
|
-
|
|
357
|
+
throw Error('nativeTokenName not found');
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
assetId = {
|
|
361
|
+
'CRV': 'curve-dao-token',
|
|
362
|
+
'EUR': 'stasis-eurs',
|
|
363
|
+
'BTC': 'bitcoin',
|
|
364
|
+
'ETH': 'ethereum',
|
|
365
|
+
'LINK': 'link',
|
|
366
|
+
}[assetId.toUpperCase()] || assetId;
|
|
367
|
+
assetId = isEth(assetId) ? nativeTokenName : assetId.toLowerCase();
|
|
368
|
+
// No EURT on Coingecko Polygon
|
|
369
|
+
if (this.chainId === 137 && assetId.toLowerCase() === this.constants.COINS.eurt) {
|
|
370
|
+
chainName = 'ethereum';
|
|
371
|
+
assetId = '0xC581b735A1688071A1746c968e0798D642EDE491'.toLowerCase(); // EURT Ethereum
|
|
372
|
+
}
|
|
373
|
+
// CRV
|
|
374
|
+
if (assetId.toLowerCase() === this.constants.ALIASES.crv) {
|
|
375
|
+
assetId = 'curve-dao-token';
|
|
376
|
+
}
|
|
377
|
+
if (this.isLiteChain && assetId.toLowerCase() === ((_f = this.constants.API_CONSTANTS) === null || _f === void 0 ? void 0 : _f.wrappedNativeTokenAddress.toLowerCase())) {
|
|
378
|
+
assetId = nativeTokenName;
|
|
379
|
+
}
|
|
380
|
+
if ((((_g = _usdRatesCache[assetId]) === null || _g === void 0 ? void 0 : _g.time) || 0) + 600000 < Date.now()) {
|
|
381
|
+
const url = [nativeTokenName, 'ethereum', 'bitcoin', 'link', 'curve-dao-token', 'stasis-eurs'].includes(assetId.toLowerCase()) ?
|
|
382
|
+
`https://api.coingecko.com/api/v3/simple/price?ids=${assetId}&vs_currencies=usd` :
|
|
383
|
+
`https://api.coingecko.com/api/v3/simple/token_price/${chainName}?contract_addresses=${assetId}&vs_currencies=usd`;
|
|
384
|
+
try {
|
|
385
|
+
const response = yield fetch(url);
|
|
386
|
+
const data = (_h = yield response.json()) !== null && _h !== void 0 ? _h : {};
|
|
387
|
+
if (response.status === 200 && ((_j = data[assetId]) === null || _j === void 0 ? void 0 : _j.usd) !== undefined) {
|
|
388
|
+
_usdRatesCache[assetId] = {
|
|
389
|
+
'rate': data[assetId].usd,
|
|
390
|
+
'time': Date.now(),
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
else {
|
|
394
|
+
if (!this.isLiteChain) {
|
|
395
|
+
console.warn(`Non-200 response for ${assetId}:`, response.status, data);
|
|
396
|
+
}
|
|
397
|
+
_usdRatesCache[assetId] = {
|
|
398
|
+
'rate': 0,
|
|
399
|
+
'time': Date.now(),
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
catch (err) {
|
|
404
|
+
if (!this.isLiteChain) {
|
|
405
|
+
console.error(`Error fetching USD rate for ${assetId}:`, err.message);
|
|
375
406
|
}
|
|
376
407
|
_usdRatesCache[assetId] = {
|
|
377
408
|
'rate': 0,
|
|
@@ -379,99 +410,102 @@ export const _getUsdRate = (assetId) => __awaiter(void 0, void 0, void 0, functi
|
|
|
379
410
|
};
|
|
380
411
|
}
|
|
381
412
|
}
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
413
|
+
return _usdRatesCache[assetId]['rate'];
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
export function getUsdRate(coin) {
|
|
417
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
418
|
+
const [coinAddress] = _getCoinAddressesNoCheck.call(this, coin);
|
|
419
|
+
return yield _getUsdRate.call(this, coinAddress);
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
export function getBaseFeeByLastBlock() {
|
|
423
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
424
|
+
const provider = this.provider;
|
|
425
|
+
try {
|
|
426
|
+
const block = yield provider.getBlock('latest');
|
|
427
|
+
if (!block) {
|
|
428
|
+
return 0.01;
|
|
385
429
|
}
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
430
|
+
return Number(block.baseFeePerGas) / (Math.pow(10, 9));
|
|
431
|
+
}
|
|
432
|
+
catch (error) {
|
|
433
|
+
throw new Error(error);
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
export function getGasPrice() {
|
|
438
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
439
|
+
const provider = this.provider;
|
|
440
|
+
return Number((Number((yield provider.getFeeData()).gasPrice) / 1e9).toFixed(2));
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
export function getGasPriceFromL1() {
|
|
444
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
445
|
+
if (L2Networks.includes(this.chainId) && this.L1WeightedGasPrice) {
|
|
446
|
+
return this.L1WeightedGasPrice + 1e9; // + 1 gwei
|
|
447
|
+
}
|
|
448
|
+
else {
|
|
449
|
+
throw Error("This method exists only for L2 networks");
|
|
450
|
+
}
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
export function getGasPriceFromL2() {
|
|
454
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
455
|
+
if (this.chainId === 42161) {
|
|
456
|
+
return yield getBaseFeeByLastBlock.call(this);
|
|
457
|
+
}
|
|
458
|
+
if (this.chainId === 196) {
|
|
459
|
+
return yield getGasPrice.call(this); // gwei
|
|
460
|
+
}
|
|
461
|
+
if (this.chainId === 324) {
|
|
462
|
+
return yield getGasPrice.call(this); // gwei
|
|
463
|
+
}
|
|
464
|
+
if (this.chainId === 5000) {
|
|
465
|
+
return yield getGasPrice.call(this); // gwei
|
|
466
|
+
}
|
|
467
|
+
if (L2Networks.includes(this.chainId)) {
|
|
468
|
+
const gasPrice = yield this.contracts[this.constants.ALIASES.gas_oracle_blob].contract.gasPrice({ "gasPrice": "0x2000000" });
|
|
469
|
+
return Number(gasPrice);
|
|
470
|
+
}
|
|
471
|
+
else {
|
|
472
|
+
throw Error("This method exists only for L2 networks");
|
|
473
|
+
}
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
export function getGasInfoForL2() {
|
|
477
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
478
|
+
if (this.chainId === 42161) {
|
|
479
|
+
const baseFee = yield getBaseFeeByLastBlock.call(this);
|
|
480
|
+
return {
|
|
481
|
+
maxFeePerGas: Number(((baseFee * 1.1) + 0.01).toFixed(2)),
|
|
482
|
+
maxPriorityFeePerGas: 0.01,
|
|
389
483
|
};
|
|
390
484
|
}
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
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
|
-
});
|
|
485
|
+
else if (this.chainId === 196) {
|
|
486
|
+
const gasPrice = yield getGasPrice.call(this);
|
|
487
|
+
return {
|
|
488
|
+
gasPrice,
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
else if (this.chainId === 324) {
|
|
492
|
+
const gasPrice = yield getGasPrice.call(this);
|
|
493
|
+
return {
|
|
494
|
+
gasPrice,
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
else if (this.chainId === 5000) {
|
|
498
|
+
const baseFee = yield getBaseFeeByLastBlock.call(this);
|
|
499
|
+
return {
|
|
500
|
+
maxFeePerGas: Number(((baseFee * 1.1) + 0.01).toFixed(2)),
|
|
501
|
+
maxPriorityFeePerGas: 0.01,
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
else {
|
|
505
|
+
throw Error("This method exists only for L2 networks");
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
}
|
|
475
509
|
export const getTxCostsUsd = (ethUsdRate, gasPrice, gas, gasPriceL1 = 0) => {
|
|
476
510
|
if (Array.isArray(gas)) {
|
|
477
511
|
return ethUsdRate * ((gas[0] * gasPrice / 1e18) + (gas[1] * gasPriceL1 / 1e18));
|
|
@@ -480,61 +514,71 @@ export const getTxCostsUsd = (ethUsdRate, gasPrice, gas, gasPriceL1 = 0) => {
|
|
|
480
514
|
return ethUsdRate * gas * gasPrice / 1e18;
|
|
481
515
|
}
|
|
482
516
|
};
|
|
483
|
-
export
|
|
484
|
-
return
|
|
485
|
-
|
|
517
|
+
export function getCurveLiteNetworks() {
|
|
518
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
519
|
+
return yield _getCurveLiteNetworks();
|
|
520
|
+
});
|
|
521
|
+
}
|
|
486
522
|
export const getNetworkNameByChainId = (chainId, networks) => {
|
|
487
523
|
const network = networks.find((network) => network.chainId === chainId);
|
|
488
524
|
return network ? network.id : "Unknown Network";
|
|
489
525
|
};
|
|
490
|
-
export
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
return
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
526
|
+
export function getNetworkConstants(chainId) {
|
|
527
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
528
|
+
if (chainId in NETWORK_CONSTANTS) {
|
|
529
|
+
return Object.assign(Object.assign({}, NETWORK_CONSTANTS[chainId]), { IS_LITE_CHAIN: false });
|
|
530
|
+
}
|
|
531
|
+
else {
|
|
532
|
+
const NAME = getNetworkNameByChainId(chainId, yield _getCurveLiteNetworks());
|
|
533
|
+
if (NAME === "Unknown Network")
|
|
534
|
+
throw Error(`Wrong chain id: ${chainId}`);
|
|
535
|
+
return Object.assign(Object.assign({}, yield _getLiteNetworksData(NAME)), { NAME, IS_LITE_CHAIN: true });
|
|
536
|
+
}
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
export function getTVL() {
|
|
540
|
+
return __awaiter(this, arguments, void 0, function* (chainId = this.chainId) {
|
|
541
|
+
const networkConstants = yield getNetworkConstants.call(this, chainId);
|
|
542
|
+
const allTypesExtendedPoolData = yield _getAllPoolsFromApi(networkConstants.NAME, this.isLiteChain);
|
|
543
|
+
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);
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
export function getVolumeApiController(network) {
|
|
547
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
548
|
+
if (this.isLiteChain && this.chainId !== 146) {
|
|
549
|
+
throw Error('This method is not supported for the lite version');
|
|
550
|
+
}
|
|
551
|
+
if (volumeNetworks.getVolumes.includes(this.chainId)) {
|
|
552
|
+
return yield _getVolumes(network);
|
|
553
|
+
}
|
|
554
|
+
if (volumeNetworks.getFactoryAPYs.includes(this.chainId)) {
|
|
555
|
+
return yield _getFactoryAPYs(network);
|
|
556
|
+
}
|
|
557
|
+
if (volumeNetworks.getSubgraphData.includes(this.chainId)) {
|
|
558
|
+
return yield _getSubgraphData(network);
|
|
559
|
+
}
|
|
560
|
+
throw Error(`Can't get volume for network: ${network}`);
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
export function getVolume() {
|
|
564
|
+
return __awaiter(this, arguments, void 0, function* (chainId = this.chainId) {
|
|
565
|
+
if (this.isLiteChain && this.chainId !== 146) {
|
|
566
|
+
throw Error('This method is not supported for the lite version');
|
|
567
|
+
}
|
|
568
|
+
const networkConstants = yield getNetworkConstants.call(this, chainId);
|
|
569
|
+
const { totalVolume, cryptoVolume, cryptoShare } = yield getVolumeApiController.call(this, networkConstants.NAME);
|
|
570
|
+
return { totalVolume, cryptoVolume, cryptoShare };
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
export function _setContracts(address, abi) {
|
|
530
574
|
const contracts = {
|
|
531
575
|
abi,
|
|
532
|
-
contract: new Contract(address, abi,
|
|
576
|
+
contract: new Contract(address, abi, this.signer || this.provider),
|
|
533
577
|
multicallContract: new MulticallContract(address, abi),
|
|
534
578
|
};
|
|
535
|
-
|
|
579
|
+
this.contracts[address] = contracts;
|
|
536
580
|
return contracts;
|
|
537
|
-
}
|
|
581
|
+
}
|
|
538
582
|
// Find k for which x * k = target_x or y * k = target_y
|
|
539
583
|
// k = max(target_x / x, target_y / y)
|
|
540
584
|
// small_x = x * k
|
|
@@ -557,37 +601,39 @@ export const _get_price_impact = (_x, _y, _small_x, _small_y, x_decimals, y_deci
|
|
|
557
601
|
return BN(0);
|
|
558
602
|
return BN(1).minus(rateBN.div(smallRateBN)).times(100);
|
|
559
603
|
};
|
|
560
|
-
export
|
|
561
|
-
|
|
562
|
-
coins
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
const
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
604
|
+
export function getCoinsData(...coins) {
|
|
605
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
606
|
+
if (coins.length == 1 && Array.isArray(coins[0]))
|
|
607
|
+
coins = coins[0];
|
|
608
|
+
coins = coins;
|
|
609
|
+
const coinAddresses = _getCoinAddressesNoCheck.call(this, coins);
|
|
610
|
+
console.log(coinAddresses);
|
|
611
|
+
const ethIndex = getEthIndex(coinAddresses);
|
|
612
|
+
if (ethIndex !== -1) {
|
|
613
|
+
coinAddresses.splice(ethIndex, 1);
|
|
614
|
+
}
|
|
615
|
+
const contractCalls = [];
|
|
616
|
+
for (const coinAddr of coinAddresses) {
|
|
617
|
+
const coinContract = new MulticallContract(coinAddr, ERC20Abi);
|
|
618
|
+
contractCalls.push(coinContract.name(), coinContract.symbol(), coinContract.decimals());
|
|
619
|
+
}
|
|
620
|
+
const _response = yield this.multicallProvider.all(contractCalls);
|
|
621
|
+
if (ethIndex !== -1) {
|
|
622
|
+
_response.splice(ethIndex * 2, 0, ...['Ethereum', 'ETH', 18]);
|
|
623
|
+
}
|
|
624
|
+
const res = [];
|
|
625
|
+
coins.forEach(() => {
|
|
626
|
+
res.push({
|
|
627
|
+
name: _response.shift(),
|
|
628
|
+
symbol: _response.shift(),
|
|
629
|
+
decimals: Number(this.formatUnits(_response.shift(), 0)),
|
|
630
|
+
});
|
|
585
631
|
});
|
|
632
|
+
return res;
|
|
586
633
|
});
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
export
|
|
590
|
-
export const hasRouter = () => "router" in curve.constants.ALIASES;
|
|
634
|
+
}
|
|
635
|
+
export function hasDepositAndStake() { return "deposit_and_stake" in this.constants.ALIASES; }
|
|
636
|
+
export function hasRouter() { return "router" in this.constants.ALIASES; }
|
|
591
637
|
export const findAbiFunction = (abi, methodName) => abi.filter((item) => item.type == 'function' && item.name === methodName);
|
|
592
638
|
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
639
|
export const findAbiSignature = (abi, methodName, signature) => findAbiFunction(abi, methodName).find((func) => func.inputs.map((i) => `${i.type}`).join(',') == signature);
|
|
@@ -609,28 +655,30 @@ export const assetTypeNameHandler = (assetTypeName) => {
|
|
|
609
655
|
return assetTypeName.toUpperCase();
|
|
610
656
|
}
|
|
611
657
|
};
|
|
612
|
-
export
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
658
|
+
export function getBasePools() {
|
|
659
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
660
|
+
const factoryContract = this.contracts[this.constants.ALIASES['stable_ng_factory']].contract;
|
|
661
|
+
const factoryMulticallContract = this.contracts[this.constants.ALIASES['stable_ng_factory']].multicallContract;
|
|
662
|
+
const basePoolCount = Number(this.formatUnits(yield factoryContract.base_pool_count(), 0));
|
|
663
|
+
const calls = [];
|
|
664
|
+
for (let i = 0; i < basePoolCount; i++) {
|
|
665
|
+
calls.push(factoryMulticallContract.base_pool_list(i));
|
|
666
|
+
}
|
|
667
|
+
const basePoolList = (yield this.multicallProvider.all(calls)).map((item) => item.toLowerCase());
|
|
668
|
+
const pools = Object.assign(Object.assign(Object.assign({}, this.constants.STABLE_NG_FACTORY_POOLS_DATA), this.constants.FACTORY_POOLS_DATA), this.constants.POOLS_DATA);
|
|
669
|
+
const basePoolIds = Object.keys(pools).filter((item) => basePoolList.includes(pools[item].swap_address));
|
|
670
|
+
return basePoolIds.map((poolId) => {
|
|
671
|
+
const pool = getPool.call(this, poolId);
|
|
672
|
+
return {
|
|
673
|
+
id: poolId,
|
|
674
|
+
name: pool.name,
|
|
675
|
+
pool: pool.address,
|
|
676
|
+
token: pool.lpToken,
|
|
677
|
+
coins: pool.underlyingCoinAddresses,
|
|
678
|
+
};
|
|
679
|
+
});
|
|
632
680
|
});
|
|
633
|
-
}
|
|
681
|
+
}
|
|
634
682
|
export function log(fnName, ...args) {
|
|
635
683
|
if (process.env.NODE_ENV === 'development') {
|
|
636
684
|
console.log(`curve-js@${new Date().toISOString()} -> ${fnName}:`, ...args);
|