@curvefi/api 2.31.1 → 2.33.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/boosting.js +385 -135
- package/lib/constants/abis/stable_calc.json +151 -0
- package/lib/constants/abis/wbeth/swap.json +1086 -0
- package/lib/constants/aliases.js +33 -11
- 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/celo.js +5 -5
- package/lib/constants/coins/ethereum.js +10 -9
- package/lib/constants/coins/fantom.js +7 -7
- package/lib/constants/coins/kava.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/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/celo.js +1 -1
- package/lib/constants/pools/ethereum.js +26 -1
- package/lib/constants/pools/fantom.js +1 -1
- package/lib/constants/pools/kava.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/utils.d.ts +1 -1
- package/lib/constants/utils.js +19 -18
- package/lib/curve.js +507 -281
- package/lib/external-api.js +132 -45
- package/lib/factory/common.js +3 -3
- package/lib/factory/constants-crypto.js +21 -21
- package/lib/factory/constants.js +32 -31
- package/lib/factory/deploy.js +336 -176
- package/lib/factory/factory-api.js +256 -180
- package/lib/factory/factory-crypto.js +309 -163
- package/lib/factory/factory.d.ts +1 -1
- package/lib/factory/factory.js +336 -186
- package/lib/index.js +98 -44
- package/lib/interfaces.d.ts +1 -1
- package/lib/pools/PoolTemplate.js +2882 -1511
- package/lib/pools/mixins/common.js +106 -22
- package/lib/pools/mixins/depositBalancedAmountsMixins.js +131 -48
- package/lib/pools/mixins/depositMixins.js +413 -144
- package/lib/pools/mixins/depositWrappedMixins.js +223 -72
- package/lib/pools/mixins/poolBalancesMixin.js +98 -22
- package/lib/pools/mixins/swapMixins.js +347 -125
- package/lib/pools/mixins/swapWrappedMixins.js +270 -88
- package/lib/pools/mixins/withdrawExpectedMixins.js +104 -23
- package/lib/pools/mixins/withdrawImbalanceMixins.js +316 -97
- package/lib/pools/mixins/withdrawImbalanceWrappedMixins.js +187 -51
- package/lib/pools/mixins/withdrawMixins.js +385 -122
- package/lib/pools/mixins/withdrawOneCoinExpectedMixins.js +88 -16
- package/lib/pools/mixins/withdrawOneCoinMixins.js +386 -123
- package/lib/pools/mixins/withdrawOneCoinWrappedExpectedMixins.js +62 -8
- package/lib/pools/mixins/withdrawOneCoinWrappedMixins.js +185 -53
- package/lib/pools/mixins/withdrawWrappedMixins.js +185 -50
- package/lib/pools/poolConstructor.js +25 -5
- package/lib/pools/utils.js +488 -298
- package/lib/router.js +636 -378
- package/lib/utils.js +675 -354
- package/package.json +1 -1
package/lib/utils.js
CHANGED
|
@@ -1,3 +1,59 @@
|
|
|
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
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
15
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
16
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
17
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
18
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
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
|
+
};
|
|
1
57
|
import axios from 'axios';
|
|
2
58
|
import { Contract } from 'ethers';
|
|
3
59
|
import { Contract as MulticallContract } from "ethcall";
|
|
@@ -5,398 +61,648 @@ import BigNumber from 'bignumber.js';
|
|
|
5
61
|
import { curve, NETWORK_CONSTANTS } from "./curve.js";
|
|
6
62
|
import { _getFactoryAPYsAndVolumes, _getLegacyAPYsAndVolumes, _getPoolsFromApi, _getSubgraphData } from "./external-api.js";
|
|
7
63
|
import ERC20Abi from './constants/abis/ERC20.json' assert { type: 'json' };
|
|
8
|
-
export
|
|
9
|
-
export const MAX_ALLOWANCE = (
|
|
64
|
+
export var ETH_ADDRESS = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
|
|
65
|
+
// export const MAX_ALLOWANCE = curve.parseUnits(new BigNumber(2).pow(256).minus(1).toFixed(), 0);
|
|
66
|
+
export var MAX_ALLOWANCE = BigInt("115792089237316195423570985008687907853269984665640564039457584007913129639935"); // 2**256 - 1
|
|
10
67
|
// Formatting numbers
|
|
11
|
-
export
|
|
68
|
+
export var _cutZeros = function (strn) {
|
|
12
69
|
return strn.replace(/0+$/gi, '').replace(/\.$/gi, '');
|
|
13
70
|
};
|
|
14
|
-
export
|
|
71
|
+
export var checkNumber = function (n) {
|
|
15
72
|
if (Number(n) !== Number(n))
|
|
16
|
-
throw Error(
|
|
73
|
+
throw Error("".concat(n, " is not a number")); // NaN
|
|
17
74
|
return n;
|
|
18
75
|
};
|
|
19
|
-
export
|
|
76
|
+
export var formatNumber = function (n, decimals) {
|
|
77
|
+
if (decimals === void 0) { decimals = 18; }
|
|
20
78
|
if (Number(n) !== Number(n))
|
|
21
|
-
throw Error(
|
|
22
|
-
|
|
79
|
+
throw Error("".concat(n, " is not a number")); // NaN
|
|
80
|
+
var _a = String(n).split("."), integer = _a[0], fractional = _a[1];
|
|
23
81
|
return !fractional ? integer : integer + "." + fractional.slice(0, decimals);
|
|
24
82
|
};
|
|
25
|
-
export
|
|
83
|
+
export var parseUnits = function (n, decimals) {
|
|
84
|
+
if (decimals === void 0) { decimals = 18; }
|
|
26
85
|
return curve.parseUnits(formatNumber(n, decimals), decimals);
|
|
27
86
|
};
|
|
28
87
|
// bignumber.js
|
|
29
|
-
export
|
|
30
|
-
export
|
|
88
|
+
export var BN = function (val) { return new BigNumber(checkNumber(val)); };
|
|
89
|
+
export var toBN = function (n, decimals) {
|
|
90
|
+
if (decimals === void 0) { decimals = 18; }
|
|
31
91
|
return BN(curve.formatUnits(n, decimals));
|
|
32
92
|
};
|
|
33
|
-
export
|
|
93
|
+
export var toStringFromBN = function (bn, decimals) {
|
|
94
|
+
if (decimals === void 0) { decimals = 18; }
|
|
34
95
|
return bn.toFixed(decimals);
|
|
35
96
|
};
|
|
36
|
-
export
|
|
97
|
+
export var fromBN = function (bn, decimals) {
|
|
98
|
+
if (decimals === void 0) { decimals = 18; }
|
|
37
99
|
return curve.parseUnits(toStringFromBN(bn, decimals), decimals);
|
|
38
100
|
};
|
|
39
101
|
// -------------------
|
|
40
|
-
export
|
|
41
|
-
export
|
|
42
|
-
export
|
|
102
|
+
export var isEth = function (address) { return address.toLowerCase() === ETH_ADDRESS.toLowerCase(); };
|
|
103
|
+
export var getEthIndex = function (addresses) { return addresses.map(function (address) { return address.toLowerCase(); }).indexOf(ETH_ADDRESS.toLowerCase()); };
|
|
104
|
+
export var mulBy1_3 = function (n) { return n * curve.parseUnits("130", 0) / curve.parseUnits("100", 0); };
|
|
43
105
|
// coins can be either addresses or symbols
|
|
44
|
-
export
|
|
106
|
+
export var _getCoinAddressesNoCheck = function () {
|
|
107
|
+
var coins = [];
|
|
108
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
109
|
+
coins[_i] = arguments[_i];
|
|
110
|
+
}
|
|
45
111
|
if (coins.length == 1 && Array.isArray(coins[0]))
|
|
46
112
|
coins = coins[0];
|
|
47
113
|
coins = coins;
|
|
48
|
-
return coins.map((c)
|
|
114
|
+
return coins.map(function (c) { return c.toLowerCase(); }).map(function (c) { return curve.constants.COINS[c] || c; });
|
|
49
115
|
};
|
|
50
|
-
export
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
116
|
+
export var _getCoinAddresses = function () {
|
|
117
|
+
var coins = [];
|
|
118
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
119
|
+
coins[_i] = arguments[_i];
|
|
120
|
+
}
|
|
121
|
+
var coinAddresses = _getCoinAddressesNoCheck.apply(void 0, coins);
|
|
122
|
+
var availableAddresses = __spreadArray(__spreadArray([], Object.keys(curve.constants.DECIMALS), true), curve.constants.GAUGES, true);
|
|
123
|
+
for (var _a = 0, coinAddresses_1 = coinAddresses; _a < coinAddresses_1.length; _a++) {
|
|
124
|
+
var coinAddr = coinAddresses_1[_a];
|
|
54
125
|
if (!availableAddresses.includes(coinAddr))
|
|
55
|
-
throw Error(
|
|
126
|
+
throw Error("Coin with address '".concat(coinAddr, "' is not available"));
|
|
56
127
|
}
|
|
57
128
|
return coinAddresses;
|
|
58
129
|
};
|
|
59
|
-
export
|
|
130
|
+
export var _getCoinDecimals = function () {
|
|
131
|
+
var coinAddresses = [];
|
|
132
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
133
|
+
coinAddresses[_i] = arguments[_i];
|
|
134
|
+
}
|
|
60
135
|
if (coinAddresses.length == 1 && Array.isArray(coinAddresses[0]))
|
|
61
136
|
coinAddresses = coinAddresses[0];
|
|
62
137
|
coinAddresses = coinAddresses;
|
|
63
|
-
return coinAddresses.map((coinAddr)
|
|
64
|
-
};
|
|
65
|
-
export
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
138
|
+
return coinAddresses.map(function (coinAddr) { var _a; return (_a = curve.constants.DECIMALS[coinAddr.toLowerCase()]) !== null && _a !== void 0 ? _a : 18; }); // 18 for gauges
|
|
139
|
+
};
|
|
140
|
+
export var _getBalances = function (coins, addresses) { return __awaiter(void 0, void 0, void 0, function () {
|
|
141
|
+
var coinAddresses, decimals, ethIndex, contractCalls, _loop_1, _i, coinAddresses_2, coinAddr, _response, ethBalances, _a, addresses_1, address, _b, _c, _balances, balances, _d, addresses_2, address;
|
|
142
|
+
return __generator(this, function (_e) {
|
|
143
|
+
switch (_e.label) {
|
|
144
|
+
case 0:
|
|
145
|
+
coinAddresses = _getCoinAddresses(coins);
|
|
146
|
+
decimals = _getCoinDecimals(coinAddresses);
|
|
147
|
+
ethIndex = getEthIndex(coinAddresses);
|
|
148
|
+
if (ethIndex !== -1) {
|
|
149
|
+
coinAddresses.splice(ethIndex, 1);
|
|
150
|
+
}
|
|
151
|
+
contractCalls = [];
|
|
152
|
+
_loop_1 = function (coinAddr) {
|
|
153
|
+
contractCalls.push.apply(contractCalls, addresses.map(function (address) { return curve.contracts[coinAddr].multicallContract.balanceOf(address); }));
|
|
154
|
+
};
|
|
155
|
+
for (_i = 0, coinAddresses_2 = coinAddresses; _i < coinAddresses_2.length; _i++) {
|
|
156
|
+
coinAddr = coinAddresses_2[_i];
|
|
157
|
+
_loop_1(coinAddr);
|
|
158
|
+
}
|
|
159
|
+
return [4 /*yield*/, curve.multicallProvider.all(contractCalls)];
|
|
160
|
+
case 1:
|
|
161
|
+
_response = _e.sent();
|
|
162
|
+
if (!(ethIndex !== -1)) return [3 /*break*/, 6];
|
|
163
|
+
ethBalances = [];
|
|
164
|
+
_a = 0, addresses_1 = addresses;
|
|
165
|
+
_e.label = 2;
|
|
166
|
+
case 2:
|
|
167
|
+
if (!(_a < addresses_1.length)) return [3 /*break*/, 5];
|
|
168
|
+
address = addresses_1[_a];
|
|
169
|
+
_c = (_b = ethBalances).push;
|
|
170
|
+
return [4 /*yield*/, curve.provider.getBalance(address)];
|
|
171
|
+
case 3:
|
|
172
|
+
_c.apply(_b, [_e.sent()]);
|
|
173
|
+
_e.label = 4;
|
|
174
|
+
case 4:
|
|
175
|
+
_a++;
|
|
176
|
+
return [3 /*break*/, 2];
|
|
177
|
+
case 5:
|
|
178
|
+
_response.splice.apply(_response, __spreadArray([ethIndex * addresses.length, 0], ethBalances, false));
|
|
179
|
+
_e.label = 6;
|
|
180
|
+
case 6:
|
|
181
|
+
_balances = {};
|
|
182
|
+
addresses.forEach(function (address, i) {
|
|
183
|
+
_balances[address] = coins.map(function (_, j) { return _response[i + (j * addresses.length)]; });
|
|
184
|
+
});
|
|
185
|
+
balances = {};
|
|
186
|
+
for (_d = 0, addresses_2 = addresses; _d < addresses_2.length; _d++) {
|
|
187
|
+
address = addresses_2[_d];
|
|
188
|
+
balances[address] = _balances[address].map(function (b, i) { return curve.formatUnits(b, decimals[i]); });
|
|
189
|
+
}
|
|
190
|
+
return [2 /*return*/, balances];
|
|
81
191
|
}
|
|
82
|
-
_response.splice(ethIndex * addresses.length, 0, ...ethBalances);
|
|
83
|
-
}
|
|
84
|
-
const _balances = {};
|
|
85
|
-
addresses.forEach((address, i) => {
|
|
86
|
-
_balances[address] = coins.map((_, j) => _response[i + (j * addresses.length)]);
|
|
87
192
|
});
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
balances[address] = _balances[address].map((b, i) => curve.formatUnits(b, decimals[i]));
|
|
91
|
-
}
|
|
92
|
-
return balances;
|
|
93
|
-
};
|
|
94
|
-
export const _prepareAddresses = (addresses) => {
|
|
193
|
+
}); };
|
|
194
|
+
export var _prepareAddresses = function (addresses) {
|
|
95
195
|
if (addresses.length == 1 && Array.isArray(addresses[0]))
|
|
96
196
|
addresses = addresses[0];
|
|
97
197
|
if (addresses.length === 0 && curve.signerAddress !== '')
|
|
98
198
|
addresses = [curve.signerAddress];
|
|
99
199
|
addresses = addresses;
|
|
100
|
-
return addresses.filter((val, idx, arr)
|
|
200
|
+
return addresses.filter(function (val, idx, arr) { return arr.indexOf(val) === idx; });
|
|
101
201
|
};
|
|
102
|
-
export
|
|
103
|
-
addresses =
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
};
|
|
107
|
-
export const _getAllowance = async (coins, address, spender) => {
|
|
108
|
-
const _coins = [...coins];
|
|
109
|
-
const ethIndex = getEthIndex(_coins);
|
|
110
|
-
if (ethIndex !== -1) {
|
|
111
|
-
_coins.splice(ethIndex, 1);
|
|
112
|
-
}
|
|
113
|
-
let allowance;
|
|
114
|
-
if (_coins.length === 1) {
|
|
115
|
-
allowance = [await curve.contracts[_coins[0]].contract.allowance(address, spender, curve.constantOptions)];
|
|
202
|
+
export var getBalances = function (coins) {
|
|
203
|
+
var addresses = [];
|
|
204
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
205
|
+
addresses[_i - 1] = arguments[_i];
|
|
116
206
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
207
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
208
|
+
var balances;
|
|
209
|
+
return __generator(this, function (_a) {
|
|
210
|
+
switch (_a.label) {
|
|
211
|
+
case 0:
|
|
212
|
+
addresses = _prepareAddresses(addresses);
|
|
213
|
+
return [4 /*yield*/, _getBalances(coins, addresses)];
|
|
214
|
+
case 1:
|
|
215
|
+
balances = _a.sent();
|
|
216
|
+
return [2 /*return*/, addresses.length === 1 ? balances[addresses[0]] : balances];
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
});
|
|
125
220
|
};
|
|
221
|
+
export var _getAllowance = function (coins, address, spender) { return __awaiter(void 0, void 0, void 0, function () {
|
|
222
|
+
var _coins, ethIndex, allowance, contractCalls;
|
|
223
|
+
return __generator(this, function (_a) {
|
|
224
|
+
switch (_a.label) {
|
|
225
|
+
case 0:
|
|
226
|
+
_coins = __spreadArray([], coins, true);
|
|
227
|
+
ethIndex = getEthIndex(_coins);
|
|
228
|
+
if (ethIndex !== -1) {
|
|
229
|
+
_coins.splice(ethIndex, 1);
|
|
230
|
+
}
|
|
231
|
+
if (!(_coins.length === 1)) return [3 /*break*/, 2];
|
|
232
|
+
return [4 /*yield*/, curve.contracts[_coins[0]].contract.allowance(address, spender, curve.constantOptions)];
|
|
233
|
+
case 1:
|
|
234
|
+
allowance = [_a.sent()];
|
|
235
|
+
return [3 /*break*/, 4];
|
|
236
|
+
case 2:
|
|
237
|
+
contractCalls = _coins.map(function (coinAddr) { return curve.contracts[coinAddr].multicallContract.allowance(address, spender); });
|
|
238
|
+
return [4 /*yield*/, curve.multicallProvider.all(contractCalls)];
|
|
239
|
+
case 3:
|
|
240
|
+
allowance = _a.sent();
|
|
241
|
+
_a.label = 4;
|
|
242
|
+
case 4:
|
|
243
|
+
if (ethIndex !== -1) {
|
|
244
|
+
allowance.splice(ethIndex, 0, MAX_ALLOWANCE);
|
|
245
|
+
}
|
|
246
|
+
return [2 /*return*/, allowance];
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
}); };
|
|
126
250
|
// coins can be either addresses or symbols
|
|
127
|
-
export
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
251
|
+
export var getAllowance = function (coins, address, spender) { return __awaiter(void 0, void 0, void 0, function () {
|
|
252
|
+
var coinAddresses, decimals, _allowance;
|
|
253
|
+
return __generator(this, function (_a) {
|
|
254
|
+
switch (_a.label) {
|
|
255
|
+
case 0:
|
|
256
|
+
coinAddresses = _getCoinAddresses(coins);
|
|
257
|
+
decimals = _getCoinDecimals(coinAddresses);
|
|
258
|
+
return [4 /*yield*/, _getAllowance(coinAddresses, address, spender)];
|
|
259
|
+
case 1:
|
|
260
|
+
_allowance = _a.sent();
|
|
261
|
+
return [2 /*return*/, _allowance.map(function (a, i) { return curve.formatUnits(a, decimals[i]); })];
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
}); };
|
|
133
265
|
// coins can be either addresses or symbols
|
|
134
|
-
export
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
if (allowance[i] < amounts[i]) {
|
|
147
|
-
const contract = curve.contracts[coins[i]].contract;
|
|
148
|
-
const _approveAmount = isMax ? MAX_ALLOWANCE : amounts[i];
|
|
149
|
-
await curve.updateFeeData();
|
|
150
|
-
if (allowance[i] > 0n) {
|
|
151
|
-
const gasLimit = mulBy1_3(await contract.approve.estimateGas(spender, 0n, curve.constantOptions));
|
|
152
|
-
txHashes.push((await contract.approve(spender, 0n, { ...curve.options, gasLimit })).hash);
|
|
153
|
-
}
|
|
154
|
-
const gasLimit = mulBy1_3(await contract.approve.estimateGas(spender, _approveAmount, curve.constantOptions));
|
|
155
|
-
txHashes.push((await contract.approve(spender, _approveAmount, { ...curve.options, gasLimit })).hash);
|
|
266
|
+
export var hasAllowance = function (coins, amounts, address, spender) { return __awaiter(void 0, void 0, void 0, function () {
|
|
267
|
+
var coinAddresses, decimals, _allowance, _amounts;
|
|
268
|
+
return __generator(this, function (_a) {
|
|
269
|
+
switch (_a.label) {
|
|
270
|
+
case 0:
|
|
271
|
+
coinAddresses = _getCoinAddresses(coins);
|
|
272
|
+
decimals = _getCoinDecimals(coinAddresses);
|
|
273
|
+
return [4 /*yield*/, _getAllowance(coinAddresses, address, spender)];
|
|
274
|
+
case 1:
|
|
275
|
+
_allowance = _a.sent();
|
|
276
|
+
_amounts = amounts.map(function (a, i) { return parseUnits(a, decimals[i]); });
|
|
277
|
+
return [2 /*return*/, _allowance.map(function (a, i) { return a >= _amounts[i]; }).reduce(function (a, b) { return a && b; })];
|
|
156
278
|
}
|
|
157
|
-
}
|
|
158
|
-
|
|
279
|
+
});
|
|
280
|
+
}); };
|
|
281
|
+
export var _ensureAllowance = function (coins, amounts, spender, isMax) {
|
|
282
|
+
if (isMax === void 0) { isMax = true; }
|
|
283
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
284
|
+
var address, allowance, txHashes, i, contract, _approveAmount, gasLimit_1, _a, _b, _c, gasLimit, _d, _e, _f;
|
|
285
|
+
return __generator(this, function (_g) {
|
|
286
|
+
switch (_g.label) {
|
|
287
|
+
case 0:
|
|
288
|
+
address = curve.signerAddress;
|
|
289
|
+
return [4 /*yield*/, _getAllowance(coins, address, spender)];
|
|
290
|
+
case 1:
|
|
291
|
+
allowance = _g.sent();
|
|
292
|
+
txHashes = [];
|
|
293
|
+
i = 0;
|
|
294
|
+
_g.label = 2;
|
|
295
|
+
case 2:
|
|
296
|
+
if (!(i < allowance.length)) return [3 /*break*/, 10];
|
|
297
|
+
if (!(allowance[i] < amounts[i])) return [3 /*break*/, 9];
|
|
298
|
+
contract = curve.contracts[coins[i]].contract;
|
|
299
|
+
_approveAmount = isMax ? MAX_ALLOWANCE : amounts[i];
|
|
300
|
+
return [4 /*yield*/, curve.updateFeeData()];
|
|
301
|
+
case 3:
|
|
302
|
+
_g.sent();
|
|
303
|
+
if (!(allowance[i] > curve.parseUnits("0"))) return [3 /*break*/, 6];
|
|
304
|
+
_a = mulBy1_3;
|
|
305
|
+
return [4 /*yield*/, contract.approve.estimateGas(spender, curve.parseUnits("0"), curve.constantOptions)];
|
|
306
|
+
case 4:
|
|
307
|
+
gasLimit_1 = _a.apply(void 0, [_g.sent()]);
|
|
308
|
+
_c = (_b = txHashes).push;
|
|
309
|
+
return [4 /*yield*/, contract.approve(spender, curve.parseUnits("0"), __assign(__assign({}, curve.options), { gasLimit: gasLimit_1 }))];
|
|
310
|
+
case 5:
|
|
311
|
+
_c.apply(_b, [(_g.sent()).hash]);
|
|
312
|
+
_g.label = 6;
|
|
313
|
+
case 6:
|
|
314
|
+
_d = mulBy1_3;
|
|
315
|
+
return [4 /*yield*/, contract.approve.estimateGas(spender, _approveAmount, curve.constantOptions)];
|
|
316
|
+
case 7:
|
|
317
|
+
gasLimit = _d.apply(void 0, [_g.sent()]);
|
|
318
|
+
_f = (_e = txHashes).push;
|
|
319
|
+
return [4 /*yield*/, contract.approve(spender, _approveAmount, __assign(__assign({}, curve.options), { gasLimit: gasLimit }))];
|
|
320
|
+
case 8:
|
|
321
|
+
_f.apply(_e, [(_g.sent()).hash]);
|
|
322
|
+
_g.label = 9;
|
|
323
|
+
case 9:
|
|
324
|
+
i++;
|
|
325
|
+
return [3 /*break*/, 2];
|
|
326
|
+
case 10: return [2 /*return*/, txHashes];
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
});
|
|
159
330
|
};
|
|
160
331
|
// coins can be either addresses or symbols
|
|
161
|
-
export
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
332
|
+
export var ensureAllowanceEstimateGas = function (coins, amounts, spender, isMax) {
|
|
333
|
+
if (isMax === void 0) { isMax = true; }
|
|
334
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
335
|
+
var coinAddresses, decimals, _amounts, address, allowance, gas, i, contract, _approveAmount, _a, _b, _c, _d;
|
|
336
|
+
return __generator(this, function (_e) {
|
|
337
|
+
switch (_e.label) {
|
|
338
|
+
case 0:
|
|
339
|
+
coinAddresses = _getCoinAddresses(coins);
|
|
340
|
+
decimals = _getCoinDecimals(coinAddresses);
|
|
341
|
+
_amounts = amounts.map(function (a, i) { return parseUnits(a, decimals[i]); });
|
|
342
|
+
address = curve.signerAddress;
|
|
343
|
+
return [4 /*yield*/, _getAllowance(coinAddresses, address, spender)];
|
|
344
|
+
case 1:
|
|
345
|
+
allowance = _e.sent();
|
|
346
|
+
gas = 0;
|
|
347
|
+
i = 0;
|
|
348
|
+
_e.label = 2;
|
|
349
|
+
case 2:
|
|
350
|
+
if (!(i < allowance.length)) return [3 /*break*/, 7];
|
|
351
|
+
if (!(allowance[i] < _amounts[i])) return [3 /*break*/, 6];
|
|
352
|
+
contract = curve.contracts[coinAddresses[i]].contract;
|
|
353
|
+
_approveAmount = isMax ? MAX_ALLOWANCE : _amounts[i];
|
|
354
|
+
if (!(allowance[i] > curve.parseUnits("0"))) return [3 /*break*/, 4];
|
|
355
|
+
_a = gas;
|
|
356
|
+
_b = Number;
|
|
357
|
+
return [4 /*yield*/, contract.approve.estimateGas(spender, curve.parseUnits("0"), curve.constantOptions)];
|
|
358
|
+
case 3:
|
|
359
|
+
gas = _a + _b.apply(void 0, [_e.sent()]);
|
|
360
|
+
_e.label = 4;
|
|
361
|
+
case 4:
|
|
362
|
+
_c = gas;
|
|
363
|
+
_d = Number;
|
|
364
|
+
return [4 /*yield*/, contract.approve.estimateGas(spender, _approveAmount, curve.constantOptions)];
|
|
365
|
+
case 5:
|
|
366
|
+
gas = _c + _d.apply(void 0, [_e.sent()]);
|
|
367
|
+
_e.label = 6;
|
|
368
|
+
case 6:
|
|
369
|
+
i++;
|
|
370
|
+
return [3 /*break*/, 2];
|
|
371
|
+
case 7: return [2 /*return*/, gas];
|
|
174
372
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
}
|
|
178
|
-
return gas;
|
|
373
|
+
});
|
|
374
|
+
});
|
|
179
375
|
};
|
|
180
376
|
// coins can be either addresses or symbols
|
|
181
|
-
export
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
377
|
+
export var ensureAllowance = function (coins, amounts, spender, isMax) {
|
|
378
|
+
if (isMax === void 0) { isMax = true; }
|
|
379
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
380
|
+
var coinAddresses, decimals, _amounts;
|
|
381
|
+
return __generator(this, function (_a) {
|
|
382
|
+
switch (_a.label) {
|
|
383
|
+
case 0:
|
|
384
|
+
coinAddresses = _getCoinAddresses(coins);
|
|
385
|
+
decimals = _getCoinDecimals(coinAddresses);
|
|
386
|
+
_amounts = amounts.map(function (a, i) { return parseUnits(a, decimals[i]); });
|
|
387
|
+
return [4 /*yield*/, _ensureAllowance(coinAddresses, _amounts, spender, isMax)];
|
|
388
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
});
|
|
190
392
|
};
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
393
|
+
export var getPoolNameBySwapAddress = function (swapAddress) {
|
|
394
|
+
var poolsData = __assign(__assign(__assign({}, curve.constants.POOLS_DATA), curve.constants.FACTORY_POOLS_DATA), curve.constants.CRYPTO_FACTORY_POOLS_DATA);
|
|
395
|
+
return Object.entries(poolsData).filter(function (_a) {
|
|
396
|
+
var _ = _a[0], poolData = _a[1];
|
|
397
|
+
return poolData.swap_address.toLowerCase() === swapAddress.toLowerCase();
|
|
398
|
+
})[0][0];
|
|
399
|
+
};
|
|
400
|
+
var _getTokenAddressBySwapAddress = function (swapAddress) {
|
|
401
|
+
var poolsData = __assign(__assign(__assign({}, curve.constants.POOLS_DATA), curve.constants.FACTORY_POOLS_DATA), curve.constants.CRYPTO_FACTORY_POOLS_DATA);
|
|
402
|
+
var res = Object.entries(poolsData).filter(function (_a) {
|
|
403
|
+
var _ = _a[0], poolData = _a[1];
|
|
404
|
+
return poolData.swap_address.toLowerCase() === swapAddress.toLowerCase();
|
|
405
|
+
});
|
|
194
406
|
if (res.length === 0)
|
|
195
407
|
return "";
|
|
196
408
|
return res[0][1].token_address;
|
|
197
409
|
};
|
|
198
|
-
export
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
const allTypesExtendedPoolData = await Promise.all(promises);
|
|
234
|
-
const apyDict = {};
|
|
235
|
-
for (const extendedPoolData of allTypesExtendedPoolData) {
|
|
236
|
-
for (const pool of extendedPoolData.poolData) {
|
|
237
|
-
if (pool.gaugeAddress) {
|
|
238
|
-
if (!pool.gaugeCrvApy) {
|
|
239
|
-
apyDict[pool.gaugeAddress.toLowerCase()] = [0, 0];
|
|
410
|
+
export var _getUsdPricesFromApi = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
411
|
+
var network, promises, allTypesExtendedPoolData, priceDict, _i, allTypesExtendedPoolData_1, extendedPoolData, _a, _b, pool, lpTokenAddress, totalSupply, _c, _d, coin, _e, _f, coin;
|
|
412
|
+
var _g, _h;
|
|
413
|
+
return __generator(this, function (_j) {
|
|
414
|
+
switch (_j.label) {
|
|
415
|
+
case 0:
|
|
416
|
+
network = curve.constants.NETWORK_NAME;
|
|
417
|
+
promises = [
|
|
418
|
+
_getPoolsFromApi(network, "main"),
|
|
419
|
+
_getPoolsFromApi(network, "crypto"),
|
|
420
|
+
_getPoolsFromApi(network, "factory"),
|
|
421
|
+
_getPoolsFromApi(network, "factory-crypto"),
|
|
422
|
+
];
|
|
423
|
+
return [4 /*yield*/, Promise.all(promises)];
|
|
424
|
+
case 1:
|
|
425
|
+
allTypesExtendedPoolData = _j.sent();
|
|
426
|
+
priceDict = {};
|
|
427
|
+
for (_i = 0, allTypesExtendedPoolData_1 = allTypesExtendedPoolData; _i < allTypesExtendedPoolData_1.length; _i++) {
|
|
428
|
+
extendedPoolData = allTypesExtendedPoolData_1[_i];
|
|
429
|
+
for (_a = 0, _b = extendedPoolData.poolData; _a < _b.length; _a++) {
|
|
430
|
+
pool = _b[_a];
|
|
431
|
+
lpTokenAddress = (_g = pool.lpTokenAddress) !== null && _g !== void 0 ? _g : pool.address;
|
|
432
|
+
totalSupply = pool.totalSupply / (Math.pow(10, 18));
|
|
433
|
+
priceDict[lpTokenAddress.toLowerCase()] = pool.usdTotal && totalSupply ? pool.usdTotal / totalSupply : 0;
|
|
434
|
+
for (_c = 0, _d = pool.coins; _c < _d.length; _c++) {
|
|
435
|
+
coin = _d[_c];
|
|
436
|
+
if (typeof coin.usdPrice === "number")
|
|
437
|
+
priceDict[coin.address.toLowerCase()] = coin.usdPrice;
|
|
438
|
+
}
|
|
439
|
+
for (_e = 0, _f = (_h = pool.gaugeRewards) !== null && _h !== void 0 ? _h : []; _e < _f.length; _e++) {
|
|
440
|
+
coin = _f[_e];
|
|
441
|
+
if (typeof coin.tokenPrice === "number")
|
|
442
|
+
priceDict[coin.tokenAddress.toLowerCase()] = coin.tokenPrice;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
240
445
|
}
|
|
241
|
-
|
|
242
|
-
|
|
446
|
+
return [2 /*return*/, priceDict];
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
}); };
|
|
450
|
+
export var _getCrvApyFromApi = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
451
|
+
var network, promises, allTypesExtendedPoolData, apyDict, _i, allTypesExtendedPoolData_2, extendedPoolData, _a, _b, pool;
|
|
452
|
+
var _c, _d;
|
|
453
|
+
return __generator(this, function (_e) {
|
|
454
|
+
switch (_e.label) {
|
|
455
|
+
case 0:
|
|
456
|
+
network = curve.constants.NETWORK_NAME;
|
|
457
|
+
promises = [
|
|
458
|
+
_getPoolsFromApi(network, "main"),
|
|
459
|
+
_getPoolsFromApi(network, "crypto"),
|
|
460
|
+
_getPoolsFromApi(network, "factory"),
|
|
461
|
+
_getPoolsFromApi(network, "factory-crypto"),
|
|
462
|
+
];
|
|
463
|
+
return [4 /*yield*/, Promise.all(promises)];
|
|
464
|
+
case 1:
|
|
465
|
+
allTypesExtendedPoolData = _e.sent();
|
|
466
|
+
apyDict = {};
|
|
467
|
+
for (_i = 0, allTypesExtendedPoolData_2 = allTypesExtendedPoolData; _i < allTypesExtendedPoolData_2.length; _i++) {
|
|
468
|
+
extendedPoolData = allTypesExtendedPoolData_2[_i];
|
|
469
|
+
for (_a = 0, _b = extendedPoolData.poolData; _a < _b.length; _a++) {
|
|
470
|
+
pool = _b[_a];
|
|
471
|
+
if (pool.gaugeAddress) {
|
|
472
|
+
if (!pool.gaugeCrvApy) {
|
|
473
|
+
apyDict[pool.gaugeAddress.toLowerCase()] = [0, 0];
|
|
474
|
+
}
|
|
475
|
+
else {
|
|
476
|
+
apyDict[pool.gaugeAddress.toLowerCase()] = [(_c = pool.gaugeCrvApy[0]) !== null && _c !== void 0 ? _c : 0, (_d = pool.gaugeCrvApy[1]) !== null && _d !== void 0 ? _d : 0];
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
243
480
|
}
|
|
244
|
-
|
|
481
|
+
return [2 /*return*/, apyDict];
|
|
245
482
|
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
483
|
+
});
|
|
484
|
+
}); };
|
|
485
|
+
export var _getRewardsFromApi = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
486
|
+
var network, promises, allTypesExtendedPoolData, rewardsDict, _i, allTypesExtendedPoolData_3, extendedPoolData, _a, _b, pool;
|
|
487
|
+
return __generator(this, function (_c) {
|
|
488
|
+
switch (_c.label) {
|
|
489
|
+
case 0:
|
|
490
|
+
network = curve.constants.NETWORK_NAME;
|
|
491
|
+
promises = [
|
|
492
|
+
_getPoolsFromApi(network, "main"),
|
|
493
|
+
_getPoolsFromApi(network, "crypto"),
|
|
494
|
+
_getPoolsFromApi(network, "factory"),
|
|
495
|
+
_getPoolsFromApi(network, "factory-crypto"),
|
|
496
|
+
];
|
|
497
|
+
return [4 /*yield*/, Promise.all(promises)];
|
|
498
|
+
case 1:
|
|
499
|
+
allTypesExtendedPoolData = _c.sent();
|
|
500
|
+
rewardsDict = {};
|
|
501
|
+
for (_i = 0, allTypesExtendedPoolData_3 = allTypesExtendedPoolData; _i < allTypesExtendedPoolData_3.length; _i++) {
|
|
502
|
+
extendedPoolData = allTypesExtendedPoolData_3[_i];
|
|
503
|
+
for (_a = 0, _b = extendedPoolData.poolData; _a < _b.length; _a++) {
|
|
504
|
+
pool = _b[_a];
|
|
505
|
+
if (pool.gaugeAddress) {
|
|
506
|
+
rewardsDict[pool.gaugeAddress.toLowerCase()] = pool.gaugeRewards;
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
return [2 /*return*/, rewardsDict];
|
|
264
511
|
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
};
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
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
|
-
|
|
512
|
+
});
|
|
513
|
+
}); };
|
|
514
|
+
var _usdRatesCache = {};
|
|
515
|
+
export var _getUsdRate = function (assetId) { return __awaiter(void 0, void 0, void 0, function () {
|
|
516
|
+
var pricesFromApi, chainName, nativeTokenName, url, response;
|
|
517
|
+
var _a, _b;
|
|
518
|
+
return __generator(this, function (_c) {
|
|
519
|
+
switch (_c.label) {
|
|
520
|
+
case 0:
|
|
521
|
+
if (curve.chainId === 1 && assetId.toLowerCase() === '0x8762db106b2c2a0bccb3a80d1ed41273552616e8')
|
|
522
|
+
return [2 /*return*/, 0]; // RSR
|
|
523
|
+
return [4 /*yield*/, _getUsdPricesFromApi()];
|
|
524
|
+
case 1:
|
|
525
|
+
pricesFromApi = _c.sent();
|
|
526
|
+
if (assetId.toLowerCase() in pricesFromApi)
|
|
527
|
+
return [2 /*return*/, pricesFromApi[assetId.toLowerCase()]];
|
|
528
|
+
if (assetId === 'USD' || (curve.chainId === 137 && (assetId.toLowerCase() === curve.constants.COINS.am3crv.toLowerCase())))
|
|
529
|
+
return [2 /*return*/, 1];
|
|
530
|
+
chainName = {
|
|
531
|
+
1: 'ethereum',
|
|
532
|
+
10: 'optimistic-ethereum',
|
|
533
|
+
100: 'xdai',
|
|
534
|
+
137: 'polygon-pos',
|
|
535
|
+
250: 'fantom',
|
|
536
|
+
1284: 'moonbeam',
|
|
537
|
+
2222: 'kava',
|
|
538
|
+
42220: 'celo',
|
|
539
|
+
43114: 'avalanche',
|
|
540
|
+
42161: 'arbitrum-one',
|
|
541
|
+
1313161554: 'aurora',
|
|
542
|
+
}[curve.chainId];
|
|
543
|
+
nativeTokenName = {
|
|
544
|
+
1: 'ethereum',
|
|
545
|
+
10: 'ethereum',
|
|
546
|
+
100: 'xdai',
|
|
547
|
+
137: 'matic-network',
|
|
548
|
+
250: 'fantom',
|
|
549
|
+
1284: 'moonbeam',
|
|
550
|
+
2222: 'kava',
|
|
551
|
+
42220: 'celo',
|
|
552
|
+
43114: 'avalanche-2',
|
|
553
|
+
42161: 'ethereum',
|
|
554
|
+
1313161554: 'ethereum',
|
|
555
|
+
}[curve.chainId];
|
|
556
|
+
if (chainName === undefined) {
|
|
557
|
+
throw Error('curve object is not initialized');
|
|
558
|
+
}
|
|
559
|
+
assetId = {
|
|
560
|
+
'CRV': 'curve-dao-token',
|
|
561
|
+
'EUR': 'stasis-eurs',
|
|
562
|
+
'BTC': 'bitcoin',
|
|
563
|
+
'ETH': 'ethereum',
|
|
564
|
+
'LINK': 'link',
|
|
565
|
+
}[assetId.toUpperCase()] || assetId;
|
|
566
|
+
assetId = isEth(assetId) ? nativeTokenName : assetId.toLowerCase();
|
|
567
|
+
// No EURT on Coingecko Polygon
|
|
568
|
+
if (curve.chainId === 137 && assetId.toLowerCase() === curve.constants.COINS.eurt) {
|
|
569
|
+
chainName = 'ethereum';
|
|
570
|
+
assetId = '0xC581b735A1688071A1746c968e0798D642EDE491'.toLowerCase(); // EURT Ethereum
|
|
571
|
+
}
|
|
572
|
+
// CRV
|
|
573
|
+
if (assetId.toLowerCase() === curve.constants.ALIASES.crv) {
|
|
574
|
+
assetId = 'curve-dao-token';
|
|
575
|
+
}
|
|
576
|
+
if (!((((_a = _usdRatesCache[assetId]) === null || _a === void 0 ? void 0 : _a.time) || 0) + 600000 < Date.now())) return [3 /*break*/, 3];
|
|
577
|
+
url = [nativeTokenName, 'ethereum', 'bitcoin', 'link', 'curve-dao-token', 'stasis-eurs'].includes(assetId.toLowerCase()) ?
|
|
578
|
+
"https://api.coingecko.com/api/v3/simple/price?ids=".concat(assetId, "&vs_currencies=usd") :
|
|
579
|
+
"https://api.coingecko.com/api/v3/simple/token_price/".concat(chainName, "?contract_addresses=").concat(assetId, "&vs_currencies=usd");
|
|
580
|
+
return [4 /*yield*/, axios.get(url)];
|
|
581
|
+
case 2:
|
|
582
|
+
response = _c.sent();
|
|
583
|
+
try {
|
|
584
|
+
_usdRatesCache[assetId] = { 'rate': (_b = response.data[assetId]['usd']) !== null && _b !== void 0 ? _b : 0, 'time': Date.now() };
|
|
585
|
+
}
|
|
586
|
+
catch (err) { // TODO pay attention!
|
|
587
|
+
_usdRatesCache[assetId] = { 'rate': 0, 'time': Date.now() };
|
|
588
|
+
}
|
|
589
|
+
_c.label = 3;
|
|
590
|
+
case 3: return [2 /*return*/, _usdRatesCache[assetId]['rate']];
|
|
330
591
|
}
|
|
331
|
-
|
|
332
|
-
|
|
592
|
+
});
|
|
593
|
+
}); };
|
|
594
|
+
export var getUsdRate = function (coin) { return __awaiter(void 0, void 0, void 0, function () {
|
|
595
|
+
var coinAddress;
|
|
596
|
+
return __generator(this, function (_a) {
|
|
597
|
+
switch (_a.label) {
|
|
598
|
+
case 0:
|
|
599
|
+
coinAddress = _getCoinAddressesNoCheck(coin)[0];
|
|
600
|
+
return [4 /*yield*/, _getUsdRate(coinAddress)];
|
|
601
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
333
602
|
}
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
const [coinAddress] = _getCoinAddressesNoCheck(coin);
|
|
339
|
-
return await _getUsdRate(coinAddress);
|
|
340
|
-
};
|
|
341
|
-
const _getNetworkName = (network = curve.chainId) => {
|
|
603
|
+
});
|
|
604
|
+
}); };
|
|
605
|
+
var _getNetworkName = function (network) {
|
|
606
|
+
if (network === void 0) { network = curve.chainId; }
|
|
342
607
|
if (typeof network === "number" && NETWORK_CONSTANTS[network]) {
|
|
343
608
|
return NETWORK_CONSTANTS[network].NAME;
|
|
344
609
|
}
|
|
345
|
-
else if (typeof network === "string" && Object.values(NETWORK_CONSTANTS).map((n)
|
|
610
|
+
else if (typeof network === "string" && Object.values(NETWORK_CONSTANTS).map(function (n) { return n.NAME; }).includes(network)) {
|
|
346
611
|
return network;
|
|
347
612
|
}
|
|
348
613
|
else {
|
|
349
|
-
throw Error(
|
|
614
|
+
throw Error("Wrong network name or id: ".concat(network));
|
|
350
615
|
}
|
|
351
616
|
};
|
|
352
|
-
|
|
617
|
+
var _getChainId = function (network) {
|
|
618
|
+
if (network === void 0) { network = curve.chainId; }
|
|
353
619
|
if (typeof network === "number" && NETWORK_CONSTANTS[network]) {
|
|
354
620
|
return network;
|
|
355
621
|
}
|
|
356
|
-
else if (typeof network === "string" && Object.values(NETWORK_CONSTANTS).map((n)
|
|
357
|
-
|
|
622
|
+
else if (typeof network === "string" && Object.values(NETWORK_CONSTANTS).map(function (n) { return n.NAME; }).includes(network)) {
|
|
623
|
+
var idx = Object.values(NETWORK_CONSTANTS).map(function (n) { return n.NAME; }).indexOf(network);
|
|
358
624
|
return Number(Object.keys(NETWORK_CONSTANTS)[idx]);
|
|
359
625
|
}
|
|
360
626
|
else {
|
|
361
|
-
throw Error(
|
|
627
|
+
throw Error("Wrong network name or id: ".concat(network));
|
|
362
628
|
}
|
|
363
629
|
};
|
|
364
|
-
export
|
|
365
|
-
network =
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
630
|
+
export var getTVL = function (network) {
|
|
631
|
+
if (network === void 0) { network = curve.chainId; }
|
|
632
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
633
|
+
var promises, allTypesExtendedPoolData;
|
|
634
|
+
return __generator(this, function (_a) {
|
|
635
|
+
switch (_a.label) {
|
|
636
|
+
case 0:
|
|
637
|
+
network = _getNetworkName(network);
|
|
638
|
+
promises = [
|
|
639
|
+
_getPoolsFromApi(network, "main"),
|
|
640
|
+
_getPoolsFromApi(network, "crypto"),
|
|
641
|
+
_getPoolsFromApi(network, "factory"),
|
|
642
|
+
_getPoolsFromApi(network, "factory-crypto"),
|
|
643
|
+
];
|
|
644
|
+
return [4 /*yield*/, Promise.all(promises)];
|
|
645
|
+
case 1:
|
|
646
|
+
allTypesExtendedPoolData = _a.sent();
|
|
647
|
+
return [2 /*return*/, allTypesExtendedPoolData.reduce(function (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)];
|
|
648
|
+
}
|
|
649
|
+
});
|
|
650
|
+
});
|
|
374
651
|
};
|
|
375
|
-
export
|
|
376
|
-
network =
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
652
|
+
export var getVolume = function (network) {
|
|
653
|
+
if (network === void 0) { network = curve.chainId; }
|
|
654
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
655
|
+
var chainId, _a, mainPoolsData, factoryPoolsData, volume, id, _i, factoryPoolsData_1, pool, lpToken, lpPrice, _b, _c, totalVolume, cryptoVolume, cryptoShare;
|
|
656
|
+
var _d;
|
|
657
|
+
return __generator(this, function (_e) {
|
|
658
|
+
switch (_e.label) {
|
|
659
|
+
case 0:
|
|
660
|
+
network = _getNetworkName(network);
|
|
661
|
+
if (!["moonbeam", "kava", "celo", "aurora"].includes(network)) return [3 /*break*/, 8];
|
|
662
|
+
chainId = _getChainId(network);
|
|
663
|
+
if (curve.chainId !== chainId)
|
|
664
|
+
throw Error("To get volume for Moonbeam, Kava, Celo or Aurora connect to the network first");
|
|
665
|
+
return [4 /*yield*/, Promise.all([
|
|
666
|
+
_getLegacyAPYsAndVolumes(network),
|
|
667
|
+
_getFactoryAPYsAndVolumes(network),
|
|
668
|
+
])];
|
|
669
|
+
case 1:
|
|
670
|
+
_a = _e.sent(), mainPoolsData = _a[0], factoryPoolsData = _a[1];
|
|
671
|
+
volume = 0;
|
|
672
|
+
for (id in mainPoolsData) {
|
|
673
|
+
volume += (_d = mainPoolsData[id].volume) !== null && _d !== void 0 ? _d : 0;
|
|
674
|
+
}
|
|
675
|
+
_i = 0, factoryPoolsData_1 = factoryPoolsData;
|
|
676
|
+
_e.label = 2;
|
|
677
|
+
case 2:
|
|
678
|
+
if (!(_i < factoryPoolsData_1.length)) return [3 /*break*/, 7];
|
|
679
|
+
pool = factoryPoolsData_1[_i];
|
|
680
|
+
lpToken = _getTokenAddressBySwapAddress(pool.poolAddress);
|
|
681
|
+
if (!lpToken) return [3 /*break*/, 4];
|
|
682
|
+
return [4 /*yield*/, _getUsdRate(lpToken)];
|
|
683
|
+
case 3:
|
|
684
|
+
_b = _e.sent();
|
|
685
|
+
return [3 /*break*/, 5];
|
|
686
|
+
case 4:
|
|
687
|
+
_b = 0;
|
|
688
|
+
_e.label = 5;
|
|
689
|
+
case 5:
|
|
690
|
+
lpPrice = _b;
|
|
691
|
+
volume += pool.volume * lpPrice;
|
|
692
|
+
_e.label = 6;
|
|
693
|
+
case 6:
|
|
694
|
+
_i++;
|
|
695
|
+
return [3 /*break*/, 2];
|
|
696
|
+
case 7: return [2 /*return*/, { totalVolume: volume, cryptoVolume: 0, cryptoShare: 0 }];
|
|
697
|
+
case 8: return [4 /*yield*/, _getSubgraphData(network)];
|
|
698
|
+
case 9:
|
|
699
|
+
_c = _e.sent(), totalVolume = _c.totalVolume, cryptoVolume = _c.cryptoVolume, cryptoShare = _c.cryptoShare;
|
|
700
|
+
return [2 /*return*/, { totalVolume: totalVolume, cryptoVolume: cryptoVolume, cryptoShare: cryptoShare }];
|
|
701
|
+
}
|
|
702
|
+
});
|
|
703
|
+
});
|
|
398
704
|
};
|
|
399
|
-
export
|
|
705
|
+
export var _setContracts = function (address, abi) {
|
|
400
706
|
curve.contracts[address] = {
|
|
401
707
|
contract: new Contract(address, abi, curve.signer || curve.provider),
|
|
402
708
|
multicallContract: new MulticallContract(address, abi),
|
|
@@ -405,51 +711,66 @@ export const _setContracts = (address, abi) => {
|
|
|
405
711
|
// Find k for which x * k = target_x or y * k = target_y
|
|
406
712
|
// k = max(target_x / x, target_y / y)
|
|
407
713
|
// small_x = x * k
|
|
408
|
-
export
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
return BigNumber.min(x_int_BN.times(k), BN(10
|
|
415
|
-
};
|
|
416
|
-
export
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
714
|
+
export var _get_small_x = function (_x, _y, x_decimals, y_decimals) {
|
|
715
|
+
var target_x = BN(Math.pow(10, (x_decimals > 5 ? x_decimals - 3 : x_decimals)));
|
|
716
|
+
var target_y = BN(Math.pow(10, (y_decimals > 5 ? y_decimals - 3 : y_decimals)));
|
|
717
|
+
var x_int_BN = toBN(_x, 0);
|
|
718
|
+
var y_int_BN = toBN(_y, 0);
|
|
719
|
+
var k = BigNumber.max(target_x.div(x_int_BN), target_y.div(y_int_BN));
|
|
720
|
+
return BigNumber.min(x_int_BN.times(k), BN(Math.pow(10, x_decimals)));
|
|
721
|
+
};
|
|
722
|
+
export var _get_price_impact = function (_x, _y, _small_x, _small_y, x_decimals, y_decimals) {
|
|
723
|
+
var x_BN = toBN(_x, x_decimals);
|
|
724
|
+
var y_BN = toBN(_y, y_decimals);
|
|
725
|
+
var small_x_BN = toBN(_small_x, x_decimals);
|
|
726
|
+
var small_y_BN = toBN(_small_y, y_decimals);
|
|
727
|
+
var rateBN = y_BN.div(x_BN);
|
|
728
|
+
var smallRateBN = small_y_BN.div(small_x_BN);
|
|
423
729
|
if (rateBN.gt(smallRateBN))
|
|
424
730
|
return BN(0);
|
|
425
731
|
return BN(1).minus(rateBN.div(smallRateBN)).times(100);
|
|
426
732
|
};
|
|
427
|
-
export
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
const coinAddresses = _getCoinAddressesNoCheck(coins);
|
|
432
|
-
console.log(coinAddresses);
|
|
433
|
-
const ethIndex = getEthIndex(coinAddresses);
|
|
434
|
-
if (ethIndex !== -1) {
|
|
435
|
-
coinAddresses.splice(ethIndex, 1);
|
|
733
|
+
export var getCoinsData = function () {
|
|
734
|
+
var coins = [];
|
|
735
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
736
|
+
coins[_i] = arguments[_i];
|
|
436
737
|
}
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
738
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
739
|
+
var coinAddresses, ethIndex, contractCalls, _a, coinAddresses_3, coinAddr, coinContract, _response, res;
|
|
740
|
+
return __generator(this, function (_b) {
|
|
741
|
+
switch (_b.label) {
|
|
742
|
+
case 0:
|
|
743
|
+
if (coins.length == 1 && Array.isArray(coins[0]))
|
|
744
|
+
coins = coins[0];
|
|
745
|
+
coins = coins;
|
|
746
|
+
coinAddresses = _getCoinAddressesNoCheck(coins);
|
|
747
|
+
console.log(coinAddresses);
|
|
748
|
+
ethIndex = getEthIndex(coinAddresses);
|
|
749
|
+
if (ethIndex !== -1) {
|
|
750
|
+
coinAddresses.splice(ethIndex, 1);
|
|
751
|
+
}
|
|
752
|
+
contractCalls = [];
|
|
753
|
+
for (_a = 0, coinAddresses_3 = coinAddresses; _a < coinAddresses_3.length; _a++) {
|
|
754
|
+
coinAddr = coinAddresses_3[_a];
|
|
755
|
+
coinContract = new MulticallContract(coinAddr, ERC20Abi);
|
|
756
|
+
contractCalls.push(coinContract.name(), coinContract.symbol(), coinContract.decimals());
|
|
757
|
+
}
|
|
758
|
+
return [4 /*yield*/, curve.multicallProvider.all(contractCalls)];
|
|
759
|
+
case 1:
|
|
760
|
+
_response = _b.sent();
|
|
761
|
+
if (ethIndex !== -1) {
|
|
762
|
+
_response.splice.apply(_response, __spreadArray([ethIndex * 2, 0], ['Ethereum', 'ETH', 18], false));
|
|
763
|
+
}
|
|
764
|
+
res = [];
|
|
765
|
+
coins.forEach(function (address, i) {
|
|
766
|
+
res.push({
|
|
767
|
+
name: _response.shift(),
|
|
768
|
+
symbol: _response.shift(),
|
|
769
|
+
decimals: Number(curve.formatUnits(_response.shift(), 0)),
|
|
770
|
+
});
|
|
771
|
+
});
|
|
772
|
+
return [2 /*return*/, res];
|
|
773
|
+
}
|
|
452
774
|
});
|
|
453
775
|
});
|
|
454
|
-
return res;
|
|
455
776
|
};
|