@curvefi/api 2.62.0 → 2.63.1
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.js +247 -645
- package/lib/constants/L2Networks.js +1 -1
- package/lib/constants/aliases.js +31 -31
- package/lib/constants/coins/arbitrum.js +5 -5
- package/lib/constants/coins/aurora.js +5 -5
- package/lib/constants/coins/avalanche.js +6 -6
- package/lib/constants/coins/base.js +5 -5
- package/lib/constants/coins/bsc.js +5 -5
- package/lib/constants/coins/celo.js +5 -5
- package/lib/constants/coins/ethereum.js +9 -9
- package/lib/constants/coins/fantom.js +7 -7
- package/lib/constants/coins/fraxtal.js +5 -5
- package/lib/constants/coins/kava.js +5 -5
- package/lib/constants/coins/mantle.js +5 -5
- package/lib/constants/coins/moonbeam.js +5 -5
- package/lib/constants/coins/optimism.js +5 -5
- package/lib/constants/coins/polygon.js +6 -6
- package/lib/constants/coins/xdai.js +5 -5
- package/lib/constants/coins/xlayer.js +5 -5
- package/lib/constants/coins/zksync.js +5 -5
- package/lib/constants/pools/arbitrum.js +1 -1
- package/lib/constants/pools/aurora.js +1 -1
- package/lib/constants/pools/avalanche.js +1 -1
- package/lib/constants/pools/base.js +1 -1
- package/lib/constants/pools/bsc.js +1 -1
- package/lib/constants/pools/celo.js +1 -1
- package/lib/constants/pools/ethereum.js +2 -2
- package/lib/constants/pools/fantom.js +1 -1
- package/lib/constants/pools/fraxtal.js +1 -1
- package/lib/constants/pools/kava.js +1 -1
- package/lib/constants/pools/mantle.js +1 -1
- package/lib/constants/pools/moonbeam.js +1 -1
- package/lib/constants/pools/optimism.js +1 -1
- package/lib/constants/pools/polygon.js +1 -1
- package/lib/constants/pools/xdai.js +1 -1
- package/lib/constants/pools/xlayer.js +1 -1
- package/lib/constants/pools/zksync.js +1 -1
- package/lib/constants/tricryptoDeployImplementations.js +3 -3
- package/lib/constants/utils.js +18 -19
- package/lib/constants/volumeNetworks.js +1 -1
- package/lib/curve.d.ts +3 -1
- package/lib/curve.js +534 -909
- package/lib/dao.js +351 -705
- package/lib/external-api.js +127 -256
- package/lib/factory/common.js +4 -4
- package/lib/factory/constants-crypto.js +33 -33
- package/lib/factory/constants.js +36 -46
- package/lib/factory/deploy.js +542 -907
- package/lib/factory/factory-api.js +205 -269
- package/lib/factory/factory-crypto.js +202 -342
- package/lib/factory/factory-tricrypto.js +164 -286
- package/lib/factory/factory-twocrypto.js +151 -269
- package/lib/factory/factory.js +245 -385
- package/lib/index.js +109 -198
- package/lib/interfaces.d.ts +1 -6
- package/lib/pools/PoolTemplate.js +1773 -3025
- package/lib/pools/gaugePool.js +112 -251
- package/lib/pools/mixins/common.js +22 -93
- package/lib/pools/mixins/depositBalancedAmountsMixins.js +52 -118
- package/lib/pools/mixins/depositMixins.js +160 -386
- package/lib/pools/mixins/depositWrappedMixins.js +79 -205
- package/lib/pools/mixins/poolBalancesMixin.js +24 -87
- package/lib/pools/mixins/swapMixins.js +139 -324
- package/lib/pools/mixins/swapWrappedMixins.js +111 -265
- package/lib/pools/mixins/withdrawExpectedMixins.js +27 -91
- package/lib/pools/mixins/withdrawImbalanceMixins.js +111 -293
- package/lib/pools/mixins/withdrawImbalanceWrappedMixins.js +58 -169
- package/lib/pools/mixins/withdrawMixins.js +139 -359
- package/lib/pools/mixins/withdrawOneCoinExpectedMixins.js +20 -75
- package/lib/pools/mixins/withdrawOneCoinMixins.js +140 -360
- package/lib/pools/mixins/withdrawOneCoinWrappedExpectedMixins.js +10 -51
- package/lib/pools/mixins/withdrawOneCoinWrappedMixins.js +60 -167
- package/lib/pools/mixins/withdrawWrappedMixins.js +57 -167
- package/lib/pools/poolConstructor.js +5 -25
- package/lib/pools/utils.js +301 -469
- package/lib/route-finder.worker.d.ts +9 -0
- package/lib/route-finder.worker.js +112 -0
- package/lib/route-graph.worker.d.ts +11 -0
- package/lib/route-graph.worker.js +334 -0
- package/lib/router.js +323 -912
- package/lib/utils.d.ts +5 -4
- package/lib/utils.js +539 -906
- package/package.json +1 -1
package/lib/utils.js
CHANGED
|
@@ -1,14 +1,3 @@
|
|
|
1
|
-
var __assign = (this && this.__assign) || function () {
|
|
2
|
-
__assign = Object.assign || function(t) {
|
|
3
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
-
s = arguments[i];
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
1
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
2
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
3
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -18,94 +7,53 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
18
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
19
8
|
});
|
|
20
9
|
};
|
|
21
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
22
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
23
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
24
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
25
|
-
function step(op) {
|
|
26
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
27
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
28
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
29
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
30
|
-
switch (op[0]) {
|
|
31
|
-
case 0: case 1: t = op; break;
|
|
32
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
33
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
34
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
35
|
-
default:
|
|
36
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
37
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
38
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
39
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
40
|
-
if (t[2]) _.ops.pop();
|
|
41
|
-
_.trys.pop(); continue;
|
|
42
|
-
}
|
|
43
|
-
op = body.call(thisArg, _);
|
|
44
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
45
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
49
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
50
|
-
if (ar || !(i in from)) {
|
|
51
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
52
|
-
ar[i] = from[i];
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
56
|
-
};
|
|
57
10
|
import axios from 'axios';
|
|
58
11
|
import { Contract } from 'ethers';
|
|
59
12
|
import { Contract as MulticallContract } from "@curvefi/ethcall";
|
|
60
13
|
import BigNumber from 'bignumber.js';
|
|
61
14
|
import { curve, NETWORK_CONSTANTS } from "./curve.js";
|
|
62
|
-
import { _getAllPoolsFromApi, _getFactoryAPYs, _getSubgraphData, _getVolumes
|
|
15
|
+
import { _getAllPoolsFromApi, _getFactoryAPYs, _getSubgraphData, _getVolumes } from "./external-api.js";
|
|
63
16
|
import ERC20Abi from './constants/abis/ERC20.json' assert { type: 'json' };
|
|
64
17
|
import { L2Networks } from './constants/L2Networks.js';
|
|
65
18
|
import { volumeNetworks } from "./constants/volumeNetworks.js";
|
|
66
19
|
import { getPool } from "./pools/index.js";
|
|
67
|
-
export
|
|
20
|
+
export const ETH_ADDRESS = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
|
|
68
21
|
// export const MAX_ALLOWANCE = curve.parseUnits(new BigNumber(2).pow(256).minus(1).toFixed(), 0);
|
|
69
|
-
export
|
|
22
|
+
export const MAX_ALLOWANCE = BigInt("115792089237316195423570985008687907853269984665640564039457584007913129639935"); // 2**256 - 1
|
|
70
23
|
// Formatting numbers
|
|
71
|
-
export
|
|
24
|
+
export const _cutZeros = (strn) => {
|
|
72
25
|
return strn.replace(/(\.\d*[1-9])0+$/gi, '$1').replace(/\.0+$/gi, '');
|
|
73
26
|
};
|
|
74
|
-
export
|
|
27
|
+
export const checkNumber = (n) => {
|
|
75
28
|
if (Number(n) !== Number(n))
|
|
76
|
-
throw Error(
|
|
29
|
+
throw Error(`${n} is not a number`); // NaN
|
|
77
30
|
return n;
|
|
78
31
|
};
|
|
79
|
-
export
|
|
80
|
-
if (decimals === void 0) { decimals = 18; }
|
|
32
|
+
export const formatNumber = (n, decimals = 18) => {
|
|
81
33
|
if (Number(n) !== Number(n))
|
|
82
|
-
throw Error(
|
|
83
|
-
|
|
34
|
+
throw Error(`${n} is not a number`); // NaN
|
|
35
|
+
const [integer, fractional] = String(n).split(".");
|
|
84
36
|
return !fractional ? integer : integer + "." + fractional.slice(0, decimals);
|
|
85
37
|
};
|
|
86
|
-
export
|
|
87
|
-
if (decimals === void 0) { decimals = 18; }
|
|
38
|
+
export const parseUnits = (n, decimals = 18) => {
|
|
88
39
|
return curve.parseUnits(formatNumber(n, decimals), decimals);
|
|
89
40
|
};
|
|
90
41
|
// bignumber.js
|
|
91
|
-
export
|
|
92
|
-
export
|
|
93
|
-
if (decimals === void 0) { decimals = 18; }
|
|
42
|
+
export const BN = (val) => new BigNumber(checkNumber(val));
|
|
43
|
+
export const toBN = (n, decimals = 18) => {
|
|
94
44
|
return BN(curve.formatUnits(n, decimals));
|
|
95
45
|
};
|
|
96
|
-
export
|
|
97
|
-
if (decimals === void 0) { decimals = 18; }
|
|
46
|
+
export const toStringFromBN = (bn, decimals = 18) => {
|
|
98
47
|
return bn.toFixed(decimals);
|
|
99
48
|
};
|
|
100
|
-
export
|
|
101
|
-
if (decimals === void 0) { decimals = 18; }
|
|
49
|
+
export const fromBN = (bn, decimals = 18) => {
|
|
102
50
|
return curve.parseUnits(toStringFromBN(bn, decimals), decimals);
|
|
103
51
|
};
|
|
104
52
|
// -------------------
|
|
105
|
-
export
|
|
106
|
-
export
|
|
107
|
-
export
|
|
108
|
-
export
|
|
53
|
+
export const isEth = (address) => address.toLowerCase() === ETH_ADDRESS.toLowerCase();
|
|
54
|
+
export const getEthIndex = (addresses) => addresses.map((address) => address.toLowerCase()).indexOf(ETH_ADDRESS.toLowerCase());
|
|
55
|
+
export const mulBy1_3 = (n) => n * curve.parseUnits("130", 0) / curve.parseUnits("100", 0);
|
|
56
|
+
export const smartNumber = (abstractNumber) => {
|
|
109
57
|
if (Array.isArray(abstractNumber)) {
|
|
110
58
|
return [Number(abstractNumber[0]), Number(abstractNumber[1])];
|
|
111
59
|
}
|
|
@@ -113,7 +61,7 @@ export var smartNumber = function (abstractNumber) {
|
|
|
113
61
|
return Number(abstractNumber);
|
|
114
62
|
}
|
|
115
63
|
};
|
|
116
|
-
export
|
|
64
|
+
export const DIGas = (gas) => {
|
|
117
65
|
if (Array.isArray(gas)) {
|
|
118
66
|
return gas[0];
|
|
119
67
|
}
|
|
@@ -121,7 +69,7 @@ export var DIGas = function (gas) {
|
|
|
121
69
|
return gas;
|
|
122
70
|
}
|
|
123
71
|
};
|
|
124
|
-
export
|
|
72
|
+
export const getGasFromArray = (gas) => {
|
|
125
73
|
if (gas[1] === 0) {
|
|
126
74
|
return gas[0];
|
|
127
75
|
}
|
|
@@ -129,7 +77,7 @@ export var getGasFromArray = function (gas) {
|
|
|
129
77
|
return gas;
|
|
130
78
|
}
|
|
131
79
|
};
|
|
132
|
-
export
|
|
80
|
+
export const gasSum = (gas, currentGas) => {
|
|
133
81
|
if (Array.isArray(currentGas)) {
|
|
134
82
|
gas[0] = gas[0] + currentGas[0];
|
|
135
83
|
gas[1] = gas[1] + currentGas[1];
|
|
@@ -140,689 +88,445 @@ export var gasSum = function (gas, currentGas) {
|
|
|
140
88
|
return gas;
|
|
141
89
|
};
|
|
142
90
|
// coins can be either addresses or symbols
|
|
143
|
-
export
|
|
144
|
-
var coins = [];
|
|
145
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
146
|
-
coins[_i] = arguments[_i];
|
|
147
|
-
}
|
|
91
|
+
export const _getCoinAddressesNoCheck = (...coins) => {
|
|
148
92
|
if (coins.length == 1 && Array.isArray(coins[0]))
|
|
149
93
|
coins = coins[0];
|
|
150
94
|
coins = coins;
|
|
151
|
-
return coins.map(
|
|
95
|
+
return coins.map((c) => c.toLowerCase()).map((c) => curve.constants.COINS[c] || c);
|
|
152
96
|
};
|
|
153
|
-
export
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
}
|
|
158
|
-
var coinAddresses = _getCoinAddressesNoCheck.apply(void 0, coins);
|
|
159
|
-
var availableAddresses = __spreadArray(__spreadArray([], Object.keys(curve.constants.DECIMALS), true), curve.constants.GAUGES, true);
|
|
160
|
-
for (var _a = 0, coinAddresses_1 = coinAddresses; _a < coinAddresses_1.length; _a++) {
|
|
161
|
-
var coinAddr = coinAddresses_1[_a];
|
|
97
|
+
export const _getCoinAddresses = (...coins) => {
|
|
98
|
+
const coinAddresses = _getCoinAddressesNoCheck(...coins);
|
|
99
|
+
const availableAddresses = [...Object.keys(curve.constants.DECIMALS), ...curve.constants.GAUGES];
|
|
100
|
+
for (const coinAddr of coinAddresses) {
|
|
162
101
|
if (!availableAddresses.includes(coinAddr))
|
|
163
|
-
throw Error(
|
|
102
|
+
throw Error(`Coin with address '${coinAddr}' is not available`);
|
|
164
103
|
}
|
|
165
104
|
return coinAddresses;
|
|
166
105
|
};
|
|
167
|
-
export
|
|
168
|
-
var coinAddresses = [];
|
|
169
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
170
|
-
coinAddresses[_i] = arguments[_i];
|
|
171
|
-
}
|
|
106
|
+
export const _getCoinDecimals = (...coinAddresses) => {
|
|
172
107
|
if (coinAddresses.length == 1 && Array.isArray(coinAddresses[0]))
|
|
173
108
|
coinAddresses = coinAddresses[0];
|
|
174
109
|
coinAddresses = coinAddresses;
|
|
175
|
-
return coinAddresses.map(
|
|
176
|
-
};
|
|
177
|
-
export
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
coinAddr = coinAddresses_2[_i];
|
|
194
|
-
_loop_1(coinAddr);
|
|
195
|
-
}
|
|
196
|
-
return [4 /*yield*/, curve.multicallProvider.all(contractCalls)];
|
|
197
|
-
case 1:
|
|
198
|
-
_response = _e.sent();
|
|
199
|
-
if (!(ethIndex !== -1)) return [3 /*break*/, 6];
|
|
200
|
-
ethBalances = [];
|
|
201
|
-
_a = 0, addresses_1 = addresses;
|
|
202
|
-
_e.label = 2;
|
|
203
|
-
case 2:
|
|
204
|
-
if (!(_a < addresses_1.length)) return [3 /*break*/, 5];
|
|
205
|
-
address = addresses_1[_a];
|
|
206
|
-
_c = (_b = ethBalances).push;
|
|
207
|
-
return [4 /*yield*/, curve.provider.getBalance(address)];
|
|
208
|
-
case 3:
|
|
209
|
-
_c.apply(_b, [_e.sent()]);
|
|
210
|
-
_e.label = 4;
|
|
211
|
-
case 4:
|
|
212
|
-
_a++;
|
|
213
|
-
return [3 /*break*/, 2];
|
|
214
|
-
case 5:
|
|
215
|
-
_response.splice.apply(_response, __spreadArray([ethIndex * addresses.length, 0], ethBalances, false));
|
|
216
|
-
_e.label = 6;
|
|
217
|
-
case 6:
|
|
218
|
-
_balances = {};
|
|
219
|
-
addresses.forEach(function (address, i) {
|
|
220
|
-
_balances[address] = coins.map(function (_, j) { return _response[i + (j * addresses.length)]; });
|
|
221
|
-
});
|
|
222
|
-
balances = {};
|
|
223
|
-
for (_d = 0, addresses_2 = addresses; _d < addresses_2.length; _d++) {
|
|
224
|
-
address = addresses_2[_d];
|
|
225
|
-
balances[address] = _balances[address].map(function (b, i) { return curve.formatUnits(b, decimals[i]); });
|
|
226
|
-
}
|
|
227
|
-
return [2 /*return*/, balances];
|
|
110
|
+
return coinAddresses.map((coinAddr) => { var _a; return (_a = curve.constants.DECIMALS[coinAddr.toLowerCase()]) !== null && _a !== void 0 ? _a : 18; }); // 18 for gauges
|
|
111
|
+
};
|
|
112
|
+
export const _getBalances = (coins, addresses) => __awaiter(void 0, void 0, void 0, function* () {
|
|
113
|
+
const coinAddresses = _getCoinAddresses(coins);
|
|
114
|
+
const decimals = _getCoinDecimals(coinAddresses);
|
|
115
|
+
const ethIndex = getEthIndex(coinAddresses);
|
|
116
|
+
if (ethIndex !== -1) {
|
|
117
|
+
coinAddresses.splice(ethIndex, 1);
|
|
118
|
+
}
|
|
119
|
+
const contractCalls = [];
|
|
120
|
+
for (const coinAddr of coinAddresses) {
|
|
121
|
+
contractCalls.push(...addresses.map((address) => curve.contracts[coinAddr].multicallContract.balanceOf(address)));
|
|
122
|
+
}
|
|
123
|
+
const _response = yield curve.multicallProvider.all(contractCalls);
|
|
124
|
+
if (ethIndex !== -1) {
|
|
125
|
+
const ethBalances = [];
|
|
126
|
+
for (const address of addresses) {
|
|
127
|
+
ethBalances.push(yield curve.provider.getBalance(address));
|
|
228
128
|
}
|
|
129
|
+
_response.splice(ethIndex * addresses.length, 0, ...ethBalances);
|
|
130
|
+
}
|
|
131
|
+
const _balances = {};
|
|
132
|
+
addresses.forEach((address, i) => {
|
|
133
|
+
_balances[address] = coins.map((_, j) => _response[i + (j * addresses.length)]);
|
|
229
134
|
});
|
|
230
|
-
|
|
231
|
-
|
|
135
|
+
const balances = {};
|
|
136
|
+
for (const address of addresses) {
|
|
137
|
+
balances[address] = _balances[address].map((b, i) => curve.formatUnits(b, decimals[i]));
|
|
138
|
+
}
|
|
139
|
+
return balances;
|
|
140
|
+
});
|
|
141
|
+
export const _prepareAddresses = (addresses) => {
|
|
232
142
|
if (addresses.length == 1 && Array.isArray(addresses[0]))
|
|
233
143
|
addresses = addresses[0];
|
|
234
144
|
if (addresses.length === 0 && curve.signerAddress !== '')
|
|
235
145
|
addresses = [curve.signerAddress];
|
|
236
146
|
addresses = addresses;
|
|
237
|
-
return addresses.filter(
|
|
147
|
+
return addresses.filter((val, idx, arr) => arr.indexOf(val) === idx);
|
|
238
148
|
};
|
|
239
|
-
export
|
|
149
|
+
export const _getAddress = (address) => {
|
|
240
150
|
address = address || curve.signerAddress;
|
|
241
151
|
if (!address)
|
|
242
152
|
throw Error("Need to connect wallet or pass address into args");
|
|
243
153
|
return address;
|
|
244
154
|
};
|
|
245
|
-
export
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
155
|
+
export const getBalances = (coins, ...addresses) => __awaiter(void 0, void 0, void 0, function* () {
|
|
156
|
+
addresses = _prepareAddresses(addresses);
|
|
157
|
+
const balances = yield _getBalances(coins, addresses);
|
|
158
|
+
return addresses.length === 1 ? balances[addresses[0]] : balances;
|
|
159
|
+
});
|
|
160
|
+
export const _getAllowance = (coins, address, spender) => __awaiter(void 0, void 0, void 0, function* () {
|
|
161
|
+
const _coins = [...coins];
|
|
162
|
+
const ethIndex = getEthIndex(_coins);
|
|
163
|
+
if (ethIndex !== -1) {
|
|
164
|
+
_coins.splice(ethIndex, 1);
|
|
165
|
+
}
|
|
166
|
+
let allowance;
|
|
167
|
+
if (_coins.length === 1) {
|
|
168
|
+
allowance = [yield curve.contracts[_coins[0]].contract.allowance(address, spender, curve.constantOptions)];
|
|
249
169
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
return [2 /*return*/, addresses.length === 1 ? balances[addresses[0]] : balances];
|
|
260
|
-
}
|
|
261
|
-
});
|
|
262
|
-
});
|
|
263
|
-
};
|
|
264
|
-
export var _getAllowance = function (coins, address, spender) { return __awaiter(void 0, void 0, void 0, function () {
|
|
265
|
-
var _coins, ethIndex, allowance, contractCalls;
|
|
266
|
-
return __generator(this, function (_a) {
|
|
267
|
-
switch (_a.label) {
|
|
268
|
-
case 0:
|
|
269
|
-
_coins = __spreadArray([], coins, true);
|
|
270
|
-
ethIndex = getEthIndex(_coins);
|
|
271
|
-
if (ethIndex !== -1) {
|
|
272
|
-
_coins.splice(ethIndex, 1);
|
|
273
|
-
}
|
|
274
|
-
if (!(_coins.length === 1)) return [3 /*break*/, 2];
|
|
275
|
-
return [4 /*yield*/, curve.contracts[_coins[0]].contract.allowance(address, spender, curve.constantOptions)];
|
|
276
|
-
case 1:
|
|
277
|
-
allowance = [_a.sent()];
|
|
278
|
-
return [3 /*break*/, 4];
|
|
279
|
-
case 2:
|
|
280
|
-
contractCalls = _coins.map(function (coinAddr) { return curve.contracts[coinAddr].multicallContract.allowance(address, spender); });
|
|
281
|
-
return [4 /*yield*/, curve.multicallProvider.all(contractCalls)];
|
|
282
|
-
case 3:
|
|
283
|
-
allowance = _a.sent();
|
|
284
|
-
_a.label = 4;
|
|
285
|
-
case 4:
|
|
286
|
-
if (ethIndex !== -1) {
|
|
287
|
-
allowance.splice(ethIndex, 0, MAX_ALLOWANCE);
|
|
288
|
-
}
|
|
289
|
-
return [2 /*return*/, allowance];
|
|
290
|
-
}
|
|
291
|
-
});
|
|
292
|
-
}); };
|
|
170
|
+
else {
|
|
171
|
+
const contractCalls = _coins.map((coinAddr) => curve.contracts[coinAddr].multicallContract.allowance(address, spender));
|
|
172
|
+
allowance = yield curve.multicallProvider.all(contractCalls);
|
|
173
|
+
}
|
|
174
|
+
if (ethIndex !== -1) {
|
|
175
|
+
allowance.splice(ethIndex, 0, MAX_ALLOWANCE);
|
|
176
|
+
}
|
|
177
|
+
return allowance;
|
|
178
|
+
});
|
|
293
179
|
// coins can be either addresses or symbols
|
|
294
|
-
export
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
decimals = _getCoinDecimals(coinAddresses);
|
|
301
|
-
return [4 /*yield*/, _getAllowance(coinAddresses, address, spender)];
|
|
302
|
-
case 1:
|
|
303
|
-
_allowance = _a.sent();
|
|
304
|
-
return [2 /*return*/, _allowance.map(function (a, i) { return curve.formatUnits(a, decimals[i]); })];
|
|
305
|
-
}
|
|
306
|
-
});
|
|
307
|
-
}); };
|
|
180
|
+
export const getAllowance = (coins, address, spender) => __awaiter(void 0, void 0, void 0, function* () {
|
|
181
|
+
const coinAddresses = _getCoinAddresses(coins);
|
|
182
|
+
const decimals = _getCoinDecimals(coinAddresses);
|
|
183
|
+
const _allowance = yield _getAllowance(coinAddresses, address, spender);
|
|
184
|
+
return _allowance.map((a, i) => curve.formatUnits(a, decimals[i]));
|
|
185
|
+
});
|
|
308
186
|
// coins can be either addresses or symbols
|
|
309
|
-
export
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
case 0:
|
|
331
|
-
address = curve.signerAddress;
|
|
332
|
-
return [4 /*yield*/, _getAllowance(coins, address, spender)];
|
|
333
|
-
case 1:
|
|
334
|
-
allowance = _e.sent();
|
|
335
|
-
txHashes = [];
|
|
336
|
-
i = 0;
|
|
337
|
-
_e.label = 2;
|
|
338
|
-
case 2:
|
|
339
|
-
if (!(i < allowance.length)) return [3 /*break*/, 12];
|
|
340
|
-
if (!(allowance[i] < amounts[i])) return [3 /*break*/, 11];
|
|
341
|
-
contract = curve.contracts[coins[i]].contract;
|
|
342
|
-
_approveAmount = isMax ? MAX_ALLOWANCE : amounts[i];
|
|
343
|
-
return [4 /*yield*/, curve.updateFeeData()];
|
|
344
|
-
case 3:
|
|
345
|
-
_e.sent();
|
|
346
|
-
if (!(allowance[i] > curve.parseUnits("0"))) return [3 /*break*/, 7];
|
|
347
|
-
_a = mulBy1_3;
|
|
348
|
-
_b = DIGas;
|
|
349
|
-
return [4 /*yield*/, contract.approve.estimateGas(spender, curve.parseUnits("0"), curve.constantOptions)];
|
|
350
|
-
case 4:
|
|
351
|
-
gasLimit_1 = _a.apply(void 0, [_b.apply(void 0, [_e.sent()])]);
|
|
352
|
-
return [4 /*yield*/, contract.approve(spender, curve.parseUnits("0"), __assign(__assign({}, curve.options), { gasLimit: gasLimit_1 }))];
|
|
353
|
-
case 5:
|
|
354
|
-
resetTx = _e.sent();
|
|
355
|
-
txHashes.push(resetTx.hash);
|
|
356
|
-
return [4 /*yield*/, resetTx.wait()];
|
|
357
|
-
case 6:
|
|
358
|
-
_e.sent();
|
|
359
|
-
_e.label = 7;
|
|
360
|
-
case 7:
|
|
361
|
-
_c = mulBy1_3;
|
|
362
|
-
_d = DIGas;
|
|
363
|
-
return [4 /*yield*/, contract.approve.estimateGas(spender, _approveAmount, curve.constantOptions)];
|
|
364
|
-
case 8:
|
|
365
|
-
gasLimit = _c.apply(void 0, [_d.apply(void 0, [_e.sent()])]);
|
|
366
|
-
return [4 /*yield*/, contract.approve(spender, _approveAmount, __assign(__assign({}, curve.options), { gasLimit: gasLimit }))];
|
|
367
|
-
case 9:
|
|
368
|
-
approveTx = _e.sent();
|
|
369
|
-
txHashes.push(approveTx.hash);
|
|
370
|
-
return [4 /*yield*/, approveTx.wait()];
|
|
371
|
-
case 10:
|
|
372
|
-
_e.sent();
|
|
373
|
-
_e.label = 11;
|
|
374
|
-
case 11:
|
|
375
|
-
i++;
|
|
376
|
-
return [3 /*break*/, 2];
|
|
377
|
-
case 12: return [2 /*return*/, txHashes];
|
|
187
|
+
export const hasAllowance = (coins, amounts, address, spender) => __awaiter(void 0, void 0, void 0, function* () {
|
|
188
|
+
const coinAddresses = _getCoinAddresses(coins);
|
|
189
|
+
const decimals = _getCoinDecimals(coinAddresses);
|
|
190
|
+
const _allowance = yield _getAllowance(coinAddresses, address, spender);
|
|
191
|
+
const _amounts = amounts.map((a, i) => parseUnits(a, decimals[i]));
|
|
192
|
+
return _allowance.map((a, i) => a >= _amounts[i]).reduce((a, b) => a && b);
|
|
193
|
+
});
|
|
194
|
+
export const _ensureAllowance = (coins, amounts, spender, isMax = true) => __awaiter(void 0, void 0, void 0, function* () {
|
|
195
|
+
const address = curve.signerAddress;
|
|
196
|
+
const allowance = yield _getAllowance(coins, address, spender);
|
|
197
|
+
const txHashes = [];
|
|
198
|
+
for (let i = 0; i < allowance.length; i++) {
|
|
199
|
+
if (allowance[i] < amounts[i]) {
|
|
200
|
+
const contract = curve.contracts[coins[i]].contract;
|
|
201
|
+
const _approveAmount = isMax ? MAX_ALLOWANCE : amounts[i];
|
|
202
|
+
yield curve.updateFeeData();
|
|
203
|
+
if (allowance[i] > curve.parseUnits("0")) {
|
|
204
|
+
const gasLimit = mulBy1_3(DIGas(yield contract.approve.estimateGas(spender, curve.parseUnits("0"), curve.constantOptions)));
|
|
205
|
+
const resetTx = yield contract.approve(spender, curve.parseUnits("0"), Object.assign(Object.assign({}, curve.options), { gasLimit }));
|
|
206
|
+
txHashes.push(resetTx.hash);
|
|
207
|
+
yield resetTx.wait();
|
|
378
208
|
}
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
209
|
+
const gasLimit = mulBy1_3(DIGas(yield contract.approve.estimateGas(spender, _approveAmount, curve.constantOptions)));
|
|
210
|
+
const approveTx = yield contract.approve(spender, _approveAmount, Object.assign(Object.assign({}, curve.options), { gasLimit }));
|
|
211
|
+
txHashes.push(approveTx.hash);
|
|
212
|
+
yield approveTx.wait();
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return txHashes;
|
|
216
|
+
});
|
|
382
217
|
// coins can be either addresses or symbols
|
|
383
|
-
export
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
_approveAmount = isMax ? MAX_ALLOWANCE : _amounts[i];
|
|
405
|
-
if (!(_allowance[i] > curve.parseUnits("0"))) return [3 /*break*/, 4];
|
|
406
|
-
_a = smartNumber;
|
|
407
|
-
return [4 /*yield*/, contract.approve.estimateGas(spender, curve.parseUnits("0"), curve.constantOptions)];
|
|
408
|
-
case 3:
|
|
409
|
-
currentGas = _a.apply(void 0, [_c.sent()]);
|
|
410
|
-
// 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
|
|
411
|
-
if (typeof currentGas === "number") {
|
|
412
|
-
currentGas = currentGas * 2;
|
|
413
|
-
}
|
|
414
|
-
else {
|
|
415
|
-
currentGas = currentGas.map(function (g) { return g * 2; });
|
|
416
|
-
}
|
|
417
|
-
gas = gasSum(gas, currentGas);
|
|
418
|
-
return [3 /*break*/, 6];
|
|
419
|
-
case 4:
|
|
420
|
-
_b = smartNumber;
|
|
421
|
-
return [4 /*yield*/, contract.approve.estimateGas(spender, _approveAmount, curve.constantOptions)];
|
|
422
|
-
case 5:
|
|
423
|
-
currentGas = _b.apply(void 0, [_c.sent()]);
|
|
424
|
-
gas = gasSum(gas, currentGas);
|
|
425
|
-
_c.label = 6;
|
|
426
|
-
case 6:
|
|
427
|
-
i++;
|
|
428
|
-
return [3 /*break*/, 2];
|
|
429
|
-
case 7: return [2 /*return*/, getGasFromArray(gas)];
|
|
218
|
+
export const ensureAllowanceEstimateGas = (coins, amounts, spender, isMax = true) => __awaiter(void 0, void 0, void 0, function* () {
|
|
219
|
+
const coinAddresses = _getCoinAddresses(coins);
|
|
220
|
+
const decimals = _getCoinDecimals(coinAddresses);
|
|
221
|
+
const _amounts = amounts.map((a, i) => parseUnits(a, decimals[i]));
|
|
222
|
+
const address = curve.signerAddress;
|
|
223
|
+
const _allowance = yield _getAllowance(coinAddresses, address, spender);
|
|
224
|
+
let gas = [0, 0];
|
|
225
|
+
for (let i = 0; i < _allowance.length; i++) {
|
|
226
|
+
if (_allowance[i] < _amounts[i]) {
|
|
227
|
+
const contract = curve.contracts[coinAddresses[i]].contract;
|
|
228
|
+
const _approveAmount = isMax ? MAX_ALLOWANCE : _amounts[i];
|
|
229
|
+
if (_allowance[i] > curve.parseUnits("0")) {
|
|
230
|
+
let currentGas = smartNumber(yield contract.approve.estimateGas(spender, curve.parseUnits("0"), curve.constantOptions));
|
|
231
|
+
// 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
|
|
232
|
+
if (typeof currentGas === "number") {
|
|
233
|
+
currentGas = currentGas * 2;
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
currentGas = currentGas.map((g) => g * 2);
|
|
237
|
+
}
|
|
238
|
+
gas = gasSum(gas, currentGas);
|
|
430
239
|
}
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
// coins can be either addresses or symbols
|
|
435
|
-
export var ensureAllowance = function (coins, amounts, spender, isMax) {
|
|
436
|
-
if (isMax === void 0) { isMax = true; }
|
|
437
|
-
return __awaiter(void 0, void 0, void 0, function () {
|
|
438
|
-
var coinAddresses, decimals, _amounts;
|
|
439
|
-
return __generator(this, function (_a) {
|
|
440
|
-
switch (_a.label) {
|
|
441
|
-
case 0:
|
|
442
|
-
coinAddresses = _getCoinAddresses(coins);
|
|
443
|
-
decimals = _getCoinDecimals(coinAddresses);
|
|
444
|
-
_amounts = amounts.map(function (a, i) { return parseUnits(a, decimals[i]); });
|
|
445
|
-
return [4 /*yield*/, _ensureAllowance(coinAddresses, _amounts, spender, isMax)];
|
|
446
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
240
|
+
else {
|
|
241
|
+
const currentGas = smartNumber(yield contract.approve.estimateGas(spender, _approveAmount, curve.constantOptions));
|
|
242
|
+
gas = gasSum(gas, currentGas);
|
|
447
243
|
}
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return getGasFromArray(gas);
|
|
247
|
+
});
|
|
248
|
+
// coins can be either addresses or symbols
|
|
249
|
+
export const ensureAllowance = (coins, amounts, spender, isMax = true) => __awaiter(void 0, void 0, void 0, function* () {
|
|
250
|
+
const coinAddresses = _getCoinAddresses(coins);
|
|
251
|
+
const decimals = _getCoinDecimals(coinAddresses);
|
|
252
|
+
const _amounts = amounts.map((a, i) => parseUnits(a, decimals[i]));
|
|
253
|
+
return yield _ensureAllowance(coinAddresses, _amounts, spender, isMax);
|
|
254
|
+
});
|
|
255
|
+
export const getPoolIdBySwapAddress = (swapAddress) => {
|
|
256
|
+
const poolsData = curve.getPoolsData();
|
|
257
|
+
const poolIds = Object.entries(poolsData).filter(([, poolData]) => poolData.swap_address.toLowerCase() === swapAddress.toLowerCase());
|
|
457
258
|
if (poolIds.length === 0)
|
|
458
259
|
return "";
|
|
459
260
|
return poolIds[0][0];
|
|
460
261
|
};
|
|
461
|
-
|
|
462
|
-
var
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
priceDict[lpTokenAddress.toLowerCase()].push({
|
|
491
|
-
price: pool.usdTotal && totalSupply ? pool.usdTotal / totalSupply : 0,
|
|
492
|
-
tvl: pool.usdTotal,
|
|
493
|
-
});
|
|
494
|
-
}
|
|
495
|
-
else {
|
|
496
|
-
priceDict[lpTokenAddress.toLowerCase()] = [];
|
|
497
|
-
priceDict[lpTokenAddress.toLowerCase()].push({
|
|
498
|
-
price: pool.usdTotal && totalSupply ? pool.usdTotal / totalSupply : 0,
|
|
499
|
-
tvl: pool.usdTotal,
|
|
500
|
-
});
|
|
501
|
-
}
|
|
502
|
-
for (_c = 0, _d = pool.coins; _c < _d.length; _c++) {
|
|
503
|
-
coin = _d[_c];
|
|
504
|
-
if (typeof coin.usdPrice === "number") {
|
|
505
|
-
if (coin.address.toLowerCase() in priceDict) {
|
|
506
|
-
priceDict[coin.address.toLowerCase()].push({
|
|
507
|
-
price: coin.usdPrice,
|
|
508
|
-
tvl: pool.usdTotal,
|
|
509
|
-
});
|
|
510
|
-
}
|
|
511
|
-
else {
|
|
512
|
-
priceDict[coin.address.toLowerCase()] = [];
|
|
513
|
-
priceDict[coin.address.toLowerCase()].push({
|
|
514
|
-
price: coin.usdPrice,
|
|
515
|
-
tvl: pool.usdTotal,
|
|
516
|
-
});
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
}
|
|
520
|
-
for (_e = 0, _f = (_h = pool.gaugeRewards) !== null && _h !== void 0 ? _h : []; _e < _f.length; _e++) {
|
|
521
|
-
coin = _f[_e];
|
|
522
|
-
if (typeof coin.tokenPrice === "number") {
|
|
523
|
-
if (coin.tokenAddress.toLowerCase() in priceDict) {
|
|
524
|
-
priceDict[coin.tokenAddress.toLowerCase()].push({
|
|
525
|
-
price: coin.tokenPrice,
|
|
526
|
-
tvl: pool.usdTotal,
|
|
527
|
-
});
|
|
528
|
-
}
|
|
529
|
-
else {
|
|
530
|
-
priceDict[coin.tokenAddress.toLowerCase()] = [];
|
|
531
|
-
priceDict[coin.tokenAddress.toLowerCase()].push({
|
|
532
|
-
price: coin.tokenPrice,
|
|
533
|
-
tvl: pool.usdTotal,
|
|
534
|
-
});
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
for (address in priceDict) {
|
|
541
|
-
if (priceDict[address].length > 0) {
|
|
542
|
-
maxTvlItem = priceDict[address].reduce(function (prev, current) {
|
|
543
|
-
if (+current.tvl > +prev.tvl) {
|
|
544
|
-
return current;
|
|
545
|
-
}
|
|
546
|
-
else {
|
|
547
|
-
return prev;
|
|
548
|
-
}
|
|
262
|
+
export const _getUsdPricesFromApi = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
263
|
+
var _a, _b;
|
|
264
|
+
const network = curve.constants.NETWORK_NAME;
|
|
265
|
+
const allTypesExtendedPoolData = yield _getAllPoolsFromApi(network);
|
|
266
|
+
const priceDict = {};
|
|
267
|
+
const priceDictByMaxTvl = {};
|
|
268
|
+
for (const extendedPoolData of allTypesExtendedPoolData) {
|
|
269
|
+
for (const pool of extendedPoolData.poolData) {
|
|
270
|
+
const lpTokenAddress = (_a = pool.lpTokenAddress) !== null && _a !== void 0 ? _a : pool.address;
|
|
271
|
+
const totalSupply = pool.totalSupply / (Math.pow(10, 18));
|
|
272
|
+
if (lpTokenAddress.toLowerCase() in priceDict) {
|
|
273
|
+
priceDict[lpTokenAddress.toLowerCase()].push({
|
|
274
|
+
price: pool.usdTotal && totalSupply ? pool.usdTotal / totalSupply : 0,
|
|
275
|
+
tvl: pool.usdTotal,
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
priceDict[lpTokenAddress.toLowerCase()] = [];
|
|
280
|
+
priceDict[lpTokenAddress.toLowerCase()].push({
|
|
281
|
+
price: pool.usdTotal && totalSupply ? pool.usdTotal / totalSupply : 0,
|
|
282
|
+
tvl: pool.usdTotal,
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
for (const coin of pool.coins) {
|
|
286
|
+
if (typeof coin.usdPrice === "number") {
|
|
287
|
+
if (coin.address.toLowerCase() in priceDict) {
|
|
288
|
+
priceDict[coin.address.toLowerCase()].push({
|
|
289
|
+
price: coin.usdPrice,
|
|
290
|
+
tvl: pool.usdTotal,
|
|
549
291
|
});
|
|
550
|
-
priceDictByMaxTvl[address] = maxTvlItem.price;
|
|
551
292
|
}
|
|
552
293
|
else {
|
|
553
|
-
|
|
294
|
+
priceDict[coin.address.toLowerCase()] = [];
|
|
295
|
+
priceDict[coin.address.toLowerCase()].push({
|
|
296
|
+
price: coin.usdPrice,
|
|
297
|
+
tvl: pool.usdTotal,
|
|
298
|
+
});
|
|
554
299
|
}
|
|
555
300
|
}
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
switch (_e.label) {
|
|
565
|
-
case 0:
|
|
566
|
-
network = curve.constants.NETWORK_NAME;
|
|
567
|
-
return [4 /*yield*/, _getAllPoolsFromApi(network)];
|
|
568
|
-
case 1:
|
|
569
|
-
allTypesExtendedPoolData = _e.sent();
|
|
570
|
-
apyDict = {};
|
|
571
|
-
for (_i = 0, allTypesExtendedPoolData_2 = allTypesExtendedPoolData; _i < allTypesExtendedPoolData_2.length; _i++) {
|
|
572
|
-
extendedPoolData = allTypesExtendedPoolData_2[_i];
|
|
573
|
-
for (_a = 0, _b = extendedPoolData.poolData; _a < _b.length; _a++) {
|
|
574
|
-
pool = _b[_a];
|
|
575
|
-
if (pool.gaugeAddress) {
|
|
576
|
-
if (!pool.gaugeCrvApy) {
|
|
577
|
-
apyDict[pool.gaugeAddress.toLowerCase()] = [0, 0];
|
|
578
|
-
}
|
|
579
|
-
else {
|
|
580
|
-
apyDict[pool.gaugeAddress.toLowerCase()] = [(_c = pool.gaugeCrvApy[0]) !== null && _c !== void 0 ? _c : 0, (_d = pool.gaugeCrvApy[1]) !== null && _d !== void 0 ? _d : 0];
|
|
581
|
-
}
|
|
582
|
-
}
|
|
301
|
+
}
|
|
302
|
+
for (const coin of (_b = pool.gaugeRewards) !== null && _b !== void 0 ? _b : []) {
|
|
303
|
+
if (typeof coin.tokenPrice === "number") {
|
|
304
|
+
if (coin.tokenAddress.toLowerCase() in priceDict) {
|
|
305
|
+
priceDict[coin.tokenAddress.toLowerCase()].push({
|
|
306
|
+
price: coin.tokenPrice,
|
|
307
|
+
tvl: pool.usdTotal,
|
|
308
|
+
});
|
|
583
309
|
}
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
var network, allTypesExtendedPoolData, rewardsDict, _i, allTypesExtendedPoolData_3, extendedPoolData, _a, _b, pool;
|
|
591
|
-
var _c;
|
|
592
|
-
return __generator(this, function (_d) {
|
|
593
|
-
switch (_d.label) {
|
|
594
|
-
case 0:
|
|
595
|
-
network = curve.constants.NETWORK_NAME;
|
|
596
|
-
return [4 /*yield*/, _getAllPoolsFromApi(network)];
|
|
597
|
-
case 1:
|
|
598
|
-
allTypesExtendedPoolData = _d.sent();
|
|
599
|
-
rewardsDict = {};
|
|
600
|
-
for (_i = 0, allTypesExtendedPoolData_3 = allTypesExtendedPoolData; _i < allTypesExtendedPoolData_3.length; _i++) {
|
|
601
|
-
extendedPoolData = allTypesExtendedPoolData_3[_i];
|
|
602
|
-
for (_a = 0, _b = extendedPoolData.poolData; _a < _b.length; _a++) {
|
|
603
|
-
pool = _b[_a];
|
|
604
|
-
if (pool.gaugeAddress) {
|
|
605
|
-
rewardsDict[pool.gaugeAddress.toLowerCase()] = ((_c = pool.gaugeRewards) !== null && _c !== void 0 ? _c : [])
|
|
606
|
-
.filter(function (r) { return curve.chainId === 1 || r.tokenAddress.toLowerCase() !== curve.constants.COINS.crv; });
|
|
607
|
-
}
|
|
310
|
+
else {
|
|
311
|
+
priceDict[coin.tokenAddress.toLowerCase()] = [];
|
|
312
|
+
priceDict[coin.tokenAddress.toLowerCase()].push({
|
|
313
|
+
price: coin.tokenPrice,
|
|
314
|
+
tvl: pool.usdTotal,
|
|
315
|
+
});
|
|
608
316
|
}
|
|
609
317
|
}
|
|
610
|
-
|
|
318
|
+
}
|
|
611
319
|
}
|
|
612
|
-
}
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
var _a, _b;
|
|
618
|
-
return __generator(this, function (_c) {
|
|
619
|
-
switch (_c.label) {
|
|
620
|
-
case 0:
|
|
621
|
-
if (curve.chainId === 1 && assetId.toLowerCase() === '0x8762db106b2c2a0bccb3a80d1ed41273552616e8')
|
|
622
|
-
return [2 /*return*/, 0]; // RSR
|
|
623
|
-
return [4 /*yield*/, _getUsdPricesFromApi()];
|
|
624
|
-
case 1:
|
|
625
|
-
pricesFromApi = _c.sent();
|
|
626
|
-
if (assetId.toLowerCase() in pricesFromApi)
|
|
627
|
-
return [2 /*return*/, pricesFromApi[assetId.toLowerCase()]];
|
|
628
|
-
if (assetId === 'USD' || (curve.chainId === 137 && (assetId.toLowerCase() === curve.constants.COINS.am3crv.toLowerCase())))
|
|
629
|
-
return [2 /*return*/, 1];
|
|
630
|
-
chainName = {
|
|
631
|
-
1: 'ethereum',
|
|
632
|
-
10: 'optimistic-ethereum',
|
|
633
|
-
56: "binance-smart-chain",
|
|
634
|
-
100: 'xdai',
|
|
635
|
-
137: 'polygon-pos',
|
|
636
|
-
196: 'x-layer',
|
|
637
|
-
250: 'fantom',
|
|
638
|
-
252: 'fraxtal',
|
|
639
|
-
324: 'zksync',
|
|
640
|
-
1284: 'moonbeam',
|
|
641
|
-
2222: 'kava',
|
|
642
|
-
5000: 'mantle',
|
|
643
|
-
8453: 'base',
|
|
644
|
-
42220: 'celo',
|
|
645
|
-
43114: 'avalanche',
|
|
646
|
-
42161: 'arbitrum-one',
|
|
647
|
-
1313161554: 'aurora',
|
|
648
|
-
}[curve.chainId];
|
|
649
|
-
nativeTokenName = {
|
|
650
|
-
1: 'ethereum',
|
|
651
|
-
10: 'ethereum',
|
|
652
|
-
56: 'binancecoin',
|
|
653
|
-
100: 'xdai',
|
|
654
|
-
137: 'matic-network',
|
|
655
|
-
196: 'okb',
|
|
656
|
-
250: 'fantom',
|
|
657
|
-
252: 'frax-ether',
|
|
658
|
-
324: 'ethereum',
|
|
659
|
-
1284: 'moonbeam',
|
|
660
|
-
2222: 'kava',
|
|
661
|
-
5000: 'mantle',
|
|
662
|
-
8453: 'ethereum',
|
|
663
|
-
42220: 'celo',
|
|
664
|
-
43114: 'avalanche-2',
|
|
665
|
-
42161: 'ethereum',
|
|
666
|
-
1313161554: 'ethereum',
|
|
667
|
-
}[curve.chainId];
|
|
668
|
-
if (chainName === undefined) {
|
|
669
|
-
throw Error('curve object is not initialized');
|
|
670
|
-
}
|
|
671
|
-
assetId = {
|
|
672
|
-
'CRV': 'curve-dao-token',
|
|
673
|
-
'EUR': 'stasis-eurs',
|
|
674
|
-
'BTC': 'bitcoin',
|
|
675
|
-
'ETH': 'ethereum',
|
|
676
|
-
'LINK': 'link',
|
|
677
|
-
}[assetId.toUpperCase()] || assetId;
|
|
678
|
-
assetId = isEth(assetId) ? nativeTokenName : assetId.toLowerCase();
|
|
679
|
-
// No EURT on Coingecko Polygon
|
|
680
|
-
if (curve.chainId === 137 && assetId.toLowerCase() === curve.constants.COINS.eurt) {
|
|
681
|
-
chainName = 'ethereum';
|
|
682
|
-
assetId = '0xC581b735A1688071A1746c968e0798D642EDE491'.toLowerCase(); // EURT Ethereum
|
|
683
|
-
}
|
|
684
|
-
// CRV
|
|
685
|
-
if (assetId.toLowerCase() === curve.constants.ALIASES.crv) {
|
|
686
|
-
assetId = 'curve-dao-token';
|
|
687
|
-
}
|
|
688
|
-
if (!((((_a = _usdRatesCache[assetId]) === null || _a === void 0 ? void 0 : _a.time) || 0) + 600000 < Date.now())) return [3 /*break*/, 3];
|
|
689
|
-
url = [nativeTokenName, 'ethereum', 'bitcoin', 'link', 'curve-dao-token', 'stasis-eurs'].includes(assetId.toLowerCase()) ?
|
|
690
|
-
"https://api.coingecko.com/api/v3/simple/price?ids=".concat(assetId, "&vs_currencies=usd") :
|
|
691
|
-
"https://api.coingecko.com/api/v3/simple/token_price/".concat(chainName, "?contract_addresses=").concat(assetId, "&vs_currencies=usd");
|
|
692
|
-
return [4 /*yield*/, axios.get(url)];
|
|
693
|
-
case 2:
|
|
694
|
-
response = _c.sent();
|
|
695
|
-
try {
|
|
696
|
-
_usdRatesCache[assetId] = { 'rate': (_b = response.data[assetId]['usd']) !== null && _b !== void 0 ? _b : 0, 'time': Date.now() };
|
|
697
|
-
}
|
|
698
|
-
catch (err) { // TODO pay attention!
|
|
699
|
-
_usdRatesCache[assetId] = { 'rate': 0, 'time': Date.now() };
|
|
700
|
-
}
|
|
701
|
-
_c.label = 3;
|
|
702
|
-
case 3: return [2 /*return*/, _usdRatesCache[assetId]['rate']];
|
|
320
|
+
}
|
|
321
|
+
for (const address in priceDict) {
|
|
322
|
+
if (priceDict[address].length) {
|
|
323
|
+
const maxTvlItem = priceDict[address].reduce((prev, current) => +current.tvl > +prev.tvl ? current : prev);
|
|
324
|
+
priceDictByMaxTvl[address] = maxTvlItem.price;
|
|
703
325
|
}
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
export var getUsdRate = function (coin) { return __awaiter(void 0, void 0, void 0, function () {
|
|
707
|
-
var coinAddress;
|
|
708
|
-
return __generator(this, function (_a) {
|
|
709
|
-
switch (_a.label) {
|
|
710
|
-
case 0:
|
|
711
|
-
coinAddress = _getCoinAddressesNoCheck(coin)[0];
|
|
712
|
-
return [4 /*yield*/, _getUsdRate(coinAddress)];
|
|
713
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
326
|
+
else {
|
|
327
|
+
priceDictByMaxTvl[address] = 0;
|
|
714
328
|
}
|
|
715
|
-
}
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
block = _a.sent();
|
|
729
|
-
if (!block) {
|
|
730
|
-
return [2 /*return*/, 0.01];
|
|
329
|
+
}
|
|
330
|
+
return priceDictByMaxTvl;
|
|
331
|
+
});
|
|
332
|
+
export const _getCrvApyFromApi = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
333
|
+
var _c, _d;
|
|
334
|
+
const network = curve.constants.NETWORK_NAME;
|
|
335
|
+
const allTypesExtendedPoolData = yield _getAllPoolsFromApi(network);
|
|
336
|
+
const apyDict = {};
|
|
337
|
+
for (const extendedPoolData of allTypesExtendedPoolData) {
|
|
338
|
+
for (const pool of extendedPoolData.poolData) {
|
|
339
|
+
if (pool.gaugeAddress) {
|
|
340
|
+
if (!pool.gaugeCrvApy) {
|
|
341
|
+
apyDict[pool.gaugeAddress.toLowerCase()] = [0, 0];
|
|
731
342
|
}
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
case 4: return [2 /*return*/];
|
|
737
|
-
}
|
|
738
|
-
});
|
|
739
|
-
}); };
|
|
740
|
-
export var getGasPrice = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
741
|
-
var provider, _a, _b;
|
|
742
|
-
return __generator(this, function (_c) {
|
|
743
|
-
switch (_c.label) {
|
|
744
|
-
case 0:
|
|
745
|
-
provider = curve.provider;
|
|
746
|
-
_a = Number;
|
|
747
|
-
_b = Number;
|
|
748
|
-
return [4 /*yield*/, provider.getFeeData()];
|
|
749
|
-
case 1: return [2 /*return*/, _a.apply(void 0, [(_b.apply(void 0, [(_c.sent()).gasPrice]) / 1e9).toFixed(2)])];
|
|
343
|
+
else {
|
|
344
|
+
apyDict[pool.gaugeAddress.toLowerCase()] = [(_c = pool.gaugeCrvApy[0]) !== null && _c !== void 0 ? _c : 0, (_d = pool.gaugeCrvApy[1]) !== null && _d !== void 0 ? _d : 0];
|
|
345
|
+
}
|
|
346
|
+
}
|
|
750
347
|
}
|
|
751
|
-
}
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
348
|
+
}
|
|
349
|
+
return apyDict;
|
|
350
|
+
});
|
|
351
|
+
export const _getRewardsFromApi = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
352
|
+
var _e;
|
|
353
|
+
const network = curve.constants.NETWORK_NAME;
|
|
354
|
+
const allTypesExtendedPoolData = yield _getAllPoolsFromApi(network);
|
|
355
|
+
const rewardsDict = {};
|
|
356
|
+
for (const extendedPoolData of allTypesExtendedPoolData) {
|
|
357
|
+
for (const pool of extendedPoolData.poolData) {
|
|
358
|
+
if (pool.gaugeAddress) {
|
|
359
|
+
rewardsDict[pool.gaugeAddress.toLowerCase()] = ((_e = pool.gaugeRewards) !== null && _e !== void 0 ? _e : [])
|
|
360
|
+
.filter((r) => curve.chainId === 1 || r.tokenAddress.toLowerCase() !== curve.constants.COINS.crv);
|
|
361
|
+
}
|
|
757
362
|
}
|
|
758
|
-
|
|
759
|
-
|
|
363
|
+
}
|
|
364
|
+
return rewardsDict;
|
|
365
|
+
});
|
|
366
|
+
const _usdRatesCache = {};
|
|
367
|
+
export const _getUsdRate = (assetId) => __awaiter(void 0, void 0, void 0, function* () {
|
|
368
|
+
var _f, _g;
|
|
369
|
+
if (curve.chainId === 1 && assetId.toLowerCase() === '0x8762db106b2c2a0bccb3a80d1ed41273552616e8')
|
|
370
|
+
return 0; // RSR
|
|
371
|
+
const pricesFromApi = yield _getUsdPricesFromApi();
|
|
372
|
+
if (assetId.toLowerCase() in pricesFromApi)
|
|
373
|
+
return pricesFromApi[assetId.toLowerCase()];
|
|
374
|
+
if (assetId === 'USD' || (curve.chainId === 137 && (assetId.toLowerCase() === curve.constants.COINS.am3crv.toLowerCase())))
|
|
375
|
+
return 1;
|
|
376
|
+
let chainName = {
|
|
377
|
+
1: 'ethereum',
|
|
378
|
+
10: 'optimistic-ethereum',
|
|
379
|
+
56: "binance-smart-chain",
|
|
380
|
+
100: 'xdai',
|
|
381
|
+
137: 'polygon-pos',
|
|
382
|
+
196: 'x-layer',
|
|
383
|
+
250: 'fantom',
|
|
384
|
+
252: 'fraxtal',
|
|
385
|
+
324: 'zksync',
|
|
386
|
+
1284: 'moonbeam',
|
|
387
|
+
2222: 'kava',
|
|
388
|
+
5000: 'mantle',
|
|
389
|
+
8453: 'base',
|
|
390
|
+
42220: 'celo',
|
|
391
|
+
43114: 'avalanche',
|
|
392
|
+
42161: 'arbitrum-one',
|
|
393
|
+
1313161554: 'aurora',
|
|
394
|
+
}[curve.chainId];
|
|
395
|
+
const nativeTokenName = {
|
|
396
|
+
1: 'ethereum',
|
|
397
|
+
10: 'ethereum',
|
|
398
|
+
56: 'binancecoin',
|
|
399
|
+
100: 'xdai',
|
|
400
|
+
137: 'matic-network',
|
|
401
|
+
196: 'okb',
|
|
402
|
+
250: 'fantom',
|
|
403
|
+
252: 'frax-ether',
|
|
404
|
+
324: 'ethereum',
|
|
405
|
+
1284: 'moonbeam',
|
|
406
|
+
2222: 'kava',
|
|
407
|
+
5000: 'mantle',
|
|
408
|
+
8453: 'ethereum',
|
|
409
|
+
42220: 'celo',
|
|
410
|
+
43114: 'avalanche-2',
|
|
411
|
+
42161: 'ethereum',
|
|
412
|
+
1313161554: 'ethereum',
|
|
413
|
+
}[curve.chainId];
|
|
414
|
+
if (chainName === undefined) {
|
|
415
|
+
throw Error('curve object is not initialized');
|
|
416
|
+
}
|
|
417
|
+
assetId = {
|
|
418
|
+
'CRV': 'curve-dao-token',
|
|
419
|
+
'EUR': 'stasis-eurs',
|
|
420
|
+
'BTC': 'bitcoin',
|
|
421
|
+
'ETH': 'ethereum',
|
|
422
|
+
'LINK': 'link',
|
|
423
|
+
}[assetId.toUpperCase()] || assetId;
|
|
424
|
+
assetId = isEth(assetId) ? nativeTokenName : assetId.toLowerCase();
|
|
425
|
+
// No EURT on Coingecko Polygon
|
|
426
|
+
if (curve.chainId === 137 && assetId.toLowerCase() === curve.constants.COINS.eurt) {
|
|
427
|
+
chainName = 'ethereum';
|
|
428
|
+
assetId = '0xC581b735A1688071A1746c968e0798D642EDE491'.toLowerCase(); // EURT Ethereum
|
|
429
|
+
}
|
|
430
|
+
// CRV
|
|
431
|
+
if (assetId.toLowerCase() === curve.constants.ALIASES.crv) {
|
|
432
|
+
assetId = 'curve-dao-token';
|
|
433
|
+
}
|
|
434
|
+
if ((((_f = _usdRatesCache[assetId]) === null || _f === void 0 ? void 0 : _f.time) || 0) + 600000 < Date.now()) {
|
|
435
|
+
const url = [nativeTokenName, 'ethereum', 'bitcoin', 'link', 'curve-dao-token', 'stasis-eurs'].includes(assetId.toLowerCase()) ?
|
|
436
|
+
`https://api.coingecko.com/api/v3/simple/price?ids=${assetId}&vs_currencies=usd` :
|
|
437
|
+
`https://api.coingecko.com/api/v3/simple/token_price/${chainName}?contract_addresses=${assetId}&vs_currencies=usd`;
|
|
438
|
+
const response = yield axios.get(url);
|
|
439
|
+
try {
|
|
440
|
+
_usdRatesCache[assetId] = { 'rate': (_g = response.data[assetId]['usd']) !== null && _g !== void 0 ? _g : 0, 'time': Date.now() };
|
|
760
441
|
}
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
}); };
|
|
764
|
-
export var getGasPriceFromL2 = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
765
|
-
var gasPrice;
|
|
766
|
-
return __generator(this, function (_a) {
|
|
767
|
-
switch (_a.label) {
|
|
768
|
-
case 0:
|
|
769
|
-
if (!(curve.chainId === 42161)) return [3 /*break*/, 2];
|
|
770
|
-
return [4 /*yield*/, getBaseFeeByLastBlock()];
|
|
771
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
772
|
-
case 2:
|
|
773
|
-
if (!(curve.chainId === 196)) return [3 /*break*/, 4];
|
|
774
|
-
return [4 /*yield*/, getGasPrice()]; // gwei
|
|
775
|
-
case 3: return [2 /*return*/, _a.sent()]; // gwei
|
|
776
|
-
case 4:
|
|
777
|
-
if (!(curve.chainId === 5000)) return [3 /*break*/, 6];
|
|
778
|
-
return [4 /*yield*/, getGasPrice()]; // gwei
|
|
779
|
-
case 5: return [2 /*return*/, _a.sent()]; // gwei
|
|
780
|
-
case 6:
|
|
781
|
-
if (!L2Networks.includes(curve.chainId)) return [3 /*break*/, 8];
|
|
782
|
-
return [4 /*yield*/, curve.contracts[curve.constants.ALIASES.gas_oracle_blob].contract.gasPrice({ "gasPrice": "0x2000000" })];
|
|
783
|
-
case 7:
|
|
784
|
-
gasPrice = _a.sent();
|
|
785
|
-
return [2 /*return*/, Number(gasPrice)];
|
|
786
|
-
case 8: throw Error("This method exists only for L2 networks");
|
|
442
|
+
catch (err) { // TODO pay attention!
|
|
443
|
+
_usdRatesCache[assetId] = { 'rate': 0, 'time': Date.now() };
|
|
787
444
|
}
|
|
788
|
-
}
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
maxPriorityFeePerGas: 0.01,
|
|
802
|
-
}];
|
|
803
|
-
case 2:
|
|
804
|
-
if (!(curve.chainId === 196)) return [3 /*break*/, 4];
|
|
805
|
-
return [4 /*yield*/, getGasPrice()];
|
|
806
|
-
case 3:
|
|
807
|
-
gasPrice = _a.sent();
|
|
808
|
-
return [2 /*return*/, {
|
|
809
|
-
gasPrice: gasPrice,
|
|
810
|
-
}];
|
|
811
|
-
case 4:
|
|
812
|
-
if (!(curve.chainId === 5000)) return [3 /*break*/, 6];
|
|
813
|
-
return [4 /*yield*/, getBaseFeeByLastBlock()];
|
|
814
|
-
case 5:
|
|
815
|
-
baseFee = _a.sent();
|
|
816
|
-
return [2 /*return*/, {
|
|
817
|
-
maxFeePerGas: Number(((baseFee * 1.1) + 0.01).toFixed(2)),
|
|
818
|
-
maxPriorityFeePerGas: 0.01,
|
|
819
|
-
}];
|
|
820
|
-
case 6: throw Error("This method exists only for L2 networks");
|
|
445
|
+
}
|
|
446
|
+
return _usdRatesCache[assetId]['rate'];
|
|
447
|
+
});
|
|
448
|
+
export const getUsdRate = (coin) => __awaiter(void 0, void 0, void 0, function* () {
|
|
449
|
+
const [coinAddress] = _getCoinAddressesNoCheck(coin);
|
|
450
|
+
return yield _getUsdRate(coinAddress);
|
|
451
|
+
});
|
|
452
|
+
export const getBaseFeeByLastBlock = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
453
|
+
const provider = curve.provider;
|
|
454
|
+
try {
|
|
455
|
+
const block = yield provider.getBlock('latest');
|
|
456
|
+
if (!block) {
|
|
457
|
+
return 0.01;
|
|
821
458
|
}
|
|
822
|
-
|
|
823
|
-
}
|
|
824
|
-
|
|
825
|
-
|
|
459
|
+
return Number(block.baseFeePerGas) / (Math.pow(10, 9));
|
|
460
|
+
}
|
|
461
|
+
catch (error) {
|
|
462
|
+
throw new Error(error);
|
|
463
|
+
}
|
|
464
|
+
});
|
|
465
|
+
export const getGasPrice = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
466
|
+
const provider = curve.provider;
|
|
467
|
+
return Number((Number((yield provider.getFeeData()).gasPrice) / 1e9).toFixed(2));
|
|
468
|
+
});
|
|
469
|
+
export const getGasPriceFromL1 = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
470
|
+
if (L2Networks.includes(curve.chainId) && curve.L1WeightedGasPrice) {
|
|
471
|
+
return curve.L1WeightedGasPrice + 1e9; // + 1 gwei
|
|
472
|
+
}
|
|
473
|
+
else {
|
|
474
|
+
throw Error("This method exists only for L2 networks");
|
|
475
|
+
}
|
|
476
|
+
});
|
|
477
|
+
export const getGasPriceFromL2 = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
478
|
+
if (curve.chainId === 42161) {
|
|
479
|
+
return yield getBaseFeeByLastBlock();
|
|
480
|
+
}
|
|
481
|
+
if (curve.chainId === 196) {
|
|
482
|
+
return yield getGasPrice(); // gwei
|
|
483
|
+
}
|
|
484
|
+
if (curve.chainId === 324) {
|
|
485
|
+
return yield getGasPrice(); // gwei
|
|
486
|
+
}
|
|
487
|
+
if (curve.chainId === 5000) {
|
|
488
|
+
return yield getGasPrice(); // gwei
|
|
489
|
+
}
|
|
490
|
+
if (L2Networks.includes(curve.chainId)) {
|
|
491
|
+
const gasPrice = yield curve.contracts[curve.constants.ALIASES.gas_oracle_blob].contract.gasPrice({ "gasPrice": "0x2000000" });
|
|
492
|
+
return Number(gasPrice);
|
|
493
|
+
}
|
|
494
|
+
else {
|
|
495
|
+
throw Error("This method exists only for L2 networks");
|
|
496
|
+
}
|
|
497
|
+
});
|
|
498
|
+
export const getGasInfoForL2 = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
499
|
+
if (curve.chainId === 42161) {
|
|
500
|
+
const baseFee = yield getBaseFeeByLastBlock();
|
|
501
|
+
return {
|
|
502
|
+
maxFeePerGas: Number(((baseFee * 1.1) + 0.01).toFixed(2)),
|
|
503
|
+
maxPriorityFeePerGas: 0.01,
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
else if (curve.chainId === 196) {
|
|
507
|
+
const gasPrice = yield getGasPrice();
|
|
508
|
+
return {
|
|
509
|
+
gasPrice,
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
else if (curve.chainId === 324) {
|
|
513
|
+
const gasPrice = yield getGasPrice();
|
|
514
|
+
return {
|
|
515
|
+
gasPrice,
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
else if (curve.chainId === 5000) {
|
|
519
|
+
const baseFee = yield getBaseFeeByLastBlock();
|
|
520
|
+
return {
|
|
521
|
+
maxFeePerGas: Number(((baseFee * 1.1) + 0.01).toFixed(2)),
|
|
522
|
+
maxPriorityFeePerGas: 0.01,
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
else {
|
|
526
|
+
throw Error("This method exists only for L2 networks");
|
|
527
|
+
}
|
|
528
|
+
});
|
|
529
|
+
export const getTxCostsUsd = (ethUsdRate, gasPrice, gas, gasPriceL1 = 0) => {
|
|
826
530
|
if (Array.isArray(gas)) {
|
|
827
531
|
return ethUsdRate * ((gas[0] * gasPrice / 1e18) + (gas[1] * gasPriceL1 / 1e18));
|
|
828
532
|
}
|
|
@@ -830,83 +534,40 @@ export var getTxCostsUsd = function (ethUsdRate, gasPrice, gas, gasPriceL1) {
|
|
|
830
534
|
return ethUsdRate * gas * gasPrice / 1e18;
|
|
831
535
|
}
|
|
832
536
|
};
|
|
833
|
-
|
|
834
|
-
if (network === void 0) { network = curve.chainId; }
|
|
537
|
+
const _getNetworkName = (network = curve.chainId) => {
|
|
835
538
|
if (typeof network === "number" && NETWORK_CONSTANTS[network]) {
|
|
836
539
|
return NETWORK_CONSTANTS[network].NAME;
|
|
837
540
|
}
|
|
838
|
-
else if (typeof network === "string" && Object.values(NETWORK_CONSTANTS).map(
|
|
839
|
-
return network;
|
|
840
|
-
}
|
|
841
|
-
else {
|
|
842
|
-
throw Error("Wrong network name or id: ".concat(network));
|
|
843
|
-
}
|
|
844
|
-
};
|
|
845
|
-
var _getChainId = function (network) {
|
|
846
|
-
if (network === void 0) { network = curve.chainId; }
|
|
847
|
-
if (typeof network === "number" && NETWORK_CONSTANTS[network]) {
|
|
541
|
+
else if (typeof network === "string" && Object.values(NETWORK_CONSTANTS).map((n) => n.NAME).includes(network)) {
|
|
848
542
|
return network;
|
|
849
543
|
}
|
|
850
|
-
else if (typeof network === "string" && Object.values(NETWORK_CONSTANTS).map(function (n) { return n.NAME; }).includes(network)) {
|
|
851
|
-
var idx = Object.values(NETWORK_CONSTANTS).map(function (n) { return n.NAME; }).indexOf(network);
|
|
852
|
-
return Number(Object.keys(NETWORK_CONSTANTS)[idx]);
|
|
853
|
-
}
|
|
854
544
|
else {
|
|
855
|
-
throw Error(
|
|
856
|
-
}
|
|
857
|
-
};
|
|
858
|
-
export
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
}
|
|
873
|
-
};
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
case 2:
|
|
882
|
-
if (!volumeNetworks.getFactoryAPYs.includes(curve.chainId)) return [3 /*break*/, 4];
|
|
883
|
-
return [4 /*yield*/, _getFactoryAPYs(network)];
|
|
884
|
-
case 3: return [2 /*return*/, _a.sent()];
|
|
885
|
-
case 4:
|
|
886
|
-
if (!volumeNetworks.getSubgraphData.includes(curve.chainId)) return [3 /*break*/, 6];
|
|
887
|
-
return [4 /*yield*/, _getSubgraphData(network)];
|
|
888
|
-
case 5: return [2 /*return*/, _a.sent()];
|
|
889
|
-
case 6: throw Error("Can't get volume for network: ".concat(network));
|
|
890
|
-
}
|
|
891
|
-
});
|
|
892
|
-
}); };
|
|
893
|
-
export var getVolume = function (network) {
|
|
894
|
-
if (network === void 0) { network = curve.chainId; }
|
|
895
|
-
return __awaiter(void 0, void 0, void 0, function () {
|
|
896
|
-
var _a, totalVolume, cryptoVolume, cryptoShare;
|
|
897
|
-
return __generator(this, function (_b) {
|
|
898
|
-
switch (_b.label) {
|
|
899
|
-
case 0:
|
|
900
|
-
network = _getNetworkName(network);
|
|
901
|
-
return [4 /*yield*/, getVolumeApiController(network)];
|
|
902
|
-
case 1:
|
|
903
|
-
_a = _b.sent(), totalVolume = _a.totalVolume, cryptoVolume = _a.cryptoVolume, cryptoShare = _a.cryptoShare;
|
|
904
|
-
return [2 /*return*/, { totalVolume: totalVolume, cryptoVolume: cryptoVolume, cryptoShare: cryptoShare }];
|
|
905
|
-
}
|
|
906
|
-
});
|
|
907
|
-
});
|
|
908
|
-
};
|
|
909
|
-
export var _setContracts = function (address, abi) {
|
|
545
|
+
throw Error(`Wrong network name or id: ${network}`);
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
export const getTVL = (network = curve.chainId) => __awaiter(void 0, void 0, void 0, function* () {
|
|
549
|
+
network = _getNetworkName(network);
|
|
550
|
+
const allTypesExtendedPoolData = yield _getAllPoolsFromApi(network);
|
|
551
|
+
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);
|
|
552
|
+
});
|
|
553
|
+
export const getVolumeApiController = (network) => __awaiter(void 0, void 0, void 0, function* () {
|
|
554
|
+
if (volumeNetworks.getVolumes.includes(curve.chainId)) {
|
|
555
|
+
return yield _getVolumes(network);
|
|
556
|
+
}
|
|
557
|
+
if (volumeNetworks.getFactoryAPYs.includes(curve.chainId)) {
|
|
558
|
+
return yield _getFactoryAPYs(network);
|
|
559
|
+
}
|
|
560
|
+
if (volumeNetworks.getSubgraphData.includes(curve.chainId)) {
|
|
561
|
+
return yield _getSubgraphData(network);
|
|
562
|
+
}
|
|
563
|
+
throw Error(`Can't get volume for network: ${network}`);
|
|
564
|
+
});
|
|
565
|
+
export const getVolume = (network = curve.chainId) => __awaiter(void 0, void 0, void 0, function* () {
|
|
566
|
+
network = _getNetworkName(network);
|
|
567
|
+
const { totalVolume, cryptoVolume, cryptoShare } = yield getVolumeApiController(network);
|
|
568
|
+
return { totalVolume, cryptoVolume, cryptoShare };
|
|
569
|
+
});
|
|
570
|
+
export const _setContracts = (address, abi) => {
|
|
910
571
|
curve.contracts[address] = {
|
|
911
572
|
contract: new Contract(address, abi, curve.signer || curve.provider),
|
|
912
573
|
multicallContract: new MulticallContract(address, abi),
|
|
@@ -915,73 +576,58 @@ export var _setContracts = function (address, abi) {
|
|
|
915
576
|
// Find k for which x * k = target_x or y * k = target_y
|
|
916
577
|
// k = max(target_x / x, target_y / y)
|
|
917
578
|
// small_x = x * k
|
|
918
|
-
export
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
579
|
+
export const _get_small_x = (_x, _y, x_decimals, y_decimals) => {
|
|
580
|
+
const target_x = BN(Math.pow(10, (x_decimals > 5 ? x_decimals - 3 : x_decimals)));
|
|
581
|
+
const target_y = BN(Math.pow(10, (y_decimals > 5 ? y_decimals - 3 : y_decimals)));
|
|
582
|
+
const x_int_BN = toBN(_x, 0);
|
|
583
|
+
const y_int_BN = toBN(_y, 0);
|
|
584
|
+
const k = BigNumber.max(target_x.div(x_int_BN), target_y.div(y_int_BN));
|
|
924
585
|
return BigNumber.min(x_int_BN.times(k), BN(Math.pow(10, x_decimals)));
|
|
925
586
|
};
|
|
926
|
-
export
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
587
|
+
export const _get_price_impact = (_x, _y, _small_x, _small_y, x_decimals, y_decimals) => {
|
|
588
|
+
const x_BN = toBN(_x, x_decimals);
|
|
589
|
+
const y_BN = toBN(_y, y_decimals);
|
|
590
|
+
const small_x_BN = toBN(_small_x, x_decimals);
|
|
591
|
+
const small_y_BN = toBN(_small_y, y_decimals);
|
|
592
|
+
const rateBN = y_BN.div(x_BN);
|
|
593
|
+
const smallRateBN = small_y_BN.div(small_x_BN);
|
|
933
594
|
if (rateBN.gt(smallRateBN))
|
|
934
595
|
return BN(0);
|
|
935
596
|
return BN(1).minus(rateBN.div(smallRateBN)).times(100);
|
|
936
597
|
};
|
|
937
|
-
export
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
return [4 /*yield*/, curve.multicallProvider.all(contractCalls)];
|
|
963
|
-
case 1:
|
|
964
|
-
_response = _b.sent();
|
|
965
|
-
if (ethIndex !== -1) {
|
|
966
|
-
_response.splice.apply(_response, __spreadArray([ethIndex * 2, 0], ['Ethereum', 'ETH', 18], false));
|
|
967
|
-
}
|
|
968
|
-
res = [];
|
|
969
|
-
coins.forEach(function (address, i) {
|
|
970
|
-
res.push({
|
|
971
|
-
name: _response.shift(),
|
|
972
|
-
symbol: _response.shift(),
|
|
973
|
-
decimals: Number(curve.formatUnits(_response.shift(), 0)),
|
|
974
|
-
});
|
|
975
|
-
});
|
|
976
|
-
return [2 /*return*/, res];
|
|
977
|
-
}
|
|
598
|
+
export const getCoinsData = (...coins) => __awaiter(void 0, void 0, void 0, function* () {
|
|
599
|
+
if (coins.length == 1 && Array.isArray(coins[0]))
|
|
600
|
+
coins = coins[0];
|
|
601
|
+
coins = coins;
|
|
602
|
+
const coinAddresses = _getCoinAddressesNoCheck(coins);
|
|
603
|
+
console.log(coinAddresses);
|
|
604
|
+
const ethIndex = getEthIndex(coinAddresses);
|
|
605
|
+
if (ethIndex !== -1) {
|
|
606
|
+
coinAddresses.splice(ethIndex, 1);
|
|
607
|
+
}
|
|
608
|
+
const contractCalls = [];
|
|
609
|
+
for (const coinAddr of coinAddresses) {
|
|
610
|
+
const coinContract = new MulticallContract(coinAddr, ERC20Abi);
|
|
611
|
+
contractCalls.push(coinContract.name(), coinContract.symbol(), coinContract.decimals());
|
|
612
|
+
}
|
|
613
|
+
const _response = yield curve.multicallProvider.all(contractCalls);
|
|
614
|
+
if (ethIndex !== -1) {
|
|
615
|
+
_response.splice(ethIndex * 2, 0, ...['Ethereum', 'ETH', 18]);
|
|
616
|
+
}
|
|
617
|
+
const res = [];
|
|
618
|
+
coins.forEach(() => {
|
|
619
|
+
res.push({
|
|
620
|
+
name: _response.shift(),
|
|
621
|
+
symbol: _response.shift(),
|
|
622
|
+
decimals: Number(curve.formatUnits(_response.shift(), 0)),
|
|
978
623
|
});
|
|
979
624
|
});
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
export
|
|
983
|
-
export
|
|
984
|
-
|
|
625
|
+
return res;
|
|
626
|
+
});
|
|
627
|
+
export const hasDepositAndStake = () => curve.constants.ALIASES.deposit_and_stake !== curve.constants.ZERO_ADDRESS;
|
|
628
|
+
export const hasRouter = () => curve.constants.ALIASES.router !== curve.constants.ZERO_ADDRESS;
|
|
629
|
+
export const getCountArgsOfMethodByContract = (contract, methodName) => {
|
|
630
|
+
const func = contract.interface.fragments.find((item) => item.name === methodName);
|
|
985
631
|
if (func) {
|
|
986
632
|
return func.inputs.length;
|
|
987
633
|
}
|
|
@@ -989,17 +635,9 @@ export var getCountArgsOfMethodByContract = function (contract, methodName) {
|
|
|
989
635
|
return -1;
|
|
990
636
|
}
|
|
991
637
|
};
|
|
992
|
-
export
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
return true;
|
|
996
|
-
}
|
|
997
|
-
else {
|
|
998
|
-
return false;
|
|
999
|
-
}
|
|
1000
|
-
};
|
|
1001
|
-
export var getPoolName = function (name) {
|
|
1002
|
-
var separatedName = name.split(": ");
|
|
638
|
+
export const isMethodExist = (contract, methodName) => contract.interface.fragments.find((item) => item.name === methodName) !== undefined;
|
|
639
|
+
export const getPoolName = (name) => {
|
|
640
|
+
const separatedName = name.split(": ");
|
|
1003
641
|
if (separatedName.length > 1) {
|
|
1004
642
|
return separatedName[1].trim();
|
|
1005
643
|
}
|
|
@@ -1007,10 +645,10 @@ export var getPoolName = function (name) {
|
|
|
1007
645
|
return separatedName[0].trim();
|
|
1008
646
|
}
|
|
1009
647
|
};
|
|
1010
|
-
export
|
|
648
|
+
export const isStableNgPool = (name) => {
|
|
1011
649
|
return name.includes('factory-stable-ng');
|
|
1012
650
|
};
|
|
1013
|
-
export
|
|
651
|
+
export const assetTypeNameHandler = (assetTypeName) => {
|
|
1014
652
|
if (assetTypeName.toUpperCase() === 'UNKNOWN') {
|
|
1015
653
|
return 'OTHER';
|
|
1016
654
|
}
|
|
@@ -1018,63 +656,58 @@ export var assetTypeNameHandler = function (assetTypeName) {
|
|
|
1018
656
|
return assetTypeName.toUpperCase();
|
|
1019
657
|
}
|
|
1020
658
|
};
|
|
1021
|
-
export
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
basePoolIds = Object.keys(pools).filter(function (item) { return basePoolList.includes(pools[item].swap_address); });
|
|
1042
|
-
return [2 /*return*/, basePoolIds.map(function (poolId) {
|
|
1043
|
-
var pool = getPool(poolId);
|
|
1044
|
-
return {
|
|
1045
|
-
id: poolId,
|
|
1046
|
-
name: pool.name,
|
|
1047
|
-
pool: pool.address,
|
|
1048
|
-
token: pool.lpToken,
|
|
1049
|
-
coins: pool.underlyingCoinAddresses,
|
|
1050
|
-
};
|
|
1051
|
-
})];
|
|
1052
|
-
}
|
|
659
|
+
export const getBasePools = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
660
|
+
const factoryContract = curve.contracts[curve.constants.ALIASES['stable_ng_factory']].contract;
|
|
661
|
+
const factoryMulticallContract = curve.contracts[curve.constants.ALIASES['stable_ng_factory']].multicallContract;
|
|
662
|
+
const basePoolCount = Number(curve.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 curve.multicallProvider.all(calls)).map((item) => item.toLowerCase());
|
|
668
|
+
const pools = Object.assign(Object.assign(Object.assign({}, curve.constants.STABLE_NG_FACTORY_POOLS_DATA), curve.constants.FACTORY_POOLS_DATA), curve.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(poolId);
|
|
672
|
+
return {
|
|
673
|
+
id: poolId,
|
|
674
|
+
name: pool.name,
|
|
675
|
+
pool: pool.address,
|
|
676
|
+
token: pool.lpToken,
|
|
677
|
+
coins: pool.underlyingCoinAddresses,
|
|
678
|
+
};
|
|
1053
679
|
});
|
|
1054
|
-
});
|
|
1055
|
-
export
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
};
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
680
|
+
});
|
|
681
|
+
export function log(fnName, ...args) {
|
|
682
|
+
if (process.env.NODE_ENV === 'development') {
|
|
683
|
+
console.log(`curve-js@${new Date().toISOString()} -> ${fnName}:`, ...args);
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
export function runWorker(code, syncFn, inputData, timeout = 30000) {
|
|
687
|
+
if (typeof Worker === 'undefined') {
|
|
688
|
+
// in nodejs run worker in main thread
|
|
689
|
+
return Promise.resolve(syncFn()(inputData));
|
|
690
|
+
}
|
|
691
|
+
const blob = new Blob([code], { type: 'application/javascript' });
|
|
692
|
+
const blobUrl = URL.createObjectURL(blob);
|
|
693
|
+
const worker = new Worker(blobUrl, { type: 'module' });
|
|
694
|
+
return new Promise((resolve, reject) => {
|
|
695
|
+
const timer = setTimeout(() => reject(new Error('Timeout')), timeout);
|
|
696
|
+
worker.onerror = (e) => {
|
|
697
|
+
clearTimeout(timer);
|
|
698
|
+
console.error(code, inputData, e);
|
|
699
|
+
reject(e);
|
|
700
|
+
};
|
|
701
|
+
worker.onmessage = (e) => {
|
|
702
|
+
const { type, result } = e.data;
|
|
703
|
+
if (type === inputData.type) {
|
|
704
|
+
clearTimeout(timer);
|
|
705
|
+
resolve(result);
|
|
706
|
+
// console.log(code, inputData, result, start - Date.now());
|
|
707
|
+
}
|
|
708
|
+
};
|
|
709
|
+
worker.postMessage(inputData);
|
|
710
|
+
}).finally(() => {
|
|
711
|
+
worker.terminate();
|
|
712
|
+
});
|
|
713
|
+
}
|