@curvefi/api 2.22.0 → 2.23.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/README.md +3 -1
- package/lib/curve.js +19 -16
- package/lib/index.d.ts +2 -1
- package/lib/index.js +1 -0
- package/lib/interfaces.d.ts +1 -1
- package/lib/router.d.ts +3 -1
- package/lib/router.js +113 -87
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1020,8 +1020,10 @@ import curve from "@curvefi/api";
|
|
|
1020
1020
|
// ]
|
|
1021
1021
|
const swapTx = await curve.router.swap('DAI', 'CRV', '1000');
|
|
1022
1022
|
// OR const swapTx = await curve.router.swap('0x6B175474E89094C44Da98b954EedeAC495271d0F', '0xD533a949740bb3306d119CC777fa900bA034cd52', '1000');
|
|
1023
|
-
console.log(swapTx);
|
|
1023
|
+
console.log(swapTx.hash);
|
|
1024
1024
|
// 0xc7ba1d60871c0295ac5471bb602c37ec0f00a71543b3a041308ebd91833f26ba
|
|
1025
|
+
const swappedAmount = await curve.router.getSwappedAmount(swapTx, 'CRV');
|
|
1026
|
+
// 1573.668171170839785062
|
|
1025
1027
|
|
|
1026
1028
|
await curve.getBalances(['DAI', 'CRV']);
|
|
1027
1029
|
// [ '8900.0', '100428.626463428100672494' ]
|
package/lib/curve.js
CHANGED
|
@@ -159,6 +159,12 @@ exports.NATIVE_TOKENS = {
|
|
|
159
159
|
address: "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
|
|
160
160
|
wrappedAddress: '0xc86c7C0eFbd6A49B35E8714C5f59D99De09A225b'.toLowerCase(),
|
|
161
161
|
},
|
|
162
|
+
42161: {
|
|
163
|
+
symbol: 'ETH',
|
|
164
|
+
wrappedSymbol: 'WETH',
|
|
165
|
+
address: "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
|
|
166
|
+
wrappedAddress: '0x82af49447d8a07e3bd95bd0d56f35241523fbab1'.toLowerCase(),
|
|
167
|
+
},
|
|
162
168
|
42220: {
|
|
163
169
|
symbol: 'CELO',
|
|
164
170
|
wrappedSymbol: 'WCELO',
|
|
@@ -171,12 +177,6 @@ exports.NATIVE_TOKENS = {
|
|
|
171
177
|
address: "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
|
|
172
178
|
wrappedAddress: '0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7'.toLowerCase(),
|
|
173
179
|
},
|
|
174
|
-
42161: {
|
|
175
|
-
symbol: 'ETH',
|
|
176
|
-
wrappedSymbol: 'WETH',
|
|
177
|
-
address: "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
|
|
178
|
-
wrappedAddress: '0x82af49447d8a07e3bd95bd0d56f35241523fbab1'.toLowerCase(),
|
|
179
|
-
},
|
|
180
180
|
1313161554: {
|
|
181
181
|
symbol: 'ETH',
|
|
182
182
|
wrappedSymbol: 'WETH',
|
|
@@ -255,6 +255,16 @@ exports.NETWORK_CONSTANTS = {
|
|
|
255
255
|
ycTokens: kava_1.ycTokensKava,
|
|
256
256
|
aTokens: kava_1.aTokensKava,
|
|
257
257
|
},
|
|
258
|
+
42161: {
|
|
259
|
+
NAME: 'arbitrum',
|
|
260
|
+
ALIASES: aliases_1.ALIASES_ARBITRUM,
|
|
261
|
+
POOLS_DATA: pools_1.POOLS_DATA_ARBITRUM,
|
|
262
|
+
COINS: arbitrum_1.COINS_ARBITRUM,
|
|
263
|
+
cTokens: arbitrum_1.cTokensArbitrum,
|
|
264
|
+
yTokens: arbitrum_1.yTokensArbitrum,
|
|
265
|
+
ycTokens: arbitrum_1.ycTokensArbitrum,
|
|
266
|
+
aTokens: arbitrum_1.aTokensArbitrum,
|
|
267
|
+
},
|
|
258
268
|
42220: {
|
|
259
269
|
NAME: 'celo',
|
|
260
270
|
ALIASES: aliases_1.ALIASES_CELO,
|
|
@@ -275,16 +285,6 @@ exports.NETWORK_CONSTANTS = {
|
|
|
275
285
|
ycTokens: avalanche_1.ycTokensAvalanche,
|
|
276
286
|
aTokens: avalanche_1.aTokensAvalanche,
|
|
277
287
|
},
|
|
278
|
-
42161: {
|
|
279
|
-
NAME: 'arbitrum',
|
|
280
|
-
ALIASES: aliases_1.ALIASES_ARBITRUM,
|
|
281
|
-
POOLS_DATA: pools_1.POOLS_DATA_ARBITRUM,
|
|
282
|
-
COINS: arbitrum_1.COINS_ARBITRUM,
|
|
283
|
-
cTokens: arbitrum_1.cTokensArbitrum,
|
|
284
|
-
yTokens: arbitrum_1.yTokensArbitrum,
|
|
285
|
-
ycTokens: arbitrum_1.ycTokensArbitrum,
|
|
286
|
-
aTokens: arbitrum_1.aTokensArbitrum,
|
|
287
|
-
},
|
|
288
288
|
1313161554: {
|
|
289
289
|
NAME: 'aurora',
|
|
290
290
|
ALIASES: aliases_1.ALIASES_AURORA,
|
|
@@ -407,6 +407,8 @@ var Curve = /** @class */ (function () {
|
|
|
407
407
|
this.constants.POOLS_DATA = exports.NETWORK_CONSTANTS[this.chainId].POOLS_DATA;
|
|
408
408
|
this.constants.COINS = exports.NETWORK_CONSTANTS[this.chainId].COINS;
|
|
409
409
|
this.constants.DECIMALS = (0, utils_1.extractDecimals)(this.constants.POOLS_DATA);
|
|
410
|
+
this.constants.DECIMALS[this.constants.NATIVE_TOKEN.address] = 18;
|
|
411
|
+
this.constants.DECIMALS[this.constants.NATIVE_TOKEN.wrappedAddress] = 18;
|
|
410
412
|
this.constants.GAUGES = (0, utils_1.extractGauges)(this.constants.POOLS_DATA);
|
|
411
413
|
_c = [
|
|
412
414
|
exports.NETWORK_CONSTANTS[this.chainId].cTokens,
|
|
@@ -521,6 +523,7 @@ var Curve = /** @class */ (function () {
|
|
|
521
523
|
};
|
|
522
524
|
}
|
|
523
525
|
}
|
|
526
|
+
this.setContract(this.constants.NATIVE_TOKEN.wrappedAddress, ERC20_json_1.default);
|
|
524
527
|
this.contracts[this.constants.ALIASES.crv] = {
|
|
525
528
|
contract: new ethers_1.Contract(this.constants.ALIASES.crv, ERC20_json_1.default, this.signer || this.provider),
|
|
526
529
|
multicallContract: new ethcall_1.Contract(this.constants.ALIASES.crv, ERC20_json_1.default),
|
package/lib/index.d.ts
CHANGED
|
@@ -122,7 +122,8 @@ declare const curve: {
|
|
|
122
122
|
priceImpact: (inputCoin: string, outputCoin: string, amount: string | number) => Promise<number>;
|
|
123
123
|
isApproved: (inputCoin: string, amount: string | number) => Promise<boolean>;
|
|
124
124
|
approve: (inputCoin: string, amount: string | number) => Promise<string[]>;
|
|
125
|
-
swap: (inputCoin: string, outputCoin: string, amount: string | number, slippage?: number) => Promise<
|
|
125
|
+
swap: (inputCoin: string, outputCoin: string, amount: string | number, slippage?: number) => Promise<ethers.ContractTransaction>;
|
|
126
|
+
getSwappedAmount: (tx: ethers.ContractTransaction, outputCoin: string) => Promise<string>;
|
|
126
127
|
estimateGas: {
|
|
127
128
|
approve: (inputCoin: string, amount: string | number) => Promise<number>;
|
|
128
129
|
swap: (inputCoin: string, outputCoin: string, amount: string | number) => Promise<number>;
|
package/lib/index.js
CHANGED
|
@@ -199,6 +199,7 @@ var curve = {
|
|
|
199
199
|
isApproved: router_1.swapIsApproved,
|
|
200
200
|
approve: router_1.swapApprove,
|
|
201
201
|
swap: router_1.swap,
|
|
202
|
+
getSwappedAmount: router_1.getSwappedAmount,
|
|
202
203
|
estimateGas: {
|
|
203
204
|
approve: router_1.swapApproveEstimateGas,
|
|
204
205
|
swap: router_1.swapEstimateGas,
|
package/lib/interfaces.d.ts
CHANGED
|
@@ -127,7 +127,7 @@ export interface IRouteStep {
|
|
|
127
127
|
outputCoinAddress: string;
|
|
128
128
|
i: number;
|
|
129
129
|
j: number;
|
|
130
|
-
swapType: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11;
|
|
130
|
+
swapType: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15;
|
|
131
131
|
swapAddress: string;
|
|
132
132
|
}
|
|
133
133
|
export interface IRoute_ {
|
package/lib/router.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ethers } from "ethers";
|
|
1
2
|
import { IRouteStep } from "./interfaces";
|
|
2
3
|
export declare const _findAllRoutesTheShorterTheBetter: (inputCoinAddress: string, outputCoinAddress: string) => Promise<IRouteStep[][]>;
|
|
3
4
|
export declare const _findAllRoutesTvl: (inputCoinAddress: string, outputCoinAddress: string) => Promise<IRouteStep[][]>;
|
|
@@ -12,4 +13,5 @@ export declare const swapIsApproved: (inputCoin: string, amount: number | string
|
|
|
12
13
|
export declare const swapApproveEstimateGas: (inputCoin: string, amount: number | string) => Promise<number>;
|
|
13
14
|
export declare const swapApprove: (inputCoin: string, amount: number | string) => Promise<string[]>;
|
|
14
15
|
export declare const swapEstimateGas: (inputCoin: string, outputCoin: string, amount: number | string) => Promise<number>;
|
|
15
|
-
export declare const swap: (inputCoin: string, outputCoin: string, amount: number | string, slippage?: number) => Promise<
|
|
16
|
+
export declare const swap: (inputCoin: string, outputCoin: string, amount: number | string, slippage?: number) => Promise<ethers.ContractTransaction>;
|
|
17
|
+
export declare const getSwappedAmount: (tx: ethers.ContractTransaction, outputCoin: string) => Promise<string>;
|
package/lib/router.js
CHANGED
|
@@ -59,7 +59,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
59
59
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
60
60
|
};
|
|
61
61
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
62
|
-
exports.swap = exports.swapEstimateGas = exports.swapApprove = exports.swapApproveEstimateGas = exports.swapIsApproved = exports.swapPriceImpact = exports.swapExpected = exports.getBestRouteAndOutput = exports._findAllRoutes = exports._findAllRoutesTvl = exports._findAllRoutesTheShorterTheBetter = void 0;
|
|
62
|
+
exports.getSwappedAmount = exports.swap = exports.swapEstimateGas = exports.swapApprove = exports.swapApproveEstimateGas = exports.swapIsApproved = exports.swapPriceImpact = exports.swapExpected = exports.getBestRouteAndOutput = exports._findAllRoutes = exports._findAllRoutesTvl = exports._findAllRoutesTheShorterTheBetter = void 0;
|
|
63
63
|
var axios_1 = __importDefault(require("axios"));
|
|
64
64
|
var memoizee_1 = __importDefault(require("memoizee"));
|
|
65
65
|
var ethers_1 = require("ethers");
|
|
@@ -82,7 +82,7 @@ var _findAllRoutesTheShorterTheBetter = function (inputCoinAddress, outputCoinAd
|
|
|
82
82
|
basePoolsSet = new Set();
|
|
83
83
|
for (_i = 0, ALL_POOLS_1 = ALL_POOLS; _i < ALL_POOLS_1.length; _i++) {
|
|
84
84
|
pool = ALL_POOLS_1[_i];
|
|
85
|
-
if (pool[1].wrapped_coin_addresses.length <
|
|
85
|
+
if (pool[1].wrapped_coin_addresses.length < 6)
|
|
86
86
|
basePoolsSet.add(pool[0]);
|
|
87
87
|
}
|
|
88
88
|
basePoolIds = Array.from(basePoolsSet);
|
|
@@ -317,54 +317,57 @@ var sortByTvl = function (a, b) { return b.minTvl - a.minTvl || b.totalTvl - a.t
|
|
|
317
317
|
var sortByLength = function (a, b) { return a.steps.length - b.steps.length || b.minTvl - a.minTvl || b.totalTvl - a.totalTvl; };
|
|
318
318
|
// Inspired by Dijkstra's algorithm
|
|
319
319
|
var _findAllRoutesTvl = function (inputCoinAddress, outputCoinAddress) { return __awaiter(void 0, void 0, void 0, function () {
|
|
320
|
-
var ALL_POOLS, amplificationCoefficientDict,
|
|
321
|
-
var
|
|
322
|
-
var _d, _e, _f, _h, _j, _k, _l, _m, _o, _p, _q, _r;
|
|
323
|
-
return __generator(this, function (
|
|
324
|
-
switch (
|
|
320
|
+
var ALL_POOLS, amplificationCoefficientDict, curCoins, nextCoins, routesByTvl, routesByLength, step, _loop_1, _i, curCoins_2, inCoin, routes;
|
|
321
|
+
var _a, _b;
|
|
322
|
+
var _c, _d, _e, _f, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
|
|
323
|
+
return __generator(this, function (_t) {
|
|
324
|
+
switch (_t.label) {
|
|
325
325
|
case 0:
|
|
326
326
|
inputCoinAddress = inputCoinAddress.toLowerCase();
|
|
327
327
|
outputCoinAddress = outputCoinAddress.toLowerCase();
|
|
328
328
|
ALL_POOLS = Object.entries(__assign(__assign(__assign({}, curve_1.curve.constants.POOLS_DATA), curve_1.curve.constants.FACTORY_POOLS_DATA), curve_1.curve.constants.CRYPTO_FACTORY_POOLS_DATA));
|
|
329
329
|
return [4 /*yield*/, (0, utils_2._getAmplificationCoefficientsFromApi)()];
|
|
330
330
|
case 1:
|
|
331
|
-
amplificationCoefficientDict =
|
|
332
|
-
basePoolsSet = new Set();
|
|
333
|
-
for (_i = 0, ALL_POOLS_3 = ALL_POOLS; _i < ALL_POOLS_3.length; _i++) {
|
|
334
|
-
pool = ALL_POOLS_3[_i];
|
|
335
|
-
if (pool[1].wrapped_coin_addresses.length < 4)
|
|
336
|
-
basePoolsSet.add(pool[0]);
|
|
337
|
-
}
|
|
338
|
-
basePoolIds = Array.from(basePoolsSet);
|
|
331
|
+
amplificationCoefficientDict = _t.sent();
|
|
339
332
|
curCoins = [inputCoinAddress];
|
|
340
333
|
nextCoins = new Set();
|
|
341
|
-
routesByTvl = (
|
|
334
|
+
routesByTvl = (_a = {},
|
|
335
|
+
_a[inputCoinAddress] = [{ steps: [], minTvl: Infinity, totalTvl: 0 }],
|
|
336
|
+
_a);
|
|
337
|
+
routesByLength = (_b = {},
|
|
342
338
|
_b[inputCoinAddress] = [{ steps: [], minTvl: Infinity, totalTvl: 0 }],
|
|
343
339
|
_b);
|
|
344
|
-
routesByLength = (_c = {},
|
|
345
|
-
_c[inputCoinAddress] = [{ steps: [], minTvl: Infinity, totalTvl: 0 }],
|
|
346
|
-
_c);
|
|
347
340
|
step = 0;
|
|
348
|
-
|
|
341
|
+
_t.label = 2;
|
|
349
342
|
case 2:
|
|
350
343
|
if (!(step < 4)) return [3 /*break*/, 8];
|
|
351
344
|
_loop_1 = function (inCoin) {
|
|
352
|
-
var
|
|
353
|
-
return __generator(this, function (
|
|
354
|
-
switch (
|
|
345
|
+
var outCoin_1, newRoutesByTvl, newRoutesByLength, _loop_2, _u, ALL_POOLS_3, _v, poolId, poolData;
|
|
346
|
+
return __generator(this, function (_w) {
|
|
347
|
+
switch (_w.label) {
|
|
355
348
|
case 0:
|
|
349
|
+
if ([curve_1.curve.constants.NATIVE_TOKEN.address, curve_1.curve.constants.NATIVE_TOKEN.wrappedAddress].includes(inCoin)) {
|
|
350
|
+
outCoin_1 = inCoin === curve_1.curve.constants.NATIVE_TOKEN.address ? curve_1.curve.constants.NATIVE_TOKEN.wrappedAddress : curve_1.curve.constants.NATIVE_TOKEN.address;
|
|
351
|
+
newRoutesByTvl = routesByTvl[inCoin].map(function (route) { return getNewRoute(route, "wrapper", curve_1.curve.constants.NATIVE_TOKEN.wrappedAddress, inCoin, outCoin_1, 0, 0, 15, ethers_1.ethers.constants.AddressZero, Infinity); });
|
|
352
|
+
newRoutesByLength = routesByLength[inCoin].map(function (route) { return getNewRoute(route, "wrapper", curve_1.curve.constants.NATIVE_TOKEN.wrappedAddress, inCoin, outCoin_1, 0, 0, 15, ethers_1.ethers.constants.AddressZero, Infinity); });
|
|
353
|
+
routesByTvl[outCoin_1] = __spreadArray(__spreadArray([], ((_c = routesByTvl[outCoin_1]) !== null && _c !== void 0 ? _c : []), true), newRoutesByTvl, true);
|
|
354
|
+
routesByTvl[outCoin_1] = filterRoutes(routesByTvl[outCoin_1], inputCoinAddress, sortByTvl);
|
|
355
|
+
routesByLength[outCoin_1] = __spreadArray(__spreadArray([], ((_d = routesByLength[outCoin_1]) !== null && _d !== void 0 ? _d : []), true), newRoutesByLength, true);
|
|
356
|
+
routesByLength[outCoin_1] = filterRoutes(routesByLength[outCoin_1], inputCoinAddress, sortByLength);
|
|
357
|
+
nextCoins.add(outCoin_1);
|
|
358
|
+
}
|
|
356
359
|
_loop_2 = function (poolId, poolData) {
|
|
357
|
-
var wrapped_coin_addresses, underlying_coin_addresses, base_pool, meta_coin_addresses, token_address, is_lending, tvlMultiplier, inCoinIndexes, tvl,
|
|
358
|
-
return __generator(this, function (
|
|
359
|
-
switch (
|
|
360
|
+
var wrapped_coin_addresses, underlying_coin_addresses, base_pool, meta_coin_addresses, token_address, is_lending, tvlMultiplier, inCoinIndexes, tvl, _x, poolAddress, coin_addresses, _loop_3, j, inCoinIndex, swapType_1, newRoutesByTvl, newRoutesByLength, _loop_4, j, _loop_5, j;
|
|
361
|
+
return __generator(this, function (_y) {
|
|
362
|
+
switch (_y.label) {
|
|
360
363
|
case 0:
|
|
361
364
|
wrapped_coin_addresses = poolData.wrapped_coin_addresses.map(function (a) { return a.toLowerCase(); });
|
|
362
365
|
underlying_coin_addresses = poolData.underlying_coin_addresses.map(function (a) { return a.toLowerCase(); });
|
|
363
366
|
base_pool = poolData.is_meta ? curve_1.curve.constants.POOLS_DATA[poolData.base_pool] : null;
|
|
364
367
|
meta_coin_addresses = base_pool ? base_pool.underlying_coin_addresses.map(function (a) { return a.toLowerCase(); }) : [];
|
|
365
368
|
token_address = poolData.token_address.toLowerCase();
|
|
366
|
-
is_lending = (
|
|
367
|
-
tvlMultiplier = poolData.is_crypto ? 1 : ((
|
|
369
|
+
is_lending = (_e = poolData.is_lending) !== null && _e !== void 0 ? _e : false;
|
|
370
|
+
tvlMultiplier = poolData.is_crypto ? 1 : ((_f = amplificationCoefficientDict[poolData.swap_address]) !== null && _f !== void 0 ? _f : 1);
|
|
368
371
|
inCoinIndexes = {
|
|
369
372
|
wrapped_coin: wrapped_coin_addresses.indexOf(inCoin),
|
|
370
373
|
underlying_coin: underlying_coin_addresses.indexOf(inCoin),
|
|
@@ -373,46 +376,51 @@ var _findAllRoutesTvl = function (inputCoinAddress, outputCoinAddress) { return
|
|
|
373
376
|
// Skip pools which don't contain inCoin
|
|
374
377
|
if (inCoinIndexes.wrapped_coin === -1 && inCoinIndexes.underlying_coin === -1 && inCoinIndexes.meta_coin === -1 && inCoin !== token_address)
|
|
375
378
|
return [2 /*return*/, "continue"];
|
|
376
|
-
|
|
379
|
+
_x = Number;
|
|
377
380
|
return [4 /*yield*/, ((0, pools_1.getPool)(poolId)).stats.totalLiquidity()];
|
|
378
381
|
case 1:
|
|
379
|
-
tvl =
|
|
382
|
+
tvl = _x.apply(void 0, [_y.sent()]) * tvlMultiplier;
|
|
380
383
|
// Skip empty pools
|
|
381
384
|
if (tvl === 0)
|
|
382
385
|
return [2 /*return*/, "continue"];
|
|
383
|
-
|
|
384
|
-
|
|
386
|
+
poolAddress = poolData.is_fake ? poolData.deposit_address : poolData.swap_address;
|
|
387
|
+
coin_addresses = (is_lending || poolData.is_fake) ? underlying_coin_addresses : wrapped_coin_addresses;
|
|
388
|
+
// LP -> wrapped coin (underlying for lending or fake pool) "swaps" (actually remove_liquidity_one_coin)
|
|
389
|
+
if (coin_addresses.length < 6 && inCoin === token_address) {
|
|
385
390
|
_loop_3 = function (j) {
|
|
386
391
|
// Looking for outputCoinAddress only on the final step
|
|
387
|
-
if (step === 3 &&
|
|
392
|
+
if (step === 3 && coin_addresses[j] !== outputCoinAddress)
|
|
388
393
|
return "continue";
|
|
389
394
|
// Exclude such cases as cvxeth -> tricrypto2 -> tusd -> susd or cvxeth -> tricrypto2 -> susd -> susd
|
|
390
|
-
var outputCoinIdx =
|
|
395
|
+
var outputCoinIdx = coin_addresses.indexOf(outputCoinAddress);
|
|
391
396
|
if (outputCoinIdx >= 0 && j !== outputCoinIdx)
|
|
392
397
|
return "continue";
|
|
393
|
-
var swapType =
|
|
394
|
-
var newRoutesByTvl = routesByTvl[inCoin].map(function (route) { return getNewRoute(route, poolId,
|
|
395
|
-
var newRoutesByLength = routesByLength[inCoin].map(function (route) { return getNewRoute(route, poolId,
|
|
396
|
-
routesByTvl[
|
|
397
|
-
routesByTvl[
|
|
398
|
-
routesByLength[
|
|
399
|
-
routesByLength[
|
|
400
|
-
nextCoins.add(
|
|
398
|
+
var swapType = poolData.is_crypto ? 14 : is_lending ? 13 : 12;
|
|
399
|
+
var newRoutesByTvl = routesByTvl[inCoin].map(function (route) { return getNewRoute(route, poolId, poolAddress, inCoin, coin_addresses[j], 0, j, swapType, ethers_1.ethers.constants.AddressZero, tvl); });
|
|
400
|
+
var newRoutesByLength = routesByLength[inCoin].map(function (route) { return getNewRoute(route, poolId, poolAddress, inCoin, coin_addresses[j], 0, j, swapType, ethers_1.ethers.constants.AddressZero, tvl); });
|
|
401
|
+
routesByTvl[coin_addresses[j]] = __spreadArray(__spreadArray([], ((_h = routesByTvl[coin_addresses[j]]) !== null && _h !== void 0 ? _h : []), true), newRoutesByTvl, true);
|
|
402
|
+
routesByTvl[coin_addresses[j]] = filterRoutes(routesByTvl[coin_addresses[j]], inputCoinAddress, sortByTvl);
|
|
403
|
+
routesByLength[coin_addresses[j]] = __spreadArray(__spreadArray([], ((_j = routesByLength[coin_addresses[j]]) !== null && _j !== void 0 ? _j : []), true), newRoutesByLength, true);
|
|
404
|
+
routesByLength[coin_addresses[j]] = filterRoutes(routesByLength[coin_addresses[j]], inputCoinAddress, sortByLength);
|
|
405
|
+
nextCoins.add(coin_addresses[j]);
|
|
401
406
|
};
|
|
402
|
-
for (j = 0; j <
|
|
407
|
+
for (j = 0; j < coin_addresses.length; j++) {
|
|
403
408
|
_loop_3(j);
|
|
404
409
|
}
|
|
405
410
|
}
|
|
406
|
-
|
|
407
|
-
if (
|
|
411
|
+
inCoinIndex = (is_lending || poolData.is_fake) ? inCoinIndexes.underlying_coin : inCoinIndexes.wrapped_coin;
|
|
412
|
+
if (coin_addresses.length < 6 && inCoinIndex >= 0) {
|
|
408
413
|
// Looking for outputCoinAddress only on the final step
|
|
409
414
|
if (!(step === 3 && token_address !== outputCoinAddress)) {
|
|
410
|
-
swapType_1 = is_lending ? 9
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
415
|
+
swapType_1 = is_lending ? 9
|
|
416
|
+
: coin_addresses.length === 2 ? 7
|
|
417
|
+
: coin_addresses.length === 3 ? 8
|
|
418
|
+
: coin_addresses.length === 4 ? 10 : 11;
|
|
419
|
+
newRoutesByTvl = routesByTvl[inCoin].map(function (route) { return getNewRoute(route, poolId, poolAddress, inCoin, token_address, coin_addresses.indexOf(inCoin), 0, swapType_1, ethers_1.ethers.constants.AddressZero, tvl); });
|
|
420
|
+
newRoutesByLength = routesByLength[inCoin].map(function (route) { return getNewRoute(route, poolId, poolAddress, inCoin, token_address, coin_addresses.indexOf(inCoin), 0, swapType_1, ethers_1.ethers.constants.AddressZero, tvl); });
|
|
421
|
+
routesByTvl[token_address] = __spreadArray(__spreadArray([], ((_k = routesByTvl[token_address]) !== null && _k !== void 0 ? _k : []), true), newRoutesByTvl, true);
|
|
414
422
|
routesByTvl[token_address] = filterRoutes(routesByTvl[token_address], inputCoinAddress, sortByTvl);
|
|
415
|
-
routesByLength[token_address] = __spreadArray(__spreadArray([], ((
|
|
423
|
+
routesByLength[token_address] = __spreadArray(__spreadArray([], ((_l = routesByLength[token_address]) !== null && _l !== void 0 ? _l : []), true), newRoutesByLength, true);
|
|
416
424
|
routesByLength[token_address] = filterRoutes(routesByLength[token_address], inputCoinAddress, sortByLength);
|
|
417
425
|
nextCoins.add(token_address);
|
|
418
426
|
}
|
|
@@ -435,9 +443,9 @@ var _findAllRoutesTvl = function (inputCoinAddress, outputCoinAddress) { return
|
|
|
435
443
|
var swapType = poolData.is_crypto ? 3 : 1;
|
|
436
444
|
var newRoutesByTvl = routesByTvl[inCoin].map(function (route) { return getNewRoute(route, poolId, poolData.swap_address, inCoin, wrapped_coin_addresses[j], inCoinIndexes.wrapped_coin, j, swapType, ethers_1.ethers.constants.AddressZero, tvl); });
|
|
437
445
|
var newRoutesByLength = routesByLength[inCoin].map(function (route) { return getNewRoute(route, poolId, poolData.swap_address, inCoin, wrapped_coin_addresses[j], inCoinIndexes.wrapped_coin, j, swapType, ethers_1.ethers.constants.AddressZero, tvl); });
|
|
438
|
-
routesByTvl[wrapped_coin_addresses[j]] = __spreadArray(__spreadArray([], ((
|
|
446
|
+
routesByTvl[wrapped_coin_addresses[j]] = __spreadArray(__spreadArray([], ((_m = routesByTvl[wrapped_coin_addresses[j]]) !== null && _m !== void 0 ? _m : []), true), newRoutesByTvl, true);
|
|
439
447
|
routesByTvl[wrapped_coin_addresses[j]] = filterRoutes(routesByTvl[wrapped_coin_addresses[j]], inputCoinAddress, sortByTvl);
|
|
440
|
-
routesByLength[wrapped_coin_addresses[j]] = __spreadArray(__spreadArray([], ((
|
|
448
|
+
routesByLength[wrapped_coin_addresses[j]] = __spreadArray(__spreadArray([], ((_o = routesByLength[wrapped_coin_addresses[j]]) !== null && _o !== void 0 ? _o : []), true), newRoutesByLength, true);
|
|
441
449
|
routesByLength[wrapped_coin_addresses[j]] = filterRoutes(routesByLength[wrapped_coin_addresses[j]], inputCoinAddress, sortByLength);
|
|
442
450
|
nextCoins.add(wrapped_coin_addresses[j]);
|
|
443
451
|
};
|
|
@@ -445,13 +453,14 @@ var _findAllRoutesTvl = function (inputCoinAddress, outputCoinAddress) { return
|
|
|
445
453
|
_loop_4(j);
|
|
446
454
|
}
|
|
447
455
|
}
|
|
456
|
+
// Only for underlying swaps
|
|
448
457
|
poolAddress = (poolData.is_crypto && poolData.is_meta) || ((base_pool === null || base_pool === void 0 ? void 0 : base_pool.is_lending) && poolData.is_factory) ?
|
|
449
458
|
poolData.deposit_address : poolData.swap_address;
|
|
450
459
|
if (!(!poolData.is_plain && inCoinIndexes.underlying_coin >= 0)) return [3 /*break*/, 5];
|
|
451
460
|
_loop_5 = function (j) {
|
|
452
|
-
var outputCoinIdx, tvl_1,
|
|
453
|
-
return __generator(this, function (
|
|
454
|
-
switch (
|
|
461
|
+
var outputCoinIdx, tvl_1, _z, hasEth, swapType, newRoutesByTvl, newRoutesByLength;
|
|
462
|
+
return __generator(this, function (_0) {
|
|
463
|
+
switch (_0.label) {
|
|
455
464
|
case 0:
|
|
456
465
|
if (j === inCoinIndexes.underlying_coin)
|
|
457
466
|
return [2 /*return*/, "continue"];
|
|
@@ -464,10 +473,10 @@ var _findAllRoutesTvl = function (inputCoinAddress, outputCoinAddress) { return
|
|
|
464
473
|
outputCoinIdx = underlying_coin_addresses.indexOf(outputCoinAddress);
|
|
465
474
|
if (outputCoinIdx >= 0 && j !== outputCoinIdx)
|
|
466
475
|
return [2 /*return*/, "continue"];
|
|
467
|
-
|
|
476
|
+
_z = Number;
|
|
468
477
|
return [4 /*yield*/, ((0, pools_1.getPool)(poolId)).stats.totalLiquidity()];
|
|
469
478
|
case 1:
|
|
470
|
-
tvl_1 =
|
|
479
|
+
tvl_1 = _z.apply(void 0, [_0.sent()]);
|
|
471
480
|
if (tvl_1 === 0)
|
|
472
481
|
return [2 /*return*/, "continue"];
|
|
473
482
|
hasEth = (inCoin === curve_1.curve.constants.NATIVE_TOKEN.address || underlying_coin_addresses[j] === curve_1.curve.constants.NATIVE_TOKEN.address);
|
|
@@ -478,9 +487,9 @@ var _findAllRoutesTvl = function (inputCoinAddress, outputCoinAddress) { return
|
|
|
478
487
|
: 2;
|
|
479
488
|
newRoutesByTvl = routesByTvl[inCoin].map(function (route) { return getNewRoute(route, poolId, poolAddress, inCoin, underlying_coin_addresses[j], inCoinIndexes.underlying_coin, j, swapType, (swapType === 5 || swapType === 6) ? poolData.swap_address : ethers_1.ethers.constants.AddressZero, tvl_1); });
|
|
480
489
|
newRoutesByLength = routesByLength[inCoin].map(function (route) { return getNewRoute(route, poolId, poolAddress, inCoin, underlying_coin_addresses[j], inCoinIndexes.underlying_coin, j, swapType, (swapType === 5 || swapType === 6) ? poolData.swap_address : ethers_1.ethers.constants.AddressZero, tvl_1); });
|
|
481
|
-
routesByTvl[underlying_coin_addresses[j]] = __spreadArray(__spreadArray([], ((
|
|
490
|
+
routesByTvl[underlying_coin_addresses[j]] = __spreadArray(__spreadArray([], ((_p = routesByTvl[underlying_coin_addresses[j]]) !== null && _p !== void 0 ? _p : []), true), newRoutesByTvl, true);
|
|
482
491
|
routesByTvl[underlying_coin_addresses[j]] = filterRoutes(routesByTvl[underlying_coin_addresses[j]], inputCoinAddress, sortByTvl);
|
|
483
|
-
routesByLength[underlying_coin_addresses[j]] = __spreadArray(__spreadArray([], ((
|
|
492
|
+
routesByLength[underlying_coin_addresses[j]] = __spreadArray(__spreadArray([], ((_q = routesByLength[underlying_coin_addresses[j]]) !== null && _q !== void 0 ? _q : []), true), newRoutesByLength, true);
|
|
484
493
|
routesByLength[underlying_coin_addresses[j]] = filterRoutes(routesByLength[underlying_coin_addresses[j]], inputCoinAddress, sortByLength);
|
|
485
494
|
nextCoins.add(underlying_coin_addresses[j]);
|
|
486
495
|
return [2 /*return*/];
|
|
@@ -488,13 +497,13 @@ var _findAllRoutesTvl = function (inputCoinAddress, outputCoinAddress) { return
|
|
|
488
497
|
});
|
|
489
498
|
};
|
|
490
499
|
j = 0;
|
|
491
|
-
|
|
500
|
+
_y.label = 2;
|
|
492
501
|
case 2:
|
|
493
502
|
if (!(j < underlying_coin_addresses.length)) return [3 /*break*/, 5];
|
|
494
503
|
return [5 /*yield**/, _loop_5(j)];
|
|
495
504
|
case 3:
|
|
496
|
-
|
|
497
|
-
|
|
505
|
+
_y.sent();
|
|
506
|
+
_y.label = 4;
|
|
498
507
|
case 4:
|
|
499
508
|
j++;
|
|
500
509
|
return [3 /*break*/, 2];
|
|
@@ -502,59 +511,53 @@ var _findAllRoutesTvl = function (inputCoinAddress, outputCoinAddress) { return
|
|
|
502
511
|
}
|
|
503
512
|
});
|
|
504
513
|
};
|
|
505
|
-
|
|
506
|
-
|
|
514
|
+
_u = 0, ALL_POOLS_3 = ALL_POOLS;
|
|
515
|
+
_w.label = 1;
|
|
507
516
|
case 1:
|
|
508
|
-
if (!(
|
|
509
|
-
|
|
517
|
+
if (!(_u < ALL_POOLS_3.length)) return [3 /*break*/, 4];
|
|
518
|
+
_v = ALL_POOLS_3[_u], poolId = _v[0], poolData = _v[1];
|
|
510
519
|
return [5 /*yield**/, _loop_2(poolId, poolData)];
|
|
511
520
|
case 2:
|
|
512
|
-
|
|
513
|
-
|
|
521
|
+
_w.sent();
|
|
522
|
+
_w.label = 3;
|
|
514
523
|
case 3:
|
|
515
|
-
|
|
524
|
+
_u++;
|
|
516
525
|
return [3 /*break*/, 1];
|
|
517
526
|
case 4: return [2 /*return*/];
|
|
518
527
|
}
|
|
519
528
|
});
|
|
520
529
|
};
|
|
521
|
-
|
|
522
|
-
|
|
530
|
+
_i = 0, curCoins_2 = curCoins;
|
|
531
|
+
_t.label = 3;
|
|
523
532
|
case 3:
|
|
524
|
-
if (!(
|
|
525
|
-
inCoin = curCoins_2[
|
|
533
|
+
if (!(_i < curCoins_2.length)) return [3 /*break*/, 6];
|
|
534
|
+
inCoin = curCoins_2[_i];
|
|
526
535
|
return [5 /*yield**/, _loop_1(inCoin)];
|
|
527
536
|
case 4:
|
|
528
|
-
|
|
529
|
-
|
|
537
|
+
_t.sent();
|
|
538
|
+
_t.label = 5;
|
|
530
539
|
case 5:
|
|
531
|
-
|
|
540
|
+
_i++;
|
|
532
541
|
return [3 /*break*/, 3];
|
|
533
542
|
case 6:
|
|
534
543
|
curCoins = Array.from(nextCoins);
|
|
535
544
|
nextCoins = new Set();
|
|
536
|
-
|
|
545
|
+
_t.label = 7;
|
|
537
546
|
case 7:
|
|
538
547
|
step++;
|
|
539
548
|
return [3 /*break*/, 2];
|
|
540
549
|
case 8:
|
|
541
|
-
routes = __spreadArray(__spreadArray([], ((
|
|
550
|
+
routes = __spreadArray(__spreadArray([], ((_r = routesByTvl[outputCoinAddress]) !== null && _r !== void 0 ? _r : []), true), ((_s = routesByLength[outputCoinAddress]) !== null && _s !== void 0 ? _s : []), true);
|
|
542
551
|
return [2 /*return*/, routes.map(function (r) { return r.steps; })];
|
|
543
552
|
}
|
|
544
553
|
});
|
|
545
554
|
}); };
|
|
546
555
|
exports._findAllRoutesTvl = _findAllRoutesTvl;
|
|
547
556
|
var _findAllRoutes = function (inputCoinAddress, outputCoinAddress) { return __awaiter(void 0, void 0, void 0, function () {
|
|
548
|
-
var routes;
|
|
549
557
|
return __generator(this, function (_a) {
|
|
550
558
|
switch (_a.label) {
|
|
551
559
|
case 0: return [4 /*yield*/, (0, exports._findAllRoutesTvl)(inputCoinAddress, outputCoinAddress)];
|
|
552
|
-
case 1:
|
|
553
|
-
routes = _a.sent();
|
|
554
|
-
if (routes.length > 0)
|
|
555
|
-
return [2 /*return*/, routes];
|
|
556
|
-
return [4 /*yield*/, (0, exports._findAllRoutesTheShorterTheBetter)(inputCoinAddress, outputCoinAddress)];
|
|
557
|
-
case 2: return [2 /*return*/, _a.sent()];
|
|
560
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
558
561
|
}
|
|
559
562
|
});
|
|
560
563
|
}); };
|
|
@@ -711,7 +714,14 @@ var _getBestRouteAndOutput = (0, memoizee_1.default)(function (inputCoinAddress,
|
|
|
711
714
|
route.outputUsd = expectedAmountsUsd[i];
|
|
712
715
|
route.txCostUsd = txCostsUsd[i];
|
|
713
716
|
});
|
|
714
|
-
return [2 /*return*/, routes.reduce(function (route1, route2) {
|
|
717
|
+
return [2 /*return*/, routes.reduce(function (route1, route2) {
|
|
718
|
+
var diff = (route1.outputUsd - route1.txCostUsd) - (route2.outputUsd - route2.txCostUsd);
|
|
719
|
+
if (diff > 0)
|
|
720
|
+
return route1;
|
|
721
|
+
if (diff === 0 && route1.steps.length < route2.steps.length)
|
|
722
|
+
return route1;
|
|
723
|
+
return route2;
|
|
724
|
+
})];
|
|
715
725
|
}
|
|
716
726
|
});
|
|
717
727
|
}); }, {
|
|
@@ -849,9 +859,25 @@ var swap = function (inputCoin, outputCoin, amount, slippage) {
|
|
|
849
859
|
case 4:
|
|
850
860
|
gasLimit = (_d.sent()).mul(curve_1.curve.chainId === 1 ? 130 : 160).div(100);
|
|
851
861
|
return [4 /*yield*/, contract.exchange_multiple(_route, _swapParams, _amount, _minRecvAmount, _factorySwapAddresses, __assign(__assign({}, curve_1.curve.options), { value: value, gasLimit: gasLimit }))];
|
|
852
|
-
case 5: return [2 /*return*/,
|
|
862
|
+
case 5: return [2 /*return*/, _d.sent()];
|
|
853
863
|
}
|
|
854
864
|
});
|
|
855
865
|
});
|
|
856
866
|
};
|
|
857
867
|
exports.swap = swap;
|
|
868
|
+
var getSwappedAmount = function (tx, outputCoin) { return __awaiter(void 0, void 0, void 0, function () {
|
|
869
|
+
var outputCoinAddress, outputCoinDecimals, txInfo, res;
|
|
870
|
+
return __generator(this, function (_a) {
|
|
871
|
+
switch (_a.label) {
|
|
872
|
+
case 0:
|
|
873
|
+
outputCoinAddress = (0, utils_1._getCoinAddresses)(outputCoin)[0];
|
|
874
|
+
outputCoinDecimals = (0, utils_1._getCoinDecimals)(outputCoinAddress)[0];
|
|
875
|
+
return [4 /*yield*/, tx.wait()];
|
|
876
|
+
case 1:
|
|
877
|
+
txInfo = _a.sent();
|
|
878
|
+
res = ethers_1.ethers.utils.defaultAbiCoder.decode(['address[9]', 'uint256[3][4]', 'address[4]', 'uint256', 'uint256'], ethers_1.ethers.utils.hexDataSlice(txInfo.logs[txInfo.logs.length - 1].data, 0));
|
|
879
|
+
return [2 /*return*/, ethers_1.ethers.utils.formatUnits(res[res.length - 1], outputCoinDecimals)];
|
|
880
|
+
}
|
|
881
|
+
});
|
|
882
|
+
}); };
|
|
883
|
+
exports.getSwappedAmount = getSwappedAmount;
|