@curvefi/api 1.4.2 → 1.5.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/pools.js CHANGED
@@ -126,6 +126,17 @@ var Pool = /** @class */ (function () {
126
126
  });
127
127
  });
128
128
  };
129
+ this.getVirtualPrice = function () { return __awaiter(_this, void 0, void 0, function () {
130
+ var _virtualPrice;
131
+ return __generator(this, function (_a) {
132
+ switch (_a.label) {
133
+ case 0: return [4 /*yield*/, curve_1.curve.contracts[this.swap].contract.get_virtual_price(curve_1.curve.constantOptions)];
134
+ case 1:
135
+ _virtualPrice = _a.sent();
136
+ return [2 /*return*/, ethers_1.ethers.utils.formatUnits(_virtualPrice)];
137
+ }
138
+ });
139
+ }); };
129
140
  this.addLiquidityExpected = function (amounts) { return __awaiter(_this, void 0, void 0, function () {
130
141
  return __generator(this, function (_a) {
131
142
  switch (_a.label) {
@@ -134,19 +145,52 @@ var Pool = /** @class */ (function () {
134
145
  }
135
146
  });
136
147
  }); };
148
+ this.addLiquidityIsApproved = function (amounts) { return __awaiter(_this, void 0, void 0, function () {
149
+ var spender;
150
+ return __generator(this, function (_a) {
151
+ switch (_a.label) {
152
+ case 0:
153
+ spender = (['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name) || this.isMeta) ? this.zap : this.swap;
154
+ return [4 /*yield*/, (0, utils_1.hasAllowance)(this.underlyingCoinAddresses, amounts, curve_1.curve.signerAddress, spender)];
155
+ case 1: return [2 /*return*/, _a.sent()];
156
+ }
157
+ });
158
+ }); };
159
+ this.addLiquidityApproveEstimateGas = function (amounts) { return __awaiter(_this, void 0, void 0, function () {
160
+ var spender;
161
+ return __generator(this, function (_a) {
162
+ switch (_a.label) {
163
+ case 0:
164
+ spender = (['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name) || this.isMeta) ? this.zap : this.swap;
165
+ return [4 /*yield*/, (0, utils_1.ensureAllowanceEstimateGas)(this.underlyingCoinAddresses, amounts, spender)];
166
+ case 1: return [2 /*return*/, _a.sent()];
167
+ }
168
+ });
169
+ }); };
170
+ this.addLiquidityApprove = function (amounts) { return __awaiter(_this, void 0, void 0, function () {
171
+ var spender;
172
+ return __generator(this, function (_a) {
173
+ switch (_a.label) {
174
+ case 0:
175
+ spender = (['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name) || this.isMeta) ? this.zap : this.swap;
176
+ return [4 /*yield*/, (0, utils_1.ensureAllowance)(this.underlyingCoinAddresses, amounts, spender)];
177
+ case 1: return [2 /*return*/, _a.sent()];
178
+ }
179
+ });
180
+ }); };
137
181
  this.addLiquidityEstimateGas = function (amounts) { return __awaiter(_this, void 0, void 0, function () {
138
- var balances, _a, _b, i, _amounts;
182
+ var balances, _a, _c, i, _amounts;
139
183
  var _this = this;
140
- return __generator(this, function (_c) {
141
- switch (_c.label) {
184
+ return __generator(this, function (_d) {
185
+ switch (_d.label) {
142
186
  case 0:
143
187
  if (amounts.length !== this.underlyingCoinAddresses.length) {
144
188
  throw Error(this.name + " pool has " + this.underlyingCoinAddresses.length + " coins (amounts provided for " + amounts.length + ")");
145
189
  }
146
- _b = (_a = Object).values;
190
+ _c = (_a = Object).values;
147
191
  return [4 /*yield*/, this.underlyingCoinBalances()];
148
192
  case 1:
149
- balances = _b.apply(_a, [_c.sent()]);
193
+ balances = _c.apply(_a, [_d.sent()]);
150
194
  for (i = 0; i < balances.length; i++) {
151
195
  if (Number(balances[i]) < Number(amounts[i])) {
152
196
  throw Error("Not enough " + this.underlyingCoins[i] + ". Actual: " + balances[i] + ", required: " + amounts[i]);
@@ -154,7 +198,7 @@ var Pool = /** @class */ (function () {
154
198
  }
155
199
  return [4 /*yield*/, (0, utils_1.hasAllowance)(this.underlyingCoinAddresses, amounts, curve_1.curve.signerAddress, this.zap || this.swap)];
156
200
  case 2:
157
- if (!(_c.sent())) {
201
+ if (!(_d.sent())) {
158
202
  throw Error("Token allowance is needed to estimate gas");
159
203
  }
160
204
  _amounts = amounts.map(function (amount, i) {
@@ -162,19 +206,60 @@ var Pool = /** @class */ (function () {
162
206
  });
163
207
  if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) return [3 /*break*/, 4];
164
208
  return [4 /*yield*/, this._addLiquidityZap(_amounts, true)];
165
- case 3: return [2 /*return*/, _c.sent()];
209
+ case 3: return [2 /*return*/, _d.sent()];
166
210
  case 4:
167
211
  if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 6];
168
212
  return [4 /*yield*/, this._addLiquidity(_amounts, true, true)];
169
- case 5: return [2 /*return*/, _c.sent()];
213
+ case 5: return [2 /*return*/, _d.sent()];
170
214
  case 6:
171
215
  if (!this.isMeta) return [3 /*break*/, 8];
172
216
  return [4 /*yield*/, this._addLiquidityMetaZap(_amounts, true)];
173
- case 7: return [2 /*return*/, _c.sent()];
217
+ case 7: return [2 /*return*/, _d.sent()];
174
218
  case 8: return [4 /*yield*/, this._addLiquiditySwap(_amounts, true)];
175
219
  case 9:
176
220
  // Plain pools
177
- return [2 /*return*/, _c.sent()];
221
+ return [2 /*return*/, _d.sent()];
222
+ }
223
+ });
224
+ }); };
225
+ this.balancedAmounts = function () { return __awaiter(_this, void 0, void 0, function () {
226
+ var swapContract, contractCalls, _poolWrappedBalances, _poolUnderlyingBalances, _poolMetaCoinBalance, _poolUnderlyingBalance, basePool, _basePoolExpectedAmounts, _rates_1, poolBalances, walletBalances, _a, _c;
227
+ var _this = this;
228
+ return __generator(this, function (_d) {
229
+ switch (_d.label) {
230
+ case 0:
231
+ swapContract = curve_1.curve.contracts[this.swap].multicallContract;
232
+ contractCalls = this.coins.map(function (_, i) { return swapContract.balances(i); });
233
+ return [4 /*yield*/, curve_1.curve.multicallProvider.all(contractCalls)];
234
+ case 1:
235
+ _poolWrappedBalances = (_d.sent());
236
+ _poolUnderlyingBalances = [];
237
+ if (!this.isMeta) return [3 /*break*/, 3];
238
+ _poolWrappedBalances.unshift(_poolWrappedBalances.pop());
239
+ _poolMetaCoinBalance = _poolWrappedBalances[0], _poolUnderlyingBalance = _poolWrappedBalances.slice(1);
240
+ basePool = new Pool(this.basePool);
241
+ return [4 /*yield*/, basePool._calcExpectedAmounts(_poolMetaCoinBalance)];
242
+ case 2:
243
+ _basePoolExpectedAmounts = _d.sent();
244
+ _poolUnderlyingBalances = __spreadArray(__spreadArray([], _poolUnderlyingBalance, true), _basePoolExpectedAmounts, true);
245
+ return [3 /*break*/, 6];
246
+ case 3:
247
+ if (!['compound', 'usdt', 'y', 'busd', 'pax', 'aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 5];
248
+ return [4 /*yield*/, this._getRates()];
249
+ case 4:
250
+ _rates_1 = _d.sent();
251
+ _poolUnderlyingBalances = _poolWrappedBalances.map(function (_b, i) { return _b.mul(_rates_1[i]).div(ethers_1.ethers.BigNumber.from(10).pow(18)); });
252
+ return [3 /*break*/, 6];
253
+ case 5:
254
+ _poolUnderlyingBalances = _poolWrappedBalances;
255
+ _d.label = 6;
256
+ case 6:
257
+ poolBalances = _poolUnderlyingBalances.map(function (_b, i) { return Number(ethers_1.ethers.utils.formatUnits(_b, _this.underlyingDecimals[i])); });
258
+ _c = (_a = Object).values;
259
+ return [4 /*yield*/, this.underlyingCoinBalances()];
260
+ case 7:
261
+ walletBalances = _c.apply(_a, [_d.sent()]).map(Number);
262
+ return [2 /*return*/, this._balancedAmounts(poolBalances, walletBalances)];
178
263
  }
179
264
  });
180
265
  }); };
@@ -211,6 +296,25 @@ var Pool = /** @class */ (function () {
211
296
  }
212
297
  });
213
298
  }); };
299
+ this.balancedWrappedAmounts = function (address) { return __awaiter(_this, void 0, void 0, function () {
300
+ var swapContract, contractCalls, poolBalances, walletBalances, _a, _c;
301
+ var _this = this;
302
+ return __generator(this, function (_d) {
303
+ switch (_d.label) {
304
+ case 0:
305
+ swapContract = curve_1.curve.contracts[this.swap].multicallContract;
306
+ contractCalls = this.coins.map(function (_, i) { return swapContract.balances(i); });
307
+ return [4 /*yield*/, curve_1.curve.multicallProvider.all(contractCalls)];
308
+ case 1:
309
+ poolBalances = (_d.sent()).map(function (_b, i) { return Number(ethers_1.ethers.utils.formatUnits(_b, _this.decimals[i])); });
310
+ _c = (_a = Object).values;
311
+ return [4 /*yield*/, this.coinBalances(address ? [address] : [])];
312
+ case 2:
313
+ walletBalances = _c.apply(_a, [_d.sent()]).map(Number);
314
+ return [2 /*return*/, this._balancedAmounts(poolBalances, walletBalances)];
315
+ }
316
+ });
317
+ }); };
214
318
  this.addLiquidityWrappedExpected = function (amounts) { return __awaiter(_this, void 0, void 0, function () {
215
319
  return __generator(this, function (_a) {
216
320
  switch (_a.label) {
@@ -219,27 +323,51 @@ var Pool = /** @class */ (function () {
219
323
  }
220
324
  });
221
325
  }); };
326
+ this.addLiquidityWrappedIsApproved = function (amounts) { return __awaiter(_this, void 0, void 0, function () {
327
+ return __generator(this, function (_a) {
328
+ switch (_a.label) {
329
+ case 0: return [4 /*yield*/, (0, utils_1.hasAllowance)(this.coinAddresses, amounts, curve_1.curve.signerAddress, this.swap)];
330
+ case 1: return [2 /*return*/, _a.sent()];
331
+ }
332
+ });
333
+ }); };
334
+ this.addLiquidityWrappedApproveEstimateGas = function (amounts) { return __awaiter(_this, void 0, void 0, function () {
335
+ return __generator(this, function (_a) {
336
+ switch (_a.label) {
337
+ case 0: return [4 /*yield*/, (0, utils_1.ensureAllowanceEstimateGas)(this.coinAddresses, amounts, this.swap)];
338
+ case 1: return [2 /*return*/, _a.sent()];
339
+ }
340
+ });
341
+ }); };
342
+ this.addLiquidityWrappedApprove = function (amounts) { return __awaiter(_this, void 0, void 0, function () {
343
+ return __generator(this, function (_a) {
344
+ switch (_a.label) {
345
+ case 0: return [4 /*yield*/, (0, utils_1.ensureAllowance)(this.coinAddresses, amounts, this.swap)];
346
+ case 1: return [2 /*return*/, _a.sent()];
347
+ }
348
+ });
349
+ }); };
222
350
  this.addLiquidityWrappedEstimateGas = function (amounts) { return __awaiter(_this, void 0, void 0, function () {
223
- var balances, _a, _b, i, _amounts;
351
+ var balances, _a, _c, i, _amounts;
224
352
  var _this = this;
225
- return __generator(this, function (_c) {
226
- switch (_c.label) {
353
+ return __generator(this, function (_d) {
354
+ switch (_d.label) {
227
355
  case 0:
228
356
  if (amounts.length !== this.coinAddresses.length) {
229
357
  throw Error(this.name + " pool has " + this.coinAddresses.length + " coins (amounts provided for " + amounts.length + ")");
230
358
  }
231
- _b = (_a = Object).values;
359
+ _c = (_a = Object).values;
232
360
  return [4 /*yield*/, this.coinBalances()];
233
361
  case 1:
234
- balances = _b.apply(_a, [_c.sent()]);
362
+ balances = _c.apply(_a, [_d.sent()]);
235
363
  for (i = 0; i < balances.length; i++) {
236
364
  if (Number(balances[i]) < Number(amounts[i])) {
237
365
  throw Error("Not enough " + this.coins[i] + ". Actual: " + balances[i] + ", required: " + amounts[i]);
238
366
  }
239
367
  }
240
- return [4 /*yield*/, (0, utils_1.hasAllowance)(this.coinAddresses, amounts, curve_1.curve.signerAddress, this.zap || this.swap)];
368
+ return [4 /*yield*/, (0, utils_1.hasAllowance)(this.coinAddresses, amounts, curve_1.curve.signerAddress, this.swap)];
241
369
  case 2:
242
- if (!(_c.sent())) {
370
+ if (!(_d.sent())) {
243
371
  throw Error("Token allowance is needed to estimate gas");
244
372
  }
245
373
  _amounts = amounts.map(function (amount, i) {
@@ -247,11 +375,11 @@ var Pool = /** @class */ (function () {
247
375
  });
248
376
  if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 4];
249
377
  return [4 /*yield*/, this._addLiquidity(_amounts, false, true)];
250
- case 3: return [2 /*return*/, _c.sent()];
378
+ case 3: return [2 /*return*/, _d.sent()];
251
379
  case 4: return [4 /*yield*/, this._addLiquiditySwap(_amounts, true)];
252
380
  case 5:
253
381
  // Lending pools with zap and metapools
254
- return [2 /*return*/, _c.sent()];
382
+ return [2 /*return*/, _d.sent()];
255
383
  }
256
384
  });
257
385
  }); };
@@ -306,13 +434,46 @@ var Pool = /** @class */ (function () {
306
434
  }
307
435
  });
308
436
  }); };
437
+ this.removeLiquidityIsApproved = function (lpTokenAmount) { return __awaiter(_this, void 0, void 0, function () {
438
+ return __generator(this, function (_a) {
439
+ switch (_a.label) {
440
+ case 0:
441
+ if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name) && !this.isMeta)
442
+ return [2 /*return*/, true];
443
+ return [4 /*yield*/, (0, utils_1.hasAllowance)([this.lpToken], [lpTokenAmount], curve_1.curve.signerAddress, this.zap)];
444
+ case 1: return [2 /*return*/, _a.sent()];
445
+ }
446
+ });
447
+ }); };
448
+ this.removeLiquidityApproveEstimateGas = function (lpTokenAmount) { return __awaiter(_this, void 0, void 0, function () {
449
+ return __generator(this, function (_a) {
450
+ switch (_a.label) {
451
+ case 0:
452
+ if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name) && !this.isMeta)
453
+ return [2 /*return*/, 0];
454
+ return [4 /*yield*/, (0, utils_1.ensureAllowanceEstimateGas)([this.lpToken], [lpTokenAmount], this.zap)];
455
+ case 1: return [2 /*return*/, _a.sent()];
456
+ }
457
+ });
458
+ }); };
459
+ this.removeLiquidityApprove = function (lpTokenAmount) { return __awaiter(_this, void 0, void 0, function () {
460
+ return __generator(this, function (_a) {
461
+ switch (_a.label) {
462
+ case 0:
463
+ if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name) && !this.isMeta)
464
+ return [2 /*return*/, []];
465
+ return [4 /*yield*/, (0, utils_1.ensureAllowance)([this.lpToken], [lpTokenAmount], this.zap)];
466
+ case 1: return [2 /*return*/, _a.sent()];
467
+ }
468
+ });
469
+ }); };
309
470
  this.removeLiquidityEstimateGas = function (lpTokenAmount) { return __awaiter(_this, void 0, void 0, function () {
310
471
  var lpTokenBalance, _a, _lpTokenAmount;
311
- return __generator(this, function (_b) {
312
- switch (_b.label) {
472
+ return __generator(this, function (_c) {
473
+ switch (_c.label) {
313
474
  case 0: return [4 /*yield*/, this.lpTokenBalances()];
314
475
  case 1:
315
- lpTokenBalance = (_b.sent())['lpToken'];
476
+ lpTokenBalance = (_c.sent())['lpToken'];
316
477
  if (Number(lpTokenBalance) < Number(lpTokenAmount)) {
317
478
  throw Error("Not enough LP tokens. Actual: " + lpTokenBalance + ", required: " + lpTokenAmount);
318
479
  }
@@ -320,8 +481,8 @@ var Pool = /** @class */ (function () {
320
481
  if (!_a) return [3 /*break*/, 3];
321
482
  return [4 /*yield*/, (0, utils_1.hasAllowance)([this.lpToken], [lpTokenAmount], curve_1.curve.signerAddress, this.zap)];
322
483
  case 2:
323
- _a = !(_b.sent());
324
- _b.label = 3;
484
+ _a = !(_c.sent());
485
+ _c.label = 3;
325
486
  case 3:
326
487
  if (_a) {
327
488
  throw Error("Token allowance is needed to estimate gas");
@@ -329,17 +490,17 @@ var Pool = /** @class */ (function () {
329
490
  _lpTokenAmount = ethers_1.ethers.utils.parseUnits(lpTokenAmount);
330
491
  if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) return [3 /*break*/, 5];
331
492
  return [4 /*yield*/, this._removeLiquidityZap(_lpTokenAmount, true)];
332
- case 4: return [2 /*return*/, _b.sent()];
493
+ case 4: return [2 /*return*/, _c.sent()];
333
494
  case 5:
334
495
  if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 7];
335
496
  return [4 /*yield*/, this._removeLiquidity(_lpTokenAmount, true, true)];
336
- case 6: return [2 /*return*/, _b.sent()];
497
+ case 6: return [2 /*return*/, _c.sent()];
337
498
  case 7:
338
499
  if (!this.isMeta) return [3 /*break*/, 9];
339
500
  return [4 /*yield*/, this._removeLiquidityMetaZap(_lpTokenAmount, true)];
340
- case 8: return [2 /*return*/, _b.sent()];
501
+ case 8: return [2 /*return*/, _c.sent()];
341
502
  case 9: return [4 /*yield*/, this._removeLiquiditySwap(_lpTokenAmount, true)];
342
- case 10: return [2 /*return*/, _b.sent()];
503
+ case 10: return [2 /*return*/, _c.sent()];
343
504
  }
344
505
  });
345
506
  }); };
@@ -427,17 +588,89 @@ var Pool = /** @class */ (function () {
427
588
  }
428
589
  });
429
590
  }); };
591
+ this.removeLiquidityImbalanceIsApproved = function (amounts) { return __awaiter(_this, void 0, void 0, function () {
592
+ var _amounts, _maxBurnAmount, _maxBurnAmount;
593
+ var _this = this;
594
+ return __generator(this, function (_a) {
595
+ switch (_a.label) {
596
+ case 0:
597
+ _amounts = amounts.map(function (amount, i) { return ethers_1.ethers.utils.parseUnits(amount, _this.underlyingDecimals[i]); });
598
+ if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) return [3 /*break*/, 3];
599
+ return [4 /*yield*/, this._calcLpTokenAmountWithUnderlying(_amounts, false)];
600
+ case 1:
601
+ _maxBurnAmount = (_a.sent()).mul(101).div(100);
602
+ return [4 /*yield*/, (0, utils_1.hasAllowance)([this.lpToken], [ethers_1.ethers.utils.formatUnits(_maxBurnAmount, 18)], curve_1.curve.signerAddress, this.zap)];
603
+ case 2: return [2 /*return*/, _a.sent()];
604
+ case 3:
605
+ if (!this.isMeta) return [3 /*break*/, 6];
606
+ return [4 /*yield*/, this._calcLpTokenAmountZap(_amounts, false)];
607
+ case 4:
608
+ _maxBurnAmount = (_a.sent()).mul(101).div(100);
609
+ return [4 /*yield*/, (0, utils_1.hasAllowance)([this.lpToken], [ethers_1.ethers.utils.formatUnits(_maxBurnAmount, 18)], curve_1.curve.signerAddress, this.zap)];
610
+ case 5: return [2 /*return*/, _a.sent()];
611
+ case 6: return [2 /*return*/, true];
612
+ }
613
+ });
614
+ }); };
615
+ this.removeLiquidityImbalanceApproveEstimateGas = function (amounts) { return __awaiter(_this, void 0, void 0, function () {
616
+ var _amounts, _maxBurnAmount, _maxBurnAmount;
617
+ var _this = this;
618
+ return __generator(this, function (_a) {
619
+ switch (_a.label) {
620
+ case 0:
621
+ _amounts = amounts.map(function (amount, i) { return ethers_1.ethers.utils.parseUnits(amount, _this.underlyingDecimals[i]); });
622
+ if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) return [3 /*break*/, 3];
623
+ return [4 /*yield*/, this._calcLpTokenAmountWithUnderlying(_amounts, false)];
624
+ case 1:
625
+ _maxBurnAmount = (_a.sent()).mul(101).div(100);
626
+ return [4 /*yield*/, (0, utils_1.ensureAllowanceEstimateGas)([this.lpToken], [ethers_1.ethers.utils.formatUnits(_maxBurnAmount, 18)], this.zap)];
627
+ case 2: return [2 /*return*/, _a.sent()];
628
+ case 3:
629
+ if (!this.isMeta) return [3 /*break*/, 6];
630
+ return [4 /*yield*/, this._calcLpTokenAmountZap(_amounts, false)];
631
+ case 4:
632
+ _maxBurnAmount = (_a.sent()).mul(101).div(100);
633
+ return [4 /*yield*/, (0, utils_1.ensureAllowanceEstimateGas)([this.lpToken], [ethers_1.ethers.utils.formatUnits(_maxBurnAmount, 18)], this.zap)];
634
+ case 5: return [2 /*return*/, _a.sent()];
635
+ case 6: return [2 /*return*/, 0];
636
+ }
637
+ });
638
+ }); };
639
+ this.removeLiquidityImbalanceApprove = function (amounts) { return __awaiter(_this, void 0, void 0, function () {
640
+ var _amounts, _maxBurnAmount, _maxBurnAmount;
641
+ var _this = this;
642
+ return __generator(this, function (_a) {
643
+ switch (_a.label) {
644
+ case 0:
645
+ _amounts = amounts.map(function (amount, i) { return ethers_1.ethers.utils.parseUnits(amount, _this.underlyingDecimals[i]); });
646
+ if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) return [3 /*break*/, 3];
647
+ return [4 /*yield*/, this._calcLpTokenAmountWithUnderlying(_amounts, false)];
648
+ case 1:
649
+ _maxBurnAmount = (_a.sent()).mul(101).div(100);
650
+ return [4 /*yield*/, (0, utils_1.ensureAllowance)([this.lpToken], [ethers_1.ethers.utils.formatUnits(_maxBurnAmount, 18)], this.zap)];
651
+ case 2: return [2 /*return*/, _a.sent()];
652
+ case 3:
653
+ if (!this.isMeta) return [3 /*break*/, 6];
654
+ return [4 /*yield*/, this._calcLpTokenAmountZap(_amounts, false)];
655
+ case 4:
656
+ _maxBurnAmount = (_a.sent()).mul(101).div(100);
657
+ return [4 /*yield*/, (0, utils_1.ensureAllowance)([this.lpToken], [ethers_1.ethers.utils.formatUnits(_maxBurnAmount, 18)], this.zap)];
658
+ case 5: return [2 /*return*/, _a.sent()];
659
+ case 6: return [2 /*return*/, []];
660
+ }
661
+ });
662
+ }); };
430
663
  this.removeLiquidityImbalanceEstimateGas = function (amounts) { return __awaiter(_this, void 0, void 0, function () {
431
664
  var lpTokenAmount, lpTokenBalance, _a, _amounts;
432
665
  var _this = this;
433
- return __generator(this, function (_b) {
434
- switch (_b.label) {
666
+ return __generator(this, function (_c) {
667
+ switch (_c.label) {
435
668
  case 0: return [4 /*yield*/, this.removeLiquidityImbalanceExpected(amounts)];
436
669
  case 1:
437
- lpTokenAmount = _b.sent();
670
+ lpTokenAmount = _c.sent();
438
671
  return [4 /*yield*/, this.lpTokenBalances()];
439
672
  case 2:
440
- lpTokenBalance = (_b.sent())['lpToken'];
673
+ lpTokenBalance = (_c.sent())['lpToken'];
441
674
  if (Number(lpTokenBalance) < Number(lpTokenAmount)) {
442
675
  throw Error("Not enough LP tokens. Actual: " + lpTokenBalance + ", required: " + lpTokenAmount);
443
676
  }
@@ -445,8 +678,8 @@ var Pool = /** @class */ (function () {
445
678
  if (!_a) return [3 /*break*/, 4];
446
679
  return [4 /*yield*/, (0, utils_1.hasAllowance)([this.lpToken], [lpTokenAmount], curve_1.curve.signerAddress, this.zap)];
447
680
  case 3:
448
- _a = !(_b.sent());
449
- _b.label = 4;
681
+ _a = !(_c.sent());
682
+ _c.label = 4;
450
683
  case 4:
451
684
  if (_a) {
452
685
  throw Error("Token allowance is needed to estimate gas");
@@ -454,17 +687,17 @@ var Pool = /** @class */ (function () {
454
687
  _amounts = amounts.map(function (amount, i) { return ethers_1.ethers.utils.parseUnits(amount, _this.underlyingDecimals[i]); });
455
688
  if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) return [3 /*break*/, 6];
456
689
  return [4 /*yield*/, this._removeLiquidityImbalanceZap(_amounts, true)];
457
- case 5: return [2 /*return*/, _b.sent()];
690
+ case 5: return [2 /*return*/, _c.sent()];
458
691
  case 6:
459
692
  if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 8];
460
693
  return [4 /*yield*/, this._removeLiquidityImbalance(_amounts, true, true)];
461
- case 7: return [2 /*return*/, _b.sent()];
694
+ case 7: return [2 /*return*/, _c.sent()];
462
695
  case 8:
463
696
  if (!this.isMeta) return [3 /*break*/, 10];
464
697
  return [4 /*yield*/, this._removeLiquidityImbalanceMetaZap(_amounts, true)];
465
- case 9: return [2 /*return*/, _b.sent()];
698
+ case 9: return [2 /*return*/, _c.sent()];
466
699
  case 10: return [4 /*yield*/, this._removeLiquidityImbalanceSwap(_amounts, true)];
467
- case 11: return [2 /*return*/, _b.sent()];
700
+ case 11: return [2 /*return*/, _c.sent()];
468
701
  }
469
702
  });
470
703
  }); };
@@ -572,13 +805,46 @@ var Pool = /** @class */ (function () {
572
805
  }
573
806
  });
574
807
  }); };
808
+ this.removeLiquidityOneCoinIsApproved = function (lpTokenAmount) { return __awaiter(_this, void 0, void 0, function () {
809
+ return __generator(this, function (_a) {
810
+ switch (_a.label) {
811
+ case 0:
812
+ if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name) && !(this.name === 'susd') && !this.isMeta)
813
+ return [2 /*return*/, true];
814
+ return [4 /*yield*/, (0, utils_1.hasAllowance)([this.lpToken], [lpTokenAmount], curve_1.curve.signerAddress, this.zap)];
815
+ case 1: return [2 /*return*/, _a.sent()];
816
+ }
817
+ });
818
+ }); };
819
+ this.removeLiquidityOneCoinApproveEstimateGas = function (lpTokenAmount) { return __awaiter(_this, void 0, void 0, function () {
820
+ return __generator(this, function (_a) {
821
+ switch (_a.label) {
822
+ case 0:
823
+ if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name) && !(this.name === 'susd') && !this.isMeta)
824
+ return [2 /*return*/, 0];
825
+ return [4 /*yield*/, (0, utils_1.ensureAllowanceEstimateGas)([this.lpToken], [lpTokenAmount], this.zap)];
826
+ case 1: return [2 /*return*/, _a.sent()];
827
+ }
828
+ });
829
+ }); };
830
+ this.removeLiquidityOneCoinApprove = function (lpTokenAmount) { return __awaiter(_this, void 0, void 0, function () {
831
+ return __generator(this, function (_a) {
832
+ switch (_a.label) {
833
+ case 0:
834
+ if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name) && !(this.name === 'susd') && !this.isMeta)
835
+ return [2 /*return*/, []];
836
+ return [4 /*yield*/, (0, utils_1.ensureAllowance)([this.lpToken], [lpTokenAmount], this.zap)];
837
+ case 1: return [2 /*return*/, _a.sent()];
838
+ }
839
+ });
840
+ }); };
575
841
  this.removeLiquidityOneCoinEstimateGas = function (lpTokenAmount, coin) { return __awaiter(_this, void 0, void 0, function () {
576
842
  var lpTokenBalance, _a, i, _lpTokenAmount;
577
- return __generator(this, function (_b) {
578
- switch (_b.label) {
843
+ return __generator(this, function (_c) {
844
+ switch (_c.label) {
579
845
  case 0: return [4 /*yield*/, this.lpTokenBalances()];
580
846
  case 1:
581
- lpTokenBalance = (_b.sent())['lpToken'];
847
+ lpTokenBalance = (_c.sent())['lpToken'];
582
848
  if (Number(lpTokenBalance) < Number(lpTokenAmount)) {
583
849
  throw Error("Not enough LP tokens. Actual: " + lpTokenBalance + ", required: " + lpTokenAmount);
584
850
  }
@@ -586,8 +852,8 @@ var Pool = /** @class */ (function () {
586
852
  if (!_a) return [3 /*break*/, 3];
587
853
  return [4 /*yield*/, (0, utils_1.hasAllowance)([this.lpToken], [lpTokenAmount], curve_1.curve.signerAddress, this.zap)];
588
854
  case 2:
589
- _a = !(_b.sent());
590
- _b.label = 3;
855
+ _a = !(_c.sent());
856
+ _c.label = 3;
591
857
  case 3:
592
858
  if (_a) {
593
859
  throw Error("Token allowance is needed to estimate gas");
@@ -596,15 +862,15 @@ var Pool = /** @class */ (function () {
596
862
  _lpTokenAmount = ethers_1.ethers.utils.parseUnits(lpTokenAmount);
597
863
  if (!(['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name) || this.name === 'susd' || this.isMeta)) return [3 /*break*/, 5];
598
864
  return [4 /*yield*/, this._removeLiquidityOneCoinZap(_lpTokenAmount, i, true)];
599
- case 4: return [2 /*return*/, _b.sent()];
865
+ case 4: return [2 /*return*/, _c.sent()];
600
866
  case 5:
601
867
  if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 7];
602
868
  return [4 /*yield*/, this._removeLiquidityOneCoin(_lpTokenAmount, i, true, true)];
603
- case 6: return [2 /*return*/, _b.sent()];
869
+ case 6: return [2 /*return*/, _c.sent()];
604
870
  case 7: return [4 /*yield*/, this._removeLiquidityOneCoinSwap(_lpTokenAmount, i, true)];
605
871
  case 8:
606
872
  // Plain pools
607
- return [2 /*return*/, _b.sent()];
873
+ return [2 /*return*/, _c.sent()];
608
874
  }
609
875
  });
610
876
  }); };
@@ -696,6 +962,30 @@ var Pool = /** @class */ (function () {
696
962
  }
697
963
  });
698
964
  }); };
965
+ this.gaugeDepositIsApproved = function (lpTokenAmount) { return __awaiter(_this, void 0, void 0, function () {
966
+ return __generator(this, function (_a) {
967
+ switch (_a.label) {
968
+ case 0: return [4 /*yield*/, (0, utils_1.hasAllowance)([this.lpToken], [lpTokenAmount], curve_1.curve.signerAddress, this.gauge)];
969
+ case 1: return [2 /*return*/, _a.sent()];
970
+ }
971
+ });
972
+ }); };
973
+ this.gaugeDepositApproveEstimateGas = function (lpTokenAmount) { return __awaiter(_this, void 0, void 0, function () {
974
+ return __generator(this, function (_a) {
975
+ switch (_a.label) {
976
+ case 0: return [4 /*yield*/, (0, utils_1.ensureAllowanceEstimateGas)([this.lpToken], [lpTokenAmount], this.gauge)];
977
+ case 1: return [2 /*return*/, _a.sent()];
978
+ }
979
+ });
980
+ }); };
981
+ this.gaugeDepositApprove = function (lpTokenAmount) { return __awaiter(_this, void 0, void 0, function () {
982
+ return __generator(this, function (_a) {
983
+ switch (_a.label) {
984
+ case 0: return [4 /*yield*/, (0, utils_1.ensureAllowance)([this.lpToken], [lpTokenAmount], this.gauge)];
985
+ case 1: return [2 /*return*/, _a.sent()];
986
+ }
987
+ });
988
+ }); };
699
989
  this.gaugeDepositEstimateGas = function (lpTokenAmount) { return __awaiter(_this, void 0, void 0, function () {
700
990
  var _lpTokenAmount;
701
991
  return __generator(this, function (_a) {
@@ -749,10 +1039,10 @@ var Pool = /** @class */ (function () {
749
1039
  addresses[_a] = arguments[_a];
750
1040
  }
751
1041
  return __awaiter(_this, void 0, void 0, function () {
752
- return __generator(this, function (_b) {
753
- switch (_b.label) {
1042
+ return __generator(this, function (_c) {
1043
+ switch (_c.label) {
754
1044
  case 0: return [4 /*yield*/, this._balances.apply(this, __spreadArray([__spreadArray(__spreadArray(['lpToken', 'gauge'], this.underlyingCoins, true), this.coins, true), __spreadArray(__spreadArray([this.lpToken, this.gauge], this.underlyingCoinAddresses, true), this.coinAddresses, true)], addresses, false))];
755
- case 1: return [2 /*return*/, _b.sent()];
1045
+ case 1: return [2 /*return*/, _c.sent()];
756
1046
  }
757
1047
  });
758
1048
  });
@@ -763,10 +1053,10 @@ var Pool = /** @class */ (function () {
763
1053
  addresses[_a] = arguments[_a];
764
1054
  }
765
1055
  return __awaiter(_this, void 0, void 0, function () {
766
- return __generator(this, function (_b) {
767
- switch (_b.label) {
1056
+ return __generator(this, function (_c) {
1057
+ switch (_c.label) {
768
1058
  case 0: return [4 /*yield*/, this._balances.apply(this, __spreadArray([['lpToken', 'gauge'], [this.lpToken, this.gauge]], addresses, false))];
769
- case 1: return [2 /*return*/, _b.sent()];
1059
+ case 1: return [2 /*return*/, _c.sent()];
770
1060
  }
771
1061
  });
772
1062
  });
@@ -777,10 +1067,10 @@ var Pool = /** @class */ (function () {
777
1067
  addresses[_a] = arguments[_a];
778
1068
  }
779
1069
  return __awaiter(_this, void 0, void 0, function () {
780
- return __generator(this, function (_b) {
781
- switch (_b.label) {
1070
+ return __generator(this, function (_c) {
1071
+ switch (_c.label) {
782
1072
  case 0: return [4 /*yield*/, this._balances.apply(this, __spreadArray([this.underlyingCoins, this.underlyingCoinAddresses], addresses, false))];
783
- case 1: return [2 /*return*/, _b.sent()];
1073
+ case 1: return [2 /*return*/, _c.sent()];
784
1074
  }
785
1075
  });
786
1076
  });
@@ -791,10 +1081,10 @@ var Pool = /** @class */ (function () {
791
1081
  addresses[_a] = arguments[_a];
792
1082
  }
793
1083
  return __awaiter(_this, void 0, void 0, function () {
794
- return __generator(this, function (_b) {
795
- switch (_b.label) {
1084
+ return __generator(this, function (_c) {
1085
+ switch (_c.label) {
796
1086
  case 0: return [4 /*yield*/, this._balances.apply(this, __spreadArray([this.coins, this.coinAddresses], addresses, false))];
797
- case 1: return [2 /*return*/, _b.sent()];
1087
+ case 1: return [2 /*return*/, _c.sent()];
798
1088
  }
799
1089
  });
800
1090
  });
@@ -805,10 +1095,10 @@ var Pool = /** @class */ (function () {
805
1095
  addresses[_a] = arguments[_a];
806
1096
  }
807
1097
  return __awaiter(_this, void 0, void 0, function () {
808
- return __generator(this, function (_b) {
809
- switch (_b.label) {
1098
+ return __generator(this, function (_c) {
1099
+ switch (_c.label) {
810
1100
  case 0: return [4 /*yield*/, this._balances.apply(this, __spreadArray([__spreadArray(__spreadArray([], this.underlyingCoins, true), this.coins, true), __spreadArray(__spreadArray([], this.underlyingCoinAddresses, true), this.coinAddresses, true)], addresses, false))];
811
- case 1: return [2 /*return*/, _b.sent()];
1101
+ case 1: return [2 /*return*/, _c.sent()];
812
1102
  }
813
1103
  });
814
1104
  });
@@ -828,40 +1118,73 @@ var Pool = /** @class */ (function () {
828
1118
  }
829
1119
  });
830
1120
  }); };
1121
+ this.exchangeIsApproved = function (inputCoin, amount) { return __awaiter(_this, void 0, void 0, function () {
1122
+ var i;
1123
+ return __generator(this, function (_a) {
1124
+ switch (_a.label) {
1125
+ case 0:
1126
+ i = this._getCoinIdx(inputCoin);
1127
+ return [4 /*yield*/, (0, utils_1.hasAllowance)([this.underlyingCoinAddresses[i]], [amount], curve_1.curve.signerAddress, this.swap)];
1128
+ case 1: return [2 /*return*/, _a.sent()];
1129
+ }
1130
+ });
1131
+ }); };
1132
+ this.exchangeApproveEstimateGas = function (inputCoin, amount) { return __awaiter(_this, void 0, void 0, function () {
1133
+ var i;
1134
+ return __generator(this, function (_a) {
1135
+ switch (_a.label) {
1136
+ case 0:
1137
+ i = this._getCoinIdx(inputCoin);
1138
+ return [4 /*yield*/, (0, utils_1.ensureAllowanceEstimateGas)([this.underlyingCoinAddresses[i]], [amount], this.swap)];
1139
+ case 1: return [2 /*return*/, _a.sent()];
1140
+ }
1141
+ });
1142
+ }); };
1143
+ this.exchangeApprove = function (inputCoin, amount) { return __awaiter(_this, void 0, void 0, function () {
1144
+ var i;
1145
+ return __generator(this, function (_a) {
1146
+ switch (_a.label) {
1147
+ case 0:
1148
+ i = this._getCoinIdx(inputCoin);
1149
+ return [4 /*yield*/, (0, utils_1.ensureAllowance)([this.underlyingCoinAddresses[i]], [amount], this.swap)];
1150
+ case 1: return [2 /*return*/, _a.sent()];
1151
+ }
1152
+ });
1153
+ }); };
831
1154
  this.exchangeEstimateGas = function (inputCoin, outputCoin, amount, maxSlippage) {
832
1155
  if (maxSlippage === void 0) { maxSlippage = 0.01; }
833
1156
  return __awaiter(_this, void 0, void 0, function () {
834
- var i, j, inputCoinBalance, _a, _b, _amount, _expected, _minRecvAmount, contract, exchangeMethod, value;
835
- return __generator(this, function (_c) {
836
- switch (_c.label) {
1157
+ var i, j, inputCoinBalance, _a, _c, _amount, _expected, _minRecvAmount, contract, exchangeMethod, value;
1158
+ return __generator(this, function (_d) {
1159
+ switch (_d.label) {
837
1160
  case 0:
838
1161
  if (this.name === 'tricrypto2') {
839
1162
  throw Error("Use exchangeTricryptoEstimateGas method for tricrypto2 pool instead");
840
1163
  }
841
1164
  i = this._getCoinIdx(inputCoin);
842
1165
  j = this._getCoinIdx(outputCoin);
843
- _b = (_a = Object).values;
1166
+ _c = (_a = Object).values;
844
1167
  return [4 /*yield*/, this.underlyingCoinBalances()];
845
1168
  case 1:
846
- inputCoinBalance = _b.apply(_a, [_c.sent()])[i];
1169
+ inputCoinBalance = _c.apply(_a, [_d.sent()])[i];
847
1170
  if (Number(inputCoinBalance) < Number(amount)) {
848
1171
  throw Error("Not enough " + this.underlyingCoins[i] + ". Actual: " + inputCoinBalance + ", required: " + amount);
849
1172
  }
850
1173
  return [4 /*yield*/, (0, utils_1.hasAllowance)([this.underlyingCoinAddresses[i]], [amount], curve_1.curve.signerAddress, this.swap)];
851
1174
  case 2:
852
- if (!(_c.sent())) {
1175
+ if (!(_d.sent())) {
853
1176
  throw Error("Token allowance is needed to estimate gas");
854
1177
  }
855
1178
  _amount = ethers_1.ethers.utils.parseUnits(amount, this.underlyingDecimals[i]);
856
1179
  return [4 /*yield*/, this._getExchangeOutput(i, j, _amount)];
857
1180
  case 3:
858
- _expected = _c.sent();
1181
+ _expected = _d.sent();
859
1182
  _minRecvAmount = _expected.mul((1 - maxSlippage) * 100).div(100);
860
1183
  contract = curve_1.curve.contracts[this.swap].contract;
861
1184
  exchangeMethod = Object.prototype.hasOwnProperty.call(contract, 'exchange_underlying') ? 'exchange_underlying' : 'exchange';
862
1185
  value = (0, utils_1.isEth)(this.underlyingCoinAddresses[i]) ? _amount : ethers_1.ethers.BigNumber.from(0);
863
1186
  return [4 /*yield*/, contract.estimateGas[exchangeMethod](i, j, _amount, _minRecvAmount, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
864
- case 4: return [2 /*return*/, (_c.sent()).toNumber()];
1187
+ case 4: return [2 /*return*/, (_d.sent()).toNumber()];
865
1188
  }
866
1189
  });
867
1190
  });
@@ -901,40 +1224,88 @@ var Pool = /** @class */ (function () {
901
1224
  });
902
1225
  });
903
1226
  };
1227
+ this.exchangeTricryptoIsApproved = function (inputCoin, amount, useEth) {
1228
+ if (useEth === void 0) { useEth = false; }
1229
+ return __awaiter(_this, void 0, void 0, function () {
1230
+ var i;
1231
+ return __generator(this, function (_a) {
1232
+ switch (_a.label) {
1233
+ case 0:
1234
+ i = this._getCoinIdx(inputCoin);
1235
+ if (i === 2 && useEth)
1236
+ return [2 /*return*/, true];
1237
+ return [4 /*yield*/, (0, utils_1.hasAllowance)([this.underlyingCoinAddresses[i]], [amount], curve_1.curve.signerAddress, this.swap)];
1238
+ case 1: return [2 /*return*/, _a.sent()];
1239
+ }
1240
+ });
1241
+ });
1242
+ };
1243
+ this.exchangeTricryptoApproveEstimateGas = function (inputCoin, amount, useEth) {
1244
+ if (useEth === void 0) { useEth = false; }
1245
+ return __awaiter(_this, void 0, void 0, function () {
1246
+ var i;
1247
+ return __generator(this, function (_a) {
1248
+ switch (_a.label) {
1249
+ case 0:
1250
+ i = this._getCoinIdx(inputCoin);
1251
+ if (i === 2 && useEth)
1252
+ return [2 /*return*/, 0];
1253
+ return [4 /*yield*/, (0, utils_1.ensureAllowanceEstimateGas)([this.underlyingCoinAddresses[i]], [amount], this.swap)];
1254
+ case 1: return [2 /*return*/, _a.sent()];
1255
+ }
1256
+ });
1257
+ });
1258
+ };
1259
+ this.exchangeTricryptoApprove = function (inputCoin, amount, useEth) {
1260
+ if (useEth === void 0) { useEth = false; }
1261
+ return __awaiter(_this, void 0, void 0, function () {
1262
+ var i;
1263
+ return __generator(this, function (_a) {
1264
+ switch (_a.label) {
1265
+ case 0:
1266
+ i = this._getCoinIdx(inputCoin);
1267
+ if (i === 2 && useEth)
1268
+ return [2 /*return*/, []];
1269
+ return [4 /*yield*/, (0, utils_1.ensureAllowance)([this.underlyingCoinAddresses[i]], [amount], this.swap)];
1270
+ case 1: return [2 /*return*/, _a.sent()];
1271
+ }
1272
+ });
1273
+ });
1274
+ };
904
1275
  this.exchangeTricryptoEstimateGas = function (inputCoin, outputCoin, amount, maxSlippage, useEth) {
905
1276
  if (maxSlippage === void 0) { maxSlippage = 0.01; }
906
1277
  if (useEth === void 0) { useEth = false; }
907
1278
  return __awaiter(_this, void 0, void 0, function () {
908
- var i, j, inputCoinBalance, _a, _b, _amount, _expected, _minRecvAmount, contract, value;
909
- return __generator(this, function (_c) {
910
- switch (_c.label) {
1279
+ var i, j, inputCoinBalance, _a, _c, _amount, _expected, _minRecvAmount, contract, value;
1280
+ return __generator(this, function (_d) {
1281
+ switch (_d.label) {
911
1282
  case 0:
912
1283
  if (this.name !== 'tricrypto2') {
913
1284
  throw Error("This method is for only tricrypto2 pool");
914
1285
  }
915
1286
  i = this._getCoinIdx(inputCoin);
916
1287
  j = this._getCoinIdx(outputCoin);
917
- _b = (_a = Object).values;
1288
+ _c = (_a = Object).values;
918
1289
  return [4 /*yield*/, this.underlyingCoinBalances()];
919
1290
  case 1:
920
- inputCoinBalance = _b.apply(_a, [_c.sent()])[i];
1291
+ inputCoinBalance = _c.apply(_a, [_d.sent()])[i];
921
1292
  if (Number(inputCoinBalance) < Number(amount)) {
922
1293
  throw Error("Not enough " + this.underlyingCoins[i] + ". Actual: " + inputCoinBalance + ", required: " + amount);
923
1294
  }
924
1295
  return [4 /*yield*/, (0, utils_1.hasAllowance)([this.underlyingCoinAddresses[i]], [amount], curve_1.curve.signerAddress, this.swap)];
925
1296
  case 2:
926
- if (!(_c.sent())) {
1297
+ if (!(_d.sent())) {
927
1298
  throw Error("Token allowance is needed to estimate gas");
928
1299
  }
929
1300
  _amount = ethers_1.ethers.utils.parseUnits(amount, this.underlyingDecimals[i]);
930
1301
  return [4 /*yield*/, this._getExchangeOutput(i, j, _amount)];
931
1302
  case 3:
932
- _expected = _c.sent();
1303
+ _expected = _d.sent();
933
1304
  _minRecvAmount = _expected.mul((1 - maxSlippage) * 100).div(100);
934
1305
  contract = curve_1.curve.contracts[this.swap].contract;
935
1306
  value = useEth && i == 2 ? _amount : ethers_1.ethers.BigNumber.from(0);
936
1307
  return [4 /*yield*/, contract.estimateGas.exchange(i, j, _amount, _minRecvAmount, useEth, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
937
- case 4: return [2 /*return*/, (_c.sent()).toNumber()];
1308
+ case 4: return [2 /*return*/, (_d.sent()).toNumber()];
938
1309
  }
939
1310
  });
940
1311
  });
@@ -992,36 +1363,69 @@ var Pool = /** @class */ (function () {
992
1363
  }
993
1364
  });
994
1365
  }); };
1366
+ this.exchangeWrappedIsApproved = function (inputCoin, amount) { return __awaiter(_this, void 0, void 0, function () {
1367
+ var i;
1368
+ return __generator(this, function (_a) {
1369
+ switch (_a.label) {
1370
+ case 0:
1371
+ i = this._getCoinIdx(inputCoin);
1372
+ return [4 /*yield*/, (0, utils_1.hasAllowance)([this.coinAddresses[i]], [amount], curve_1.curve.signerAddress, this.swap)];
1373
+ case 1: return [2 /*return*/, _a.sent()];
1374
+ }
1375
+ });
1376
+ }); };
1377
+ this.exchangeWrappedApproveEstimateGas = function (inputCoin, amount) { return __awaiter(_this, void 0, void 0, function () {
1378
+ var i;
1379
+ return __generator(this, function (_a) {
1380
+ switch (_a.label) {
1381
+ case 0:
1382
+ i = this._getCoinIdx(inputCoin);
1383
+ return [4 /*yield*/, (0, utils_1.ensureAllowanceEstimateGas)([this.coinAddresses[i]], [amount], this.swap)];
1384
+ case 1: return [2 /*return*/, _a.sent()];
1385
+ }
1386
+ });
1387
+ }); };
1388
+ this.exchangeWrappedApprove = function (inputCoin, amount) { return __awaiter(_this, void 0, void 0, function () {
1389
+ var i;
1390
+ return __generator(this, function (_a) {
1391
+ switch (_a.label) {
1392
+ case 0:
1393
+ i = this._getCoinIdx(inputCoin);
1394
+ return [4 /*yield*/, (0, utils_1.ensureAllowance)([this.coinAddresses[i]], [amount], this.swap)];
1395
+ case 1: return [2 /*return*/, _a.sent()];
1396
+ }
1397
+ });
1398
+ }); };
995
1399
  this.exchangeWrappedEstimateGas = function (inputCoin, outputCoin, amount, maxSlippage) {
996
1400
  if (maxSlippage === void 0) { maxSlippage = 0.01; }
997
1401
  return __awaiter(_this, void 0, void 0, function () {
998
- var i, j, inputCoinBalance, _a, _b, _amount, _expected, _minRecvAmount, contract, value;
999
- return __generator(this, function (_c) {
1000
- switch (_c.label) {
1402
+ var i, j, inputCoinBalance, _a, _c, _amount, _expected, _minRecvAmount, contract, value;
1403
+ return __generator(this, function (_d) {
1404
+ switch (_d.label) {
1001
1405
  case 0:
1002
1406
  i = this._getCoinIdx(inputCoin, false);
1003
1407
  j = this._getCoinIdx(outputCoin, false);
1004
- _b = (_a = Object).values;
1408
+ _c = (_a = Object).values;
1005
1409
  return [4 /*yield*/, this.coinBalances()];
1006
1410
  case 1:
1007
- inputCoinBalance = _b.apply(_a, [_c.sent()])[i];
1411
+ inputCoinBalance = _c.apply(_a, [_d.sent()])[i];
1008
1412
  if (Number(inputCoinBalance) < Number(amount)) {
1009
1413
  throw Error("Not enough " + this.coins[i] + ". Actual: " + inputCoinBalance + ", required: " + amount);
1010
1414
  }
1011
1415
  return [4 /*yield*/, (0, utils_1.hasAllowance)([this.coinAddresses[i]], [amount], curve_1.curve.signerAddress, this.swap)];
1012
1416
  case 2:
1013
- if (!(_c.sent())) {
1417
+ if (!(_d.sent())) {
1014
1418
  throw Error("Token allowance is needed to estimate gas");
1015
1419
  }
1016
1420
  _amount = ethers_1.ethers.utils.parseUnits(amount, this.decimals[i]);
1017
1421
  return [4 /*yield*/, this._getExchangeOutputWrapped(i, j, _amount)];
1018
1422
  case 3:
1019
- _expected = _c.sent();
1423
+ _expected = _d.sent();
1020
1424
  _minRecvAmount = _expected.mul((1 - maxSlippage) * 100).div(100);
1021
1425
  contract = curve_1.curve.contracts[this.swap].contract;
1022
1426
  value = (0, utils_1.isEth)(this.coinAddresses[i]) ? _amount : ethers_1.ethers.BigNumber.from(0);
1023
1427
  return [4 /*yield*/, contract.estimateGas.exchange(i, j, _amount, _minRecvAmount, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
1024
- case 4: return [2 /*return*/, (_c.sent()).toNumber()];
1428
+ case 4: return [2 /*return*/, (_d.sent()).toNumber()];
1025
1429
  }
1026
1430
  });
1027
1431
  });
@@ -1063,9 +1467,9 @@ var Pool = /** @class */ (function () {
1063
1467
  addresses[_a] = arguments[_a];
1064
1468
  }
1065
1469
  return __awaiter(_this, void 0, void 0, function () {
1066
- var votingEscrowContract, gaugeContract, contractCalls, response, _b, veTotalSupply, gaugeTotalSupply, resultBN, result, _c, _d, entry;
1067
- return __generator(this, function (_e) {
1068
- switch (_e.label) {
1470
+ var votingEscrowContract, gaugeContract, contractCalls, response, _c, veTotalSupply, gaugeTotalSupply, resultBN, result, _d, _e, entry;
1471
+ return __generator(this, function (_f) {
1472
+ switch (_f.label) {
1069
1473
  case 0:
1070
1474
  if (addresses.length == 1 && Array.isArray(addresses[0]))
1071
1475
  addresses = addresses[0];
@@ -1077,15 +1481,15 @@ var Pool = /** @class */ (function () {
1077
1481
  });
1078
1482
  return [4 /*yield*/, curve_1.curve.multicallProvider.all(contractCalls)];
1079
1483
  case 1:
1080
- response = (_e.sent()).map(function (value) { return (0, utils_1.toBN)(value); });
1081
- _b = response.splice(0, 2), veTotalSupply = _b[0], gaugeTotalSupply = _b[1];
1484
+ response = (_f.sent()).map(function (value) { return (0, utils_1.toBN)(value); });
1485
+ _c = response.splice(0, 2), veTotalSupply = _c[0], gaugeTotalSupply = _c[1];
1082
1486
  resultBN = {};
1083
1487
  addresses.forEach(function (acct, i) {
1084
1488
  resultBN[acct] = response[i].div(veTotalSupply).times(gaugeTotalSupply);
1085
1489
  });
1086
1490
  result = {};
1087
- for (_c = 0, _d = Object.entries(resultBN); _c < _d.length; _c++) {
1088
- entry = _d[_c];
1491
+ for (_d = 0, _e = Object.entries(resultBN); _d < _e.length; _d++) {
1492
+ entry = _e[_d];
1089
1493
  result[entry[0]] = (0, utils_1.toStringFromBN)(entry[1]);
1090
1494
  }
1091
1495
  return [2 /*return*/, result];
@@ -1099,9 +1503,9 @@ var Pool = /** @class */ (function () {
1099
1503
  accounts[_a] = arguments[_a];
1100
1504
  }
1101
1505
  return __awaiter(_this, void 0, void 0, function () {
1102
- var votingEscrowContract, lpTokenContract, gaugeContract, contractCalls, response, _b, veTotalSupply, gaugeTotalSupply, votingPower, totalBalance, _c, accounts_1, acct, totalPower, optimalBN, _d, accounts_2, acct, amount, _e, accounts_3, acct, optimal, _f, _g, entry;
1103
- return __generator(this, function (_h) {
1104
- switch (_h.label) {
1506
+ var votingEscrowContract, lpTokenContract, gaugeContract, contractCalls, response, _c, veTotalSupply, gaugeTotalSupply, votingPower, totalBalance, _d, accounts_1, acct, totalPower, optimalBN, _e, accounts_2, acct, amount, _f, accounts_3, acct, optimal, _g, _h, entry;
1507
+ return __generator(this, function (_k) {
1508
+ switch (_k.label) {
1105
1509
  case 0:
1106
1510
  if (accounts.length == 1 && Array.isArray(accounts[0]))
1107
1511
  accounts = accounts[0];
@@ -1114,12 +1518,12 @@ var Pool = /** @class */ (function () {
1114
1518
  });
1115
1519
  return [4 /*yield*/, curve_1.curve.multicallProvider.all(contractCalls)];
1116
1520
  case 1:
1117
- response = (_h.sent()).map(function (value) { return (0, utils_1.toBN)(value); });
1118
- _b = response.splice(0, 2), veTotalSupply = _b[0], gaugeTotalSupply = _b[1];
1521
+ response = (_k.sent()).map(function (value) { return (0, utils_1.toBN)(value); });
1522
+ _c = response.splice(0, 2), veTotalSupply = _c[0], gaugeTotalSupply = _c[1];
1119
1523
  votingPower = {};
1120
1524
  totalBalance = (0, utils_1.BN)(0);
1121
- for (_c = 0, accounts_1 = accounts; _c < accounts_1.length; _c++) {
1122
- acct = accounts_1[_c];
1525
+ for (_d = 0, accounts_1 = accounts; _d < accounts_1.length; _d++) {
1526
+ acct = accounts_1[_d];
1123
1527
  votingPower[acct] = response[0];
1124
1528
  totalBalance = totalBalance.plus(response[1]).plus(response[2]);
1125
1529
  response.splice(0, 3);
@@ -1127,8 +1531,8 @@ var Pool = /** @class */ (function () {
1127
1531
  totalPower = Object.values(votingPower).reduce(function (sum, item) { return sum.plus(item); });
1128
1532
  optimalBN = Object.fromEntries(accounts.map(function (acc) { return [acc, (0, utils_1.BN)(0)]; }));
1129
1533
  if (totalBalance.lt(gaugeTotalSupply.times(totalPower).div(veTotalSupply))) {
1130
- for (_d = 0, accounts_2 = accounts; _d < accounts_2.length; _d++) {
1131
- acct = accounts_2[_d];
1534
+ for (_e = 0, accounts_2 = accounts; _e < accounts_2.length; _e++) {
1535
+ acct = accounts_2[_e];
1132
1536
  amount = gaugeTotalSupply.times(votingPower[acct]).div(veTotalSupply).lt(totalBalance) ?
1133
1537
  gaugeTotalSupply.times(votingPower[acct]).div(veTotalSupply) : totalBalance;
1134
1538
  optimalBN[acct] = amount;
@@ -1140,16 +1544,16 @@ var Pool = /** @class */ (function () {
1140
1544
  }
1141
1545
  else {
1142
1546
  if (totalPower.lt(0)) {
1143
- for (_e = 0, accounts_3 = accounts; _e < accounts_3.length; _e++) {
1144
- acct = accounts_3[_e];
1547
+ for (_f = 0, accounts_3 = accounts; _f < accounts_3.length; _f++) {
1548
+ acct = accounts_3[_f];
1145
1549
  optimalBN[acct] = totalBalance.times(votingPower[acct]).div(totalPower);
1146
1550
  }
1147
1551
  }
1148
1552
  optimalBN[accounts[0]] = optimalBN[accounts[0]].plus(totalBalance.minus(Object.values(optimalBN).reduce(function (sum, item) { return sum.plus(item); })));
1149
1553
  }
1150
1554
  optimal = {};
1151
- for (_f = 0, _g = Object.entries(optimalBN); _f < _g.length; _f++) {
1152
- entry = _g[_f];
1555
+ for (_g = 0, _h = Object.entries(optimalBN); _g < _h.length; _g++) {
1556
+ entry = _h[_g];
1153
1557
  optimal[entry[0]] = (0, utils_1.toStringFromBN)(entry[1]);
1154
1558
  }
1155
1559
  return [2 /*return*/, optimal];
@@ -1159,8 +1563,8 @@ var Pool = /** @class */ (function () {
1159
1563
  };
1160
1564
  this.boost = function (address) { return __awaiter(_this, void 0, void 0, function () {
1161
1565
  var gaugeContract, _a, workingBalance, balance, boost;
1162
- return __generator(this, function (_b) {
1163
- switch (_b.label) {
1566
+ return __generator(this, function (_c) {
1567
+ switch (_c.label) {
1164
1568
  case 0:
1165
1569
  gaugeContract = curve_1.curve.contracts[this.gauge].multicallContract;
1166
1570
  return [4 /*yield*/, curve_1.curve.multicallProvider.all([
@@ -1168,7 +1572,7 @@ var Pool = /** @class */ (function () {
1168
1572
  gaugeContract.balanceOf(address),
1169
1573
  ])];
1170
1574
  case 1:
1171
- _a = (_b.sent()).map(function (value) { return Number(ethers_1.ethers.utils.formatUnits(value)); }), workingBalance = _a[0], balance = _a[1];
1575
+ _a = (_c.sent()).map(function (value) { return Number(ethers_1.ethers.utils.formatUnits(value)); }), workingBalance = _a[0], balance = _a[1];
1172
1576
  boost = workingBalance / (0.4 * balance);
1173
1577
  return [2 /*return*/, boost.toFixed(4).replace(/([0-9])0+$/, '$1')];
1174
1578
  }
@@ -1176,8 +1580,8 @@ var Pool = /** @class */ (function () {
1176
1580
  }); };
1177
1581
  this.getApy = function () { return __awaiter(_this, void 0, void 0, function () {
1178
1582
  var swapContract, gaugeContract, gaugeControllerContract, _a, inflation, weight, workingSupply, virtualPrice, rate, crvRate, baseApy, boostedApy;
1179
- return __generator(this, function (_b) {
1180
- switch (_b.label) {
1583
+ return __generator(this, function (_c) {
1584
+ switch (_c.label) {
1181
1585
  case 0:
1182
1586
  swapContract = curve_1.curve.contracts[this.swap].multicallContract;
1183
1587
  gaugeContract = curve_1.curve.contracts[this.gauge].multicallContract;
@@ -1189,11 +1593,11 @@ var Pool = /** @class */ (function () {
1189
1593
  swapContract.get_virtual_price(),
1190
1594
  ])];
1191
1595
  case 1:
1192
- _a = (_b.sent()).map(function (value) { return (0, utils_1.toBN)(value); }), inflation = _a[0], weight = _a[1], workingSupply = _a[2], virtualPrice = _a[3];
1596
+ _a = (_c.sent()).map(function (value) { return (0, utils_1.toBN)(value); }), inflation = _a[0], weight = _a[1], workingSupply = _a[2], virtualPrice = _a[3];
1193
1597
  rate = inflation.times(weight).times(31536000).div(workingSupply).times(0.4).div(virtualPrice);
1194
1598
  return [4 /*yield*/, (0, utils_1.getCrvRate)()];
1195
1599
  case 2:
1196
- crvRate = _b.sent();
1600
+ crvRate = _c.sent();
1197
1601
  baseApy = rate.times(crvRate);
1198
1602
  boostedApy = baseApy.times(2.5);
1199
1603
  return [2 /*return*/, [baseApy.toFixed(4), boostedApy.toFixed(4)]];
@@ -1224,37 +1628,37 @@ var Pool = /** @class */ (function () {
1224
1628
  return idx;
1225
1629
  };
1226
1630
  this._getRates = function () { return __awaiter(_this, void 0, void 0, function () {
1227
- var _rates, i, addr, _a, _b, _c, _d;
1228
- return __generator(this, function (_e) {
1229
- switch (_e.label) {
1631
+ var _rates, i, addr, _a, _c, _d, _e;
1632
+ return __generator(this, function (_f) {
1633
+ switch (_f.label) {
1230
1634
  case 0:
1231
1635
  _rates = [];
1232
1636
  i = 0;
1233
- _e.label = 1;
1637
+ _f.label = 1;
1234
1638
  case 1:
1235
1639
  if (!(i < this.coinAddresses.length)) return [3 /*break*/, 9];
1236
1640
  addr = this.coinAddresses[i];
1237
1641
  if (!this.useLending[i]) return [3 /*break*/, 7];
1238
1642
  if (!['compound', 'usdt', 'ib'].includes(this.name)) return [3 /*break*/, 3];
1239
- _b = (_a = _rates).push;
1643
+ _c = (_a = _rates).push;
1240
1644
  return [4 /*yield*/, curve_1.curve.contracts[addr].contract.exchangeRateStored()];
1241
1645
  case 2:
1242
- _b.apply(_a, [_e.sent()]);
1646
+ _c.apply(_a, [_f.sent()]);
1243
1647
  return [3 /*break*/, 6];
1244
1648
  case 3:
1245
1649
  if (!['y', 'busd', 'pax'].includes(this.name)) return [3 /*break*/, 5];
1246
- _d = (_c = _rates).push;
1650
+ _e = (_d = _rates).push;
1247
1651
  return [4 /*yield*/, curve_1.curve.contracts[addr].contract.getPricePerFullShare()];
1248
1652
  case 4:
1249
- _d.apply(_c, [_e.sent()]);
1653
+ _e.apply(_d, [_f.sent()]);
1250
1654
  return [3 /*break*/, 6];
1251
1655
  case 5:
1252
1656
  _rates.push(ethers_1.ethers.BigNumber.from(10).pow(18)); // Aave ratio 1:1
1253
- _e.label = 6;
1657
+ _f.label = 6;
1254
1658
  case 6: return [3 /*break*/, 8];
1255
1659
  case 7:
1256
1660
  _rates.push(ethers_1.ethers.BigNumber.from(10).pow(18));
1257
- _e.label = 8;
1661
+ _f.label = 8;
1258
1662
  case 8:
1259
1663
  i++;
1260
1664
  return [3 /*break*/, 1];
@@ -1268,9 +1672,9 @@ var Pool = /** @class */ (function () {
1268
1672
  addresses[_a - 2] = arguments[_a];
1269
1673
  }
1270
1674
  return __awaiter(_this, void 0, void 0, function () {
1271
- var coinNames, coinAddresses, i, rawBalances, balances, _b, addresses_1, address, _c, coinNames_1, coinName;
1272
- return __generator(this, function (_d) {
1273
- switch (_d.label) {
1675
+ var coinNames, coinAddresses, i, rawBalances, balances, _c, addresses_1, address, _d, coinNames_1, coinName;
1676
+ return __generator(this, function (_e) {
1677
+ switch (_e.label) {
1274
1678
  case 0:
1275
1679
  coinNames = [];
1276
1680
  coinAddresses = [];
@@ -1288,13 +1692,13 @@ var Pool = /** @class */ (function () {
1288
1692
  addresses = (0, utils_1._prepareAddresses)(addresses);
1289
1693
  return [4 /*yield*/, (0, utils_1._getBalances)(coinAddresses, addresses)];
1290
1694
  case 1:
1291
- rawBalances = _d.sent();
1695
+ rawBalances = _e.sent();
1292
1696
  balances = {};
1293
- for (_b = 0, addresses_1 = addresses; _b < addresses_1.length; _b++) {
1294
- address = addresses_1[_b];
1697
+ for (_c = 0, addresses_1 = addresses; _c < addresses_1.length; _c++) {
1698
+ address = addresses_1[_c];
1295
1699
  balances[address] = {};
1296
- for (_c = 0, coinNames_1 = coinNames; _c < coinNames_1.length; _c++) {
1297
- coinName = coinNames_1[_c];
1700
+ for (_d = 0, coinNames_1 = coinNames; _d < coinNames_1.length; _d++) {
1701
+ coinName = coinNames_1[_d];
1298
1702
  balances[address][coinName] = rawBalances[address].shift();
1299
1703
  }
1300
1704
  }
@@ -1303,6 +1707,18 @@ var Pool = /** @class */ (function () {
1303
1707
  });
1304
1708
  });
1305
1709
  };
1710
+ this._balancedAmounts = function (poolBalances, walletBalances) {
1711
+ var poolBalancesRatios = poolBalances.map(function (b) { return b / poolBalances.reduce(function (a, b) { return a + b; }); });
1712
+ // Cross factors for each wallet balance used as reference to see the
1713
+ // max that can be used according to the lowest relative wallet balance
1714
+ var balancesAmountsForEachScenario = walletBalances.map(function (_, i) { return (walletBalances.map(function (_, j) { return (poolBalancesRatios[j] * walletBalances[i] / poolBalancesRatios[i]); })); });
1715
+ var firstCoinBalanceForEachScenario = balancesAmountsForEachScenario.map(function (_a) {
1716
+ var a = _a[0];
1717
+ return a;
1718
+ });
1719
+ var scenarioWithLowestBalances = firstCoinBalanceForEachScenario.indexOf(Math.min.apply(Math, firstCoinBalanceForEachScenario));
1720
+ return balancesAmountsForEachScenario[scenarioWithLowestBalances].map(String);
1721
+ };
1306
1722
  this._calcLpTokenAmount = function (_amounts, isDeposit) {
1307
1723
  if (isDeposit === void 0) { isDeposit = true; }
1308
1724
  return __awaiter(_this, void 0, void 0, function () {
@@ -1457,25 +1873,25 @@ var Pool = /** @class */ (function () {
1457
1873
  if (estimateGas === void 0) { estimateGas = false; }
1458
1874
  return __awaiter(_this, void 0, void 0, function () {
1459
1875
  var coinAddresses, _minMintAmount, _a, contract, ethIndex, value, gas, gasLimit;
1460
- return __generator(this, function (_b) {
1461
- switch (_b.label) {
1876
+ return __generator(this, function (_c) {
1877
+ switch (_c.label) {
1462
1878
  case 0:
1463
1879
  coinAddresses = useUnderlying ? this.underlyingCoinAddresses : this.coinAddresses;
1464
1880
  if (!!estimateGas) return [3 /*break*/, 2];
1465
1881
  return [4 /*yield*/, (0, utils_1._ensureAllowance)(coinAddresses, _amounts, this.swap)];
1466
1882
  case 1:
1467
- _b.sent();
1468
- _b.label = 2;
1883
+ _c.sent();
1884
+ _c.label = 2;
1469
1885
  case 2:
1470
1886
  if (!useUnderlying) return [3 /*break*/, 4];
1471
1887
  return [4 /*yield*/, this._calcLpTokenAmountWithUnderlying(_amounts)];
1472
1888
  case 3:
1473
- _a = _b.sent();
1889
+ _a = _c.sent();
1474
1890
  return [3 /*break*/, 6];
1475
1891
  case 4: return [4 /*yield*/, this._calcLpTokenAmount(_amounts)];
1476
1892
  case 5:
1477
- _a = _b.sent();
1478
- _b.label = 6;
1893
+ _a = _c.sent();
1894
+ _c.label = 6;
1479
1895
  case 6:
1480
1896
  _minMintAmount = _a;
1481
1897
  _minMintAmount = _minMintAmount.mul(99).div(100);
@@ -1484,33 +1900,33 @@ var Pool = /** @class */ (function () {
1484
1900
  value = _amounts[ethIndex] || ethers_1.ethers.BigNumber.from(0);
1485
1901
  return [4 /*yield*/, contract.estimateGas.add_liquidity(_amounts, _minMintAmount, useUnderlying, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
1486
1902
  case 7:
1487
- gas = _b.sent();
1903
+ gas = _c.sent();
1488
1904
  if (estimateGas) {
1489
1905
  return [2 /*return*/, gas.toNumber()];
1490
1906
  }
1491
1907
  gasLimit = gas.mul(130).div(100);
1492
1908
  return [4 /*yield*/, contract.add_liquidity(_amounts, _minMintAmount, useUnderlying, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit, value: value }))];
1493
- case 8: return [2 /*return*/, (_b.sent()).hash];
1909
+ case 8: return [2 /*return*/, (_c.sent()).hash];
1494
1910
  }
1495
1911
  });
1496
1912
  });
1497
1913
  };
1498
1914
  this._calcExpectedAmounts = function (_lpTokenAmount) { return __awaiter(_this, void 0, void 0, function () {
1499
- var coinBalancesBN, i, _balance, totalSupplyBN, _a, expectedAmountsBN, _b, coinBalancesBN_1, coinBalance;
1915
+ var coinBalancesBN, i, _balance, totalSupplyBN, _a, expectedAmountsBN, _c, coinBalancesBN_1, coinBalance;
1500
1916
  var _this = this;
1501
- return __generator(this, function (_c) {
1502
- switch (_c.label) {
1917
+ return __generator(this, function (_d) {
1918
+ switch (_d.label) {
1503
1919
  case 0:
1504
1920
  coinBalancesBN = [];
1505
1921
  i = 0;
1506
- _c.label = 1;
1922
+ _d.label = 1;
1507
1923
  case 1:
1508
1924
  if (!(i < this.coinAddresses.length)) return [3 /*break*/, 4];
1509
1925
  return [4 /*yield*/, curve_1.curve.contracts[this.swap].contract.balances(i, curve_1.curve.constantOptions)];
1510
1926
  case 2:
1511
- _balance = _c.sent();
1927
+ _balance = _d.sent();
1512
1928
  coinBalancesBN.push((0, utils_1.toBN)(_balance, this.decimals[i]));
1513
- _c.label = 3;
1929
+ _d.label = 3;
1514
1930
  case 3:
1515
1931
  i++;
1516
1932
  return [3 /*break*/, 1];
@@ -1518,10 +1934,10 @@ var Pool = /** @class */ (function () {
1518
1934
  _a = utils_1.toBN;
1519
1935
  return [4 /*yield*/, curve_1.curve.contracts[this.lpToken].contract.totalSupply(curve_1.curve.constantOptions)];
1520
1936
  case 5:
1521
- totalSupplyBN = _a.apply(void 0, [_c.sent()]);
1937
+ totalSupplyBN = _a.apply(void 0, [_d.sent()]);
1522
1938
  expectedAmountsBN = [];
1523
- for (_b = 0, coinBalancesBN_1 = coinBalancesBN; _b < coinBalancesBN_1.length; _b++) {
1524
- coinBalance = coinBalancesBN_1[_b];
1939
+ for (_c = 0, coinBalancesBN_1 = coinBalancesBN; _c < coinBalancesBN_1.length; _c++) {
1940
+ coinBalance = coinBalancesBN_1[_c];
1525
1941
  expectedAmountsBN.push(coinBalance.times((0, utils_1.toBN)(_lpTokenAmount)).div(totalSupplyBN));
1526
1942
  }
1527
1943
  return [2 /*return*/, expectedAmountsBN.map(function (amount, i) { return (0, utils_1.fromBN)(amount, _this.decimals[i]); })];
@@ -1679,30 +2095,30 @@ var Pool = /** @class */ (function () {
1679
2095
  if (estimateGas === void 0) { estimateGas = false; }
1680
2096
  return __awaiter(_this, void 0, void 0, function () {
1681
2097
  var _minAmounts, _a, contract, gas, gasLimit;
1682
- return __generator(this, function (_b) {
1683
- switch (_b.label) {
2098
+ return __generator(this, function (_c) {
2099
+ switch (_c.label) {
1684
2100
  case 0:
1685
2101
  if (!useUnderlying) return [3 /*break*/, 2];
1686
2102
  return [4 /*yield*/, this._calcMinUnderlyingAmounts(_lpTokenAmount)];
1687
2103
  case 1:
1688
- _a = _b.sent();
2104
+ _a = _c.sent();
1689
2105
  return [3 /*break*/, 4];
1690
2106
  case 2: return [4 /*yield*/, this._calcMinAmounts(_lpTokenAmount)];
1691
2107
  case 3:
1692
- _a = _b.sent();
1693
- _b.label = 4;
2108
+ _a = _c.sent();
2109
+ _c.label = 4;
1694
2110
  case 4:
1695
2111
  _minAmounts = _a;
1696
2112
  contract = curve_1.curve.contracts[this.swap].contract;
1697
2113
  return [4 /*yield*/, contract.estimateGas.remove_liquidity(_lpTokenAmount, _minAmounts, useUnderlying, curve_1.curve.constantOptions)];
1698
2114
  case 5:
1699
- gas = _b.sent();
2115
+ gas = _c.sent();
1700
2116
  if (estimateGas) {
1701
2117
  return [2 /*return*/, gas.toNumber()];
1702
2118
  }
1703
2119
  gasLimit = gas.mul(130).div(100);
1704
2120
  return [4 /*yield*/, contract.remove_liquidity(_lpTokenAmount, _minAmounts, useUnderlying, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit }))];
1705
- case 6: return [2 /*return*/, (_b.sent()).hash];
2121
+ case 6: return [2 /*return*/, (_c.sent()).hash];
1706
2122
  }
1707
2123
  });
1708
2124
  });
@@ -1803,31 +2219,31 @@ var Pool = /** @class */ (function () {
1803
2219
  if (estimateGas === void 0) { estimateGas = false; }
1804
2220
  return __awaiter(_this, void 0, void 0, function () {
1805
2221
  var _maxBurnAmount, _a, contract, gas, gasLimit;
1806
- return __generator(this, function (_b) {
1807
- switch (_b.label) {
2222
+ return __generator(this, function (_c) {
2223
+ switch (_c.label) {
1808
2224
  case 0:
1809
2225
  if (!useUnderlying) return [3 /*break*/, 2];
1810
2226
  return [4 /*yield*/, this._calcLpTokenAmountWithUnderlying(_amounts, false)];
1811
2227
  case 1:
1812
- _a = _b.sent();
2228
+ _a = _c.sent();
1813
2229
  return [3 /*break*/, 4];
1814
2230
  case 2: return [4 /*yield*/, this._calcLpTokenAmount(_amounts, false)];
1815
2231
  case 3:
1816
- _a = _b.sent();
1817
- _b.label = 4;
2232
+ _a = _c.sent();
2233
+ _c.label = 4;
1818
2234
  case 4:
1819
2235
  _maxBurnAmount = _a;
1820
2236
  _maxBurnAmount = _maxBurnAmount.mul(101).div(100);
1821
2237
  contract = curve_1.curve.contracts[this.swap].contract;
1822
2238
  return [4 /*yield*/, contract.estimateGas.remove_liquidity_imbalance(_amounts, _maxBurnAmount, useUnderlying, curve_1.curve.constantOptions)];
1823
2239
  case 5:
1824
- gas = _b.sent();
2240
+ gas = _c.sent();
1825
2241
  if (estimateGas) {
1826
2242
  return [2 /*return*/, gas.toNumber()];
1827
2243
  }
1828
2244
  gasLimit = gas.mul(130).div(100);
1829
2245
  return [4 /*yield*/, contract.remove_liquidity_imbalance(_amounts, _maxBurnAmount, useUnderlying, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit }))];
1830
- case 6: return [2 /*return*/, (_b.sent()).hash];
2246
+ case 6: return [2 /*return*/, (_c.sent()).hash];
1831
2247
  }
1832
2248
  });
1833
2249
  });
@@ -1932,31 +2348,31 @@ var Pool = /** @class */ (function () {
1932
2348
  if (estimateGas === void 0) { estimateGas = false; }
1933
2349
  return __awaiter(_this, void 0, void 0, function () {
1934
2350
  var _minAmount, _a, contract, gas, gasLimit;
1935
- return __generator(this, function (_b) {
1936
- switch (_b.label) {
2351
+ return __generator(this, function (_c) {
2352
+ switch (_c.label) {
1937
2353
  case 0:
1938
2354
  if (!(this.name === 'ib')) return [3 /*break*/, 2];
1939
2355
  return [4 /*yield*/, this._calcWithdrawOneCoin(_lpTokenAmount, i, useUnderlying)];
1940
2356
  case 1:
1941
- _a = _b.sent();
2357
+ _a = _c.sent();
1942
2358
  return [3 /*break*/, 4];
1943
2359
  case 2: return [4 /*yield*/, this._calcWithdrawOneCoinSwap(_lpTokenAmount, i)];
1944
2360
  case 3:
1945
- _a = _b.sent();
1946
- _b.label = 4;
2361
+ _a = _c.sent();
2362
+ _c.label = 4;
1947
2363
  case 4:
1948
2364
  _minAmount = _a;
1949
2365
  _minAmount = _minAmount.mul(99).div(100);
1950
2366
  contract = curve_1.curve.contracts[this.swap].contract;
1951
2367
  return [4 /*yield*/, contract.estimateGas.remove_liquidity_one_coin(_lpTokenAmount, i, _minAmount, useUnderlying, curve_1.curve.constantOptions)];
1952
2368
  case 5:
1953
- gas = _b.sent();
2369
+ gas = _c.sent();
1954
2370
  if (estimateGas) {
1955
2371
  return [2 /*return*/, gas.toNumber()];
1956
2372
  }
1957
2373
  gasLimit = gas.mul(130).div(100);
1958
2374
  return [4 /*yield*/, contract.remove_liquidity_one_coin(_lpTokenAmount, i, _minAmount, useUnderlying, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit }))];
1959
- case 6: return [2 /*return*/, (_b.sent()).hash];
2375
+ case 6: return [2 /*return*/, (_c.sent()).hash];
1960
2376
  }
1961
2377
  });
1962
2378
  });
@@ -2000,18 +2416,27 @@ var Pool = /** @class */ (function () {
2000
2416
  this.basePool = poolData.base_pool || '';
2001
2417
  this.isFactory = poolData.is_factory || false;
2002
2418
  this.estimateGas = {
2419
+ addLiquidityApprove: this.addLiquidityApproveEstimateGas,
2003
2420
  addLiquidity: this.addLiquidityEstimateGas,
2421
+ addLiquidityWrappedApprove: this.addLiquidityWrappedApproveEstimateGas,
2004
2422
  addLiquidityWrapped: this.addLiquidityWrappedEstimateGas,
2423
+ gaugeDepositApprove: this.gaugeDepositApproveEstimateGas,
2005
2424
  gaugeDeposit: this.gaugeDepositEstimateGas,
2006
2425
  gaugeWithdraw: this.gaugeWithdrawEstimateGas,
2426
+ removeLiquidityApprove: this.removeLiquidityApproveEstimateGas,
2007
2427
  removeLiquidity: this.removeLiquidityEstimateGas,
2008
2428
  removeLiquidityWrapped: this.removeLiquidityWrappedEstimateGas,
2429
+ removeLiquidityImbalanceApprove: this.removeLiquidityImbalanceApproveEstimateGas,
2009
2430
  removeLiquidityImbalance: this.removeLiquidityImbalanceEstimateGas,
2010
2431
  removeLiquidityImbalanceWrapped: this.removeLiquidityImbalanceWrappedEstimateGas,
2432
+ removeLiquidityOneCoinApprove: this.removeLiquidityOneCoinApproveEstimateGas,
2011
2433
  removeLiquidityOneCoin: this.removeLiquidityOneCoinEstimateGas,
2012
2434
  removeLiquidityOneCoinWrapped: this.removeLiquidityOneCoinWrappedEstimateGas,
2435
+ exchangeApprove: this.exchangeApproveEstimateGas,
2013
2436
  exchange: this.exchangeEstimateGas,
2437
+ exchangeTricryptoApprove: this.exchangeTricryptoApproveEstimateGas,
2014
2438
  exchangeTricrypto: this.exchangeTricryptoEstimateGas,
2439
+ exchangeWrappedApprove: this.exchangeWrappedApproveEstimateGas,
2015
2440
  exchangeWrapped: this.exchangeWrappedEstimateGas,
2016
2441
  };
2017
2442
  if (this.isMeta) {
@@ -2027,8 +2452,8 @@ exports.Pool = Pool;
2027
2452
  // --------- Exchange Using All Pools ---------
2028
2453
  var _getBestPoolAndOutput = function (inputCoinAddress, outputCoinAddress, inputCoinDecimals, amount) { return __awaiter(void 0, void 0, void 0, function () {
2029
2454
  var tricryptoCoins, addressProviderContract, registryExchangeAddress, registryExchangeContract, _amount, _a, poolAddress, output;
2030
- return __generator(this, function (_b) {
2031
- switch (_b.label) {
2455
+ return __generator(this, function (_c) {
2456
+ switch (_c.label) {
2032
2457
  case 0:
2033
2458
  tricryptoCoins = [coins_1.COINS.usdt.toLowerCase(), coins_1.COINS.wbtc.toLowerCase(), coins_1.COINS.weth.toLowerCase()];
2034
2459
  if (tricryptoCoins.includes(inputCoinAddress.toLowerCase()) && tricryptoCoins.includes(outputCoinAddress.toLowerCase())) {
@@ -2037,27 +2462,27 @@ var _getBestPoolAndOutput = function (inputCoinAddress, outputCoinAddress, input
2037
2462
  addressProviderContract = curve_1.curve.contracts[curve_1.ALIASES.address_provider].contract;
2038
2463
  return [4 /*yield*/, addressProviderContract.get_address(2, curve_1.curve.constantOptions)];
2039
2464
  case 1:
2040
- registryExchangeAddress = _b.sent();
2465
+ registryExchangeAddress = _c.sent();
2041
2466
  registryExchangeContract = new ethers_1.ethers.Contract(registryExchangeAddress, registry_exchange_json_1.default, curve_1.curve.signer || curve_1.curve.provider);
2042
2467
  _amount = ethers_1.ethers.utils.parseUnits(amount.toString(), inputCoinDecimals);
2043
2468
  return [4 /*yield*/, registryExchangeContract.get_best_rate(inputCoinAddress, outputCoinAddress, _amount, curve_1.curve.constantOptions)];
2044
2469
  case 2:
2045
- _a = _b.sent(), poolAddress = _a[0], output = _a[1];
2470
+ _a = _c.sent(), poolAddress = _a[0], output = _a[1];
2046
2471
  return [2 /*return*/, { poolAddress: poolAddress, output: output }];
2047
2472
  }
2048
2473
  });
2049
2474
  }); };
2050
2475
  exports._getBestPoolAndOutput = _getBestPoolAndOutput;
2051
2476
  var getBestPoolAndOutput = function (inputCoin, outputCoin, amount) { return __awaiter(void 0, void 0, void 0, function () {
2052
- var _a, inputCoinAddress, outputCoinAddress, _b, inputCoinDecimals, outputCoinDecimals, _c, poolAddress, _output, output;
2053
- return __generator(this, function (_d) {
2054
- switch (_d.label) {
2477
+ var _a, inputCoinAddress, outputCoinAddress, _c, inputCoinDecimals, outputCoinDecimals, _d, poolAddress, _output, output;
2478
+ return __generator(this, function (_e) {
2479
+ switch (_e.label) {
2055
2480
  case 0:
2056
2481
  _a = (0, utils_1._getCoinAddresses)(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
2057
- _b = (0, utils_1._getCoinDecimals)(inputCoinAddress, outputCoinAddress), inputCoinDecimals = _b[0], outputCoinDecimals = _b[1];
2482
+ _c = (0, utils_1._getCoinDecimals)(inputCoinAddress, outputCoinAddress), inputCoinDecimals = _c[0], outputCoinDecimals = _c[1];
2058
2483
  return [4 /*yield*/, (0, exports._getBestPoolAndOutput)(inputCoinAddress, outputCoinAddress, inputCoinDecimals, amount)];
2059
2484
  case 1:
2060
- _c = _d.sent(), poolAddress = _c.poolAddress, _output = _c.output;
2485
+ _d = _e.sent(), poolAddress = _d.poolAddress, _output = _d.output;
2061
2486
  output = ethers_1.ethers.utils.formatUnits(_output, outputCoinDecimals);
2062
2487
  return [2 /*return*/, { poolAddress: poolAddress, output: output }];
2063
2488
  }
@@ -2076,35 +2501,35 @@ exports.exchangeExpected = exchangeExpected;
2076
2501
  var exchangeEstimateGas = function (inputCoin, outputCoin, amount, maxSlippage) {
2077
2502
  if (maxSlippage === void 0) { maxSlippage = 0.01; }
2078
2503
  return __awaiter(void 0, void 0, void 0, function () {
2079
- var _a, inputCoinAddress, outputCoinAddress, inputCoinDecimals, addressProviderContract, registryAddress, registryContract, poolAddress, poolName, _b, _i, _j, is_underlying, i, j, pool;
2080
- return __generator(this, function (_c) {
2081
- switch (_c.label) {
2504
+ var _a, inputCoinAddress, outputCoinAddress, inputCoinDecimals, addressProviderContract, registryAddress, registryContract, poolAddress, poolName, _c, _i, _j, is_underlying, i, j, pool;
2505
+ return __generator(this, function (_d) {
2506
+ switch (_d.label) {
2082
2507
  case 0:
2083
2508
  _a = (0, utils_1._getCoinAddresses)(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
2084
2509
  inputCoinDecimals = (0, utils_1._getCoinDecimals)(inputCoinAddress)[0];
2085
2510
  addressProviderContract = curve_1.curve.contracts[curve_1.ALIASES.address_provider].contract;
2086
2511
  return [4 /*yield*/, addressProviderContract.get_registry()];
2087
2512
  case 1:
2088
- registryAddress = _c.sent();
2513
+ registryAddress = _d.sent();
2089
2514
  registryContract = new ethers_1.ethers.Contract(registryAddress, registry_json_1.default, curve_1.curve.signer);
2090
2515
  return [4 /*yield*/, (0, exports._getBestPoolAndOutput)(inputCoinAddress, outputCoinAddress, inputCoinDecimals, amount)];
2091
2516
  case 2:
2092
- poolAddress = (_c.sent()).poolAddress;
2517
+ poolAddress = (_d.sent()).poolAddress;
2093
2518
  if (poolAddress === "0x0000000000000000000000000000000000000000") {
2094
2519
  throw new Error("This pair can't be exchanged");
2095
2520
  }
2096
2521
  poolName = (0, utils_1.getPoolNameBySwapAddress)(poolAddress);
2097
2522
  return [4 /*yield*/, registryContract.get_coin_indices(poolAddress, inputCoinAddress, outputCoinAddress)];
2098
2523
  case 3:
2099
- _b = _c.sent(), _i = _b[0], _j = _b[1], is_underlying = _b[2];
2524
+ _c = _d.sent(), _i = _c[0], _j = _c[1], is_underlying = _c[2];
2100
2525
  i = Number(_i.toString());
2101
2526
  j = Number(_j.toString());
2102
2527
  pool = new Pool(poolName);
2103
2528
  if (!is_underlying) return [3 /*break*/, 5];
2104
2529
  return [4 /*yield*/, pool.estimateGas.exchange(i, j, amount, maxSlippage)];
2105
- case 4: return [2 /*return*/, _c.sent()];
2530
+ case 4: return [2 /*return*/, _d.sent()];
2106
2531
  case 5: return [4 /*yield*/, pool.estimateGas.exchangeWrapped(i, j, amount, maxSlippage)];
2107
- case 6: return [2 /*return*/, _c.sent()];
2532
+ case 6: return [2 /*return*/, _d.sent()];
2108
2533
  }
2109
2534
  });
2110
2535
  });
@@ -2113,35 +2538,35 @@ exports.exchangeEstimateGas = exchangeEstimateGas;
2113
2538
  var exchange = function (inputCoin, outputCoin, amount, maxSlippage) {
2114
2539
  if (maxSlippage === void 0) { maxSlippage = 0.01; }
2115
2540
  return __awaiter(void 0, void 0, void 0, function () {
2116
- var _a, inputCoinAddress, outputCoinAddress, inputCoinDecimals, addressProviderContract, registryAddress, registryContract, poolAddress, poolName, _b, _i, _j, is_underlying, i, j, pool;
2117
- return __generator(this, function (_c) {
2118
- switch (_c.label) {
2541
+ var _a, inputCoinAddress, outputCoinAddress, inputCoinDecimals, addressProviderContract, registryAddress, registryContract, poolAddress, poolName, _c, _i, _j, is_underlying, i, j, pool;
2542
+ return __generator(this, function (_d) {
2543
+ switch (_d.label) {
2119
2544
  case 0:
2120
2545
  _a = (0, utils_1._getCoinAddresses)(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
2121
2546
  inputCoinDecimals = (0, utils_1._getCoinDecimals)(inputCoinAddress)[0];
2122
2547
  addressProviderContract = curve_1.curve.contracts[curve_1.ALIASES.address_provider].contract;
2123
2548
  return [4 /*yield*/, addressProviderContract.get_registry()];
2124
2549
  case 1:
2125
- registryAddress = _c.sent();
2550
+ registryAddress = _d.sent();
2126
2551
  registryContract = new ethers_1.ethers.Contract(registryAddress, registry_json_1.default, curve_1.curve.signer);
2127
2552
  return [4 /*yield*/, (0, exports._getBestPoolAndOutput)(inputCoinAddress, outputCoinAddress, inputCoinDecimals, amount)];
2128
2553
  case 2:
2129
- poolAddress = (_c.sent()).poolAddress;
2554
+ poolAddress = (_d.sent()).poolAddress;
2130
2555
  if (poolAddress === "0x0000000000000000000000000000000000000000") {
2131
2556
  throw new Error("This pair can't be exchanged");
2132
2557
  }
2133
2558
  poolName = (0, utils_1.getPoolNameBySwapAddress)(poolAddress);
2134
2559
  return [4 /*yield*/, registryContract.get_coin_indices(poolAddress, inputCoinAddress, outputCoinAddress)];
2135
2560
  case 3:
2136
- _b = _c.sent(), _i = _b[0], _j = _b[1], is_underlying = _b[2];
2561
+ _c = _d.sent(), _i = _c[0], _j = _c[1], is_underlying = _c[2];
2137
2562
  i = Number(_i.toString());
2138
2563
  j = Number(_j.toString());
2139
2564
  pool = new Pool(poolName);
2140
2565
  if (!is_underlying) return [3 /*break*/, 5];
2141
2566
  return [4 /*yield*/, pool.exchange(i, j, amount, maxSlippage)];
2142
- case 4: return [2 /*return*/, _c.sent()];
2567
+ case 4: return [2 /*return*/, _d.sent()];
2143
2568
  case 5: return [4 /*yield*/, pool.exchangeWrapped(i, j, amount, maxSlippage)];
2144
- case 6: return [2 /*return*/, _c.sent()];
2569
+ case 6: return [2 /*return*/, _d.sent()];
2145
2570
  }
2146
2571
  });
2147
2572
  });
@@ -2150,13 +2575,13 @@ exports.exchange = exchange;
2150
2575
  // --------- Cross-Asset Exchange ---------
2151
2576
  var crossAssetExchangeAvailable = function (inputCoin, outputCoin) { return __awaiter(void 0, void 0, void 0, function () {
2152
2577
  var _a, inputCoinAddress, outputCoinAddress, routerContract;
2153
- return __generator(this, function (_b) {
2154
- switch (_b.label) {
2578
+ return __generator(this, function (_c) {
2579
+ switch (_c.label) {
2155
2580
  case 0:
2156
2581
  _a = (0, utils_1._getCoinAddresses)(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
2157
2582
  return [4 /*yield*/, curve_1.curve.contracts[curve_1.ALIASES.router].contract];
2158
2583
  case 1:
2159
- routerContract = _b.sent();
2584
+ routerContract = _c.sent();
2160
2585
  return [2 /*return*/, routerContract.can_route(inputCoinAddress, outputCoinAddress, curve_1.curve.constantOptions)];
2161
2586
  }
2162
2587
  });
@@ -2174,24 +2599,24 @@ var _getSmallAmountForCoin = function (coinAddress) {
2174
2599
  };
2175
2600
  exports._getSmallAmountForCoin = _getSmallAmountForCoin;
2176
2601
  var _crossAssetExchangeInfo = function (inputCoinAddress, outputCoinAddress, inputCoinDecimals, outputCoinDecimals, amount) { return __awaiter(void 0, void 0, void 0, function () {
2177
- var routerContract, _amount, amountBN, _a, route, indices, _expected, expectedBN, exchangeRateBN, _smallAmount, smallAmountBN, _b, _expectedSmall, expectedSmallBN, exchangeSmallRateBN, slippage;
2178
- return __generator(this, function (_c) {
2179
- switch (_c.label) {
2602
+ var routerContract, _amount, amountBN, _a, route, indices, _expected, expectedBN, exchangeRateBN, _smallAmount, smallAmountBN, _c, _expectedSmall, expectedSmallBN, exchangeSmallRateBN, slippage;
2603
+ return __generator(this, function (_d) {
2604
+ switch (_d.label) {
2180
2605
  case 0: return [4 /*yield*/, curve_1.curve.contracts[curve_1.ALIASES.router].contract];
2181
2606
  case 1:
2182
- routerContract = _c.sent();
2607
+ routerContract = _d.sent();
2183
2608
  _amount = ethers_1.ethers.utils.parseUnits(amount, inputCoinDecimals);
2184
2609
  amountBN = (0, utils_1.toBN)(_amount, inputCoinDecimals);
2185
2610
  return [4 /*yield*/, routerContract.get_exchange_routing(inputCoinAddress, outputCoinAddress, _amount, curve_1.curve.constantOptions)];
2186
2611
  case 2:
2187
- _a = _c.sent(), route = _a[0], indices = _a[1], _expected = _a[2];
2612
+ _a = _d.sent(), route = _a[0], indices = _a[1], _expected = _a[2];
2188
2613
  expectedBN = (0, utils_1.toBN)(_expected, outputCoinDecimals);
2189
2614
  exchangeRateBN = expectedBN.div(amountBN);
2190
2615
  _smallAmount = ethers_1.ethers.utils.parseUnits((0, exports._getSmallAmountForCoin)(inputCoinAddress), inputCoinDecimals);
2191
2616
  smallAmountBN = (0, utils_1.toBN)(_smallAmount, inputCoinDecimals);
2192
2617
  return [4 /*yield*/, routerContract.get_exchange_routing(inputCoinAddress, outputCoinAddress, _smallAmount, curve_1.curve.constantOptions)];
2193
2618
  case 3:
2194
- _b = _c.sent(), _expectedSmall = _b[2];
2619
+ _c = _d.sent(), _expectedSmall = _c[2];
2195
2620
  expectedSmallBN = (0, utils_1.toBN)(_expectedSmall, outputCoinDecimals);
2196
2621
  exchangeSmallRateBN = expectedSmallBN.div(smallAmountBN);
2197
2622
  slippage = 1 - exchangeRateBN.div(exchangeSmallRateBN).toNumber();
@@ -2201,15 +2626,15 @@ var _crossAssetExchangeInfo = function (inputCoinAddress, outputCoinAddress, inp
2201
2626
  }); };
2202
2627
  exports._crossAssetExchangeInfo = _crossAssetExchangeInfo;
2203
2628
  var crossAssetExchangeOutputAndSlippage = function (inputCoin, outputCoin, amount) { return __awaiter(void 0, void 0, void 0, function () {
2204
- var _a, inputCoinAddress, outputCoinAddress, _b, inputCoinDecimals, outputCoinDecimals, _c, _expected, slippage, output;
2205
- return __generator(this, function (_d) {
2206
- switch (_d.label) {
2629
+ var _a, inputCoinAddress, outputCoinAddress, _c, inputCoinDecimals, outputCoinDecimals, _d, _expected, slippage, output;
2630
+ return __generator(this, function (_e) {
2631
+ switch (_e.label) {
2207
2632
  case 0:
2208
2633
  _a = (0, utils_1._getCoinAddresses)(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
2209
- _b = (0, utils_1._getCoinDecimals)(inputCoinAddress, outputCoinAddress), inputCoinDecimals = _b[0], outputCoinDecimals = _b[1];
2634
+ _c = (0, utils_1._getCoinDecimals)(inputCoinAddress, outputCoinAddress), inputCoinDecimals = _c[0], outputCoinDecimals = _c[1];
2210
2635
  return [4 /*yield*/, (0, exports._crossAssetExchangeInfo)(inputCoinAddress, outputCoinAddress, inputCoinDecimals, outputCoinDecimals, amount)];
2211
2636
  case 1:
2212
- _c = _d.sent(), _expected = _c._expected, slippage = _c.slippage;
2637
+ _d = _e.sent(), _expected = _d._expected, slippage = _d.slippage;
2213
2638
  output = ethers_1.ethers.utils.formatUnits(_expected, outputCoinDecimals);
2214
2639
  return [2 /*return*/, { output: output, slippage: slippage }];
2215
2640
  }
@@ -2217,19 +2642,19 @@ var crossAssetExchangeOutputAndSlippage = function (inputCoin, outputCoin, amoun
2217
2642
  }); };
2218
2643
  exports.crossAssetExchangeOutputAndSlippage = crossAssetExchangeOutputAndSlippage;
2219
2644
  var crossAssetExchangeExpected = function (inputCoin, outputCoin, amount) { return __awaiter(void 0, void 0, void 0, function () {
2220
- var _a, inputCoinAddress, outputCoinAddress, _b, inputCoinDecimals, outputCoinDecimals, routerContract, _amount, _c, _expected;
2221
- return __generator(this, function (_d) {
2222
- switch (_d.label) {
2645
+ var _a, inputCoinAddress, outputCoinAddress, _c, inputCoinDecimals, outputCoinDecimals, routerContract, _amount, _d, _expected;
2646
+ return __generator(this, function (_e) {
2647
+ switch (_e.label) {
2223
2648
  case 0:
2224
2649
  _a = (0, utils_1._getCoinAddresses)(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
2225
- _b = (0, utils_1._getCoinDecimals)(inputCoinAddress, outputCoinAddress), inputCoinDecimals = _b[0], outputCoinDecimals = _b[1];
2650
+ _c = (0, utils_1._getCoinDecimals)(inputCoinAddress, outputCoinAddress), inputCoinDecimals = _c[0], outputCoinDecimals = _c[1];
2226
2651
  return [4 /*yield*/, curve_1.curve.contracts[curve_1.ALIASES.router].contract];
2227
2652
  case 1:
2228
- routerContract = _d.sent();
2653
+ routerContract = _e.sent();
2229
2654
  _amount = ethers_1.ethers.utils.parseUnits(amount, inputCoinDecimals);
2230
2655
  return [4 /*yield*/, routerContract.get_exchange_routing(inputCoinAddress, outputCoinAddress, _amount, curve_1.curve.constantOptions)];
2231
2656
  case 2:
2232
- _c = _d.sent(), _expected = _c[2];
2657
+ _d = _e.sent(), _expected = _d[2];
2233
2658
  return [2 /*return*/, ethers_1.ethers.utils.formatUnits(_expected, outputCoinDecimals)];
2234
2659
  }
2235
2660
  });
@@ -2238,38 +2663,38 @@ exports.crossAssetExchangeExpected = crossAssetExchangeExpected;
2238
2663
  var crossAssetExchangeEstimateGas = function (inputCoin, outputCoin, amount, maxSlippage) {
2239
2664
  if (maxSlippage === void 0) { maxSlippage = 0.02; }
2240
2665
  return __awaiter(void 0, void 0, void 0, function () {
2241
- var _a, inputCoinAddress, outputCoinAddress, inputCoinBalance, _b, inputCoinDecimals, outputCoinDecimals, _amount, _c, route, indices, _expected, minRecvAmountBN, _minRecvAmount, value, routerContract;
2242
- return __generator(this, function (_d) {
2243
- switch (_d.label) {
2666
+ var _a, inputCoinAddress, outputCoinAddress, inputCoinBalance, _c, inputCoinDecimals, outputCoinDecimals, _amount, _d, route, indices, _expected, minRecvAmountBN, _minRecvAmount, value, routerContract;
2667
+ return __generator(this, function (_e) {
2668
+ switch (_e.label) {
2244
2669
  case 0: return [4 /*yield*/, (0, exports.crossAssetExchangeAvailable)(inputCoin, outputCoin)];
2245
2670
  case 1:
2246
- if (!(_d.sent()))
2671
+ if (!(_e.sent()))
2247
2672
  throw Error("Such exchange is not available");
2248
2673
  _a = (0, utils_1._getCoinAddresses)(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
2249
2674
  return [4 /*yield*/, (0, utils_1._getBalances)([inputCoinAddress], [curve_1.curve.signerAddress])];
2250
2675
  case 2:
2251
- inputCoinBalance = (_d.sent())[curve_1.curve.signerAddress];
2676
+ inputCoinBalance = (_e.sent())[curve_1.curve.signerAddress];
2252
2677
  if (Number(inputCoinBalance) < Number(amount)) {
2253
2678
  throw Error("Not enough " + inputCoin + ". Actual: " + inputCoinBalance + ", required: " + amount);
2254
2679
  }
2255
2680
  return [4 /*yield*/, (0, utils_1.hasAllowance)([inputCoinAddress], [amount], curve_1.curve.signerAddress, curve_1.ALIASES.router)];
2256
2681
  case 3:
2257
- if (!(_d.sent())) {
2682
+ if (!(_e.sent())) {
2258
2683
  throw Error("Token allowance is needed to estimate gas");
2259
2684
  }
2260
- _b = (0, utils_1._getCoinDecimals)(inputCoinAddress, outputCoinAddress), inputCoinDecimals = _b[0], outputCoinDecimals = _b[1];
2685
+ _c = (0, utils_1._getCoinDecimals)(inputCoinAddress, outputCoinAddress), inputCoinDecimals = _c[0], outputCoinDecimals = _c[1];
2261
2686
  _amount = ethers_1.ethers.utils.parseUnits(amount, inputCoinDecimals);
2262
2687
  return [4 /*yield*/, (0, exports._crossAssetExchangeInfo)(inputCoinAddress, outputCoinAddress, inputCoinDecimals, outputCoinDecimals, amount)];
2263
2688
  case 4:
2264
- _c = _d.sent(), route = _c.route, indices = _c.indices, _expected = _c._expected;
2689
+ _d = _e.sent(), route = _d.route, indices = _d.indices, _expected = _d._expected;
2265
2690
  minRecvAmountBN = (0, utils_1.toBN)(_expected, outputCoinDecimals).times((1 - maxSlippage));
2266
2691
  _minRecvAmount = (0, utils_1.fromBN)(minRecvAmountBN, outputCoinDecimals);
2267
2692
  value = (0, utils_1.isEth)(inputCoinAddress) ? _amount : 0;
2268
2693
  return [4 /*yield*/, curve_1.curve.contracts[curve_1.ALIASES.router].contract];
2269
2694
  case 5:
2270
- routerContract = _d.sent();
2695
+ routerContract = _e.sent();
2271
2696
  return [4 /*yield*/, routerContract.estimateGas.exchange(_amount, route, indices, _minRecvAmount, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
2272
- case 6: return [2 /*return*/, (_d.sent()).toNumber()];
2697
+ case 6: return [2 /*return*/, (_e.sent()).toNumber()];
2273
2698
  }
2274
2699
  });
2275
2700
  });
@@ -2278,36 +2703,36 @@ exports.crossAssetExchangeEstimateGas = crossAssetExchangeEstimateGas;
2278
2703
  var crossAssetExchange = function (inputCoin, outputCoin, amount, maxSlippage) {
2279
2704
  if (maxSlippage === void 0) { maxSlippage = 0.02; }
2280
2705
  return __awaiter(void 0, void 0, void 0, function () {
2281
- var _a, inputCoinAddress, outputCoinAddress, _b, inputCoinDecimals, outputCoinDecimals, _amount, _c, route, indices, _expected, minRecvAmountBN, _minRecvAmount, value, routerContract, gasLimit;
2282
- return __generator(this, function (_d) {
2283
- switch (_d.label) {
2706
+ var _a, inputCoinAddress, outputCoinAddress, _c, inputCoinDecimals, outputCoinDecimals, _amount, _d, route, indices, _expected, minRecvAmountBN, _minRecvAmount, value, routerContract, gasLimit;
2707
+ return __generator(this, function (_e) {
2708
+ switch (_e.label) {
2284
2709
  case 0: return [4 /*yield*/, (0, exports.crossAssetExchangeAvailable)(inputCoin, outputCoin)];
2285
2710
  case 1:
2286
- if (!(_d.sent()))
2711
+ if (!(_e.sent()))
2287
2712
  throw Error("Such exchange is not available");
2288
2713
  _a = (0, utils_1._getCoinAddresses)(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
2289
- _b = (0, utils_1._getCoinDecimals)(inputCoinAddress, outputCoinAddress), inputCoinDecimals = _b[0], outputCoinDecimals = _b[1];
2714
+ _c = (0, utils_1._getCoinDecimals)(inputCoinAddress, outputCoinAddress), inputCoinDecimals = _c[0], outputCoinDecimals = _c[1];
2290
2715
  _amount = ethers_1.ethers.utils.parseUnits(amount, inputCoinDecimals);
2291
2716
  return [4 /*yield*/, (0, exports._crossAssetExchangeInfo)(inputCoinAddress, outputCoinAddress, inputCoinDecimals, outputCoinDecimals, amount)];
2292
2717
  case 2:
2293
- _c = _d.sent(), route = _c.route, indices = _c.indices, _expected = _c._expected;
2718
+ _d = _e.sent(), route = _d.route, indices = _d.indices, _expected = _d._expected;
2294
2719
  minRecvAmountBN = (0, utils_1.toBN)(_expected, outputCoinDecimals).times((1 - maxSlippage));
2295
2720
  _minRecvAmount = (0, utils_1.fromBN)(minRecvAmountBN, outputCoinDecimals);
2296
2721
  value = (0, utils_1.isEth)(inputCoinAddress) ? _amount : 0;
2297
2722
  return [4 /*yield*/, curve_1.curve.contracts[curve_1.ALIASES.router].contract];
2298
2723
  case 3:
2299
- routerContract = _d.sent();
2724
+ routerContract = _e.sent();
2300
2725
  return [4 /*yield*/, (0, utils_1._ensureAllowance)([inputCoinAddress], [_amount], curve_1.ALIASES.router)];
2301
2726
  case 4:
2302
- _d.sent();
2727
+ _e.sent();
2303
2728
  return [4 /*yield*/, curve_1.curve.updateFeeData()];
2304
2729
  case 5:
2305
- _d.sent();
2730
+ _e.sent();
2306
2731
  return [4 /*yield*/, routerContract.estimateGas.exchange(_amount, route, indices, _minRecvAmount, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
2307
2732
  case 6:
2308
- gasLimit = (_d.sent()).mul(130).div(100);
2733
+ gasLimit = (_e.sent()).mul(130).div(100);
2309
2734
  return [4 /*yield*/, routerContract.exchange(_amount, route, indices, _minRecvAmount, __assign(__assign({}, curve_1.curve.options), { value: value, gasLimit: gasLimit }))];
2310
- case 7: return [2 /*return*/, (_d.sent()).hash];
2735
+ case 7: return [2 /*return*/, (_e.sent()).hash];
2311
2736
  }
2312
2737
  });
2313
2738
  });