@curvefi/api 1.2.0 → 1.4.2

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
@@ -46,16 +46,20 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
46
46
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
47
  }
48
48
  };
49
- var __spreadArray = (this && this.__spreadArray) || function (to, from) {
50
- for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
51
- to[j] = from[i];
52
- return to;
49
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
50
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
51
+ if (ar || !(i in from)) {
52
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
53
+ ar[i] = from[i];
54
+ }
55
+ }
56
+ return to.concat(ar || Array.prototype.slice.call(from));
53
57
  };
54
58
  var __importDefault = (this && this.__importDefault) || function (mod) {
55
59
  return (mod && mod.__esModule) ? mod : { "default": mod };
56
60
  };
57
61
  Object.defineProperty(exports, "__esModule", { value: true });
58
- exports.crossAssetExchange = exports.crossAssetExchangeExpected = exports.crossAssetExchangeOutputAndSlippage = exports._crossAssetExchangeInfo = exports._getSmallAmountForCoin = exports.crossAssetExchangeAvailable = exports.exchange = exports.exchangeExpected = exports.getBestPoolAndOutput = exports._getBestPoolAndOutput = exports.Pool = void 0;
62
+ exports.crossAssetExchange = exports.crossAssetExchangeEstimateGas = exports.crossAssetExchangeExpected = exports.crossAssetExchangeOutputAndSlippage = exports._crossAssetExchangeInfo = exports._getSmallAmountForCoin = exports.crossAssetExchangeAvailable = exports.exchange = exports.exchangeEstimateGas = exports.exchangeExpected = exports.getBestPoolAndOutput = exports._getBestPoolAndOutput = exports.Pool = void 0;
59
63
  var ethers_1 = require("ethers");
60
64
  var utils_1 = require("./utils");
61
65
  var registry_exchange_json_1 = __importDefault(require("./constants/abis/json/registry_exchange.json"));
@@ -130,6 +134,50 @@ var Pool = /** @class */ (function () {
130
134
  }
131
135
  });
132
136
  }); };
137
+ this.addLiquidityEstimateGas = function (amounts) { return __awaiter(_this, void 0, void 0, function () {
138
+ var balances, _a, _b, i, _amounts;
139
+ var _this = this;
140
+ return __generator(this, function (_c) {
141
+ switch (_c.label) {
142
+ case 0:
143
+ if (amounts.length !== this.underlyingCoinAddresses.length) {
144
+ throw Error(this.name + " pool has " + this.underlyingCoinAddresses.length + " coins (amounts provided for " + amounts.length + ")");
145
+ }
146
+ _b = (_a = Object).values;
147
+ return [4 /*yield*/, this.underlyingCoinBalances()];
148
+ case 1:
149
+ balances = _b.apply(_a, [_c.sent()]);
150
+ for (i = 0; i < balances.length; i++) {
151
+ if (Number(balances[i]) < Number(amounts[i])) {
152
+ throw Error("Not enough " + this.underlyingCoins[i] + ". Actual: " + balances[i] + ", required: " + amounts[i]);
153
+ }
154
+ }
155
+ return [4 /*yield*/, (0, utils_1.hasAllowance)(this.underlyingCoinAddresses, amounts, curve_1.curve.signerAddress, this.zap || this.swap)];
156
+ case 2:
157
+ if (!(_c.sent())) {
158
+ throw Error("Token allowance is needed to estimate gas");
159
+ }
160
+ _amounts = amounts.map(function (amount, i) {
161
+ return ethers_1.ethers.utils.parseUnits(amount, _this.underlyingDecimals[i]);
162
+ });
163
+ if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) return [3 /*break*/, 4];
164
+ return [4 /*yield*/, this._addLiquidityZap(_amounts, true)];
165
+ case 3: return [2 /*return*/, _c.sent()];
166
+ case 4:
167
+ if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 6];
168
+ return [4 /*yield*/, this._addLiquidity(_amounts, true, true)];
169
+ case 5: return [2 /*return*/, _c.sent()];
170
+ case 6:
171
+ if (!this.isMeta) return [3 /*break*/, 8];
172
+ return [4 /*yield*/, this._addLiquidityMetaZap(_amounts, true)];
173
+ case 7: return [2 /*return*/, _c.sent()];
174
+ case 8: return [4 /*yield*/, this._addLiquiditySwap(_amounts, true)];
175
+ case 9:
176
+ // Plain pools
177
+ return [2 /*return*/, _c.sent()];
178
+ }
179
+ });
180
+ }); };
133
181
  this.addLiquidity = function (amounts) { return __awaiter(_this, void 0, void 0, function () {
134
182
  var _amounts;
135
183
  var _this = this;
@@ -142,19 +190,22 @@ var Pool = /** @class */ (function () {
142
190
  _amounts = amounts.map(function (amount, i) {
143
191
  return ethers_1.ethers.utils.parseUnits(amount, _this.underlyingDecimals[i]);
144
192
  });
145
- if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) return [3 /*break*/, 2];
193
+ return [4 /*yield*/, curve_1.curve.updateFeeData()];
194
+ case 1:
195
+ _a.sent();
196
+ if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) return [3 /*break*/, 3];
146
197
  return [4 /*yield*/, this._addLiquidityZap(_amounts)];
147
- case 1: return [2 /*return*/, _a.sent()];
148
- case 2:
149
- if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 4];
198
+ case 2: return [2 /*return*/, _a.sent()];
199
+ case 3:
200
+ if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 5];
150
201
  return [4 /*yield*/, this._addLiquidity(_amounts, true)];
151
- case 3: return [2 /*return*/, _a.sent()];
152
- case 4:
153
- if (!this.isMeta) return [3 /*break*/, 6];
202
+ case 4: return [2 /*return*/, _a.sent()];
203
+ case 5:
204
+ if (!this.isMeta) return [3 /*break*/, 7];
154
205
  return [4 /*yield*/, this._addLiquidityMetaZap(_amounts)];
155
- case 5: return [2 /*return*/, _a.sent()];
156
- case 6: return [4 /*yield*/, this._addLiquiditySwap(_amounts)];
157
- case 7:
206
+ case 6: return [2 /*return*/, _a.sent()];
207
+ case 7: return [4 /*yield*/, this._addLiquiditySwap(_amounts)];
208
+ case 8:
158
209
  // Plain pools
159
210
  return [2 /*return*/, _a.sent()];
160
211
  }
@@ -168,6 +219,42 @@ var Pool = /** @class */ (function () {
168
219
  }
169
220
  });
170
221
  }); };
222
+ this.addLiquidityWrappedEstimateGas = function (amounts) { return __awaiter(_this, void 0, void 0, function () {
223
+ var balances, _a, _b, i, _amounts;
224
+ var _this = this;
225
+ return __generator(this, function (_c) {
226
+ switch (_c.label) {
227
+ case 0:
228
+ if (amounts.length !== this.coinAddresses.length) {
229
+ throw Error(this.name + " pool has " + this.coinAddresses.length + " coins (amounts provided for " + amounts.length + ")");
230
+ }
231
+ _b = (_a = Object).values;
232
+ return [4 /*yield*/, this.coinBalances()];
233
+ case 1:
234
+ balances = _b.apply(_a, [_c.sent()]);
235
+ for (i = 0; i < balances.length; i++) {
236
+ if (Number(balances[i]) < Number(amounts[i])) {
237
+ throw Error("Not enough " + this.coins[i] + ". Actual: " + balances[i] + ", required: " + amounts[i]);
238
+ }
239
+ }
240
+ return [4 /*yield*/, (0, utils_1.hasAllowance)(this.coinAddresses, amounts, curve_1.curve.signerAddress, this.zap || this.swap)];
241
+ case 2:
242
+ if (!(_c.sent())) {
243
+ throw Error("Token allowance is needed to estimate gas");
244
+ }
245
+ _amounts = amounts.map(function (amount, i) {
246
+ return ethers_1.ethers.utils.parseUnits(amount, _this.decimals[i]);
247
+ });
248
+ if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 4];
249
+ return [4 /*yield*/, this._addLiquidity(_amounts, false, true)];
250
+ case 3: return [2 /*return*/, _c.sent()];
251
+ case 4: return [4 /*yield*/, this._addLiquiditySwap(_amounts, true)];
252
+ case 5:
253
+ // Lending pools with zap and metapools
254
+ return [2 /*return*/, _c.sent()];
255
+ }
256
+ });
257
+ }); };
171
258
  this.addLiquidityWrapped = function (amounts) { return __awaiter(_this, void 0, void 0, function () {
172
259
  var _amounts;
173
260
  var _this = this;
@@ -180,11 +267,14 @@ var Pool = /** @class */ (function () {
180
267
  _amounts = amounts.map(function (amount, i) {
181
268
  return ethers_1.ethers.utils.parseUnits(amount, _this.decimals[i]);
182
269
  });
183
- if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 2];
270
+ return [4 /*yield*/, curve_1.curve.updateFeeData()];
271
+ case 1:
272
+ _a.sent();
273
+ if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 3];
184
274
  return [4 /*yield*/, this._addLiquidity(_amounts, false)];
185
- case 1: return [2 /*return*/, _a.sent()];
186
- case 2: return [4 /*yield*/, this._addLiquiditySwap(_amounts)];
187
- case 3:
275
+ case 2: return [2 /*return*/, _a.sent()];
276
+ case 3: return [4 /*yield*/, this._addLiquiditySwap(_amounts)];
277
+ case 4:
188
278
  // Lending pools with zap and metapools
189
279
  return [2 /*return*/, _a.sent()];
190
280
  }
@@ -216,25 +306,65 @@ var Pool = /** @class */ (function () {
216
306
  }
217
307
  });
218
308
  }); };
309
+ this.removeLiquidityEstimateGas = function (lpTokenAmount) { return __awaiter(_this, void 0, void 0, function () {
310
+ var lpTokenBalance, _a, _lpTokenAmount;
311
+ return __generator(this, function (_b) {
312
+ switch (_b.label) {
313
+ case 0: return [4 /*yield*/, this.lpTokenBalances()];
314
+ case 1:
315
+ lpTokenBalance = (_b.sent())['lpToken'];
316
+ if (Number(lpTokenBalance) < Number(lpTokenAmount)) {
317
+ throw Error("Not enough LP tokens. Actual: " + lpTokenBalance + ", required: " + lpTokenAmount);
318
+ }
319
+ _a = this.zap;
320
+ if (!_a) return [3 /*break*/, 3];
321
+ return [4 /*yield*/, (0, utils_1.hasAllowance)([this.lpToken], [lpTokenAmount], curve_1.curve.signerAddress, this.zap)];
322
+ case 2:
323
+ _a = !(_b.sent());
324
+ _b.label = 3;
325
+ case 3:
326
+ if (_a) {
327
+ throw Error("Token allowance is needed to estimate gas");
328
+ }
329
+ _lpTokenAmount = ethers_1.ethers.utils.parseUnits(lpTokenAmount);
330
+ if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) return [3 /*break*/, 5];
331
+ return [4 /*yield*/, this._removeLiquidityZap(_lpTokenAmount, true)];
332
+ case 4: return [2 /*return*/, _b.sent()];
333
+ case 5:
334
+ if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 7];
335
+ return [4 /*yield*/, this._removeLiquidity(_lpTokenAmount, true, true)];
336
+ case 6: return [2 /*return*/, _b.sent()];
337
+ case 7:
338
+ if (!this.isMeta) return [3 /*break*/, 9];
339
+ return [4 /*yield*/, this._removeLiquidityMetaZap(_lpTokenAmount, true)];
340
+ case 8: return [2 /*return*/, _b.sent()];
341
+ case 9: return [4 /*yield*/, this._removeLiquiditySwap(_lpTokenAmount, true)];
342
+ case 10: return [2 /*return*/, _b.sent()];
343
+ }
344
+ });
345
+ }); };
219
346
  this.removeLiquidity = function (lpTokenAmount) { return __awaiter(_this, void 0, void 0, function () {
220
347
  var _lpTokenAmount;
221
348
  return __generator(this, function (_a) {
222
349
  switch (_a.label) {
223
350
  case 0:
224
351
  _lpTokenAmount = ethers_1.ethers.utils.parseUnits(lpTokenAmount);
225
- if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) return [3 /*break*/, 2];
352
+ return [4 /*yield*/, curve_1.curve.updateFeeData()];
353
+ case 1:
354
+ _a.sent();
355
+ if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) return [3 /*break*/, 3];
226
356
  return [4 /*yield*/, this._removeLiquidityZap(_lpTokenAmount)];
227
- case 1: return [2 /*return*/, _a.sent()];
228
- case 2:
229
- if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 4];
357
+ case 2: return [2 /*return*/, _a.sent()];
358
+ case 3:
359
+ if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 5];
230
360
  return [4 /*yield*/, this._removeLiquidity(_lpTokenAmount, true)];
231
- case 3: return [2 /*return*/, _a.sent()];
232
- case 4:
233
- if (!this.isMeta) return [3 /*break*/, 6];
361
+ case 4: return [2 /*return*/, _a.sent()];
362
+ case 5:
363
+ if (!this.isMeta) return [3 /*break*/, 7];
234
364
  return [4 /*yield*/, this._removeLiquidityMetaZap(_lpTokenAmount)];
235
- case 5: return [2 /*return*/, _a.sent()];
236
- case 6: return [4 /*yield*/, this._removeLiquiditySwap(_lpTokenAmount)];
237
- case 7: return [2 /*return*/, _a.sent()];
365
+ case 6: return [2 /*return*/, _a.sent()];
366
+ case 7: return [4 /*yield*/, this._removeLiquiditySwap(_lpTokenAmount)];
367
+ case 8: return [2 /*return*/, _a.sent()];
238
368
  }
239
369
  });
240
370
  }); };
@@ -252,17 +382,40 @@ var Pool = /** @class */ (function () {
252
382
  }
253
383
  });
254
384
  }); };
385
+ this.removeLiquidityWrappedEstimateGas = function (lpTokenAmount) { return __awaiter(_this, void 0, void 0, function () {
386
+ var _lpTokenAmount, lpTokenBalance;
387
+ return __generator(this, function (_a) {
388
+ switch (_a.label) {
389
+ case 0:
390
+ _lpTokenAmount = ethers_1.ethers.utils.parseUnits(lpTokenAmount);
391
+ return [4 /*yield*/, this.lpTokenBalances()];
392
+ case 1:
393
+ lpTokenBalance = (_a.sent())['lpToken'];
394
+ if (Number(lpTokenBalance) < Number(lpTokenAmount)) {
395
+ throw Error("Not enough LP tokens. Actual: " + lpTokenBalance + ", required: " + lpTokenAmount);
396
+ }
397
+ if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 3];
398
+ return [4 /*yield*/, this._removeLiquidity(_lpTokenAmount, false, true)];
399
+ case 2: return [2 /*return*/, _a.sent()];
400
+ case 3: return [4 /*yield*/, this._removeLiquiditySwap(_lpTokenAmount)];
401
+ case 4: return [2 /*return*/, _a.sent()];
402
+ }
403
+ });
404
+ }); };
255
405
  this.removeLiquidityWrapped = function (lpTokenAmount) { return __awaiter(_this, void 0, void 0, function () {
256
406
  var _lpTokenAmount;
257
407
  return __generator(this, function (_a) {
258
408
  switch (_a.label) {
259
409
  case 0:
260
410
  _lpTokenAmount = ethers_1.ethers.utils.parseUnits(lpTokenAmount);
261
- if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 2];
411
+ return [4 /*yield*/, curve_1.curve.updateFeeData()];
412
+ case 1:
413
+ _a.sent();
414
+ if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 3];
262
415
  return [4 /*yield*/, this._removeLiquidity(_lpTokenAmount, false)];
263
- case 1: return [2 /*return*/, _a.sent()];
264
- case 2: return [4 /*yield*/, this._removeLiquiditySwap(_lpTokenAmount)];
265
- case 3: return [2 /*return*/, _a.sent()];
416
+ case 2: return [2 /*return*/, _a.sent()];
417
+ case 3: return [4 /*yield*/, this._removeLiquiditySwap(_lpTokenAmount)];
418
+ case 4: return [2 /*return*/, _a.sent()];
266
419
  }
267
420
  });
268
421
  }); };
@@ -274,6 +427,47 @@ var Pool = /** @class */ (function () {
274
427
  }
275
428
  });
276
429
  }); };
430
+ this.removeLiquidityImbalanceEstimateGas = function (amounts) { return __awaiter(_this, void 0, void 0, function () {
431
+ var lpTokenAmount, lpTokenBalance, _a, _amounts;
432
+ var _this = this;
433
+ return __generator(this, function (_b) {
434
+ switch (_b.label) {
435
+ case 0: return [4 /*yield*/, this.removeLiquidityImbalanceExpected(amounts)];
436
+ case 1:
437
+ lpTokenAmount = _b.sent();
438
+ return [4 /*yield*/, this.lpTokenBalances()];
439
+ case 2:
440
+ lpTokenBalance = (_b.sent())['lpToken'];
441
+ if (Number(lpTokenBalance) < Number(lpTokenAmount)) {
442
+ throw Error("Not enough LP tokens. Actual: " + lpTokenBalance + ", required: " + lpTokenAmount);
443
+ }
444
+ _a = this.zap;
445
+ if (!_a) return [3 /*break*/, 4];
446
+ return [4 /*yield*/, (0, utils_1.hasAllowance)([this.lpToken], [lpTokenAmount], curve_1.curve.signerAddress, this.zap)];
447
+ case 3:
448
+ _a = !(_b.sent());
449
+ _b.label = 4;
450
+ case 4:
451
+ if (_a) {
452
+ throw Error("Token allowance is needed to estimate gas");
453
+ }
454
+ _amounts = amounts.map(function (amount, i) { return ethers_1.ethers.utils.parseUnits(amount, _this.underlyingDecimals[i]); });
455
+ if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) return [3 /*break*/, 6];
456
+ return [4 /*yield*/, this._removeLiquidityImbalanceZap(_amounts, true)];
457
+ case 5: return [2 /*return*/, _b.sent()];
458
+ case 6:
459
+ if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 8];
460
+ return [4 /*yield*/, this._removeLiquidityImbalance(_amounts, true, true)];
461
+ case 7: return [2 /*return*/, _b.sent()];
462
+ case 8:
463
+ if (!this.isMeta) return [3 /*break*/, 10];
464
+ return [4 /*yield*/, this._removeLiquidityImbalanceMetaZap(_amounts, true)];
465
+ case 9: return [2 /*return*/, _b.sent()];
466
+ case 10: return [4 /*yield*/, this._removeLiquidityImbalanceSwap(_amounts, true)];
467
+ case 11: return [2 /*return*/, _b.sent()];
468
+ }
469
+ });
470
+ }); };
277
471
  this.removeLiquidityImbalance = function (amounts) { return __awaiter(_this, void 0, void 0, function () {
278
472
  var _amounts;
279
473
  var _this = this;
@@ -281,19 +475,22 @@ var Pool = /** @class */ (function () {
281
475
  switch (_a.label) {
282
476
  case 0:
283
477
  _amounts = amounts.map(function (amount, i) { return ethers_1.ethers.utils.parseUnits(amount, _this.underlyingDecimals[i]); });
284
- if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) return [3 /*break*/, 2];
478
+ return [4 /*yield*/, curve_1.curve.updateFeeData()];
479
+ case 1:
480
+ _a.sent();
481
+ if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) return [3 /*break*/, 3];
285
482
  return [4 /*yield*/, this._removeLiquidityImbalanceZap(_amounts)];
286
- case 1: return [2 /*return*/, _a.sent()];
287
- case 2:
288
- if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 4];
483
+ case 2: return [2 /*return*/, _a.sent()];
484
+ case 3:
485
+ if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 5];
289
486
  return [4 /*yield*/, this._removeLiquidityImbalance(_amounts, true)];
290
- case 3: return [2 /*return*/, _a.sent()];
291
- case 4:
292
- if (!this.isMeta) return [3 /*break*/, 6];
487
+ case 4: return [2 /*return*/, _a.sent()];
488
+ case 5:
489
+ if (!this.isMeta) return [3 /*break*/, 7];
293
490
  return [4 /*yield*/, this._removeLiquidityImbalanceMetaZap(_amounts)];
294
- case 5: return [2 /*return*/, _a.sent()];
295
- case 6: return [4 /*yield*/, this._removeLiquidityImbalanceSwap(_amounts)];
296
- case 7: return [2 /*return*/, _a.sent()];
491
+ case 6: return [2 /*return*/, _a.sent()];
492
+ case 7: return [4 /*yield*/, this._removeLiquidityImbalanceSwap(_amounts)];
493
+ case 8: return [2 /*return*/, _a.sent()];
297
494
  }
298
495
  });
299
496
  }); };
@@ -305,21 +502,50 @@ var Pool = /** @class */ (function () {
305
502
  }
306
503
  });
307
504
  }); };
308
- this.removeLiquidityImbalanceWrapped = function (amounts) { return __awaiter(_this, void 0, void 0, function () {
309
- var _amounts;
505
+ this.removeLiquidityImbalanceWrappedEstimateGas = function (amounts) { return __awaiter(_this, void 0, void 0, function () {
506
+ var lpTokenAmount, lpTokenBalance, _amounts;
310
507
  var _this = this;
311
508
  return __generator(this, function (_a) {
312
509
  switch (_a.label) {
313
- case 0:
510
+ case 0: return [4 /*yield*/, this.removeLiquidityImbalanceExpected(amounts)];
511
+ case 1:
512
+ lpTokenAmount = _a.sent();
513
+ return [4 /*yield*/, this.lpTokenBalances()];
514
+ case 2:
515
+ lpTokenBalance = (_a.sent())['lpToken'];
516
+ if (Number(lpTokenBalance) < Number(lpTokenAmount)) {
517
+ throw Error("Not enough LP tokens. Actual: " + lpTokenBalance + ", required: " + lpTokenAmount);
518
+ }
314
519
  _amounts = amounts.map(function (amount, i) { return ethers_1.ethers.utils.parseUnits(amount, _this.decimals[i]); });
315
- if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 2];
316
- return [4 /*yield*/, this._removeLiquidityImbalance(_amounts, false)];
317
- case 1: return [2 /*return*/, _a.sent()];
318
- case 2: return [4 /*yield*/, this._removeLiquidityImbalanceSwap(_amounts)];
520
+ if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 4];
521
+ return [4 /*yield*/, this._removeLiquidityImbalance(_amounts, false, true)];
319
522
  case 3: return [2 /*return*/, _a.sent()];
523
+ case 4: return [4 /*yield*/, this._removeLiquidityImbalanceSwap(_amounts, true)];
524
+ case 5: return [2 /*return*/, _a.sent()];
320
525
  }
321
526
  });
322
527
  }); };
528
+ this.removeLiquidityImbalanceWrapped = function (amounts, estimateGas) {
529
+ if (estimateGas === void 0) { estimateGas = false; }
530
+ return __awaiter(_this, void 0, void 0, function () {
531
+ var _amounts;
532
+ var _this = this;
533
+ return __generator(this, function (_a) {
534
+ switch (_a.label) {
535
+ case 0:
536
+ _amounts = amounts.map(function (amount, i) { return ethers_1.ethers.utils.parseUnits(amount, _this.decimals[i]); });
537
+ return [4 /*yield*/, curve_1.curve.updateFeeData()];
538
+ case 1:
539
+ _a.sent();
540
+ if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 3];
541
+ return [4 /*yield*/, this._removeLiquidityImbalance(_amounts, false, estimateGas)];
542
+ case 2: return [2 /*return*/, _a.sent()];
543
+ case 3: return [4 /*yield*/, this._removeLiquidityImbalanceSwap(_amounts, estimateGas)];
544
+ case 4: return [2 /*return*/, _a.sent()];
545
+ }
546
+ });
547
+ });
548
+ };
323
549
  this.removeLiquidityOneCoinExpected = function (lpTokenAmount, coin) { return __awaiter(_this, void 0, void 0, function () {
324
550
  var i, _lpTokenAmount, _expected;
325
551
  return __generator(this, function (_a) {
@@ -346,6 +572,42 @@ var Pool = /** @class */ (function () {
346
572
  }
347
573
  });
348
574
  }); };
575
+ this.removeLiquidityOneCoinEstimateGas = function (lpTokenAmount, coin) { return __awaiter(_this, void 0, void 0, function () {
576
+ var lpTokenBalance, _a, i, _lpTokenAmount;
577
+ return __generator(this, function (_b) {
578
+ switch (_b.label) {
579
+ case 0: return [4 /*yield*/, this.lpTokenBalances()];
580
+ case 1:
581
+ lpTokenBalance = (_b.sent())['lpToken'];
582
+ if (Number(lpTokenBalance) < Number(lpTokenAmount)) {
583
+ throw Error("Not enough LP tokens. Actual: " + lpTokenBalance + ", required: " + lpTokenAmount);
584
+ }
585
+ _a = this.zap;
586
+ if (!_a) return [3 /*break*/, 3];
587
+ return [4 /*yield*/, (0, utils_1.hasAllowance)([this.lpToken], [lpTokenAmount], curve_1.curve.signerAddress, this.zap)];
588
+ case 2:
589
+ _a = !(_b.sent());
590
+ _b.label = 3;
591
+ case 3:
592
+ if (_a) {
593
+ throw Error("Token allowance is needed to estimate gas");
594
+ }
595
+ i = this._getCoinIdx(coin);
596
+ _lpTokenAmount = ethers_1.ethers.utils.parseUnits(lpTokenAmount);
597
+ if (!(['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name) || this.name === 'susd' || this.isMeta)) return [3 /*break*/, 5];
598
+ return [4 /*yield*/, this._removeLiquidityOneCoinZap(_lpTokenAmount, i, true)];
599
+ case 4: return [2 /*return*/, _b.sent()];
600
+ case 5:
601
+ if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 7];
602
+ return [4 /*yield*/, this._removeLiquidityOneCoin(_lpTokenAmount, i, true, true)];
603
+ case 6: return [2 /*return*/, _b.sent()];
604
+ case 7: return [4 /*yield*/, this._removeLiquidityOneCoinSwap(_lpTokenAmount, i, true)];
605
+ case 8:
606
+ // Plain pools
607
+ return [2 /*return*/, _b.sent()];
608
+ }
609
+ });
610
+ }); };
349
611
  this.removeLiquidityOneCoin = function (lpTokenAmount, coin) { return __awaiter(_this, void 0, void 0, function () {
350
612
  var i, _lpTokenAmount;
351
613
  return __generator(this, function (_a) {
@@ -353,15 +615,18 @@ var Pool = /** @class */ (function () {
353
615
  case 0:
354
616
  i = this._getCoinIdx(coin);
355
617
  _lpTokenAmount = ethers_1.ethers.utils.parseUnits(lpTokenAmount);
356
- if (!(['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name) || this.name === 'susd' || this.isMeta)) return [3 /*break*/, 2];
618
+ return [4 /*yield*/, curve_1.curve.updateFeeData()];
619
+ case 1:
620
+ _a.sent();
621
+ if (!(['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name) || this.name === 'susd' || this.isMeta)) return [3 /*break*/, 3];
357
622
  return [4 /*yield*/, this._removeLiquidityOneCoinZap(_lpTokenAmount, i)];
358
- case 1: return [2 /*return*/, _a.sent()];
359
- case 2:
360
- if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 4];
623
+ case 2: return [2 /*return*/, _a.sent()];
624
+ case 3:
625
+ if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 5];
361
626
  return [4 /*yield*/, this._removeLiquidityOneCoin(_lpTokenAmount, i, true)];
362
- case 3: return [2 /*return*/, _a.sent()];
363
- case 4: return [4 /*yield*/, this._removeLiquidityOneCoinSwap(_lpTokenAmount, i)];
364
- case 5:
627
+ case 4: return [2 /*return*/, _a.sent()];
628
+ case 5: return [4 /*yield*/, this._removeLiquidityOneCoinSwap(_lpTokenAmount, i)];
629
+ case 6:
365
630
  // Plain pools
366
631
  return [2 /*return*/, _a.sent()];
367
632
  }
@@ -387,6 +652,29 @@ var Pool = /** @class */ (function () {
387
652
  }
388
653
  });
389
654
  }); };
655
+ this.removeLiquidityOneCoinWrappedEstimateGas = function (lpTokenAmount, coin) { return __awaiter(_this, void 0, void 0, function () {
656
+ var lpTokenBalance, i, _lpTokenAmount;
657
+ return __generator(this, function (_a) {
658
+ switch (_a.label) {
659
+ case 0: return [4 /*yield*/, this.lpTokenBalances()];
660
+ case 1:
661
+ lpTokenBalance = (_a.sent())['lpToken'];
662
+ if (Number(lpTokenBalance) < Number(lpTokenAmount)) {
663
+ throw Error("Not enough LP tokens. Actual: " + lpTokenBalance + ", required: " + lpTokenAmount);
664
+ }
665
+ i = this._getCoinIdx(coin, false);
666
+ if (['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) {
667
+ throw Error(this.name + " pool doesn't have remove_liquidity_one_coin method for wrapped tokens");
668
+ }
669
+ _lpTokenAmount = ethers_1.ethers.utils.parseUnits(lpTokenAmount);
670
+ if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 3];
671
+ return [4 /*yield*/, this._removeLiquidityOneCoin(_lpTokenAmount, i, false, true)];
672
+ case 2: return [2 /*return*/, _a.sent()];
673
+ case 3: return [4 /*yield*/, this._removeLiquidityOneCoinSwap(_lpTokenAmount, i, true)];
674
+ case 4: return [2 /*return*/, _a.sent()];
675
+ }
676
+ });
677
+ }); };
390
678
  this.removeLiquidityOneCoinWrapped = function (lpTokenAmount, coin) { return __awaiter(_this, void 0, void 0, function () {
391
679
  var i, _lpTokenAmount;
392
680
  return __generator(this, function (_a) {
@@ -396,12 +684,26 @@ var Pool = /** @class */ (function () {
396
684
  if (['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) {
397
685
  throw Error(this.name + " pool doesn't have remove_liquidity_one_coin method for wrapped tokens");
398
686
  }
687
+ return [4 /*yield*/, curve_1.curve.updateFeeData()];
688
+ case 1:
689
+ _a.sent();
399
690
  _lpTokenAmount = ethers_1.ethers.utils.parseUnits(lpTokenAmount);
400
- if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 2];
691
+ if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 3];
401
692
  return [4 /*yield*/, this._removeLiquidityOneCoin(_lpTokenAmount, i, false)];
402
- case 1: return [2 /*return*/, _a.sent()];
403
- case 2: return [4 /*yield*/, this._removeLiquidityOneCoinSwap(_lpTokenAmount, i)];
404
- case 3: return [2 /*return*/, _a.sent()];
693
+ case 2: return [2 /*return*/, _a.sent()];
694
+ case 3: return [4 /*yield*/, this._removeLiquidityOneCoinSwap(_lpTokenAmount, i)];
695
+ case 4: return [2 /*return*/, _a.sent()];
696
+ }
697
+ });
698
+ }); };
699
+ this.gaugeDepositEstimateGas = function (lpTokenAmount) { return __awaiter(_this, void 0, void 0, function () {
700
+ var _lpTokenAmount;
701
+ return __generator(this, function (_a) {
702
+ switch (_a.label) {
703
+ case 0:
704
+ _lpTokenAmount = ethers_1.ethers.utils.parseUnits(lpTokenAmount);
705
+ return [4 /*yield*/, curve_1.curve.contracts[this.gauge].contract.estimateGas.deposit(_lpTokenAmount, curve_1.curve.options)];
706
+ case 1: return [2 /*return*/, (_a.sent()).toNumber()];
405
707
  }
406
708
  });
407
709
  }); };
@@ -411,7 +713,7 @@ var Pool = /** @class */ (function () {
411
713
  switch (_a.label) {
412
714
  case 0:
413
715
  _lpTokenAmount = ethers_1.ethers.utils.parseUnits(lpTokenAmount);
414
- return [4 /*yield*/, utils_1._ensureAllowance([this.lpToken], [_lpTokenAmount], this.gauge)];
716
+ return [4 /*yield*/, (0, utils_1._ensureAllowance)([this.lpToken], [_lpTokenAmount], this.gauge)];
415
717
  case 1:
416
718
  _a.sent();
417
719
  return [4 /*yield*/, curve_1.curve.contracts[this.gauge].contract.deposit(_lpTokenAmount, curve_1.curve.options)];
@@ -419,6 +721,17 @@ var Pool = /** @class */ (function () {
419
721
  }
420
722
  });
421
723
  }); };
724
+ this.gaugeWithdrawEstimateGas = function (lpTokenAmount) { return __awaiter(_this, void 0, void 0, function () {
725
+ var _lpTokenAmount;
726
+ return __generator(this, function (_a) {
727
+ switch (_a.label) {
728
+ case 0:
729
+ _lpTokenAmount = ethers_1.ethers.utils.parseUnits(lpTokenAmount);
730
+ return [4 /*yield*/, curve_1.curve.contracts[this.gauge].contract.estimateGas.withdraw(_lpTokenAmount, curve_1.curve.options)];
731
+ case 1: return [2 /*return*/, (_a.sent()).toNumber()];
732
+ }
733
+ });
734
+ }); };
422
735
  this.gaugeWithdraw = function (lpTokenAmount) { return __awaiter(_this, void 0, void 0, function () {
423
736
  var _lpTokenAmount;
424
737
  return __generator(this, function (_a) {
@@ -438,7 +751,7 @@ var Pool = /** @class */ (function () {
438
751
  return __awaiter(_this, void 0, void 0, function () {
439
752
  return __generator(this, function (_b) {
440
753
  switch (_b.label) {
441
- case 0: return [4 /*yield*/, this._balances.apply(this, __spreadArray([__spreadArray(__spreadArray(['lpToken', 'gauge'], this.underlyingCoins), this.coins), __spreadArray(__spreadArray([this.lpToken, this.gauge], this.underlyingCoinAddresses), this.coinAddresses)], addresses))];
754
+ 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))];
442
755
  case 1: return [2 /*return*/, _b.sent()];
443
756
  }
444
757
  });
@@ -452,7 +765,7 @@ var Pool = /** @class */ (function () {
452
765
  return __awaiter(_this, void 0, void 0, function () {
453
766
  return __generator(this, function (_b) {
454
767
  switch (_b.label) {
455
- case 0: return [4 /*yield*/, this._balances.apply(this, __spreadArray([['lpToken', 'gauge'], [this.lpToken, this.gauge]], addresses))];
768
+ case 0: return [4 /*yield*/, this._balances.apply(this, __spreadArray([['lpToken', 'gauge'], [this.lpToken, this.gauge]], addresses, false))];
456
769
  case 1: return [2 /*return*/, _b.sent()];
457
770
  }
458
771
  });
@@ -466,7 +779,7 @@ var Pool = /** @class */ (function () {
466
779
  return __awaiter(_this, void 0, void 0, function () {
467
780
  return __generator(this, function (_b) {
468
781
  switch (_b.label) {
469
- case 0: return [4 /*yield*/, this._balances.apply(this, __spreadArray([this.underlyingCoins, this.underlyingCoinAddresses], addresses))];
782
+ case 0: return [4 /*yield*/, this._balances.apply(this, __spreadArray([this.underlyingCoins, this.underlyingCoinAddresses], addresses, false))];
470
783
  case 1: return [2 /*return*/, _b.sent()];
471
784
  }
472
785
  });
@@ -480,7 +793,7 @@ var Pool = /** @class */ (function () {
480
793
  return __awaiter(_this, void 0, void 0, function () {
481
794
  return __generator(this, function (_b) {
482
795
  switch (_b.label) {
483
- case 0: return [4 /*yield*/, this._balances.apply(this, __spreadArray([this.coins, this.coinAddresses], addresses))];
796
+ case 0: return [4 /*yield*/, this._balances.apply(this, __spreadArray([this.coins, this.coinAddresses], addresses, false))];
484
797
  case 1: return [2 /*return*/, _b.sent()];
485
798
  }
486
799
  });
@@ -494,7 +807,7 @@ var Pool = /** @class */ (function () {
494
807
  return __awaiter(_this, void 0, void 0, function () {
495
808
  return __generator(this, function (_b) {
496
809
  switch (_b.label) {
497
- case 0: return [4 /*yield*/, this._balances.apply(this, __spreadArray([__spreadArray(__spreadArray([], this.underlyingCoins), this.coins), __spreadArray(__spreadArray([], this.underlyingCoinAddresses), this.coinAddresses)], addresses))];
810
+ 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))];
498
811
  case 1: return [2 /*return*/, _b.sent()];
499
812
  }
500
813
  });
@@ -515,6 +828,44 @@ var Pool = /** @class */ (function () {
515
828
  }
516
829
  });
517
830
  }); };
831
+ this.exchangeEstimateGas = function (inputCoin, outputCoin, amount, maxSlippage) {
832
+ if (maxSlippage === void 0) { maxSlippage = 0.01; }
833
+ 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) {
837
+ case 0:
838
+ if (this.name === 'tricrypto2') {
839
+ throw Error("Use exchangeTricryptoEstimateGas method for tricrypto2 pool instead");
840
+ }
841
+ i = this._getCoinIdx(inputCoin);
842
+ j = this._getCoinIdx(outputCoin);
843
+ _b = (_a = Object).values;
844
+ return [4 /*yield*/, this.underlyingCoinBalances()];
845
+ case 1:
846
+ inputCoinBalance = _b.apply(_a, [_c.sent()])[i];
847
+ if (Number(inputCoinBalance) < Number(amount)) {
848
+ throw Error("Not enough " + this.underlyingCoins[i] + ". Actual: " + inputCoinBalance + ", required: " + amount);
849
+ }
850
+ return [4 /*yield*/, (0, utils_1.hasAllowance)([this.underlyingCoinAddresses[i]], [amount], curve_1.curve.signerAddress, this.swap)];
851
+ case 2:
852
+ if (!(_c.sent())) {
853
+ throw Error("Token allowance is needed to estimate gas");
854
+ }
855
+ _amount = ethers_1.ethers.utils.parseUnits(amount, this.underlyingDecimals[i]);
856
+ return [4 /*yield*/, this._getExchangeOutput(i, j, _amount)];
857
+ case 3:
858
+ _expected = _c.sent();
859
+ _minRecvAmount = _expected.mul((1 - maxSlippage) * 100).div(100);
860
+ contract = curve_1.curve.contracts[this.swap].contract;
861
+ exchangeMethod = Object.prototype.hasOwnProperty.call(contract, 'exchange_underlying') ? 'exchange_underlying' : 'exchange';
862
+ value = (0, utils_1.isEth)(this.underlyingCoinAddresses[i]) ? _amount : ethers_1.ethers.BigNumber.from(0);
863
+ 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()];
865
+ }
866
+ });
867
+ });
868
+ };
518
869
  this.exchange = function (inputCoin, outputCoin, amount, maxSlippage) {
519
870
  if (maxSlippage === void 0) { maxSlippage = 0.01; }
520
871
  return __awaiter(_this, void 0, void 0, function () {
@@ -532,17 +883,58 @@ var Pool = /** @class */ (function () {
532
883
  case 1:
533
884
  _expected = _a.sent();
534
885
  _minRecvAmount = _expected.mul((1 - maxSlippage) * 100).div(100);
535
- return [4 /*yield*/, utils_1._ensureAllowance([this.underlyingCoinAddresses[i]], [_amount], this.swap)];
886
+ return [4 /*yield*/, (0, utils_1._ensureAllowance)([this.underlyingCoinAddresses[i]], [_amount], this.swap)];
536
887
  case 2:
537
888
  _a.sent();
538
889
  contract = curve_1.curve.contracts[this.swap].contract;
539
890
  exchangeMethod = Object.prototype.hasOwnProperty.call(contract, 'exchange_underlying') ? 'exchange_underlying' : 'exchange';
540
- value = utils_1.isEth(this.underlyingCoinAddresses[i]) ? _amount : ethers_1.ethers.BigNumber.from(0);
541
- return [4 /*yield*/, contract.estimateGas[exchangeMethod](i, j, _amount, _minRecvAmount, __assign(__assign({}, curve_1.curve.options), { value: value }))];
891
+ value = (0, utils_1.isEth)(this.underlyingCoinAddresses[i]) ? _amount : ethers_1.ethers.BigNumber.from(0);
892
+ return [4 /*yield*/, curve_1.curve.updateFeeData()];
542
893
  case 3:
894
+ _a.sent();
895
+ return [4 /*yield*/, contract.estimateGas[exchangeMethod](i, j, _amount, _minRecvAmount, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
896
+ case 4:
543
897
  gasLimit = (_a.sent()).mul(130).div(100);
544
898
  return [4 /*yield*/, contract[exchangeMethod](i, j, _amount, _minRecvAmount, __assign(__assign({}, curve_1.curve.options), { value: value, gasLimit: gasLimit }))];
545
- case 4: return [2 /*return*/, (_a.sent()).hash];
899
+ case 5: return [2 /*return*/, (_a.sent()).hash];
900
+ }
901
+ });
902
+ });
903
+ };
904
+ this.exchangeTricryptoEstimateGas = function (inputCoin, outputCoin, amount, maxSlippage, useEth) {
905
+ if (maxSlippage === void 0) { maxSlippage = 0.01; }
906
+ if (useEth === void 0) { useEth = false; }
907
+ 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) {
911
+ case 0:
912
+ if (this.name !== 'tricrypto2') {
913
+ throw Error("This method is for only tricrypto2 pool");
914
+ }
915
+ i = this._getCoinIdx(inputCoin);
916
+ j = this._getCoinIdx(outputCoin);
917
+ _b = (_a = Object).values;
918
+ return [4 /*yield*/, this.underlyingCoinBalances()];
919
+ case 1:
920
+ inputCoinBalance = _b.apply(_a, [_c.sent()])[i];
921
+ if (Number(inputCoinBalance) < Number(amount)) {
922
+ throw Error("Not enough " + this.underlyingCoins[i] + ". Actual: " + inputCoinBalance + ", required: " + amount);
923
+ }
924
+ return [4 /*yield*/, (0, utils_1.hasAllowance)([this.underlyingCoinAddresses[i]], [amount], curve_1.curve.signerAddress, this.swap)];
925
+ case 2:
926
+ if (!(_c.sent())) {
927
+ throw Error("Token allowance is needed to estimate gas");
928
+ }
929
+ _amount = ethers_1.ethers.utils.parseUnits(amount, this.underlyingDecimals[i]);
930
+ return [4 /*yield*/, this._getExchangeOutput(i, j, _amount)];
931
+ case 3:
932
+ _expected = _c.sent();
933
+ _minRecvAmount = _expected.mul((1 - maxSlippage) * 100).div(100);
934
+ contract = curve_1.curve.contracts[this.swap].contract;
935
+ value = useEth && i == 2 ? _amount : ethers_1.ethers.BigNumber.from(0);
936
+ 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()];
546
938
  }
547
939
  });
548
940
  });
@@ -566,18 +958,21 @@ var Pool = /** @class */ (function () {
566
958
  _expected = _a.sent();
567
959
  _minRecvAmount = _expected.mul((1 - maxSlippage) * 100).div(100);
568
960
  if (!(i !== 2 || !useEth)) return [3 /*break*/, 3];
569
- return [4 /*yield*/, utils_1._ensureAllowance([this.underlyingCoinAddresses[i]], [_amount], this.swap)];
961
+ return [4 /*yield*/, (0, utils_1._ensureAllowance)([this.underlyingCoinAddresses[i]], [_amount], this.swap)];
570
962
  case 2:
571
963
  _a.sent();
572
964
  _a.label = 3;
573
965
  case 3:
574
966
  contract = curve_1.curve.contracts[this.swap].contract;
575
967
  value = useEth && i == 2 ? _amount : ethers_1.ethers.BigNumber.from(0);
576
- return [4 /*yield*/, contract.estimateGas.exchange(i, j, _amount, _minRecvAmount, useEth, __assign(__assign({}, curve_1.curve.options), { value: value }))];
968
+ return [4 /*yield*/, curve_1.curve.updateFeeData()];
577
969
  case 4:
970
+ _a.sent();
971
+ return [4 /*yield*/, contract.estimateGas.exchange(i, j, _amount, _minRecvAmount, useEth, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
972
+ case 5:
578
973
  gasLimit = (_a.sent()).mul(130).div(100);
579
974
  return [4 /*yield*/, contract.exchange(i, j, _amount, _minRecvAmount, useEth, __assign(__assign({}, curve_1.curve.options), { value: value, gasLimit: gasLimit }))];
580
- case 5: return [2 /*return*/, (_a.sent()).hash];
975
+ case 6: return [2 /*return*/, (_a.sent()).hash];
581
976
  }
582
977
  });
583
978
  });
@@ -597,30 +992,67 @@ var Pool = /** @class */ (function () {
597
992
  }
598
993
  });
599
994
  }); };
600
- this.exchangeWrapped = function (inputCoin, outputCoin, amount, maxSlippage) {
995
+ this.exchangeWrappedEstimateGas = function (inputCoin, outputCoin, amount, maxSlippage) {
601
996
  if (maxSlippage === void 0) { maxSlippage = 0.01; }
602
997
  return __awaiter(_this, void 0, void 0, function () {
603
- var i, j, _amount, _expected, _minRecvAmount, contract, value, gasLimit;
604
- return __generator(this, function (_a) {
605
- switch (_a.label) {
998
+ var i, j, inputCoinBalance, _a, _b, _amount, _expected, _minRecvAmount, contract, value;
999
+ return __generator(this, function (_c) {
1000
+ switch (_c.label) {
606
1001
  case 0:
607
1002
  i = this._getCoinIdx(inputCoin, false);
608
1003
  j = this._getCoinIdx(outputCoin, false);
1004
+ _b = (_a = Object).values;
1005
+ return [4 /*yield*/, this.coinBalances()];
1006
+ case 1:
1007
+ inputCoinBalance = _b.apply(_a, [_c.sent()])[i];
1008
+ if (Number(inputCoinBalance) < Number(amount)) {
1009
+ throw Error("Not enough " + this.coins[i] + ". Actual: " + inputCoinBalance + ", required: " + amount);
1010
+ }
1011
+ return [4 /*yield*/, (0, utils_1.hasAllowance)([this.coinAddresses[i]], [amount], curve_1.curve.signerAddress, this.swap)];
1012
+ case 2:
1013
+ if (!(_c.sent())) {
1014
+ throw Error("Token allowance is needed to estimate gas");
1015
+ }
609
1016
  _amount = ethers_1.ethers.utils.parseUnits(amount, this.decimals[i]);
610
1017
  return [4 /*yield*/, this._getExchangeOutputWrapped(i, j, _amount)];
611
- case 1:
612
- _expected = _a.sent();
1018
+ case 3:
1019
+ _expected = _c.sent();
613
1020
  _minRecvAmount = _expected.mul((1 - maxSlippage) * 100).div(100);
614
- return [4 /*yield*/, utils_1._ensureAllowance([this.coinAddresses[i]], [_amount], this.swap)];
615
- case 2:
616
- _a.sent();
617
1021
  contract = curve_1.curve.contracts[this.swap].contract;
618
- value = utils_1.isEth(this.underlyingCoinAddresses[i]) ? _amount : ethers_1.ethers.BigNumber.from(0);
619
- return [4 /*yield*/, contract.estimateGas.exchange(i, j, _amount, _minRecvAmount, __assign(__assign({}, curve_1.curve.options), { value: value }))];
1022
+ value = (0, utils_1.isEth)(this.coinAddresses[i]) ? _amount : ethers_1.ethers.BigNumber.from(0);
1023
+ 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()];
1025
+ }
1026
+ });
1027
+ });
1028
+ };
1029
+ this.exchangeWrapped = function (inputCoin, outputCoin, amount, maxSlippage) {
1030
+ if (maxSlippage === void 0) { maxSlippage = 0.01; }
1031
+ return __awaiter(_this, void 0, void 0, function () {
1032
+ var i, j, _amount, _expected, _minRecvAmount, contract, value, gasLimit;
1033
+ return __generator(this, function (_a) {
1034
+ switch (_a.label) {
1035
+ case 0:
1036
+ i = this._getCoinIdx(inputCoin, false);
1037
+ j = this._getCoinIdx(outputCoin, false);
1038
+ _amount = ethers_1.ethers.utils.parseUnits(amount, this.decimals[i]);
1039
+ return [4 /*yield*/, this._getExchangeOutputWrapped(i, j, _amount)];
1040
+ case 1:
1041
+ _expected = _a.sent();
1042
+ _minRecvAmount = _expected.mul((1 - maxSlippage) * 100).div(100);
1043
+ return [4 /*yield*/, (0, utils_1._ensureAllowance)([this.coinAddresses[i]], [_amount], this.swap)];
1044
+ case 2:
1045
+ _a.sent();
1046
+ contract = curve_1.curve.contracts[this.swap].contract;
1047
+ value = (0, utils_1.isEth)(this.coinAddresses[i]) ? _amount : ethers_1.ethers.BigNumber.from(0);
1048
+ return [4 /*yield*/, curve_1.curve.updateFeeData()];
620
1049
  case 3:
1050
+ _a.sent();
1051
+ return [4 /*yield*/, contract.estimateGas.exchange(i, j, _amount, _minRecvAmount, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
1052
+ case 4:
621
1053
  gasLimit = (_a.sent()).mul(130).div(100);
622
1054
  return [4 /*yield*/, contract.exchange(i, j, _amount, _minRecvAmount, __assign(__assign({}, curve_1.curve.options), { value: value, gasLimit: gasLimit }))];
623
- case 4: return [2 /*return*/, (_a.sent()).hash];
1055
+ case 5: return [2 /*return*/, (_a.sent()).hash];
624
1056
  }
625
1057
  });
626
1058
  });
@@ -645,7 +1077,7 @@ var Pool = /** @class */ (function () {
645
1077
  });
646
1078
  return [4 /*yield*/, curve_1.curve.multicallProvider.all(contractCalls)];
647
1079
  case 1:
648
- response = (_e.sent()).map(function (value) { return utils_1.toBN(value); });
1080
+ response = (_e.sent()).map(function (value) { return (0, utils_1.toBN)(value); });
649
1081
  _b = response.splice(0, 2), veTotalSupply = _b[0], gaugeTotalSupply = _b[1];
650
1082
  resultBN = {};
651
1083
  addresses.forEach(function (acct, i) {
@@ -654,7 +1086,7 @@ var Pool = /** @class */ (function () {
654
1086
  result = {};
655
1087
  for (_c = 0, _d = Object.entries(resultBN); _c < _d.length; _c++) {
656
1088
  entry = _d[_c];
657
- result[entry[0]] = utils_1.toStringFromBN(entry[1]);
1089
+ result[entry[0]] = (0, utils_1.toStringFromBN)(entry[1]);
658
1090
  }
659
1091
  return [2 /*return*/, result];
660
1092
  }
@@ -682,10 +1114,10 @@ var Pool = /** @class */ (function () {
682
1114
  });
683
1115
  return [4 /*yield*/, curve_1.curve.multicallProvider.all(contractCalls)];
684
1116
  case 1:
685
- response = (_h.sent()).map(function (value) { return utils_1.toBN(value); });
1117
+ response = (_h.sent()).map(function (value) { return (0, utils_1.toBN)(value); });
686
1118
  _b = response.splice(0, 2), veTotalSupply = _b[0], gaugeTotalSupply = _b[1];
687
1119
  votingPower = {};
688
- totalBalance = utils_1.BN(0);
1120
+ totalBalance = (0, utils_1.BN)(0);
689
1121
  for (_c = 0, accounts_1 = accounts; _c < accounts_1.length; _c++) {
690
1122
  acct = accounts_1[_c];
691
1123
  votingPower[acct] = response[0];
@@ -693,7 +1125,7 @@ var Pool = /** @class */ (function () {
693
1125
  response.splice(0, 3);
694
1126
  }
695
1127
  totalPower = Object.values(votingPower).reduce(function (sum, item) { return sum.plus(item); });
696
- optimalBN = Object.fromEntries(accounts.map(function (acc) { return [acc, utils_1.BN(0)]; }));
1128
+ optimalBN = Object.fromEntries(accounts.map(function (acc) { return [acc, (0, utils_1.BN)(0)]; }));
697
1129
  if (totalBalance.lt(gaugeTotalSupply.times(totalPower).div(veTotalSupply))) {
698
1130
  for (_d = 0, accounts_2 = accounts; _d < accounts_2.length; _d++) {
699
1131
  acct = accounts_2[_d];
@@ -718,7 +1150,7 @@ var Pool = /** @class */ (function () {
718
1150
  optimal = {};
719
1151
  for (_f = 0, _g = Object.entries(optimalBN); _f < _g.length; _f++) {
720
1152
  entry = _g[_f];
721
- optimal[entry[0]] = utils_1.toStringFromBN(entry[1]);
1153
+ optimal[entry[0]] = (0, utils_1.toStringFromBN)(entry[1]);
722
1154
  }
723
1155
  return [2 /*return*/, optimal];
724
1156
  }
@@ -757,9 +1189,9 @@ var Pool = /** @class */ (function () {
757
1189
  swapContract.get_virtual_price(),
758
1190
  ])];
759
1191
  case 1:
760
- _a = (_b.sent()).map(function (value) { return utils_1.toBN(value); }), inflation = _a[0], weight = _a[1], workingSupply = _a[2], virtualPrice = _a[3];
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];
761
1193
  rate = inflation.times(weight).times(31536000).div(workingSupply).times(0.4).div(virtualPrice);
762
- return [4 /*yield*/, utils_1.getCrvRate()];
1194
+ return [4 /*yield*/, (0, utils_1.getCrvRate)()];
763
1195
  case 2:
764
1196
  crvRate = _b.sent();
765
1197
  baseApy = rate.times(crvRate);
@@ -853,8 +1285,8 @@ var Pool = /** @class */ (function () {
853
1285
  coinNames.push('ETH');
854
1286
  coinAddresses.push('0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE');
855
1287
  }
856
- addresses = utils_1._prepareAddresses(addresses);
857
- return [4 /*yield*/, utils_1._getBalances(coinAddresses, addresses)];
1288
+ addresses = (0, utils_1._prepareAddresses)(addresses);
1289
+ return [4 /*yield*/, (0, utils_1._getBalances)(coinAddresses, addresses)];
858
1290
  case 1:
859
1291
  rawBalances = _d.sent();
860
1292
  balances = {};
@@ -876,7 +1308,7 @@ var Pool = /** @class */ (function () {
876
1308
  return __awaiter(_this, void 0, void 0, function () {
877
1309
  return __generator(this, function (_a) {
878
1310
  switch (_a.label) {
879
- case 0: return [4 /*yield*/, curve_1.curve.contracts[this.swap].contract.calc_token_amount(_amounts, isDeposit, curve_1.curve.options)];
1311
+ case 0: return [4 /*yield*/, curve_1.curve.contracts[this.swap].contract.calc_token_amount(_amounts, isDeposit, curve_1.curve.constantOptions)];
880
1312
  case 1: return [2 /*return*/, _a.sent()];
881
1313
  }
882
1314
  });
@@ -891,9 +1323,9 @@ var Pool = /** @class */ (function () {
891
1323
  case 0:
892
1324
  contract = curve_1.curve.contracts[this.zap].contract;
893
1325
  if (!this.isFactory) return [3 /*break*/, 2];
894
- return [4 /*yield*/, contract.calc_token_amount(this.swap, _amounts, isDeposit, curve_1.curve.options)];
1326
+ return [4 /*yield*/, contract.calc_token_amount(this.swap, _amounts, isDeposit, curve_1.curve.constantOptions)];
895
1327
  case 1: return [2 /*return*/, _a.sent()];
896
- case 2: return [4 /*yield*/, contract.calc_token_amount(_amounts, isDeposit, curve_1.curve.options)];
1328
+ case 2: return [4 /*yield*/, contract.calc_token_amount(_amounts, isDeposit, curve_1.curve.constantOptions)];
897
1329
  case 3: return [2 /*return*/, _a.sent()];
898
1330
  }
899
1331
  });
@@ -911,112 +1343,154 @@ var Pool = /** @class */ (function () {
911
1343
  _wrapped_amounts = _underlying_amounts.map(function (amount, i) {
912
1344
  return amount.mul(ethers_1.ethers.BigNumber.from(10).pow(18)).div(_rates[i]);
913
1345
  });
914
- return [4 /*yield*/, curve_1.curve.contracts[this.swap].contract.calc_token_amount(_wrapped_amounts, isDeposit, curve_1.curve.options)];
1346
+ return [4 /*yield*/, curve_1.curve.contracts[this.swap].contract.calc_token_amount(_wrapped_amounts, isDeposit, curve_1.curve.constantOptions)];
915
1347
  case 2: return [2 /*return*/, _a.sent()];
916
1348
  }
917
1349
  });
918
1350
  });
919
1351
  };
920
- this._addLiquiditySwap = function (_amounts) { return __awaiter(_this, void 0, void 0, function () {
921
- var _minMintAmount, ethIndex, value, contract, gasLimit;
922
- return __generator(this, function (_a) {
923
- switch (_a.label) {
924
- case 0: return [4 /*yield*/, utils_1._ensureAllowance(this.coinAddresses, _amounts, this.swap)];
925
- case 1:
926
- _a.sent();
927
- return [4 /*yield*/, this._calcLpTokenAmount(_amounts)];
928
- case 2:
929
- _minMintAmount = (_a.sent()).mul(99).div(100);
930
- ethIndex = utils_1.getEthIndex(this.coinAddresses);
931
- value = _amounts[ethIndex] || ethers_1.ethers.BigNumber.from(0);
932
- contract = curve_1.curve.contracts[this.swap].contract;
933
- return [4 /*yield*/, contract.estimateGas.add_liquidity(_amounts, _minMintAmount, __assign(__assign({}, curve_1.curve.options), { value: value }))];
934
- case 3:
935
- gasLimit = (_a.sent()).mul(130).div(100);
936
- return [4 /*yield*/, contract.add_liquidity(_amounts, _minMintAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit, value: value }))];
937
- case 4: return [2 /*return*/, (_a.sent()).hash];
938
- }
1352
+ this._addLiquiditySwap = function (_amounts, estimateGas) {
1353
+ if (estimateGas === void 0) { estimateGas = false; }
1354
+ return __awaiter(_this, void 0, void 0, function () {
1355
+ var _minMintAmount, ethIndex, value, contract, gas, gasLimit;
1356
+ return __generator(this, function (_a) {
1357
+ switch (_a.label) {
1358
+ case 0:
1359
+ if (!!estimateGas) return [3 /*break*/, 2];
1360
+ return [4 /*yield*/, (0, utils_1._ensureAllowance)(this.coinAddresses, _amounts, this.swap)];
1361
+ case 1:
1362
+ _a.sent();
1363
+ _a.label = 2;
1364
+ case 2: return [4 /*yield*/, this._calcLpTokenAmount(_amounts)];
1365
+ case 3:
1366
+ _minMintAmount = (_a.sent()).mul(99).div(100);
1367
+ ethIndex = (0, utils_1.getEthIndex)(this.coinAddresses);
1368
+ value = _amounts[ethIndex] || ethers_1.ethers.BigNumber.from(0);
1369
+ contract = curve_1.curve.contracts[this.swap].contract;
1370
+ return [4 /*yield*/, contract.estimateGas.add_liquidity(_amounts, _minMintAmount, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
1371
+ case 4:
1372
+ gas = _a.sent();
1373
+ if (estimateGas) {
1374
+ return [2 /*return*/, gas.toNumber()];
1375
+ }
1376
+ gasLimit = gas.mul(130).div(100);
1377
+ return [4 /*yield*/, contract.add_liquidity(_amounts, _minMintAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit, value: value }))];
1378
+ case 5: return [2 /*return*/, (_a.sent()).hash];
1379
+ }
1380
+ });
939
1381
  });
940
- }); };
941
- this._addLiquidityZap = function (_amounts) { return __awaiter(_this, void 0, void 0, function () {
942
- var _minMintAmount, ethIndex, value, contract, gasLimit;
943
- return __generator(this, function (_a) {
944
- switch (_a.label) {
945
- case 0: return [4 /*yield*/, utils_1._ensureAllowance(this.underlyingCoinAddresses, _amounts, this.zap)];
946
- case 1:
947
- _a.sent();
948
- return [4 /*yield*/, this._calcLpTokenAmountWithUnderlying(_amounts)];
949
- case 2:
950
- _minMintAmount = (_a.sent()).mul(99).div(100);
951
- ethIndex = utils_1.getEthIndex(this.underlyingCoinAddresses);
952
- value = _amounts[ethIndex] || ethers_1.ethers.BigNumber.from(0);
953
- contract = curve_1.curve.contracts[this.zap].contract;
954
- return [4 /*yield*/, contract.estimateGas.add_liquidity(_amounts, _minMintAmount, __assign(__assign({}, curve_1.curve.options), { value: value }))];
955
- case 3:
956
- gasLimit = (_a.sent()).mul(130).div(100);
957
- return [4 /*yield*/, contract.add_liquidity(_amounts, _minMintAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit, value: value }))];
958
- case 4: return [2 /*return*/, (_a.sent()).hash];
959
- }
1382
+ };
1383
+ this._addLiquidityZap = function (_amounts, estimateGas) {
1384
+ if (estimateGas === void 0) { estimateGas = false; }
1385
+ return __awaiter(_this, void 0, void 0, function () {
1386
+ var _minMintAmount, ethIndex, value, contract, gas, gasLimit;
1387
+ return __generator(this, function (_a) {
1388
+ switch (_a.label) {
1389
+ case 0:
1390
+ if (!!estimateGas) return [3 /*break*/, 2];
1391
+ return [4 /*yield*/, (0, utils_1._ensureAllowance)(this.underlyingCoinAddresses, _amounts, this.zap)];
1392
+ case 1:
1393
+ _a.sent();
1394
+ _a.label = 2;
1395
+ case 2: return [4 /*yield*/, this._calcLpTokenAmountWithUnderlying(_amounts)];
1396
+ case 3:
1397
+ _minMintAmount = (_a.sent()).mul(99).div(100);
1398
+ ethIndex = (0, utils_1.getEthIndex)(this.underlyingCoinAddresses);
1399
+ value = _amounts[ethIndex] || ethers_1.ethers.BigNumber.from(0);
1400
+ contract = curve_1.curve.contracts[this.zap].contract;
1401
+ return [4 /*yield*/, contract.estimateGas.add_liquidity(_amounts, _minMintAmount, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
1402
+ case 4:
1403
+ gas = _a.sent();
1404
+ if (estimateGas) {
1405
+ return [2 /*return*/, gas.toNumber()];
1406
+ }
1407
+ gasLimit = gas.mul(130).div(100);
1408
+ return [4 /*yield*/, contract.add_liquidity(_amounts, _minMintAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit, value: value }))];
1409
+ case 5: return [2 /*return*/, (_a.sent()).hash];
1410
+ }
1411
+ });
960
1412
  });
961
- }); };
962
- this._addLiquidityMetaZap = function (_amounts) { return __awaiter(_this, void 0, void 0, function () {
963
- var _minMintAmount, ethIndex, value, contract, gasLimit_1, gasLimit;
964
- return __generator(this, function (_a) {
965
- switch (_a.label) {
966
- case 0: return [4 /*yield*/, utils_1._ensureAllowance(this.underlyingCoinAddresses, _amounts, this.zap)];
967
- case 1:
968
- _a.sent();
969
- return [4 /*yield*/, this._calcLpTokenAmountZap(_amounts)];
970
- case 2:
971
- _minMintAmount = (_a.sent()).mul(99).div(100);
972
- ethIndex = utils_1.getEthIndex(this.underlyingCoinAddresses);
973
- value = _amounts[ethIndex] || ethers_1.ethers.BigNumber.from(0);
974
- contract = curve_1.curve.contracts[this.zap].contract;
975
- if (!this.isFactory) return [3 /*break*/, 5];
976
- return [4 /*yield*/, contract.estimateGas.add_liquidity(this.swap, _amounts, _minMintAmount, __assign(__assign({}, curve_1.curve.options), { value: value }))];
977
- case 3:
978
- gasLimit_1 = (_a.sent()).mul(130).div(100);
979
- return [4 /*yield*/, contract.add_liquidity(this.swap, _amounts, _minMintAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit_1, value: value }))];
980
- case 4: return [2 /*return*/, (_a.sent()).hash];
981
- case 5: return [4 /*yield*/, contract.estimateGas.add_liquidity(_amounts, _minMintAmount, __assign(__assign({}, curve_1.curve.options), { value: value }))];
982
- case 6:
983
- gasLimit = (_a.sent()).mul(130).div(100);
984
- return [4 /*yield*/, contract.add_liquidity(_amounts, _minMintAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit, value: value }))];
985
- case 7: return [2 /*return*/, (_a.sent()).hash];
986
- }
1413
+ };
1414
+ this._addLiquidityMetaZap = function (_amounts, estimateGas) {
1415
+ if (estimateGas === void 0) { estimateGas = false; }
1416
+ return __awaiter(_this, void 0, void 0, function () {
1417
+ var _minMintAmount, ethIndex, value, contract, gas_1, gasLimit_1, gas, gasLimit;
1418
+ return __generator(this, function (_a) {
1419
+ switch (_a.label) {
1420
+ case 0:
1421
+ if (!!estimateGas) return [3 /*break*/, 2];
1422
+ return [4 /*yield*/, (0, utils_1._ensureAllowance)(this.underlyingCoinAddresses, _amounts, this.zap)];
1423
+ case 1:
1424
+ _a.sent();
1425
+ _a.label = 2;
1426
+ case 2: return [4 /*yield*/, this._calcLpTokenAmountZap(_amounts)];
1427
+ case 3:
1428
+ _minMintAmount = (_a.sent()).mul(99).div(100);
1429
+ ethIndex = (0, utils_1.getEthIndex)(this.underlyingCoinAddresses);
1430
+ value = _amounts[ethIndex] || ethers_1.ethers.BigNumber.from(0);
1431
+ contract = curve_1.curve.contracts[this.zap].contract;
1432
+ if (!this.isFactory) return [3 /*break*/, 6];
1433
+ return [4 /*yield*/, contract.estimateGas.add_liquidity(this.swap, _amounts, _minMintAmount, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
1434
+ case 4:
1435
+ gas_1 = _a.sent();
1436
+ if (estimateGas) {
1437
+ return [2 /*return*/, gas_1.toNumber()];
1438
+ }
1439
+ gasLimit_1 = gas_1.mul(130).div(100);
1440
+ return [4 /*yield*/, contract.add_liquidity(this.swap, _amounts, _minMintAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit_1, value: value }))];
1441
+ case 5: return [2 /*return*/, (_a.sent()).hash];
1442
+ case 6: return [4 /*yield*/, contract.estimateGas.add_liquidity(_amounts, _minMintAmount, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
1443
+ case 7:
1444
+ gas = _a.sent();
1445
+ if (estimateGas) {
1446
+ return [2 /*return*/, gas.toNumber()];
1447
+ }
1448
+ gasLimit = gas.mul(130).div(100);
1449
+ return [4 /*yield*/, contract.add_liquidity(_amounts, _minMintAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit, value: value }))];
1450
+ case 8: return [2 /*return*/, (_a.sent()).hash];
1451
+ }
1452
+ });
987
1453
  });
988
- }); };
989
- this._addLiquidity = function (_amounts, useUnderlying) {
1454
+ };
1455
+ this._addLiquidity = function (_amounts, useUnderlying, estimateGas) {
990
1456
  if (useUnderlying === void 0) { useUnderlying = true; }
1457
+ if (estimateGas === void 0) { estimateGas = false; }
991
1458
  return __awaiter(_this, void 0, void 0, function () {
992
- var coinAddresses, _minMintAmount, _a, contract, ethIndex, value, gasLimit;
1459
+ var coinAddresses, _minMintAmount, _a, contract, ethIndex, value, gas, gasLimit;
993
1460
  return __generator(this, function (_b) {
994
1461
  switch (_b.label) {
995
1462
  case 0:
996
1463
  coinAddresses = useUnderlying ? this.underlyingCoinAddresses : this.coinAddresses;
997
- return [4 /*yield*/, utils_1._ensureAllowance(coinAddresses, _amounts, this.swap)];
1464
+ if (!!estimateGas) return [3 /*break*/, 2];
1465
+ return [4 /*yield*/, (0, utils_1._ensureAllowance)(coinAddresses, _amounts, this.swap)];
998
1466
  case 1:
999
1467
  _b.sent();
1000
- if (!useUnderlying) return [3 /*break*/, 3];
1001
- return [4 /*yield*/, this._calcLpTokenAmountWithUnderlying(_amounts)];
1468
+ _b.label = 2;
1002
1469
  case 2:
1470
+ if (!useUnderlying) return [3 /*break*/, 4];
1471
+ return [4 /*yield*/, this._calcLpTokenAmountWithUnderlying(_amounts)];
1472
+ case 3:
1003
1473
  _a = _b.sent();
1004
- return [3 /*break*/, 5];
1005
- case 3: return [4 /*yield*/, this._calcLpTokenAmount(_amounts)];
1006
- case 4:
1007
- _a = _b.sent();
1008
- _b.label = 5;
1474
+ return [3 /*break*/, 6];
1475
+ case 4: return [4 /*yield*/, this._calcLpTokenAmount(_amounts)];
1009
1476
  case 5:
1477
+ _a = _b.sent();
1478
+ _b.label = 6;
1479
+ case 6:
1010
1480
  _minMintAmount = _a;
1011
1481
  _minMintAmount = _minMintAmount.mul(99).div(100);
1012
1482
  contract = curve_1.curve.contracts[this.swap].contract;
1013
- ethIndex = utils_1.getEthIndex(this.underlyingCoinAddresses);
1483
+ ethIndex = (0, utils_1.getEthIndex)(this.underlyingCoinAddresses);
1014
1484
  value = _amounts[ethIndex] || ethers_1.ethers.BigNumber.from(0);
1015
- return [4 /*yield*/, contract.estimateGas.add_liquidity(_amounts, _minMintAmount, useUnderlying, __assign(__assign({}, curve_1.curve.options), { value: value }))];
1016
- case 6:
1017
- gasLimit = (_b.sent()).mul(130).div(100);
1485
+ return [4 /*yield*/, contract.estimateGas.add_liquidity(_amounts, _minMintAmount, useUnderlying, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
1486
+ case 7:
1487
+ gas = _b.sent();
1488
+ if (estimateGas) {
1489
+ return [2 /*return*/, gas.toNumber()];
1490
+ }
1491
+ gasLimit = gas.mul(130).div(100);
1018
1492
  return [4 /*yield*/, contract.add_liquidity(_amounts, _minMintAmount, useUnderlying, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit, value: value }))];
1019
- case 7: return [2 /*return*/, (_b.sent()).hash];
1493
+ case 8: return [2 /*return*/, (_b.sent()).hash];
1020
1494
  }
1021
1495
  });
1022
1496
  });
@@ -1032,25 +1506,25 @@ var Pool = /** @class */ (function () {
1032
1506
  _c.label = 1;
1033
1507
  case 1:
1034
1508
  if (!(i < this.coinAddresses.length)) return [3 /*break*/, 4];
1035
- return [4 /*yield*/, curve_1.curve.contracts[this.swap].contract.balances(i, curve_1.curve.options)];
1509
+ return [4 /*yield*/, curve_1.curve.contracts[this.swap].contract.balances(i, curve_1.curve.constantOptions)];
1036
1510
  case 2:
1037
1511
  _balance = _c.sent();
1038
- coinBalancesBN.push(utils_1.toBN(_balance, this.decimals[i]));
1512
+ coinBalancesBN.push((0, utils_1.toBN)(_balance, this.decimals[i]));
1039
1513
  _c.label = 3;
1040
1514
  case 3:
1041
1515
  i++;
1042
1516
  return [3 /*break*/, 1];
1043
1517
  case 4:
1044
1518
  _a = utils_1.toBN;
1045
- return [4 /*yield*/, curve_1.curve.contracts[this.lpToken].contract.totalSupply(curve_1.curve.options)];
1519
+ return [4 /*yield*/, curve_1.curve.contracts[this.lpToken].contract.totalSupply(curve_1.curve.constantOptions)];
1046
1520
  case 5:
1047
1521
  totalSupplyBN = _a.apply(void 0, [_c.sent()]);
1048
1522
  expectedAmountsBN = [];
1049
1523
  for (_b = 0, coinBalancesBN_1 = coinBalancesBN; _b < coinBalancesBN_1.length; _b++) {
1050
1524
  coinBalance = coinBalancesBN_1[_b];
1051
- expectedAmountsBN.push(coinBalance.times(utils_1.toBN(_lpTokenAmount)).div(totalSupplyBN));
1525
+ expectedAmountsBN.push(coinBalance.times((0, utils_1.toBN)(_lpTokenAmount)).div(totalSupplyBN));
1052
1526
  }
1053
- return [2 /*return*/, expectedAmountsBN.map(function (amount, i) { return utils_1.fromBN(amount, _this.decimals[i]); })];
1527
+ return [2 /*return*/, expectedAmountsBN.map(function (amount, i) { return (0, utils_1.fromBN)(amount, _this.decimals[i]); })];
1054
1528
  }
1055
1529
  });
1056
1530
  }); };
@@ -1097,7 +1571,7 @@ var Pool = /** @class */ (function () {
1097
1571
  return [4 /*yield*/, basePool._calcExpectedAmounts(_expectedMetaCoinAmount)];
1098
1572
  case 2:
1099
1573
  _basePoolExpectedAmounts = _a.sent();
1100
- return [2 /*return*/, __spreadArray(__spreadArray([], _expectedUnderlyingAmounts), _basePoolExpectedAmounts)];
1574
+ return [2 /*return*/, __spreadArray(__spreadArray([], _expectedUnderlyingAmounts, true), _basePoolExpectedAmounts, true)];
1101
1575
  }
1102
1576
  });
1103
1577
  }); };
@@ -1109,70 +1583,102 @@ var Pool = /** @class */ (function () {
1109
1583
  }
1110
1584
  });
1111
1585
  }); };
1112
- this._removeLiquiditySwap = function (_lpTokenAmount) { return __awaiter(_this, void 0, void 0, function () {
1113
- var _minAmounts, contract, gasLimit;
1114
- return __generator(this, function (_a) {
1115
- switch (_a.label) {
1116
- case 0: return [4 /*yield*/, this._calcMinAmounts(_lpTokenAmount)];
1117
- case 1:
1118
- _minAmounts = _a.sent();
1119
- contract = curve_1.curve.contracts[this.swap].contract;
1120
- return [4 /*yield*/, contract.estimateGas.remove_liquidity(_lpTokenAmount, _minAmounts, curve_1.curve.options)];
1121
- case 2:
1122
- gasLimit = (_a.sent()).mul(130).div(100);
1123
- return [4 /*yield*/, contract.remove_liquidity(_lpTokenAmount, _minAmounts, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit }))];
1124
- case 3: return [2 /*return*/, (_a.sent()).hash];
1125
- }
1586
+ this._removeLiquiditySwap = function (_lpTokenAmount, estimateGas) {
1587
+ if (estimateGas === void 0) { estimateGas = false; }
1588
+ return __awaiter(_this, void 0, void 0, function () {
1589
+ var _minAmounts, contract, gas, gasLimit;
1590
+ return __generator(this, function (_a) {
1591
+ switch (_a.label) {
1592
+ case 0: return [4 /*yield*/, this._calcMinAmounts(_lpTokenAmount)];
1593
+ case 1:
1594
+ _minAmounts = _a.sent();
1595
+ contract = curve_1.curve.contracts[this.swap].contract;
1596
+ return [4 /*yield*/, contract.estimateGas.remove_liquidity(_lpTokenAmount, _minAmounts, curve_1.curve.constantOptions)];
1597
+ case 2:
1598
+ gas = _a.sent();
1599
+ if (estimateGas) {
1600
+ return [2 /*return*/, gas.toNumber()];
1601
+ }
1602
+ gasLimit = gas.mul(130).div(100);
1603
+ return [4 /*yield*/, contract.remove_liquidity(_lpTokenAmount, _minAmounts, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit }))];
1604
+ case 3: return [2 /*return*/, (_a.sent()).hash];
1605
+ }
1606
+ });
1126
1607
  });
1127
- }); };
1128
- this._removeLiquidityZap = function (_lpTokenAmount) { return __awaiter(_this, void 0, void 0, function () {
1129
- var _minAmounts, contract, gasLimit;
1130
- return __generator(this, function (_a) {
1131
- switch (_a.label) {
1132
- case 0: return [4 /*yield*/, this._calcMinUnderlyingAmounts(_lpTokenAmount)];
1133
- case 1:
1134
- _minAmounts = _a.sent();
1135
- return [4 /*yield*/, utils_1._ensureAllowance([this.lpToken], [_lpTokenAmount], this.zap)];
1136
- case 2:
1137
- _a.sent();
1138
- contract = curve_1.curve.contracts[this.zap].contract;
1139
- return [4 /*yield*/, contract.estimateGas.remove_liquidity(_lpTokenAmount, _minAmounts, curve_1.curve.options)];
1140
- case 3:
1141
- gasLimit = (_a.sent()).mul(130).div(100);
1142
- return [4 /*yield*/, contract.remove_liquidity(_lpTokenAmount, _minAmounts, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit }))];
1143
- case 4: return [2 /*return*/, (_a.sent()).hash];
1144
- }
1608
+ };
1609
+ this._removeLiquidityZap = function (_lpTokenAmount, estimateGas) {
1610
+ if (estimateGas === void 0) { estimateGas = false; }
1611
+ return __awaiter(_this, void 0, void 0, function () {
1612
+ var _minAmounts, contract, gas, gasLimit;
1613
+ return __generator(this, function (_a) {
1614
+ switch (_a.label) {
1615
+ case 0:
1616
+ if (!!estimateGas) return [3 /*break*/, 2];
1617
+ return [4 /*yield*/, (0, utils_1._ensureAllowance)([this.lpToken], [_lpTokenAmount], this.zap)];
1618
+ case 1:
1619
+ _a.sent();
1620
+ _a.label = 2;
1621
+ case 2: return [4 /*yield*/, this._calcMinUnderlyingAmounts(_lpTokenAmount)];
1622
+ case 3:
1623
+ _minAmounts = _a.sent();
1624
+ contract = curve_1.curve.contracts[this.zap].contract;
1625
+ return [4 /*yield*/, contract.estimateGas.remove_liquidity(_lpTokenAmount, _minAmounts, curve_1.curve.constantOptions)];
1626
+ case 4:
1627
+ gas = _a.sent();
1628
+ if (estimateGas) {
1629
+ return [2 /*return*/, gas.toNumber()];
1630
+ }
1631
+ gasLimit = gas.mul(130).div(100);
1632
+ return [4 /*yield*/, contract.remove_liquidity(_lpTokenAmount, _minAmounts, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit }))];
1633
+ case 5: return [2 /*return*/, (_a.sent()).hash];
1634
+ }
1635
+ });
1145
1636
  });
1146
- }); };
1147
- this._removeLiquidityMetaZap = function (_lpTokenAmount) { return __awaiter(_this, void 0, void 0, function () {
1148
- var _minAmounts, contract, gasLimit_2, gasLimit;
1149
- return __generator(this, function (_a) {
1150
- switch (_a.label) {
1151
- case 0: return [4 /*yield*/, this._calcMinUnderlyingAmountsMeta(_lpTokenAmount)];
1152
- case 1:
1153
- _minAmounts = _a.sent();
1154
- return [4 /*yield*/, utils_1._ensureAllowance([this.lpToken], [_lpTokenAmount], this.zap)];
1155
- case 2:
1156
- _a.sent();
1157
- contract = curve_1.curve.contracts[this.zap].contract;
1158
- if (!this.isFactory) return [3 /*break*/, 5];
1159
- return [4 /*yield*/, contract.estimateGas.remove_liquidity(this.swap, _lpTokenAmount, _minAmounts, curve_1.curve.options)];
1160
- case 3:
1161
- gasLimit_2 = (_a.sent()).mul(130).div(100);
1162
- return [4 /*yield*/, contract.remove_liquidity(this.swap, _lpTokenAmount, _minAmounts, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit_2 }))];
1163
- case 4: return [2 /*return*/, (_a.sent()).hash];
1164
- case 5: return [4 /*yield*/, contract.estimateGas.remove_liquidity(_lpTokenAmount, _minAmounts, curve_1.curve.options)];
1165
- case 6:
1166
- gasLimit = (_a.sent()).mul(130).div(100);
1167
- return [4 /*yield*/, contract.remove_liquidity(_lpTokenAmount, _minAmounts, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit }))];
1168
- case 7: return [2 /*return*/, (_a.sent()).hash];
1169
- }
1637
+ };
1638
+ this._removeLiquidityMetaZap = function (_lpTokenAmount, estimateGas) {
1639
+ if (estimateGas === void 0) { estimateGas = false; }
1640
+ return __awaiter(_this, void 0, void 0, function () {
1641
+ var _minAmounts, contract, gas_2, gasLimit_2, gas, gasLimit;
1642
+ return __generator(this, function (_a) {
1643
+ switch (_a.label) {
1644
+ case 0:
1645
+ if (!!estimateGas) return [3 /*break*/, 2];
1646
+ return [4 /*yield*/, (0, utils_1._ensureAllowance)([this.lpToken], [_lpTokenAmount], this.zap)];
1647
+ case 1:
1648
+ _a.sent();
1649
+ _a.label = 2;
1650
+ case 2: return [4 /*yield*/, this._calcMinUnderlyingAmountsMeta(_lpTokenAmount)];
1651
+ case 3:
1652
+ _minAmounts = _a.sent();
1653
+ contract = curve_1.curve.contracts[this.zap].contract;
1654
+ if (!this.isFactory) return [3 /*break*/, 6];
1655
+ return [4 /*yield*/, contract.estimateGas.remove_liquidity(this.swap, _lpTokenAmount, _minAmounts, curve_1.curve.constantOptions)];
1656
+ case 4:
1657
+ gas_2 = _a.sent();
1658
+ if (estimateGas) {
1659
+ return [2 /*return*/, gas_2.toNumber()];
1660
+ }
1661
+ gasLimit_2 = gas_2.mul(130).div(100);
1662
+ return [4 /*yield*/, contract.remove_liquidity(this.swap, _lpTokenAmount, _minAmounts, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit_2 }))];
1663
+ case 5: return [2 /*return*/, (_a.sent()).hash];
1664
+ case 6: return [4 /*yield*/, contract.estimateGas.remove_liquidity(_lpTokenAmount, _minAmounts, curve_1.curve.constantOptions)];
1665
+ case 7:
1666
+ gas = _a.sent();
1667
+ if (estimateGas) {
1668
+ return [2 /*return*/, gas.toNumber()];
1669
+ }
1670
+ gasLimit = gas.mul(130).div(100);
1671
+ return [4 /*yield*/, contract.remove_liquidity(_lpTokenAmount, _minAmounts, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit }))];
1672
+ case 8: return [2 /*return*/, (_a.sent()).hash];
1673
+ }
1674
+ });
1170
1675
  });
1171
- }); };
1172
- this._removeLiquidity = function (_lpTokenAmount, useUnderlying) {
1676
+ };
1677
+ this._removeLiquidity = function (_lpTokenAmount, useUnderlying, estimateGas) {
1173
1678
  if (useUnderlying === void 0) { useUnderlying = true; }
1679
+ if (estimateGas === void 0) { estimateGas = false; }
1174
1680
  return __awaiter(_this, void 0, void 0, function () {
1175
- var _minAmounts, _a, contract, gasLimit;
1681
+ var _minAmounts, _a, contract, gas, gasLimit;
1176
1682
  return __generator(this, function (_b) {
1177
1683
  switch (_b.label) {
1178
1684
  case 0:
@@ -1188,79 +1694,115 @@ var Pool = /** @class */ (function () {
1188
1694
  case 4:
1189
1695
  _minAmounts = _a;
1190
1696
  contract = curve_1.curve.contracts[this.swap].contract;
1191
- return [4 /*yield*/, contract.estimateGas.remove_liquidity(_lpTokenAmount, _minAmounts, useUnderlying, curve_1.curve.options)];
1697
+ return [4 /*yield*/, contract.estimateGas.remove_liquidity(_lpTokenAmount, _minAmounts, useUnderlying, curve_1.curve.constantOptions)];
1192
1698
  case 5:
1193
- gasLimit = (_b.sent()).mul(130).div(100);
1699
+ gas = _b.sent();
1700
+ if (estimateGas) {
1701
+ return [2 /*return*/, gas.toNumber()];
1702
+ }
1703
+ gasLimit = gas.mul(130).div(100);
1194
1704
  return [4 /*yield*/, contract.remove_liquidity(_lpTokenAmount, _minAmounts, useUnderlying, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit }))];
1195
1705
  case 6: return [2 /*return*/, (_b.sent()).hash];
1196
1706
  }
1197
1707
  });
1198
1708
  });
1199
1709
  };
1200
- this._removeLiquidityImbalanceSwap = function (_amounts) { return __awaiter(_this, void 0, void 0, function () {
1201
- var _maxBurnAmount, contract, gasLimit;
1202
- return __generator(this, function (_a) {
1203
- switch (_a.label) {
1204
- case 0: return [4 /*yield*/, this._calcLpTokenAmount(_amounts, false)];
1205
- case 1:
1206
- _maxBurnAmount = (_a.sent()).mul(101).div(100);
1207
- contract = curve_1.curve.contracts[this.swap].contract;
1208
- return [4 /*yield*/, contract.estimateGas.remove_liquidity_imbalance(_amounts, _maxBurnAmount, curve_1.curve.options)];
1209
- case 2:
1210
- gasLimit = (_a.sent()).mul(130).div(100);
1211
- return [4 /*yield*/, contract.remove_liquidity_imbalance(_amounts, _maxBurnAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit }))];
1212
- case 3: return [2 /*return*/, (_a.sent()).hash];
1213
- }
1710
+ this._removeLiquidityImbalanceSwap = function (_amounts, estimateGas) {
1711
+ if (estimateGas === void 0) { estimateGas = false; }
1712
+ return __awaiter(_this, void 0, void 0, function () {
1713
+ var _maxBurnAmount, contract, gas, gasLimit;
1714
+ return __generator(this, function (_a) {
1715
+ switch (_a.label) {
1716
+ case 0: return [4 /*yield*/, this._calcLpTokenAmount(_amounts, false)];
1717
+ case 1:
1718
+ _maxBurnAmount = (_a.sent()).mul(101).div(100);
1719
+ contract = curve_1.curve.contracts[this.swap].contract;
1720
+ return [4 /*yield*/, contract.estimateGas.remove_liquidity_imbalance(_amounts, _maxBurnAmount, curve_1.curve.constantOptions)];
1721
+ case 2:
1722
+ gas = _a.sent();
1723
+ if (estimateGas) {
1724
+ return [2 /*return*/, gas.toNumber()];
1725
+ }
1726
+ gasLimit = gas.mul(130).div(100);
1727
+ return [4 /*yield*/, contract.remove_liquidity_imbalance(_amounts, _maxBurnAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit }))];
1728
+ case 3: return [2 /*return*/, (_a.sent()).hash];
1729
+ }
1730
+ });
1214
1731
  });
1215
- }); };
1216
- this._removeLiquidityImbalanceZap = function (_amounts) { return __awaiter(_this, void 0, void 0, function () {
1217
- var _maxBurnAmount, contract, gasLimit;
1218
- return __generator(this, function (_a) {
1219
- switch (_a.label) {
1220
- case 0: return [4 /*yield*/, this._calcLpTokenAmountWithUnderlying(_amounts, false)];
1221
- case 1:
1222
- _maxBurnAmount = (_a.sent()).mul(101).div(100);
1223
- return [4 /*yield*/, utils_1._ensureAllowance([this.lpToken], [_maxBurnAmount], this.zap)];
1224
- case 2:
1225
- _a.sent();
1226
- contract = curve_1.curve.contracts[this.zap].contract;
1227
- return [4 /*yield*/, contract.estimateGas.remove_liquidity_imbalance(_amounts, _maxBurnAmount, curve_1.curve.options)];
1228
- case 3:
1229
- gasLimit = (_a.sent()).mul(130).div(100);
1230
- return [4 /*yield*/, contract.remove_liquidity_imbalance(_amounts, _maxBurnAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit }))];
1231
- case 4: return [2 /*return*/, (_a.sent())];
1232
- }
1732
+ };
1733
+ this._removeLiquidityImbalanceZap = function (_amounts, estimateGas) {
1734
+ if (estimateGas === void 0) { estimateGas = false; }
1735
+ return __awaiter(_this, void 0, void 0, function () {
1736
+ var _maxBurnAmount, contract, gas, gasLimit;
1737
+ return __generator(this, function (_a) {
1738
+ switch (_a.label) {
1739
+ case 0: return [4 /*yield*/, this._calcLpTokenAmountWithUnderlying(_amounts, false)];
1740
+ case 1:
1741
+ _maxBurnAmount = (_a.sent()).mul(101).div(100);
1742
+ if (!!estimateGas) return [3 /*break*/, 3];
1743
+ return [4 /*yield*/, (0, utils_1._ensureAllowance)([this.lpToken], [_maxBurnAmount], this.zap)];
1744
+ case 2:
1745
+ _a.sent();
1746
+ _a.label = 3;
1747
+ case 3:
1748
+ contract = curve_1.curve.contracts[this.zap].contract;
1749
+ return [4 /*yield*/, contract.estimateGas.remove_liquidity_imbalance(_amounts, _maxBurnAmount, curve_1.curve.constantOptions)];
1750
+ case 4:
1751
+ gas = _a.sent();
1752
+ if (estimateGas) {
1753
+ return [2 /*return*/, gas.toNumber()];
1754
+ }
1755
+ gasLimit = gas.mul(130).div(100);
1756
+ return [4 /*yield*/, contract.remove_liquidity_imbalance(_amounts, _maxBurnAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit }))];
1757
+ case 5: return [2 /*return*/, (_a.sent())];
1758
+ }
1759
+ });
1233
1760
  });
1234
- }); };
1235
- this._removeLiquidityImbalanceMetaZap = function (_amounts) { return __awaiter(_this, void 0, void 0, function () {
1236
- var _maxBurnAmount, contract, gasLimit_3, gasLimit;
1237
- return __generator(this, function (_a) {
1238
- switch (_a.label) {
1239
- case 0: return [4 /*yield*/, this._calcLpTokenAmountZap(_amounts, false)];
1240
- case 1:
1241
- _maxBurnAmount = (_a.sent()).mul(101).div(100);
1242
- return [4 /*yield*/, utils_1._ensureAllowance([this.lpToken], [_maxBurnAmount], this.zap)];
1243
- case 2:
1244
- _a.sent();
1245
- contract = curve_1.curve.contracts[this.zap].contract;
1246
- if (!this.isFactory) return [3 /*break*/, 5];
1247
- return [4 /*yield*/, contract.estimateGas.remove_liquidity_imbalance(this.swap, _amounts, _maxBurnAmount, curve_1.curve.options)];
1248
- case 3:
1249
- gasLimit_3 = (_a.sent()).mul(130).div(100);
1250
- return [4 /*yield*/, contract.remove_liquidity_imbalance(this.swap, _amounts, _maxBurnAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit_3 }))];
1251
- case 4: return [2 /*return*/, (_a.sent())];
1252
- case 5: return [4 /*yield*/, contract.estimateGas.remove_liquidity_imbalance(_amounts, _maxBurnAmount, curve_1.curve.options)];
1253
- case 6:
1254
- gasLimit = (_a.sent()).mul(130).div(100);
1255
- return [4 /*yield*/, contract.remove_liquidity_imbalance(_amounts, _maxBurnAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit }))];
1256
- case 7: return [2 /*return*/, (_a.sent())];
1257
- }
1761
+ };
1762
+ this._removeLiquidityImbalanceMetaZap = function (_amounts, estimateGas) {
1763
+ if (estimateGas === void 0) { estimateGas = false; }
1764
+ return __awaiter(_this, void 0, void 0, function () {
1765
+ var _maxBurnAmount, contract, gas_3, gasLimit_3, gas, gasLimit;
1766
+ return __generator(this, function (_a) {
1767
+ switch (_a.label) {
1768
+ case 0: return [4 /*yield*/, this._calcLpTokenAmountZap(_amounts, false)];
1769
+ case 1:
1770
+ _maxBurnAmount = (_a.sent()).mul(101).div(100);
1771
+ if (!!estimateGas) return [3 /*break*/, 3];
1772
+ return [4 /*yield*/, (0, utils_1._ensureAllowance)([this.lpToken], [_maxBurnAmount], this.zap)];
1773
+ case 2:
1774
+ _a.sent();
1775
+ _a.label = 3;
1776
+ case 3:
1777
+ contract = curve_1.curve.contracts[this.zap].contract;
1778
+ if (!this.isFactory) return [3 /*break*/, 6];
1779
+ return [4 /*yield*/, contract.estimateGas.remove_liquidity_imbalance(this.swap, _amounts, _maxBurnAmount, curve_1.curve.constantOptions)];
1780
+ case 4:
1781
+ gas_3 = _a.sent();
1782
+ if (estimateGas) {
1783
+ return [2 /*return*/, gas_3.toNumber()];
1784
+ }
1785
+ gasLimit_3 = gas_3.mul(130).div(100);
1786
+ return [4 /*yield*/, contract.remove_liquidity_imbalance(this.swap, _amounts, _maxBurnAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit_3 }))];
1787
+ case 5: return [2 /*return*/, (_a.sent())];
1788
+ case 6: return [4 /*yield*/, contract.estimateGas.remove_liquidity_imbalance(_amounts, _maxBurnAmount, curve_1.curve.constantOptions)];
1789
+ case 7:
1790
+ gas = _a.sent();
1791
+ if (estimateGas) {
1792
+ return [2 /*return*/, gas.toNumber()];
1793
+ }
1794
+ gasLimit = gas.mul(130).div(100);
1795
+ return [4 /*yield*/, contract.remove_liquidity_imbalance(_amounts, _maxBurnAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit }))];
1796
+ case 8: return [2 /*return*/, (_a.sent())];
1797
+ }
1798
+ });
1258
1799
  });
1259
- }); };
1260
- this._removeLiquidityImbalance = function (_amounts, useUnderlying) {
1800
+ };
1801
+ this._removeLiquidityImbalance = function (_amounts, useUnderlying, estimateGas) {
1261
1802
  if (useUnderlying === void 0) { useUnderlying = true; }
1803
+ if (estimateGas === void 0) { estimateGas = false; }
1262
1804
  return __awaiter(_this, void 0, void 0, function () {
1263
- var _maxBurnAmount, _a, contract, gasLimit;
1805
+ var _maxBurnAmount, _a, contract, gas, gasLimit;
1264
1806
  return __generator(this, function (_b) {
1265
1807
  switch (_b.label) {
1266
1808
  case 0:
@@ -1277,9 +1819,13 @@ var Pool = /** @class */ (function () {
1277
1819
  _maxBurnAmount = _a;
1278
1820
  _maxBurnAmount = _maxBurnAmount.mul(101).div(100);
1279
1821
  contract = curve_1.curve.contracts[this.swap].contract;
1280
- return [4 /*yield*/, contract.estimateGas.remove_liquidity_imbalance(_amounts, _maxBurnAmount, useUnderlying, curve_1.curve.options)];
1822
+ return [4 /*yield*/, contract.estimateGas.remove_liquidity_imbalance(_amounts, _maxBurnAmount, useUnderlying, curve_1.curve.constantOptions)];
1281
1823
  case 5:
1282
- gasLimit = (_b.sent()).mul(130).div(100);
1824
+ gas = _b.sent();
1825
+ if (estimateGas) {
1826
+ return [2 /*return*/, gas.toNumber()];
1827
+ }
1828
+ gasLimit = gas.mul(130).div(100);
1283
1829
  return [4 /*yield*/, contract.remove_liquidity_imbalance(_amounts, _maxBurnAmount, useUnderlying, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit }))];
1284
1830
  case 6: return [2 /*return*/, (_b.sent()).hash];
1285
1831
  }
@@ -1289,7 +1835,7 @@ var Pool = /** @class */ (function () {
1289
1835
  this._calcWithdrawOneCoinSwap = function (_lpTokenAmount, i) { return __awaiter(_this, void 0, void 0, function () {
1290
1836
  return __generator(this, function (_a) {
1291
1837
  switch (_a.label) {
1292
- case 0: return [4 /*yield*/, curve_1.curve.contracts[this.swap].contract.calc_withdraw_one_coin(_lpTokenAmount, i, curve_1.curve.options)];
1838
+ case 0: return [4 /*yield*/, curve_1.curve.contracts[this.swap].contract.calc_withdraw_one_coin(_lpTokenAmount, i, curve_1.curve.constantOptions)];
1293
1839
  case 1: return [2 /*return*/, _a.sent()];
1294
1840
  }
1295
1841
  });
@@ -1301,9 +1847,9 @@ var Pool = /** @class */ (function () {
1301
1847
  case 0:
1302
1848
  contract = curve_1.curve.contracts[this.zap].contract;
1303
1849
  if (!this.isFactory) return [3 /*break*/, 2];
1304
- return [4 /*yield*/, contract.calc_withdraw_one_coin(this.swap, _lpTokenAmount, i, curve_1.curve.options)];
1850
+ return [4 /*yield*/, contract.calc_withdraw_one_coin(this.swap, _lpTokenAmount, i, curve_1.curve.constantOptions)];
1305
1851
  case 1: return [2 /*return*/, (_a.sent())];
1306
- case 2: return [4 /*yield*/, contract.calc_withdraw_one_coin(_lpTokenAmount, i, curve_1.curve.options)];
1852
+ case 2: return [4 /*yield*/, contract.calc_withdraw_one_coin(_lpTokenAmount, i, curve_1.curve.constantOptions)];
1307
1853
  case 3: return [2 /*return*/, _a.sent()];
1308
1854
  }
1309
1855
  });
@@ -1313,57 +1859,79 @@ var Pool = /** @class */ (function () {
1313
1859
  return __awaiter(_this, void 0, void 0, function () {
1314
1860
  return __generator(this, function (_a) {
1315
1861
  switch (_a.label) {
1316
- case 0: return [4 /*yield*/, curve_1.curve.contracts[this.swap].contract.calc_withdraw_one_coin(_lpTokenAmount, i, useUnderlying, curve_1.curve.options)];
1862
+ case 0: return [4 /*yield*/, curve_1.curve.contracts[this.swap].contract.calc_withdraw_one_coin(_lpTokenAmount, i, useUnderlying, curve_1.curve.constantOptions)];
1317
1863
  case 1: return [2 /*return*/, _a.sent()];
1318
1864
  }
1319
1865
  });
1320
1866
  });
1321
1867
  };
1322
- this._removeLiquidityOneCoinSwap = function (_lpTokenAmount, i) { return __awaiter(_this, void 0, void 0, function () {
1323
- var _minAmount, contract, gasLimit;
1324
- return __generator(this, function (_a) {
1325
- switch (_a.label) {
1326
- case 0: return [4 /*yield*/, this._calcWithdrawOneCoinSwap(_lpTokenAmount, i)];
1327
- case 1:
1328
- _minAmount = (_a.sent()).mul(99).div(100);
1329
- contract = curve_1.curve.contracts[this.swap].contract;
1330
- return [4 /*yield*/, contract.estimateGas.remove_liquidity_one_coin(_lpTokenAmount, i, _minAmount, curve_1.curve.options)];
1331
- case 2:
1332
- gasLimit = (_a.sent()).mul(130).div(100);
1333
- return [4 /*yield*/, contract.remove_liquidity_one_coin(_lpTokenAmount, i, _minAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit }))];
1334
- case 3: return [2 /*return*/, (_a.sent()).hash];
1335
- }
1868
+ this._removeLiquidityOneCoinSwap = function (_lpTokenAmount, i, estimateGas) {
1869
+ if (estimateGas === void 0) { estimateGas = false; }
1870
+ return __awaiter(_this, void 0, void 0, function () {
1871
+ var _minAmount, contract, gas, gasLimit;
1872
+ return __generator(this, function (_a) {
1873
+ switch (_a.label) {
1874
+ case 0: return [4 /*yield*/, this._calcWithdrawOneCoinSwap(_lpTokenAmount, i)];
1875
+ case 1:
1876
+ _minAmount = (_a.sent()).mul(99).div(100);
1877
+ contract = curve_1.curve.contracts[this.swap].contract;
1878
+ return [4 /*yield*/, contract.estimateGas.remove_liquidity_one_coin(_lpTokenAmount, i, _minAmount, curve_1.curve.constantOptions)];
1879
+ case 2:
1880
+ gas = _a.sent();
1881
+ if (estimateGas) {
1882
+ return [2 /*return*/, gas.toNumber()];
1883
+ }
1884
+ gasLimit = gas.mul(130).div(100);
1885
+ return [4 /*yield*/, contract.remove_liquidity_one_coin(_lpTokenAmount, i, _minAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit }))];
1886
+ case 3: return [2 /*return*/, (_a.sent()).hash];
1887
+ }
1888
+ });
1336
1889
  });
1337
- }); };
1338
- this._removeLiquidityOneCoinZap = function (_lpTokenAmount, i) { return __awaiter(_this, void 0, void 0, function () {
1339
- var _minAmount, contract, gasLimit_4, gasLimit;
1340
- return __generator(this, function (_a) {
1341
- switch (_a.label) {
1342
- case 0: return [4 /*yield*/, this._calcWithdrawOneCoinZap(_lpTokenAmount, i)];
1343
- case 1:
1344
- _minAmount = (_a.sent()).mul(99).div(100);
1345
- return [4 /*yield*/, utils_1._ensureAllowance([this.lpToken], [_lpTokenAmount], this.zap)];
1346
- case 2:
1347
- _a.sent();
1348
- contract = curve_1.curve.contracts[this.zap].contract;
1349
- if (!this.isFactory) return [3 /*break*/, 5];
1350
- return [4 /*yield*/, contract.estimateGas.remove_liquidity_one_coin(this.swap, _lpTokenAmount, i, _minAmount, curve_1.curve.options)];
1351
- case 3:
1352
- gasLimit_4 = (_a.sent()).mul(130).div(100);
1353
- return [4 /*yield*/, contract.remove_liquidity_one_coin(this.swap, _lpTokenAmount, i, _minAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit_4 }))];
1354
- case 4: return [2 /*return*/, (_a.sent()).hash];
1355
- case 5: return [4 /*yield*/, contract.estimateGas.remove_liquidity_one_coin(_lpTokenAmount, i, _minAmount, curve_1.curve.options)];
1356
- case 6:
1357
- gasLimit = (_a.sent()).mul(130).div(100);
1358
- return [4 /*yield*/, contract.remove_liquidity_one_coin(_lpTokenAmount, i, _minAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit }))];
1359
- case 7: return [2 /*return*/, (_a.sent()).hash];
1360
- }
1890
+ };
1891
+ this._removeLiquidityOneCoinZap = function (_lpTokenAmount, i, estimateGas) {
1892
+ if (estimateGas === void 0) { estimateGas = false; }
1893
+ return __awaiter(_this, void 0, void 0, function () {
1894
+ var _minAmount, contract, gas_4, gasLimit_4, gas, gasLimit;
1895
+ return __generator(this, function (_a) {
1896
+ switch (_a.label) {
1897
+ case 0:
1898
+ if (!!estimateGas) return [3 /*break*/, 2];
1899
+ return [4 /*yield*/, (0, utils_1._ensureAllowance)([this.lpToken], [_lpTokenAmount], this.zap)];
1900
+ case 1:
1901
+ _a.sent();
1902
+ _a.label = 2;
1903
+ case 2: return [4 /*yield*/, this._calcWithdrawOneCoinZap(_lpTokenAmount, i)];
1904
+ case 3:
1905
+ _minAmount = (_a.sent()).mul(99).div(100);
1906
+ contract = curve_1.curve.contracts[this.zap].contract;
1907
+ if (!this.isFactory) return [3 /*break*/, 6];
1908
+ return [4 /*yield*/, contract.estimateGas.remove_liquidity_one_coin(this.swap, _lpTokenAmount, i, _minAmount, curve_1.curve.constantOptions)];
1909
+ case 4:
1910
+ gas_4 = _a.sent();
1911
+ if (estimateGas) {
1912
+ return [2 /*return*/, gas_4.toNumber()];
1913
+ }
1914
+ gasLimit_4 = gas_4.mul(130).div(100);
1915
+ return [4 /*yield*/, contract.remove_liquidity_one_coin(this.swap, _lpTokenAmount, i, _minAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit_4 }))];
1916
+ case 5: return [2 /*return*/, (_a.sent()).hash];
1917
+ case 6: return [4 /*yield*/, contract.estimateGas.remove_liquidity_one_coin(_lpTokenAmount, i, _minAmount, curve_1.curve.constantOptions)];
1918
+ case 7:
1919
+ gas = _a.sent();
1920
+ if (estimateGas) {
1921
+ return [2 /*return*/, gas.toNumber()];
1922
+ }
1923
+ gasLimit = gas.mul(130).div(100);
1924
+ return [4 /*yield*/, contract.remove_liquidity_one_coin(_lpTokenAmount, i, _minAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit }))];
1925
+ case 8: return [2 /*return*/, (_a.sent()).hash];
1926
+ }
1927
+ });
1361
1928
  });
1362
- }); };
1363
- this._removeLiquidityOneCoin = function (_lpTokenAmount, i, useUnderlying) {
1929
+ };
1930
+ this._removeLiquidityOneCoin = function (_lpTokenAmount, i, useUnderlying, estimateGas) {
1364
1931
  if (useUnderlying === void 0) { useUnderlying = true; }
1932
+ if (estimateGas === void 0) { estimateGas = false; }
1365
1933
  return __awaiter(_this, void 0, void 0, function () {
1366
- var _minAmount, _a, contract, gasLimit;
1934
+ var _minAmount, _a, contract, gas, gasLimit;
1367
1935
  return __generator(this, function (_b) {
1368
1936
  switch (_b.label) {
1369
1937
  case 0:
@@ -1380,9 +1948,13 @@ var Pool = /** @class */ (function () {
1380
1948
  _minAmount = _a;
1381
1949
  _minAmount = _minAmount.mul(99).div(100);
1382
1950
  contract = curve_1.curve.contracts[this.swap].contract;
1383
- return [4 /*yield*/, contract.estimateGas.remove_liquidity_one_coin(_lpTokenAmount, i, _minAmount, useUnderlying, curve_1.curve.options)];
1951
+ return [4 /*yield*/, contract.estimateGas.remove_liquidity_one_coin(_lpTokenAmount, i, _minAmount, useUnderlying, curve_1.curve.constantOptions)];
1384
1952
  case 5:
1385
- gasLimit = (_b.sent()).mul(130).div(100);
1953
+ gas = _b.sent();
1954
+ if (estimateGas) {
1955
+ return [2 /*return*/, gas.toNumber()];
1956
+ }
1957
+ gasLimit = gas.mul(130).div(100);
1386
1958
  return [4 /*yield*/, contract.remove_liquidity_one_coin(_lpTokenAmount, i, _minAmount, useUnderlying, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit }))];
1387
1959
  case 6: return [2 /*return*/, (_b.sent()).hash];
1388
1960
  }
@@ -1396,9 +1968,9 @@ var Pool = /** @class */ (function () {
1396
1968
  case 0:
1397
1969
  contract = curve_1.curve.contracts[this.swap].contract;
1398
1970
  if (!Object.prototype.hasOwnProperty.call(contract, 'get_dy_underlying')) return [3 /*break*/, 2];
1399
- return [4 /*yield*/, contract.get_dy_underlying(i, j, _amount, curve_1.curve.options)];
1971
+ return [4 /*yield*/, contract.get_dy_underlying(i, j, _amount, curve_1.curve.constantOptions)];
1400
1972
  case 1: return [2 /*return*/, _a.sent()];
1401
- case 2: return [4 /*yield*/, contract.get_dy(i, j, _amount, curve_1.curve.options)];
1973
+ case 2: return [4 /*yield*/, contract.get_dy(i, j, _amount, curve_1.curve.constantOptions)];
1402
1974
  case 3: return [2 /*return*/, _a.sent()];
1403
1975
  }
1404
1976
  });
@@ -1406,7 +1978,7 @@ var Pool = /** @class */ (function () {
1406
1978
  this._getExchangeOutputWrapped = function (i, j, _amount) { return __awaiter(_this, void 0, void 0, function () {
1407
1979
  return __generator(this, function (_a) {
1408
1980
  switch (_a.label) {
1409
- case 0: return [4 /*yield*/, curve_1.curve.contracts[this.swap].contract.get_dy(i, j, _amount, curve_1.curve.options)];
1981
+ case 0: return [4 /*yield*/, curve_1.curve.contracts[this.swap].contract.get_dy(i, j, _amount, curve_1.curve.constantOptions)];
1410
1982
  case 1: return [2 /*return*/, _a.sent()];
1411
1983
  }
1412
1984
  });
@@ -1427,10 +1999,25 @@ var Pool = /** @class */ (function () {
1427
1999
  this.isMeta = poolData.is_meta || false;
1428
2000
  this.basePool = poolData.base_pool || '';
1429
2001
  this.isFactory = poolData.is_factory || false;
2002
+ this.estimateGas = {
2003
+ addLiquidity: this.addLiquidityEstimateGas,
2004
+ addLiquidityWrapped: this.addLiquidityWrappedEstimateGas,
2005
+ gaugeDeposit: this.gaugeDepositEstimateGas,
2006
+ gaugeWithdraw: this.gaugeWithdrawEstimateGas,
2007
+ removeLiquidity: this.removeLiquidityEstimateGas,
2008
+ removeLiquidityWrapped: this.removeLiquidityWrappedEstimateGas,
2009
+ removeLiquidityImbalance: this.removeLiquidityImbalanceEstimateGas,
2010
+ removeLiquidityImbalanceWrapped: this.removeLiquidityImbalanceWrappedEstimateGas,
2011
+ removeLiquidityOneCoin: this.removeLiquidityOneCoinEstimateGas,
2012
+ removeLiquidityOneCoinWrapped: this.removeLiquidityOneCoinWrappedEstimateGas,
2013
+ exchange: this.exchangeEstimateGas,
2014
+ exchangeTricrypto: this.exchangeTricryptoEstimateGas,
2015
+ exchangeWrapped: this.exchangeWrappedEstimateGas,
2016
+ };
1430
2017
  if (this.isMeta) {
1431
2018
  var metaCoins = poolData.meta_coin_addresses;
1432
2019
  var metaCoinDecimals = poolData.meta_coin_decimals;
1433
- this.underlyingCoinAddresses = __spreadArray([this.underlyingCoinAddresses[0]], metaCoins);
2020
+ this.underlyingCoinAddresses = __spreadArray([this.underlyingCoinAddresses[0]], metaCoins, true);
1434
2021
  this.underlyingDecimals = metaCoinDecimals;
1435
2022
  }
1436
2023
  }
@@ -1448,13 +2035,12 @@ var _getBestPoolAndOutput = function (inputCoinAddress, outputCoinAddress, input
1448
2035
  throw new Error("This pair can't be exchanged");
1449
2036
  }
1450
2037
  addressProviderContract = curve_1.curve.contracts[curve_1.ALIASES.address_provider].contract;
1451
- return [4 /*yield*/, addressProviderContract.get_address(2, curve_1.curve.options)];
2038
+ return [4 /*yield*/, addressProviderContract.get_address(2, curve_1.curve.constantOptions)];
1452
2039
  case 1:
1453
2040
  registryExchangeAddress = _b.sent();
1454
- console.log(registryExchangeAddress);
1455
2041
  registryExchangeContract = new ethers_1.ethers.Contract(registryExchangeAddress, registry_exchange_json_1.default, curve_1.curve.signer || curve_1.curve.provider);
1456
2042
  _amount = ethers_1.ethers.utils.parseUnits(amount.toString(), inputCoinDecimals);
1457
- return [4 /*yield*/, registryExchangeContract.get_best_rate(inputCoinAddress, outputCoinAddress, _amount, curve_1.curve.options)];
2043
+ return [4 /*yield*/, registryExchangeContract.get_best_rate(inputCoinAddress, outputCoinAddress, _amount, curve_1.curve.constantOptions)];
1458
2044
  case 2:
1459
2045
  _a = _b.sent(), poolAddress = _a[0], output = _a[1];
1460
2046
  return [2 /*return*/, { poolAddress: poolAddress, output: output }];
@@ -1467,9 +2053,9 @@ var getBestPoolAndOutput = function (inputCoin, outputCoin, amount) { return __a
1467
2053
  return __generator(this, function (_d) {
1468
2054
  switch (_d.label) {
1469
2055
  case 0:
1470
- _a = utils_1._getCoinAddresses(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
1471
- _b = utils_1._getCoinDecimals(inputCoinAddress, outputCoinAddress), inputCoinDecimals = _b[0], outputCoinDecimals = _b[1];
1472
- return [4 /*yield*/, exports._getBestPoolAndOutput(inputCoinAddress, outputCoinAddress, inputCoinDecimals, amount)];
2056
+ _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];
2058
+ return [4 /*yield*/, (0, exports._getBestPoolAndOutput)(inputCoinAddress, outputCoinAddress, inputCoinDecimals, amount)];
1473
2059
  case 1:
1474
2060
  _c = _d.sent(), poolAddress = _c.poolAddress, _output = _c.output;
1475
2061
  output = ethers_1.ethers.utils.formatUnits(_output, outputCoinDecimals);
@@ -1481,12 +2067,49 @@ exports.getBestPoolAndOutput = getBestPoolAndOutput;
1481
2067
  var exchangeExpected = function (inputCoin, outputCoin, amount) { return __awaiter(void 0, void 0, void 0, function () {
1482
2068
  return __generator(this, function (_a) {
1483
2069
  switch (_a.label) {
1484
- case 0: return [4 /*yield*/, exports.getBestPoolAndOutput(inputCoin, outputCoin, amount)];
2070
+ case 0: return [4 /*yield*/, (0, exports.getBestPoolAndOutput)(inputCoin, outputCoin, amount)];
1485
2071
  case 1: return [2 /*return*/, (_a.sent())['output']];
1486
2072
  }
1487
2073
  });
1488
2074
  }); };
1489
2075
  exports.exchangeExpected = exchangeExpected;
2076
+ var exchangeEstimateGas = function (inputCoin, outputCoin, amount, maxSlippage) {
2077
+ if (maxSlippage === void 0) { maxSlippage = 0.01; }
2078
+ 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) {
2082
+ case 0:
2083
+ _a = (0, utils_1._getCoinAddresses)(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
2084
+ inputCoinDecimals = (0, utils_1._getCoinDecimals)(inputCoinAddress)[0];
2085
+ addressProviderContract = curve_1.curve.contracts[curve_1.ALIASES.address_provider].contract;
2086
+ return [4 /*yield*/, addressProviderContract.get_registry()];
2087
+ case 1:
2088
+ registryAddress = _c.sent();
2089
+ registryContract = new ethers_1.ethers.Contract(registryAddress, registry_json_1.default, curve_1.curve.signer);
2090
+ return [4 /*yield*/, (0, exports._getBestPoolAndOutput)(inputCoinAddress, outputCoinAddress, inputCoinDecimals, amount)];
2091
+ case 2:
2092
+ poolAddress = (_c.sent()).poolAddress;
2093
+ if (poolAddress === "0x0000000000000000000000000000000000000000") {
2094
+ throw new Error("This pair can't be exchanged");
2095
+ }
2096
+ poolName = (0, utils_1.getPoolNameBySwapAddress)(poolAddress);
2097
+ return [4 /*yield*/, registryContract.get_coin_indices(poolAddress, inputCoinAddress, outputCoinAddress)];
2098
+ case 3:
2099
+ _b = _c.sent(), _i = _b[0], _j = _b[1], is_underlying = _b[2];
2100
+ i = Number(_i.toString());
2101
+ j = Number(_j.toString());
2102
+ pool = new Pool(poolName);
2103
+ if (!is_underlying) return [3 /*break*/, 5];
2104
+ return [4 /*yield*/, pool.estimateGas.exchange(i, j, amount, maxSlippage)];
2105
+ case 4: return [2 /*return*/, _c.sent()];
2106
+ case 5: return [4 /*yield*/, pool.estimateGas.exchangeWrapped(i, j, amount, maxSlippage)];
2107
+ case 6: return [2 /*return*/, _c.sent()];
2108
+ }
2109
+ });
2110
+ });
2111
+ };
2112
+ exports.exchangeEstimateGas = exchangeEstimateGas;
1490
2113
  var exchange = function (inputCoin, outputCoin, amount, maxSlippage) {
1491
2114
  if (maxSlippage === void 0) { maxSlippage = 0.01; }
1492
2115
  return __awaiter(void 0, void 0, void 0, function () {
@@ -1494,21 +2117,20 @@ var exchange = function (inputCoin, outputCoin, amount, maxSlippage) {
1494
2117
  return __generator(this, function (_c) {
1495
2118
  switch (_c.label) {
1496
2119
  case 0:
1497
- _a = utils_1._getCoinAddresses(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
1498
- inputCoinDecimals = utils_1._getCoinDecimals(inputCoinAddress)[0];
2120
+ _a = (0, utils_1._getCoinAddresses)(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
2121
+ inputCoinDecimals = (0, utils_1._getCoinDecimals)(inputCoinAddress)[0];
1499
2122
  addressProviderContract = curve_1.curve.contracts[curve_1.ALIASES.address_provider].contract;
1500
2123
  return [4 /*yield*/, addressProviderContract.get_registry()];
1501
2124
  case 1:
1502
2125
  registryAddress = _c.sent();
1503
- console.log(registryAddress);
1504
2126
  registryContract = new ethers_1.ethers.Contract(registryAddress, registry_json_1.default, curve_1.curve.signer);
1505
- return [4 /*yield*/, exports._getBestPoolAndOutput(inputCoinAddress, outputCoinAddress, inputCoinDecimals, amount)];
2127
+ return [4 /*yield*/, (0, exports._getBestPoolAndOutput)(inputCoinAddress, outputCoinAddress, inputCoinDecimals, amount)];
1506
2128
  case 2:
1507
2129
  poolAddress = (_c.sent()).poolAddress;
1508
2130
  if (poolAddress === "0x0000000000000000000000000000000000000000") {
1509
2131
  throw new Error("This pair can't be exchanged");
1510
2132
  }
1511
- poolName = utils_1.getPoolNameBySwapAddress(poolAddress);
2133
+ poolName = (0, utils_1.getPoolNameBySwapAddress)(poolAddress);
1512
2134
  return [4 /*yield*/, registryContract.get_coin_indices(poolAddress, inputCoinAddress, outputCoinAddress)];
1513
2135
  case 3:
1514
2136
  _b = _c.sent(), _i = _b[0], _j = _b[1], is_underlying = _b[2];
@@ -1531,11 +2153,11 @@ var crossAssetExchangeAvailable = function (inputCoin, outputCoin) { return __aw
1531
2153
  return __generator(this, function (_b) {
1532
2154
  switch (_b.label) {
1533
2155
  case 0:
1534
- _a = utils_1._getCoinAddresses(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
2156
+ _a = (0, utils_1._getCoinAddresses)(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
1535
2157
  return [4 /*yield*/, curve_1.curve.contracts[curve_1.ALIASES.router].contract];
1536
2158
  case 1:
1537
2159
  routerContract = _b.sent();
1538
- return [2 /*return*/, routerContract.can_route(inputCoinAddress, outputCoinAddress, curve_1.curve.options)];
2160
+ return [2 /*return*/, routerContract.can_route(inputCoinAddress, outputCoinAddress, curve_1.curve.constantOptions)];
1539
2161
  }
1540
2162
  });
1541
2163
  }); };
@@ -1559,18 +2181,18 @@ var _crossAssetExchangeInfo = function (inputCoinAddress, outputCoinAddress, inp
1559
2181
  case 1:
1560
2182
  routerContract = _c.sent();
1561
2183
  _amount = ethers_1.ethers.utils.parseUnits(amount, inputCoinDecimals);
1562
- amountBN = utils_1.toBN(_amount, inputCoinDecimals);
1563
- return [4 /*yield*/, routerContract.get_exchange_routing(inputCoinAddress, outputCoinAddress, _amount, curve_1.curve.options)];
2184
+ amountBN = (0, utils_1.toBN)(_amount, inputCoinDecimals);
2185
+ return [4 /*yield*/, routerContract.get_exchange_routing(inputCoinAddress, outputCoinAddress, _amount, curve_1.curve.constantOptions)];
1564
2186
  case 2:
1565
2187
  _a = _c.sent(), route = _a[0], indices = _a[1], _expected = _a[2];
1566
- expectedBN = utils_1.toBN(_expected, outputCoinDecimals);
2188
+ expectedBN = (0, utils_1.toBN)(_expected, outputCoinDecimals);
1567
2189
  exchangeRateBN = expectedBN.div(amountBN);
1568
- _smallAmount = ethers_1.ethers.utils.parseUnits(exports._getSmallAmountForCoin(inputCoinAddress), inputCoinDecimals);
1569
- smallAmountBN = utils_1.toBN(_smallAmount, inputCoinDecimals);
1570
- return [4 /*yield*/, routerContract.get_exchange_routing(inputCoinAddress, outputCoinAddress, _smallAmount, curve_1.curve.options)];
2190
+ _smallAmount = ethers_1.ethers.utils.parseUnits((0, exports._getSmallAmountForCoin)(inputCoinAddress), inputCoinDecimals);
2191
+ smallAmountBN = (0, utils_1.toBN)(_smallAmount, inputCoinDecimals);
2192
+ return [4 /*yield*/, routerContract.get_exchange_routing(inputCoinAddress, outputCoinAddress, _smallAmount, curve_1.curve.constantOptions)];
1571
2193
  case 3:
1572
2194
  _b = _c.sent(), _expectedSmall = _b[2];
1573
- expectedSmallBN = utils_1.toBN(_expectedSmall, outputCoinDecimals);
2195
+ expectedSmallBN = (0, utils_1.toBN)(_expectedSmall, outputCoinDecimals);
1574
2196
  exchangeSmallRateBN = expectedSmallBN.div(smallAmountBN);
1575
2197
  slippage = 1 - exchangeRateBN.div(exchangeSmallRateBN).toNumber();
1576
2198
  return [2 /*return*/, { route: route, indices: indices, _expected: _expected, slippage: slippage }];
@@ -1583,9 +2205,9 @@ var crossAssetExchangeOutputAndSlippage = function (inputCoin, outputCoin, amoun
1583
2205
  return __generator(this, function (_d) {
1584
2206
  switch (_d.label) {
1585
2207
  case 0:
1586
- _a = utils_1._getCoinAddresses(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
1587
- _b = utils_1._getCoinDecimals(inputCoinAddress, outputCoinAddress), inputCoinDecimals = _b[0], outputCoinDecimals = _b[1];
1588
- return [4 /*yield*/, exports._crossAssetExchangeInfo(inputCoinAddress, outputCoinAddress, inputCoinDecimals, outputCoinDecimals, amount)];
2208
+ _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];
2210
+ return [4 /*yield*/, (0, exports._crossAssetExchangeInfo)(inputCoinAddress, outputCoinAddress, inputCoinDecimals, outputCoinDecimals, amount)];
1589
2211
  case 1:
1590
2212
  _c = _d.sent(), _expected = _c._expected, slippage = _c.slippage;
1591
2213
  output = ethers_1.ethers.utils.formatUnits(_expected, outputCoinDecimals);
@@ -1599,13 +2221,13 @@ var crossAssetExchangeExpected = function (inputCoin, outputCoin, amount) { retu
1599
2221
  return __generator(this, function (_d) {
1600
2222
  switch (_d.label) {
1601
2223
  case 0:
1602
- _a = utils_1._getCoinAddresses(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
1603
- _b = utils_1._getCoinDecimals(inputCoinAddress, outputCoinAddress), inputCoinDecimals = _b[0], outputCoinDecimals = _b[1];
2224
+ _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];
1604
2226
  return [4 /*yield*/, curve_1.curve.contracts[curve_1.ALIASES.router].contract];
1605
2227
  case 1:
1606
2228
  routerContract = _d.sent();
1607
2229
  _amount = ethers_1.ethers.utils.parseUnits(amount, inputCoinDecimals);
1608
- return [4 /*yield*/, routerContract.get_exchange_routing(inputCoinAddress, outputCoinAddress, _amount, curve_1.curve.options)];
2230
+ return [4 /*yield*/, routerContract.get_exchange_routing(inputCoinAddress, outputCoinAddress, _amount, curve_1.curve.constantOptions)];
1609
2231
  case 2:
1610
2232
  _c = _d.sent(), _expected = _c[2];
1611
2233
  return [2 /*return*/, ethers_1.ethers.utils.formatUnits(_expected, outputCoinDecimals)];
@@ -1613,36 +2235,79 @@ var crossAssetExchangeExpected = function (inputCoin, outputCoin, amount) { retu
1613
2235
  });
1614
2236
  }); };
1615
2237
  exports.crossAssetExchangeExpected = crossAssetExchangeExpected;
2238
+ var crossAssetExchangeEstimateGas = function (inputCoin, outputCoin, amount, maxSlippage) {
2239
+ if (maxSlippage === void 0) { maxSlippage = 0.02; }
2240
+ 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) {
2244
+ case 0: return [4 /*yield*/, (0, exports.crossAssetExchangeAvailable)(inputCoin, outputCoin)];
2245
+ case 1:
2246
+ if (!(_d.sent()))
2247
+ throw Error("Such exchange is not available");
2248
+ _a = (0, utils_1._getCoinAddresses)(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
2249
+ return [4 /*yield*/, (0, utils_1._getBalances)([inputCoinAddress], [curve_1.curve.signerAddress])];
2250
+ case 2:
2251
+ inputCoinBalance = (_d.sent())[curve_1.curve.signerAddress];
2252
+ if (Number(inputCoinBalance) < Number(amount)) {
2253
+ throw Error("Not enough " + inputCoin + ". Actual: " + inputCoinBalance + ", required: " + amount);
2254
+ }
2255
+ return [4 /*yield*/, (0, utils_1.hasAllowance)([inputCoinAddress], [amount], curve_1.curve.signerAddress, curve_1.ALIASES.router)];
2256
+ case 3:
2257
+ if (!(_d.sent())) {
2258
+ throw Error("Token allowance is needed to estimate gas");
2259
+ }
2260
+ _b = (0, utils_1._getCoinDecimals)(inputCoinAddress, outputCoinAddress), inputCoinDecimals = _b[0], outputCoinDecimals = _b[1];
2261
+ _amount = ethers_1.ethers.utils.parseUnits(amount, inputCoinDecimals);
2262
+ return [4 /*yield*/, (0, exports._crossAssetExchangeInfo)(inputCoinAddress, outputCoinAddress, inputCoinDecimals, outputCoinDecimals, amount)];
2263
+ case 4:
2264
+ _c = _d.sent(), route = _c.route, indices = _c.indices, _expected = _c._expected;
2265
+ minRecvAmountBN = (0, utils_1.toBN)(_expected, outputCoinDecimals).times((1 - maxSlippage));
2266
+ _minRecvAmount = (0, utils_1.fromBN)(minRecvAmountBN, outputCoinDecimals);
2267
+ value = (0, utils_1.isEth)(inputCoinAddress) ? _amount : 0;
2268
+ return [4 /*yield*/, curve_1.curve.contracts[curve_1.ALIASES.router].contract];
2269
+ case 5:
2270
+ routerContract = _d.sent();
2271
+ 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()];
2273
+ }
2274
+ });
2275
+ });
2276
+ };
2277
+ exports.crossAssetExchangeEstimateGas = crossAssetExchangeEstimateGas;
1616
2278
  var crossAssetExchange = function (inputCoin, outputCoin, amount, maxSlippage) {
1617
2279
  if (maxSlippage === void 0) { maxSlippage = 0.02; }
1618
2280
  return __awaiter(void 0, void 0, void 0, function () {
1619
2281
  var _a, inputCoinAddress, outputCoinAddress, _b, inputCoinDecimals, outputCoinDecimals, _amount, _c, route, indices, _expected, minRecvAmountBN, _minRecvAmount, value, routerContract, gasLimit;
1620
2282
  return __generator(this, function (_d) {
1621
2283
  switch (_d.label) {
1622
- case 0: return [4 /*yield*/, exports.crossAssetExchangeAvailable(inputCoin, outputCoin)];
2284
+ case 0: return [4 /*yield*/, (0, exports.crossAssetExchangeAvailable)(inputCoin, outputCoin)];
1623
2285
  case 1:
1624
2286
  if (!(_d.sent()))
1625
2287
  throw Error("Such exchange is not available");
1626
- _a = utils_1._getCoinAddresses(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
1627
- _b = utils_1._getCoinDecimals(inputCoinAddress, outputCoinAddress), inputCoinDecimals = _b[0], outputCoinDecimals = _b[1];
2288
+ _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];
1628
2290
  _amount = ethers_1.ethers.utils.parseUnits(amount, inputCoinDecimals);
1629
- return [4 /*yield*/, exports._crossAssetExchangeInfo(inputCoinAddress, outputCoinAddress, inputCoinDecimals, outputCoinDecimals, amount)];
2291
+ return [4 /*yield*/, (0, exports._crossAssetExchangeInfo)(inputCoinAddress, outputCoinAddress, inputCoinDecimals, outputCoinDecimals, amount)];
1630
2292
  case 2:
1631
2293
  _c = _d.sent(), route = _c.route, indices = _c.indices, _expected = _c._expected;
1632
- minRecvAmountBN = utils_1.toBN(_expected, outputCoinDecimals).times((1 - maxSlippage));
1633
- _minRecvAmount = utils_1.fromBN(minRecvAmountBN, outputCoinDecimals);
1634
- value = utils_1.isEth(inputCoinAddress) ? _amount : 0;
2294
+ minRecvAmountBN = (0, utils_1.toBN)(_expected, outputCoinDecimals).times((1 - maxSlippage));
2295
+ _minRecvAmount = (0, utils_1.fromBN)(minRecvAmountBN, outputCoinDecimals);
2296
+ value = (0, utils_1.isEth)(inputCoinAddress) ? _amount : 0;
1635
2297
  return [4 /*yield*/, curve_1.curve.contracts[curve_1.ALIASES.router].contract];
1636
2298
  case 3:
1637
2299
  routerContract = _d.sent();
1638
- return [4 /*yield*/, utils_1._ensureAllowance([inputCoinAddress], [_amount], curve_1.ALIASES.router)];
2300
+ return [4 /*yield*/, (0, utils_1._ensureAllowance)([inputCoinAddress], [_amount], curve_1.ALIASES.router)];
1639
2301
  case 4:
1640
2302
  _d.sent();
1641
- return [4 /*yield*/, routerContract.estimateGas.exchange(_amount, route, indices, _minRecvAmount, __assign(__assign({}, curve_1.curve.options), { value: value }))];
2303
+ return [4 /*yield*/, curve_1.curve.updateFeeData()];
1642
2304
  case 5:
2305
+ _d.sent();
2306
+ return [4 /*yield*/, routerContract.estimateGas.exchange(_amount, route, indices, _minRecvAmount, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
2307
+ case 6:
1643
2308
  gasLimit = (_d.sent()).mul(130).div(100);
1644
2309
  return [4 /*yield*/, routerContract.exchange(_amount, route, indices, _minRecvAmount, __assign(__assign({}, curve_1.curve.options), { value: value, gasLimit: gasLimit }))];
1645
- case 6: return [2 /*return*/, (_d.sent()).hash];
2310
+ case 7: return [2 /*return*/, (_d.sent()).hash];
1646
2311
  }
1647
2312
  });
1648
2313
  });