@curvefi/api 1.19.0 → 1.20.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.
@@ -1,49 +1,47 @@
1
1
  [
2
2
  {
3
- "name": "TokenExchange",
3
+ "anonymous": false,
4
4
  "inputs": [
5
5
  {
6
+ "indexed": true,
6
7
  "name": "buyer",
7
- "type": "address",
8
- "indexed": true
8
+ "type": "address"
9
9
  },
10
10
  {
11
+ "indexed": true,
11
12
  "name": "receiver",
12
- "type": "address",
13
- "indexed": true
13
+ "type": "address"
14
14
  },
15
15
  {
16
+ "indexed": true,
16
17
  "name": "pool",
17
- "type": "address",
18
- "indexed": true
18
+ "type": "address"
19
19
  },
20
20
  {
21
+ "indexed": false,
21
22
  "name": "token_sold",
22
- "type": "address",
23
- "indexed": false
23
+ "type": "address"
24
24
  },
25
25
  {
26
+ "indexed": false,
26
27
  "name": "token_bought",
27
- "type": "address",
28
- "indexed": false
28
+ "type": "address"
29
29
  },
30
30
  {
31
+ "indexed": false,
31
32
  "name": "amount_sold",
32
- "type": "uint256",
33
- "indexed": false
33
+ "type": "uint256"
34
34
  },
35
35
  {
36
+ "indexed": false,
36
37
  "name": "amount_bought",
37
- "type": "uint256",
38
- "indexed": false
38
+ "type": "uint256"
39
39
  }
40
40
  ],
41
- "anonymous": false,
41
+ "name": "TokenExchange",
42
42
  "type": "event"
43
43
  },
44
44
  {
45
- "stateMutability": "nonpayable",
46
- "type": "constructor",
47
45
  "inputs": [
48
46
  {
49
47
  "name": "_address_provider",
@@ -54,16 +52,17 @@
54
52
  "type": "address"
55
53
  }
56
54
  ],
57
- "outputs": []
55
+ "name": "constructor",
56
+ "outputs": [],
57
+ "stateMutability": "nonpayable",
58
+ "type": "constructor"
58
59
  },
59
60
  {
60
61
  "stateMutability": "payable",
61
62
  "type": "fallback"
62
63
  },
63
64
  {
64
- "stateMutability": "payable",
65
- "type": "function",
66
- "name": "exchange_with_best_rate",
65
+ "gas": "490981085",
67
66
  "inputs": [
68
67
  {
69
68
  "name": "_from",
@@ -82,17 +81,18 @@
82
81
  "type": "uint256"
83
82
  }
84
83
  ],
84
+ "name": "exchange_with_best_rate",
85
85
  "outputs": [
86
86
  {
87
87
  "name": "",
88
88
  "type": "uint256"
89
89
  }
90
- ]
90
+ ],
91
+ "stateMutability": "payable",
92
+ "type": "function"
91
93
  },
92
94
  {
93
- "stateMutability": "payable",
94
- "type": "function",
95
- "name": "exchange_with_best_rate",
95
+ "gas": "490981085",
96
96
  "inputs": [
97
97
  {
98
98
  "name": "_from",
@@ -115,17 +115,52 @@
115
115
  "type": "address"
116
116
  }
117
117
  ],
118
+ "name": "exchange_with_best_rate",
118
119
  "outputs": [
119
120
  {
120
121
  "name": "",
121
122
  "type": "uint256"
122
123
  }
123
- ]
124
+ ],
125
+ "stateMutability": "payable",
126
+ "type": "function"
124
127
  },
125
128
  {
126
- "stateMutability": "payable",
127
- "type": "function",
129
+ "gas": "308925",
130
+ "inputs": [
131
+ {
132
+ "name": "_pool",
133
+ "type": "address"
134
+ },
135
+ {
136
+ "name": "_from",
137
+ "type": "address"
138
+ },
139
+ {
140
+ "name": "_to",
141
+ "type": "address"
142
+ },
143
+ {
144
+ "name": "_amount",
145
+ "type": "uint256"
146
+ },
147
+ {
148
+ "name": "_expected",
149
+ "type": "uint256"
150
+ }
151
+ ],
128
152
  "name": "exchange",
153
+ "outputs": [
154
+ {
155
+ "name": "",
156
+ "type": "uint256"
157
+ }
158
+ ],
159
+ "stateMutability": "payable",
160
+ "type": "function"
161
+ },
162
+ {
163
+ "gas": "308925",
129
164
  "inputs": [
130
165
  {
131
166
  "name": "_pool",
@@ -146,19 +181,58 @@
146
181
  {
147
182
  "name": "_expected",
148
183
  "type": "uint256"
184
+ },
185
+ {
186
+ "name": "_receiver",
187
+ "type": "address"
149
188
  }
150
189
  ],
190
+ "name": "exchange",
151
191
  "outputs": [
152
192
  {
153
193
  "name": "",
154
194
  "type": "uint256"
155
195
  }
156
- ]
196
+ ],
197
+ "stateMutability": "payable",
198
+ "type": "function"
157
199
  },
158
200
  {
159
- "stateMutability": "view",
160
- "type": "function",
161
- "name": "get_best_rate",
201
+ "gas": "278915",
202
+ "inputs": [
203
+ {
204
+ "name": "_route",
205
+ "type": "address[9]"
206
+ },
207
+ {
208
+ "name": "_swap_params",
209
+ "type": "uint256[3][4]"
210
+ },
211
+ {
212
+ "name": "_amount",
213
+ "type": "uint256"
214
+ },
215
+ {
216
+ "name": "_expected",
217
+ "type": "uint256"
218
+ },
219
+ {
220
+ "name": "_pools",
221
+ "type": "address[4]"
222
+ }
223
+ ],
224
+ "name": "exchange_multiple",
225
+ "outputs": [
226
+ {
227
+ "name": "",
228
+ "type": "uint256"
229
+ }
230
+ ],
231
+ "stateMutability": "payable",
232
+ "type": "function"
233
+ },
234
+ {
235
+ "gas": "1584665657",
162
236
  "inputs": [
163
237
  {
164
238
  "name": "_from",
@@ -173,6 +247,7 @@
173
247
  "type": "uint256"
174
248
  }
175
249
  ],
250
+ "name": "get_best_rate",
176
251
  "outputs": [
177
252
  {
178
253
  "name": "",
@@ -183,12 +258,45 @@
183
258
  "type": "uint256"
184
259
  }
185
260
  ],
186
- "gas": "395840312"
261
+ "stateMutability": "view",
262
+ "type": "function"
187
263
  },
188
264
  {
265
+ "gas": "1584665657",
266
+ "inputs": [
267
+ {
268
+ "name": "_from",
269
+ "type": "address"
270
+ },
271
+ {
272
+ "name": "_to",
273
+ "type": "address"
274
+ },
275
+ {
276
+ "name": "_amount",
277
+ "type": "uint256"
278
+ },
279
+ {
280
+ "name": "_exclude_pools",
281
+ "type": "address[8]"
282
+ }
283
+ ],
284
+ "name": "get_best_rate",
285
+ "outputs": [
286
+ {
287
+ "name": "",
288
+ "type": "address"
289
+ },
290
+ {
291
+ "name": "",
292
+ "type": "uint256"
293
+ }
294
+ ],
189
295
  "stateMutability": "view",
190
- "type": "function",
191
- "name": "get_exchange_amount",
296
+ "type": "function"
297
+ },
298
+ {
299
+ "gas": "14635",
192
300
  "inputs": [
193
301
  {
194
302
  "name": "_pool",
@@ -207,18 +315,18 @@
207
315
  "type": "uint256"
208
316
  }
209
317
  ],
318
+ "name": "get_exchange_amount",
210
319
  "outputs": [
211
320
  {
212
321
  "name": "",
213
322
  "type": "uint256"
214
323
  }
215
324
  ],
216
- "gas": "5104"
325
+ "stateMutability": "view",
326
+ "type": "function"
217
327
  },
218
328
  {
219
- "stateMutability": "view",
220
- "type": "function",
221
- "name": "get_input_amount",
329
+ "gas": "21156",
222
330
  "inputs": [
223
331
  {
224
332
  "name": "_pool",
@@ -237,18 +345,18 @@
237
345
  "type": "uint256"
238
346
  }
239
347
  ],
348
+ "name": "get_input_amount",
240
349
  "outputs": [
241
350
  {
242
351
  "name": "",
243
352
  "type": "uint256"
244
353
  }
245
354
  ],
246
- "gas": "15358"
355
+ "stateMutability": "view",
356
+ "type": "function"
247
357
  },
248
358
  {
249
- "stateMutability": "view",
250
- "type": "function",
251
- "name": "get_exchange_amounts",
359
+ "gas": "24943",
252
360
  "inputs": [
253
361
  {
254
362
  "name": "_pool",
@@ -267,49 +375,49 @@
267
375
  "type": "uint256[100]"
268
376
  }
269
377
  ],
378
+ "name": "get_exchange_amounts",
270
379
  "outputs": [
271
380
  {
272
381
  "name": "",
273
382
  "type": "uint256[100]"
274
383
  }
275
384
  ],
276
- "gas": "17925"
385
+ "stateMutability": "view",
386
+ "type": "function"
277
387
  },
278
388
  {
279
- "stateMutability": "view",
280
- "type": "function",
281
- "name": "get_calculator",
389
+ "gas": "2654",
282
390
  "inputs": [
283
391
  {
284
392
  "name": "_pool",
285
393
  "type": "address"
286
394
  }
287
395
  ],
396
+ "name": "get_calculator",
288
397
  "outputs": [
289
398
  {
290
399
  "name": "",
291
400
  "type": "address"
292
401
  }
293
402
  ],
294
- "gas": "2429"
403
+ "stateMutability": "view",
404
+ "type": "function"
295
405
  },
296
406
  {
297
- "stateMutability": "nonpayable",
298
- "type": "function",
299
- "name": "update_registry_address",
407
+ "gas": "109973",
300
408
  "inputs": [],
409
+ "name": "update_registry_address",
301
410
  "outputs": [
302
411
  {
303
412
  "name": "",
304
413
  "type": "bool"
305
414
  }
306
415
  ],
307
- "gas": "72066"
416
+ "stateMutability": "nonpayable",
417
+ "type": "function"
308
418
  },
309
419
  {
310
- "stateMutability": "nonpayable",
311
- "type": "function",
312
- "name": "set_calculator",
420
+ "gas": "38057",
313
421
  "inputs": [
314
422
  {
315
423
  "name": "_pool",
@@ -320,118 +428,133 @@
320
428
  "type": "address"
321
429
  }
322
430
  ],
431
+ "name": "set_calculator",
323
432
  "outputs": [
324
433
  {
325
434
  "name": "",
326
435
  "type": "bool"
327
436
  }
328
437
  ],
329
- "gas": "37159"
438
+ "stateMutability": "nonpayable",
439
+ "type": "function"
330
440
  },
331
441
  {
332
- "stateMutability": "nonpayable",
333
- "type": "function",
334
- "name": "set_default_calculator",
442
+ "gas": "37815",
335
443
  "inputs": [
336
444
  {
337
445
  "name": "_calculator",
338
446
  "type": "address"
339
447
  }
340
448
  ],
449
+ "name": "set_default_calculator",
341
450
  "outputs": [
342
451
  {
343
452
  "name": "",
344
453
  "type": "bool"
345
454
  }
346
455
  ],
347
- "gas": "36974"
456
+ "stateMutability": "nonpayable",
457
+ "type": "function"
348
458
  },
349
459
  {
350
- "stateMutability": "nonpayable",
351
- "type": "function",
352
- "name": "claim_balance",
460
+ "gas": "38659",
353
461
  "inputs": [
354
462
  {
355
463
  "name": "_token",
356
464
  "type": "address"
357
465
  }
358
466
  ],
467
+ "name": "claim_balance",
359
468
  "outputs": [
360
469
  {
361
470
  "name": "",
362
471
  "type": "bool"
363
472
  }
364
473
  ],
365
- "gas": "37798"
474
+ "stateMutability": "nonpayable",
475
+ "type": "function"
366
476
  },
367
477
  {
368
- "stateMutability": "nonpayable",
369
- "type": "function",
370
- "name": "set_killed",
478
+ "gas": "37875",
371
479
  "inputs": [
372
480
  {
373
481
  "name": "_is_killed",
374
482
  "type": "bool"
375
483
  }
376
484
  ],
485
+ "name": "set_killed",
377
486
  "outputs": [
378
487
  {
379
488
  "name": "",
380
489
  "type": "bool"
381
490
  }
382
491
  ],
383
- "gas": "37034"
492
+ "stateMutability": "nonpayable",
493
+ "type": "function"
384
494
  },
385
495
  {
386
- "stateMutability": "view",
387
- "type": "function",
388
- "name": "registry",
496
+ "gas": "1706",
389
497
  "inputs": [],
498
+ "name": "registry",
390
499
  "outputs": [
391
500
  {
392
501
  "name": "",
393
502
  "type": "address"
394
503
  }
395
504
  ],
396
- "gas": "1538"
505
+ "stateMutability": "view",
506
+ "type": "function"
397
507
  },
398
508
  {
399
- "stateMutability": "view",
400
- "type": "function",
401
- "name": "factory_registry",
509
+ "gas": "1736",
402
510
  "inputs": [],
511
+ "name": "factory_registry",
403
512
  "outputs": [
404
513
  {
405
514
  "name": "",
406
515
  "type": "address"
407
516
  }
408
517
  ],
409
- "gas": "1568"
518
+ "stateMutability": "view",
519
+ "type": "function"
410
520
  },
411
521
  {
412
- "stateMutability": "view",
413
- "type": "function",
414
- "name": "default_calculator",
522
+ "gas": "1766",
415
523
  "inputs": [],
524
+ "name": "crypto_registry",
416
525
  "outputs": [
417
526
  {
418
527
  "name": "",
419
528
  "type": "address"
420
529
  }
421
530
  ],
422
- "gas": "1598"
531
+ "stateMutability": "view",
532
+ "type": "function"
423
533
  },
424
534
  {
535
+ "gas": "1796",
536
+ "inputs": [],
537
+ "name": "default_calculator",
538
+ "outputs": [
539
+ {
540
+ "name": "",
541
+ "type": "address"
542
+ }
543
+ ],
425
544
  "stateMutability": "view",
426
- "type": "function",
427
- "name": "is_killed",
545
+ "type": "function"
546
+ },
547
+ {
548
+ "gas": "1826",
428
549
  "inputs": [],
550
+ "name": "is_killed",
429
551
  "outputs": [
430
552
  {
431
553
  "name": "",
432
554
  "type": "bool"
433
555
  }
434
556
  ],
435
- "gas": "1628"
557
+ "stateMutability": "view",
558
+ "type": "function"
436
559
  }
437
560
  ]
package/lib/index.d.ts CHANGED
@@ -39,6 +39,7 @@ declare const curve: {
39
39
  hasAllowance: (coins: string[], amounts: string[], address: string, spender: string) => Promise<boolean>;
40
40
  ensureAllowance: (coins: string[], amounts: string[], spender: string) => Promise<string[]>;
41
41
  getBestPoolAndOutput: (inputCoin: string, outputCoin: string, amount: string) => Promise<{
42
+ poolName: string;
42
43
  poolAddress: string;
43
44
  output: string;
44
45
  }>;
@@ -1,3 +1,4 @@
1
+ import { ethers } from "ethers";
1
2
  export interface DictInterface<T> {
2
3
  [index: string]: T;
3
4
  }
@@ -72,3 +73,14 @@ export interface IPoolStats {
72
73
  export interface IStats {
73
74
  [index: string]: IPoolStats;
74
75
  }
76
+ export interface ISinglePoolSwapData {
77
+ poolName: string;
78
+ poolAddress: string;
79
+ i: number;
80
+ j: number;
81
+ swapType: 1 | 2 | 3 | 4;
82
+ swapAddress: string;
83
+ }
84
+ export interface ISinglePoolSwapDataAndOutput extends ISinglePoolSwapData {
85
+ _output: ethers.BigNumber;
86
+ }
package/lib/pools.d.ts CHANGED
@@ -222,6 +222,7 @@ export declare class Pool {
222
222
  private _getExchangeOutputWrapped;
223
223
  }
224
224
  export declare const getBestPoolAndOutput: (inputCoin: string, outputCoin: string, amount: string) => Promise<{
225
+ poolName: string;
225
226
  poolAddress: string;
226
227
  output: string;
227
228
  }>;
package/lib/pools.js CHANGED
@@ -3587,8 +3587,8 @@ var Pool = /** @class */ (function () {
3587
3587
  exports.Pool = Pool;
3588
3588
  // --------- Exchange Using All Pools ---------
3589
3589
  var _estimatedGasForPoolsCache = {};
3590
- var _estimateGasForPools = function (poolAddresses, inputCoinAddress, outputCoinAddress, _amount) { return __awaiter(void 0, void 0, void 0, function () {
3591
- var registryExchangeContract, sortedCoins, gasPromises, _i, poolAddresses_1, poolAddress, key, gasPromise, _gasAmounts_1, err_1;
3590
+ var _estimateGasForPools = function (pools, inputCoinAddress, outputCoinAddress, _amount) { return __awaiter(void 0, void 0, void 0, function () {
3591
+ var registryExchangeContract, sortedCoins, gasPromises, _i, pools_1, pool, key, gasPromise, _route, _swapParams, _pools, _gasAmounts_1, err_1;
3592
3592
  var _a;
3593
3593
  return __generator(this, function (_c) {
3594
3594
  switch (_c.label) {
@@ -3596,12 +3596,15 @@ var _estimateGasForPools = function (poolAddresses, inputCoinAddress, outputCoin
3596
3596
  registryExchangeContract = curve_1.curve.contracts[curve_1.ALIASES.registry_exchange].contract;
3597
3597
  sortedCoins = [inputCoinAddress, outputCoinAddress].sort();
3598
3598
  gasPromises = [];
3599
- for (_i = 0, poolAddresses_1 = poolAddresses; _i < poolAddresses_1.length; _i++) {
3600
- poolAddress = poolAddresses_1[_i];
3601
- key = "".concat(poolAddress, "-").concat(sortedCoins[0], "-").concat(sortedCoins[1]);
3599
+ for (_i = 0, pools_1 = pools; _i < pools_1.length; _i++) {
3600
+ pool = pools_1[_i];
3601
+ key = "".concat(pool.poolAddress, "-").concat(sortedCoins[0], "-").concat(sortedCoins[1]);
3602
3602
  gasPromise = void 0;
3603
+ _route = [inputCoinAddress, pool.poolAddress, outputCoinAddress].concat(Array(6).fill(ethers_1.ethers.constants.AddressZero));
3604
+ _swapParams = [[pool.i, pool.j, pool.swapType]].concat(Array(3).fill([0, 0, 0]));
3605
+ _pools = [pool.swapAddress].concat(Array(3).fill(ethers_1.ethers.constants.AddressZero));
3603
3606
  if ((((_a = _estimatedGasForPoolsCache[key]) === null || _a === void 0 ? void 0 : _a.time) || 0) + 3600000 < Date.now()) {
3604
- gasPromise = registryExchangeContract.estimateGas.exchange(poolAddress, inputCoinAddress, outputCoinAddress, _amount, 0, curve_1.curve.constantOptions);
3607
+ gasPromise = registryExchangeContract.estimateGas.exchange_multiple(_route, _swapParams, _amount, 0, _pools, curve_1.curve.constantOptions);
3605
3608
  }
3606
3609
  else {
3607
3610
  gasPromise = Promise.resolve(_estimatedGasForPoolsCache[key].gas);
@@ -3614,101 +3617,168 @@ var _estimateGasForPools = function (poolAddresses, inputCoinAddress, outputCoin
3614
3617
  return [4 /*yield*/, Promise.all(gasPromises)];
3615
3618
  case 2:
3616
3619
  _gasAmounts_1 = _c.sent();
3617
- poolAddresses.forEach(function (poolAddress, i) {
3618
- var key = "".concat(poolAddress, "-").concat(sortedCoins[0], "-").concat(sortedCoins[1]);
3620
+ pools.forEach(function (pool, i) {
3621
+ var key = "".concat(pool.poolAddress, "-").concat(sortedCoins[0], "-").concat(sortedCoins[1]);
3619
3622
  _estimatedGasForPoolsCache[key] = { 'gas': _gasAmounts_1[i], 'time': Date.now() };
3620
3623
  });
3621
3624
  return [2 /*return*/, _gasAmounts_1.map(function (_g) { return Number(ethers_1.ethers.utils.formatUnits(_g, 0)); })];
3622
3625
  case 3:
3623
3626
  err_1 = _c.sent();
3624
- return [2 /*return*/, poolAddresses.map(function () { return 0; })];
3627
+ return [2 /*return*/, pools.map(function () { return 0; })];
3625
3628
  case 4: return [2 /*return*/];
3626
3629
  }
3627
3630
  });
3628
3631
  }); };
3632
+ var _getAvailablePools = function (inputCoinAddress, outputCoinAddress) {
3633
+ return Object.entries(__assign(__assign(__assign({}, curve_1.POOLS_DATA), curve_1.curve.constants.FACTORY_POOLS_DATA), curve_1.curve.constants.CRYPTO_FACTORY_POOLS_DATA)).map(function (_a) {
3634
+ var _c;
3635
+ var poolName = _a[0], poolData = _a[1];
3636
+ var coin_addresses = poolData.coin_addresses.map(function (a) { return a.toLowerCase(); });
3637
+ var underlying_coin_addresses = poolData.underlying_coin_addresses.map(function (a) { return a.toLowerCase(); });
3638
+ var meta_coin_addresses = ((_c = poolData.meta_coin_addresses) === null || _c === void 0 ? void 0 : _c.map(function (a) { return a.toLowerCase(); })) || [];
3639
+ var inputCoinIndexes = {
3640
+ coin: coin_addresses.indexOf(inputCoinAddress.toLowerCase()),
3641
+ underlying_coin: underlying_coin_addresses.indexOf(inputCoinAddress.toLowerCase()),
3642
+ meta_coin: meta_coin_addresses ? meta_coin_addresses.indexOf(inputCoinAddress.toLowerCase()) : -1,
3643
+ };
3644
+ var outputCoinIndexes = {
3645
+ coin: coin_addresses.indexOf(outputCoinAddress.toLowerCase()),
3646
+ underlying_coin: underlying_coin_addresses.indexOf(outputCoinAddress.toLowerCase()),
3647
+ meta_coin: meta_coin_addresses ? meta_coin_addresses.indexOf(outputCoinAddress.toLowerCase()) : -1,
3648
+ };
3649
+ // Only for underlying swaps
3650
+ var poolAddress = ["eurtusd", "xautusd", "atricrypto3"].includes(poolName) ||
3651
+ (curve_1.curve.chainId === 137 && poolData.is_factory) ? poolData.deposit_address : poolData.swap_address;
3652
+ if (inputCoinIndexes.coin >= 0 && outputCoinIndexes.coin >= 0 && poolName !== "atricrypto3") {
3653
+ var swapType = poolData.is_crypto ? 3 : 1;
3654
+ return {
3655
+ poolName: poolName,
3656
+ poolAddress: poolData.swap_address,
3657
+ i: inputCoinIndexes.coin,
3658
+ j: outputCoinIndexes.coin,
3659
+ swapType: swapType,
3660
+ swapAddress: ethers_1.ethers.constants.AddressZero,
3661
+ };
3662
+ }
3663
+ else if (inputCoinIndexes.underlying_coin >= 0 &&
3664
+ outputCoinIndexes.underlying_coin >= 0 &&
3665
+ !(poolName === "atricrypto3" && (inputCoinIndexes.meta_coin >= 0 && outputCoinIndexes.meta_coin >= 0))) {
3666
+ var swapType = poolData.is_crypto && (poolData.is_fake || poolData.is_meta) ? 4 : poolData.is_crypto ? 3 : 2;
3667
+ return {
3668
+ poolName: poolName,
3669
+ poolAddress: poolAddress,
3670
+ i: inputCoinIndexes.underlying_coin,
3671
+ j: outputCoinIndexes.underlying_coin,
3672
+ swapType: swapType,
3673
+ swapAddress: ethers_1.ethers.constants.AddressZero,
3674
+ };
3675
+ }
3676
+ else if (inputCoinIndexes.coin === 0 && outputCoinIndexes.meta_coin >= 0 && poolName !== "atricrypto3") {
3677
+ var swapType = (curve_1.curve.chainId === 137 && poolData.is_factory) ? 5 : poolData.is_crypto ? 4 : 2;
3678
+ return {
3679
+ poolName: poolName,
3680
+ poolAddress: poolAddress,
3681
+ i: inputCoinIndexes.coin,
3682
+ j: outputCoinIndexes.meta_coin + 1,
3683
+ swapType: swapType,
3684
+ swapAddress: swapType === 5 ? poolData.swap_address : ethers_1.ethers.constants.AddressZero,
3685
+ };
3686
+ }
3687
+ else if (inputCoinIndexes.meta_coin >= 0 && outputCoinIndexes.coin === 0 && poolName !== "atricrypto3") {
3688
+ var swapType = (curve_1.curve.chainId === 137 && poolData.is_factory) ? 5 : poolData.is_crypto ? 4 : 2;
3689
+ return {
3690
+ poolName: poolName,
3691
+ poolAddress: poolAddress,
3692
+ i: inputCoinIndexes.meta_coin + 1,
3693
+ j: outputCoinIndexes.coin,
3694
+ swapType: swapType,
3695
+ swapAddress: swapType === 5 ? poolData.swap_address : ethers_1.ethers.constants.AddressZero,
3696
+ };
3697
+ }
3698
+ else {
3699
+ return null;
3700
+ }
3701
+ }).filter(function (pool) { return pool !== null; });
3702
+ };
3629
3703
  var _getBestPoolAndOutput = function (inputCoinAddress, outputCoinAddress, amount) { return __awaiter(void 0, void 0, void 0, function () {
3630
- var availablePools, _a, inputCoinDecimals, outputCoinDecimals, _amount, poolAddress, registryExchangeContract, _output, registryExchangeMulticall, calls, _i, availablePools_1, pool, _c, _expectedAmounts, gasAmounts, outputCoinUsdRate, gasData, ethUsdRate, gasPrice, expectedAmounts, expectedAmountsUsd, txCostsUsd, bestPool;
3631
- return __generator(this, function (_d) {
3632
- switch (_d.label) {
3704
+ var _a, inputCoinDecimals, outputCoinDecimals, _amount, availablePoolsRaw, availablePools, _i, availablePoolsRaw_1, pool, poolName, poolAddress, i, j, swapType, swapAddress, contract, _c, _d, err_2, _e, poolName, poolAddress, i, j, swapType, _output, swapAddress, _f, gasAmounts, outputCoinUsdRate, gasData, ethUsdRate, gasPrice, expectedAmounts, expectedAmountsUsd, txCostsUsd;
3705
+ return __generator(this, function (_h) {
3706
+ switch (_h.label) {
3633
3707
  case 0:
3634
- availablePools = Object.entries(curve_1.POOLS_DATA).map(function (pool) {
3635
- var _a;
3636
- var coin_addresses = pool[1].coin_addresses.map(function (a) { return a.toLowerCase(); });
3637
- var underlying_coin_addresses = pool[1].underlying_coin_addresses.map(function (a) { return a.toLowerCase(); });
3638
- var meta_coin_addresses = (_a = pool[1].meta_coin_addresses) === null || _a === void 0 ? void 0 : _a.map(function (a) { return a.toLowerCase(); });
3639
- var inputCoinIndexes = [
3640
- coin_addresses.indexOf(inputCoinAddress.toLowerCase()),
3641
- underlying_coin_addresses.indexOf(inputCoinAddress.toLowerCase()),
3642
- meta_coin_addresses ? meta_coin_addresses.indexOf(inputCoinAddress.toLowerCase()) : -1,
3643
- ];
3644
- var outputCoinIndexes = [
3645
- coin_addresses.indexOf(outputCoinAddress.toLowerCase()),
3646
- underlying_coin_addresses.indexOf(outputCoinAddress.toLowerCase()),
3647
- meta_coin_addresses ? meta_coin_addresses.indexOf(outputCoinAddress.toLowerCase()) : -1,
3648
- ];
3649
- if (pool[0] === 'atricrypto3') {
3650
- return null;
3651
- }
3652
- if (inputCoinIndexes[0] >= 0 && outputCoinIndexes[0] >= 0) {
3653
- return { poolAddress: pool[1].swap_address, _output: ethers_1.ethers.BigNumber.from(0), outputUsd: 0, txCostUsd: 0 };
3654
- }
3655
- else if (inputCoinIndexes[1] >= 0 && outputCoinIndexes[1] >= 0) {
3656
- return { poolAddress: pool[1].swap_address, _output: ethers_1.ethers.BigNumber.from(0), outputUsd: 0, txCostUsd: 0 };
3657
- }
3658
- else if (inputCoinIndexes[0] === 0 && outputCoinIndexes[2] >= 0 && !['eurtusd', "xautusd"].includes(pool[0])) {
3659
- return { poolAddress: pool[1].swap_address, _output: ethers_1.ethers.BigNumber.from(0), outputUsd: 0, txCostUsd: 0 };
3660
- }
3661
- else if (inputCoinIndexes[2] >= 0 && outputCoinIndexes[0] === 0 && !['eurtusd', "xautusd"].includes(pool[0])) {
3662
- return { poolAddress: pool[1].swap_address, _output: ethers_1.ethers.BigNumber.from(0), outputUsd: 0, txCostUsd: 0 };
3663
- }
3664
- else {
3665
- return null;
3666
- }
3667
- }).filter(function (pool) { return pool !== null; });
3668
3708
  _a = (0, utils_1._getCoinDecimals)(inputCoinAddress, outputCoinAddress), inputCoinDecimals = _a[0], outputCoinDecimals = _a[1];
3669
3709
  _amount = ethers_1.ethers.utils.parseUnits(amount.toString(), inputCoinDecimals);
3670
- if (availablePools.length === 0) {
3671
- return [2 /*return*/, { poolAddress: "0x0000000000000000000000000000000000000000", _output: ethers_1.ethers.BigNumber.from(0) }];
3672
- }
3673
- if (!(availablePools.length === 1)) return [3 /*break*/, 2];
3674
- poolAddress = availablePools[0].poolAddress;
3675
- registryExchangeContract = curve_1.curve.contracts[curve_1.ALIASES.registry_exchange].contract;
3676
- return [4 /*yield*/, registryExchangeContract.get_exchange_amount(poolAddress, inputCoinAddress, outputCoinAddress, _amount, curve_1.curve.constantOptions)];
3710
+ availablePoolsRaw = _getAvailablePools(inputCoinAddress, outputCoinAddress).map(function (data) { return (__assign(__assign({}, data), { _output: ethers_1.ethers.BigNumber.from(0), outputUsd: 0, txCostUsd: 0 })); });
3711
+ availablePools = [];
3712
+ _i = 0, availablePoolsRaw_1 = availablePoolsRaw;
3713
+ _h.label = 1;
3677
3714
  case 1:
3678
- _output = _d.sent();
3679
- return [2 /*return*/, { poolAddress: poolAddress, _output: _output }];
3715
+ if (!(_i < availablePoolsRaw_1.length)) return [3 /*break*/, 9];
3716
+ pool = availablePoolsRaw_1[_i];
3717
+ poolName = pool.poolName, poolAddress = pool.poolAddress, i = pool.i, j = pool.j, swapType = pool.swapType, swapAddress = pool.swapAddress;
3718
+ contract = curve_1.curve.contracts[swapAddress === ethers_1.ethers.constants.AddressZero ? poolAddress : swapAddress].contract;
3719
+ _h.label = 2;
3680
3720
  case 2:
3681
- registryExchangeMulticall = curve_1.curve.contracts[curve_1.ALIASES.registry_exchange].multicallContract;
3682
- calls = [];
3683
- for (_i = 0, availablePools_1 = availablePools; _i < availablePools_1.length; _i++) {
3684
- pool = availablePools_1[_i];
3685
- calls.push(registryExchangeMulticall.get_exchange_amount(pool.poolAddress, inputCoinAddress, outputCoinAddress, _amount));
3721
+ _h.trys.push([2, 7, , 8]);
3722
+ _c = pool;
3723
+ if (![2, 4, 5].includes(swapType)) return [3 /*break*/, 4];
3724
+ return [4 /*yield*/, contract.get_dy_underlying(i, j, _amount, curve_1.curve.constantOptions)];
3725
+ case 3:
3726
+ _d = _h.sent();
3727
+ return [3 /*break*/, 6];
3728
+ case 4: return [4 /*yield*/, contract.get_dy(i, j, _amount, curve_1.curve.constantOptions)];
3729
+ case 5:
3730
+ _d = _h.sent();
3731
+ _h.label = 6;
3732
+ case 6:
3733
+ _c._output = _d;
3734
+ availablePools.push(pool);
3735
+ return [3 /*break*/, 8];
3736
+ case 7:
3737
+ err_2 = _h.sent();
3738
+ console.log("Pool ".concat(poolName, " is empty or very imbalanced"));
3739
+ return [3 /*break*/, 8];
3740
+ case 8:
3741
+ _i++;
3742
+ return [3 /*break*/, 1];
3743
+ case 9:
3744
+ if (availablePools.length === 0) {
3745
+ return [2 /*return*/, {
3746
+ poolName: "",
3747
+ poolAddress: ethers_1.ethers.constants.AddressZero,
3748
+ i: 0,
3749
+ j: 0,
3750
+ swapType: 1,
3751
+ swapAddress: ethers_1.ethers.constants.AddressZero,
3752
+ _output: ethers_1.ethers.BigNumber.from(0),
3753
+ }];
3754
+ }
3755
+ if (availablePools.length === 1) {
3756
+ _e = availablePools[0], poolName = _e.poolName, poolAddress = _e.poolAddress, i = _e.i, j = _e.j, swapType = _e.swapType, _output = _e._output, swapAddress = _e.swapAddress;
3757
+ return [2 /*return*/, { poolName: poolName, poolAddress: poolAddress, i: i, j: j, swapType: swapType, _output: _output, swapAddress: swapAddress }];
3686
3758
  }
3687
3759
  return [4 /*yield*/, Promise.all([
3688
- curve_1.curve.multicallProvider.all(calls),
3689
- _estimateGasForPools(availablePools.map(function (pool) { return pool.poolAddress; }), inputCoinAddress, outputCoinAddress, _amount),
3760
+ // curve.multicallProvider.all(calls),
3761
+ _estimateGasForPools(availablePools, inputCoinAddress, outputCoinAddress, _amount),
3690
3762
  (0, utils_1._getUsdRate)(outputCoinAddress),
3691
3763
  axios_1.default.get("https://api.curve.fi/api/getGas"),
3692
- (0, utils_1._getUsdRate)(curve_1.COINS.eth),
3764
+ (0, utils_1._getUsdRate)(curve_1.curve.chainId === 137 ? curve_1.COINS.matic : curve_1.COINS.eth),
3693
3765
  ])];
3694
- case 3:
3695
- _c = _d.sent(), _expectedAmounts = _c[0], gasAmounts = _c[1], outputCoinUsdRate = _c[2], gasData = _c[3], ethUsdRate = _c[4];
3766
+ case 10:
3767
+ _f = _h.sent(), gasAmounts = _f[0], outputCoinUsdRate = _f[1], gasData = _f[2], ethUsdRate = _f[3];
3696
3768
  gasPrice = gasData.data.data.gas.standard;
3697
- expectedAmounts = _expectedAmounts.map(function (_amount) { return Number(ethers_1.ethers.utils.formatUnits(_amount, outputCoinDecimals)); });
3769
+ expectedAmounts = (availablePools).map(function (swapData) { return Number(ethers_1.ethers.utils.formatUnits(swapData._output, outputCoinDecimals)); });
3698
3770
  expectedAmountsUsd = expectedAmounts.map(function (a) { return a * outputCoinUsdRate; });
3699
3771
  txCostsUsd = gasAmounts.map(function (a) { return ethUsdRate * a * gasPrice / 1e18; });
3700
3772
  availablePools.forEach(function (pool, i) {
3701
- pool._output = _expectedAmounts[i];
3702
3773
  pool.outputUsd = expectedAmountsUsd[i];
3703
3774
  pool.txCostUsd = txCostsUsd[i];
3704
3775
  });
3705
- bestPool = availablePools.reduce(function (pool1, pool2) { return (pool1.outputUsd - pool1.txCostUsd) - (pool2.outputUsd - pool2.txCostUsd) >= 0 ? pool1 : pool2; });
3706
- return [2 /*return*/, { poolAddress: bestPool.poolAddress, _output: bestPool._output }];
3776
+ return [2 /*return*/, availablePools.reduce(function (pool1, pool2) { return (pool1.outputUsd - pool1.txCostUsd) - (pool2.outputUsd - pool2.txCostUsd) >= 0 ? pool1 : pool2; })];
3707
3777
  }
3708
3778
  });
3709
3779
  }); };
3710
3780
  var getBestPoolAndOutput = function (inputCoin, outputCoin, amount) { return __awaiter(void 0, void 0, void 0, function () {
3711
- var _a, inputCoinAddress, outputCoinAddress, outputCoinDecimals, _c, poolAddress, _output;
3781
+ var _a, inputCoinAddress, outputCoinAddress, outputCoinDecimals, _c, poolName, poolAddress, _output;
3712
3782
  return __generator(this, function (_d) {
3713
3783
  switch (_d.label) {
3714
3784
  case 0:
@@ -3716,8 +3786,8 @@ var getBestPoolAndOutput = function (inputCoin, outputCoin, amount) { return __a
3716
3786
  outputCoinDecimals = (0, utils_1._getCoinDecimals)(outputCoinAddress)[0];
3717
3787
  return [4 /*yield*/, _getBestPoolAndOutput(inputCoinAddress, outputCoinAddress, amount)];
3718
3788
  case 1:
3719
- _c = _d.sent(), poolAddress = _c.poolAddress, _output = _c._output;
3720
- return [2 /*return*/, { poolAddress: poolAddress, output: ethers_1.ethers.utils.formatUnits(_output, outputCoinDecimals) }];
3789
+ _c = _d.sent(), poolName = _c.poolName, poolAddress = _c.poolAddress, _output = _c._output;
3790
+ return [2 /*return*/, { poolName: poolName, poolAddress: poolAddress, output: ethers_1.ethers.utils.formatUnits(_output, outputCoinDecimals) }];
3721
3791
  }
3722
3792
  });
3723
3793
  }); };
@@ -3761,7 +3831,7 @@ exports.exchangeApprove = exchangeApprove;
3761
3831
  var exchangeEstimateGas = function (inputCoin, outputCoin, amount, maxSlippage) {
3762
3832
  if (maxSlippage === void 0) { maxSlippage = 0.01; }
3763
3833
  return __awaiter(void 0, void 0, void 0, function () {
3764
- var _a, inputCoinAddress, outputCoinAddress, _c, inputCoinDecimals, outputCoinDecimals, _d, poolAddress, _output, _amount, minRecvAmountBN, _minRecvAmount, contract, value;
3834
+ var _a, inputCoinAddress, outputCoinAddress, _c, inputCoinDecimals, outputCoinDecimals, _d, poolAddress, i, j, swapType, swapAddress, _output, _route, _swapParams, _pools, _amount, minRecvAmountBN, _minRecvAmount, contract, value;
3765
3835
  return __generator(this, function (_e) {
3766
3836
  switch (_e.label) {
3767
3837
  case 0:
@@ -3769,10 +3839,13 @@ var exchangeEstimateGas = function (inputCoin, outputCoin, amount, maxSlippage)
3769
3839
  _c = (0, utils_1._getCoinDecimals)(inputCoinAddress, outputCoinAddress), inputCoinDecimals = _c[0], outputCoinDecimals = _c[1];
3770
3840
  return [4 /*yield*/, _getBestPoolAndOutput(inputCoinAddress, outputCoinAddress, amount)];
3771
3841
  case 1:
3772
- _d = _e.sent(), poolAddress = _d.poolAddress, _output = _d._output;
3842
+ _d = _e.sent(), poolAddress = _d.poolAddress, i = _d.i, j = _d.j, swapType = _d.swapType, swapAddress = _d.swapAddress, _output = _d._output;
3773
3843
  if (poolAddress === "0x0000000000000000000000000000000000000000") {
3774
3844
  throw new Error("This pair can't be exchanged");
3775
3845
  }
3846
+ _route = [inputCoinAddress, poolAddress, outputCoinAddress].concat(Array(6).fill(ethers_1.ethers.constants.AddressZero));
3847
+ _swapParams = [[i, j, swapType]].concat(Array(3).fill([0, 0, 0]));
3848
+ _pools = [swapAddress].concat(Array(3).fill(ethers_1.ethers.constants.AddressZero));
3776
3849
  _amount = ethers_1.ethers.utils.parseUnits(amount, inputCoinDecimals);
3777
3850
  minRecvAmountBN = (0, utils_1.toBN)(_output, outputCoinDecimals).times(1 - maxSlippage);
3778
3851
  _minRecvAmount = (0, utils_1.fromBN)(minRecvAmountBN, outputCoinDecimals);
@@ -3781,7 +3854,7 @@ var exchangeEstimateGas = function (inputCoin, outputCoin, amount, maxSlippage)
3781
3854
  return [4 /*yield*/, curve_1.curve.updateFeeData()];
3782
3855
  case 2:
3783
3856
  _e.sent();
3784
- return [4 /*yield*/, contract.estimateGas.exchange(poolAddress, inputCoinAddress, outputCoinAddress, _amount, _minRecvAmount, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
3857
+ return [4 /*yield*/, contract.estimateGas.exchange_multiple(_route, _swapParams, _amount, _minRecvAmount, _pools, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
3785
3858
  case 3: return [2 /*return*/, (_e.sent()).toNumber()];
3786
3859
  }
3787
3860
  });
@@ -3791,7 +3864,7 @@ exports.exchangeEstimateGas = exchangeEstimateGas;
3791
3864
  var exchange = function (inputCoin, outputCoin, amount, maxSlippage) {
3792
3865
  if (maxSlippage === void 0) { maxSlippage = 0.01; }
3793
3866
  return __awaiter(void 0, void 0, void 0, function () {
3794
- var _a, inputCoinAddress, outputCoinAddress, _c, inputCoinDecimals, outputCoinDecimals, _d, poolAddress, _output, _amount, minRecvAmountBN, _minRecvAmount, contract, value, gasLimit;
3867
+ var _a, inputCoinAddress, outputCoinAddress, _c, inputCoinDecimals, outputCoinDecimals, _d, poolAddress, i, j, swapType, swapAddress, _output, _route, _swapParams, _pools, _amount, minRecvAmountBN, _minRecvAmount, contract, value, gasLimit;
3795
3868
  return __generator(this, function (_e) {
3796
3869
  switch (_e.label) {
3797
3870
  case 0:
@@ -3802,10 +3875,13 @@ var exchange = function (inputCoin, outputCoin, amount, maxSlippage) {
3802
3875
  _e.sent();
3803
3876
  return [4 /*yield*/, _getBestPoolAndOutput(inputCoinAddress, outputCoinAddress, amount)];
3804
3877
  case 2:
3805
- _d = _e.sent(), poolAddress = _d.poolAddress, _output = _d._output;
3878
+ _d = _e.sent(), poolAddress = _d.poolAddress, i = _d.i, j = _d.j, swapType = _d.swapType, swapAddress = _d.swapAddress, _output = _d._output;
3806
3879
  if (poolAddress === "0x0000000000000000000000000000000000000000") {
3807
3880
  throw new Error("This pair can't be exchanged");
3808
3881
  }
3882
+ _route = [inputCoinAddress, poolAddress, outputCoinAddress].concat(Array(6).fill(ethers_1.ethers.constants.AddressZero));
3883
+ _swapParams = [[i, j, swapType]].concat(Array(3).fill([0, 0, 0]));
3884
+ _pools = [swapAddress].concat(Array(3).fill(ethers_1.ethers.constants.AddressZero));
3809
3885
  _amount = ethers_1.ethers.utils.parseUnits(amount, inputCoinDecimals);
3810
3886
  minRecvAmountBN = (0, utils_1.toBN)(_output, outputCoinDecimals).times(1 - maxSlippage);
3811
3887
  _minRecvAmount = (0, utils_1.fromBN)(minRecvAmountBN, outputCoinDecimals);
@@ -3814,10 +3890,10 @@ var exchange = function (inputCoin, outputCoin, amount, maxSlippage) {
3814
3890
  return [4 /*yield*/, curve_1.curve.updateFeeData()];
3815
3891
  case 3:
3816
3892
  _e.sent();
3817
- return [4 /*yield*/, contract.estimateGas.exchange(poolAddress, inputCoinAddress, outputCoinAddress, _amount, _minRecvAmount, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
3893
+ return [4 /*yield*/, contract.estimateGas.exchange_multiple(_route, _swapParams, _amount, _minRecvAmount, _pools, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
3818
3894
  case 4:
3819
3895
  gasLimit = (_e.sent()).mul(130).div(100);
3820
- return [4 /*yield*/, contract.exchange(poolAddress, inputCoinAddress, outputCoinAddress, _amount, _minRecvAmount, __assign(__assign({}, curve_1.curve.options), { value: value, gasLimit: gasLimit }))];
3896
+ return [4 /*yield*/, contract.exchange_multiple(_route, _swapParams, _amount, _minRecvAmount, _pools, __assign(__assign({}, curve_1.curve.options), { value: value, gasLimit: gasLimit }))];
3821
3897
  case 5: return [2 /*return*/, (_e.sent()).hash];
3822
3898
  }
3823
3899
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@curvefi/api",
3
- "version": "1.19.0",
3
+ "version": "1.20.0",
4
4
  "description": "JavaScript library for curve.fi",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {