@curvefi/api 2.66.29 → 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.
- 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/utils.d.ts +14 -1
- package/lib/constants/utils.js +21 -6
- 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 +481 -435
- 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
|
|
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,318 @@ 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
|
-
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
|
-
|
|
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
|
-
};
|
|
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
|
-
|
|
374
|
-
|
|
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
|
-
|
|
383
|
-
|
|
384
|
-
|
|
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
|
-
|
|
387
|
-
|
|
388
|
-
|
|
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
|
-
|
|
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
|
-
});
|
|
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
|
|
484
|
-
return
|
|
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
|
|
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
|
-
|
|
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,
|
|
574
|
+
contract: new Contract(address, abi, this.signer || this.provider),
|
|
533
575
|
multicallContract: new MulticallContract(address, abi),
|
|
534
576
|
};
|
|
535
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
588
|
-
|
|
589
|
-
export
|
|
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
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
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);
|