@strkfarm/sdk 1.1.60 → 1.1.61

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.
@@ -80424,13 +80424,20 @@ spurious results.`);
80424
80424
  * @param retry - Current retry attempt number (default 0)
80425
80425
  * @param adjustmentFactor - Percentage to adjust swap amount by (default 1)
80426
80426
  * @param isToken0Deficit - Whether token0 balance needs increasing (default true)
80427
+ * @param MAX_RETRIES - Maximum number of retries (default 40)
80428
+ * @param sameErrorCount - For certain errors, we just retry with same amount again. This is the count of such retries (default { count: 0, error: null })
80429
+ * @param MAX_SAME_ERROR_COUNT - For certain errors, we just retry with same amount again. This limits such retries (default 10)
80427
80430
  * @returns Array of contract calls needed for rebalancing
80428
- * @throws Error if max retries reached without successful rebalance
80431
+ * @throws Error if max retries reached without successful rebalance or max same errors reached
80429
80432
  */
80430
- async rebalanceIter(swapInfo, acc, estimateCall, isSellTokenToken0 = true, retry = 0, lowerLimit = 0n, upperLimit = 0n, MAX_RETRIES = 40) {
80433
+ async rebalanceIter(swapInfo, acc, estimateCall, isSellTokenToken0 = true, retry = 0, lowerLimit = 0n, upperLimit = 0n, MAX_RETRIES = 40, sameErrorCount = { count: 0, error: null }, MAX_SAME_ERROR_COUNT = 10) {
80431
80434
  logger2.verbose(
80432
- `Rebalancing ${this.metadata.name}: retry=${retry}, lowerLimit=${lowerLimit}, upperLimit=${upperLimit}, isSellTokenToken0=${isSellTokenToken0}`
80435
+ `Rebalancing ${this.metadata.name}: retry=${retry}, lowerLimit=${lowerLimit}, upperLimit=${upperLimit}, isSellTokenToken0=${isSellTokenToken0}, MAX_RETRIES=${MAX_RETRIES}, sameErrorCount=${sameErrorCount.error} (${sameErrorCount.count})`
80433
80436
  );
80437
+ if (sameErrorCount.count >= MAX_SAME_ERROR_COUNT) {
80438
+ logger2.error(`Rebalance failed after ${MAX_SAME_ERROR_COUNT} same errors`);
80439
+ throw new Error(`Rebalance failed after ${MAX_SAME_ERROR_COUNT} same errors`);
80440
+ }
80434
80441
  const fromAmount = uint256_exports.uint256ToBN(swapInfo.token_from_amount);
80435
80442
  logger2.verbose(
80436
80443
  `Selling ${fromAmount.toString()} of token ${swapInfo.token_from_address}`
@@ -80449,7 +80456,7 @@ spurious results.`);
80449
80456
  );
80450
80457
  const newSwapInfo = { ...swapInfo };
80451
80458
  const currentAmount = Web3Number.fromWei(fromAmount.toString(), 18);
80452
- logger2.verbose(`Current amount: ${currentAmount.toString()}`);
80459
+ logger2.verbose(`Current amount: ${currentAmount.toString()}, lowerLimit: ${lowerLimit.toString()}, upperLimit: ${upperLimit.toString()}`);
80453
80460
  if (err2.message.includes("invalid token0 balance") || err2.message.includes("invalid token0 amount")) {
80454
80461
  if (!isSellTokenToken0) {
80455
80462
  logger2.verbose("Reducing swap amount - excess token0");
@@ -80496,6 +80503,30 @@ spurious results.`);
80496
80503
  }
80497
80504
  newSwapInfo.token_from_amount = uint256_exports.bnToUint256(nextAmount);
80498
80505
  }
80506
+ } else if (err2.message.includes("Residual tokens")) {
80507
+ logger2.error("Residual tokens");
80508
+ if (sameErrorCount.error == "Residual tokens") {
80509
+ sameErrorCount.count++;
80510
+ } else {
80511
+ sameErrorCount.error = "Residual tokens";
80512
+ sameErrorCount.count = 1;
80513
+ }
80514
+ } else if (err2.message.includes("Insufficient tokens received")) {
80515
+ logger2.error("Insufficient tokens received");
80516
+ if (sameErrorCount.error == "Insufficient tokens received") {
80517
+ sameErrorCount.count++;
80518
+ } else {
80519
+ sameErrorCount.error = "Insufficient tokens received";
80520
+ sameErrorCount.count = 1;
80521
+ }
80522
+ } else if (err2.message.includes("Could not reach the end of the program")) {
80523
+ logger2.error("Could not reach the end of the program, may be the block is full (could be a temp/permanent gas issue)");
80524
+ if (sameErrorCount.error == "Could not reach the end of the program") {
80525
+ sameErrorCount.count++;
80526
+ } else {
80527
+ sameErrorCount.error = "Could not reach the end of the program";
80528
+ sameErrorCount.count = 1;
80529
+ }
80499
80530
  } else {
80500
80531
  logger2.error("Unexpected error:", err2);
80501
80532
  throw err2;
@@ -80508,7 +80539,10 @@ spurious results.`);
80508
80539
  isSellTokenToken0,
80509
80540
  retry + 1,
80510
80541
  lowerLimit,
80511
- upperLimit
80542
+ upperLimit,
80543
+ MAX_RETRIES,
80544
+ sameErrorCount,
80545
+ MAX_SAME_ERROR_COUNT
80512
80546
  );
80513
80547
  }
80514
80548
  }
@@ -16501,13 +16501,20 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
16501
16501
  * @param retry - Current retry attempt number (default 0)
16502
16502
  * @param adjustmentFactor - Percentage to adjust swap amount by (default 1)
16503
16503
  * @param isToken0Deficit - Whether token0 balance needs increasing (default true)
16504
+ * @param MAX_RETRIES - Maximum number of retries (default 40)
16505
+ * @param sameErrorCount - For certain errors, we just retry with same amount again. This is the count of such retries (default { count: 0, error: null })
16506
+ * @param MAX_SAME_ERROR_COUNT - For certain errors, we just retry with same amount again. This limits such retries (default 10)
16504
16507
  * @returns Array of contract calls needed for rebalancing
16505
- * @throws Error if max retries reached without successful rebalance
16508
+ * @throws Error if max retries reached without successful rebalance or max same errors reached
16506
16509
  */
16507
- async rebalanceIter(swapInfo, acc, estimateCall, isSellTokenToken0 = true, retry = 0, lowerLimit = 0n, upperLimit = 0n, MAX_RETRIES = 40) {
16510
+ async rebalanceIter(swapInfo, acc, estimateCall, isSellTokenToken0 = true, retry = 0, lowerLimit = 0n, upperLimit = 0n, MAX_RETRIES = 40, sameErrorCount = { count: 0, error: null }, MAX_SAME_ERROR_COUNT = 10) {
16508
16511
  logger.verbose(
16509
- `Rebalancing ${this.metadata.name}: retry=${retry}, lowerLimit=${lowerLimit}, upperLimit=${upperLimit}, isSellTokenToken0=${isSellTokenToken0}`
16512
+ `Rebalancing ${this.metadata.name}: retry=${retry}, lowerLimit=${lowerLimit}, upperLimit=${upperLimit}, isSellTokenToken0=${isSellTokenToken0}, MAX_RETRIES=${MAX_RETRIES}, sameErrorCount=${sameErrorCount.error} (${sameErrorCount.count})`
16510
16513
  );
16514
+ if (sameErrorCount.count >= MAX_SAME_ERROR_COUNT) {
16515
+ logger.error(`Rebalance failed after ${MAX_SAME_ERROR_COUNT} same errors`);
16516
+ throw new Error(`Rebalance failed after ${MAX_SAME_ERROR_COUNT} same errors`);
16517
+ }
16511
16518
  const fromAmount = uint2564.uint256ToBN(swapInfo.token_from_amount);
16512
16519
  logger.verbose(
16513
16520
  `Selling ${fromAmount.toString()} of token ${swapInfo.token_from_address}`
@@ -16526,7 +16533,7 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
16526
16533
  );
16527
16534
  const newSwapInfo = { ...swapInfo };
16528
16535
  const currentAmount = Web3Number.fromWei(fromAmount.toString(), 18);
16529
- logger.verbose(`Current amount: ${currentAmount.toString()}`);
16536
+ logger.verbose(`Current amount: ${currentAmount.toString()}, lowerLimit: ${lowerLimit.toString()}, upperLimit: ${upperLimit.toString()}`);
16530
16537
  if (err.message.includes("invalid token0 balance") || err.message.includes("invalid token0 amount")) {
16531
16538
  if (!isSellTokenToken0) {
16532
16539
  logger.verbose("Reducing swap amount - excess token0");
@@ -16573,6 +16580,30 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
16573
16580
  }
16574
16581
  newSwapInfo.token_from_amount = uint2564.bnToUint256(nextAmount);
16575
16582
  }
16583
+ } else if (err.message.includes("Residual tokens")) {
16584
+ logger.error("Residual tokens");
16585
+ if (sameErrorCount.error == "Residual tokens") {
16586
+ sameErrorCount.count++;
16587
+ } else {
16588
+ sameErrorCount.error = "Residual tokens";
16589
+ sameErrorCount.count = 1;
16590
+ }
16591
+ } else if (err.message.includes("Insufficient tokens received")) {
16592
+ logger.error("Insufficient tokens received");
16593
+ if (sameErrorCount.error == "Insufficient tokens received") {
16594
+ sameErrorCount.count++;
16595
+ } else {
16596
+ sameErrorCount.error = "Insufficient tokens received";
16597
+ sameErrorCount.count = 1;
16598
+ }
16599
+ } else if (err.message.includes("Could not reach the end of the program")) {
16600
+ logger.error("Could not reach the end of the program, may be the block is full (could be a temp/permanent gas issue)");
16601
+ if (sameErrorCount.error == "Could not reach the end of the program") {
16602
+ sameErrorCount.count++;
16603
+ } else {
16604
+ sameErrorCount.error = "Could not reach the end of the program";
16605
+ sameErrorCount.count = 1;
16606
+ }
16576
16607
  } else {
16577
16608
  logger.error("Unexpected error:", err);
16578
16609
  throw err;
@@ -16585,7 +16616,10 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
16585
16616
  isSellTokenToken0,
16586
16617
  retry + 1,
16587
16618
  lowerLimit,
16588
- upperLimit
16619
+ upperLimit,
16620
+ MAX_RETRIES,
16621
+ sameErrorCount,
16622
+ MAX_SAME_ERROR_COUNT
16589
16623
  );
16590
16624
  }
16591
16625
  }
package/dist/index.d.ts CHANGED
@@ -813,10 +813,16 @@ declare class EkuboCLVault extends BaseStrategy<DualTokenInfo, DualActionAmount>
813
813
  * @param retry - Current retry attempt number (default 0)
814
814
  * @param adjustmentFactor - Percentage to adjust swap amount by (default 1)
815
815
  * @param isToken0Deficit - Whether token0 balance needs increasing (default true)
816
+ * @param MAX_RETRIES - Maximum number of retries (default 40)
817
+ * @param sameErrorCount - For certain errors, we just retry with same amount again. This is the count of such retries (default { count: 0, error: null })
818
+ * @param MAX_SAME_ERROR_COUNT - For certain errors, we just retry with same amount again. This limits such retries (default 10)
816
819
  * @returns Array of contract calls needed for rebalancing
817
- * @throws Error if max retries reached without successful rebalance
820
+ * @throws Error if max retries reached without successful rebalance or max same errors reached
818
821
  */
819
- rebalanceIter(swapInfo: SwapInfo, acc: Account, estimateCall: (swapInfo: SwapInfo) => Promise<Call[]>, isSellTokenToken0?: boolean, retry?: number, lowerLimit?: bigint, upperLimit?: bigint, MAX_RETRIES?: number): Promise<Call[]>;
822
+ rebalanceIter(swapInfo: SwapInfo, acc: Account, estimateCall: (swapInfo: SwapInfo) => Promise<Call[]>, isSellTokenToken0?: boolean, retry?: number, lowerLimit?: bigint, upperLimit?: bigint, MAX_RETRIES?: number, sameErrorCount?: {
823
+ count: number;
824
+ error: null | string;
825
+ }, MAX_SAME_ERROR_COUNT?: number): Promise<Call[]>;
820
826
  static tickToi129(tick: number): {
821
827
  mag: number;
822
828
  sign: number;
package/dist/index.js CHANGED
@@ -16501,13 +16501,20 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
16501
16501
  * @param retry - Current retry attempt number (default 0)
16502
16502
  * @param adjustmentFactor - Percentage to adjust swap amount by (default 1)
16503
16503
  * @param isToken0Deficit - Whether token0 balance needs increasing (default true)
16504
+ * @param MAX_RETRIES - Maximum number of retries (default 40)
16505
+ * @param sameErrorCount - For certain errors, we just retry with same amount again. This is the count of such retries (default { count: 0, error: null })
16506
+ * @param MAX_SAME_ERROR_COUNT - For certain errors, we just retry with same amount again. This limits such retries (default 10)
16504
16507
  * @returns Array of contract calls needed for rebalancing
16505
- * @throws Error if max retries reached without successful rebalance
16508
+ * @throws Error if max retries reached without successful rebalance or max same errors reached
16506
16509
  */
16507
- async rebalanceIter(swapInfo, acc, estimateCall, isSellTokenToken0 = true, retry = 0, lowerLimit = 0n, upperLimit = 0n, MAX_RETRIES = 40) {
16510
+ async rebalanceIter(swapInfo, acc, estimateCall, isSellTokenToken0 = true, retry = 0, lowerLimit = 0n, upperLimit = 0n, MAX_RETRIES = 40, sameErrorCount = { count: 0, error: null }, MAX_SAME_ERROR_COUNT = 10) {
16508
16511
  logger.verbose(
16509
- `Rebalancing ${this.metadata.name}: retry=${retry}, lowerLimit=${lowerLimit}, upperLimit=${upperLimit}, isSellTokenToken0=${isSellTokenToken0}`
16512
+ `Rebalancing ${this.metadata.name}: retry=${retry}, lowerLimit=${lowerLimit}, upperLimit=${upperLimit}, isSellTokenToken0=${isSellTokenToken0}, MAX_RETRIES=${MAX_RETRIES}, sameErrorCount=${sameErrorCount.error} (${sameErrorCount.count})`
16510
16513
  );
16514
+ if (sameErrorCount.count >= MAX_SAME_ERROR_COUNT) {
16515
+ logger.error(`Rebalance failed after ${MAX_SAME_ERROR_COUNT} same errors`);
16516
+ throw new Error(`Rebalance failed after ${MAX_SAME_ERROR_COUNT} same errors`);
16517
+ }
16511
16518
  const fromAmount = import_starknet11.uint256.uint256ToBN(swapInfo.token_from_amount);
16512
16519
  logger.verbose(
16513
16520
  `Selling ${fromAmount.toString()} of token ${swapInfo.token_from_address}`
@@ -16526,7 +16533,7 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
16526
16533
  );
16527
16534
  const newSwapInfo = { ...swapInfo };
16528
16535
  const currentAmount = Web3Number.fromWei(fromAmount.toString(), 18);
16529
- logger.verbose(`Current amount: ${currentAmount.toString()}`);
16536
+ logger.verbose(`Current amount: ${currentAmount.toString()}, lowerLimit: ${lowerLimit.toString()}, upperLimit: ${upperLimit.toString()}`);
16530
16537
  if (err.message.includes("invalid token0 balance") || err.message.includes("invalid token0 amount")) {
16531
16538
  if (!isSellTokenToken0) {
16532
16539
  logger.verbose("Reducing swap amount - excess token0");
@@ -16573,6 +16580,30 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
16573
16580
  }
16574
16581
  newSwapInfo.token_from_amount = import_starknet11.uint256.bnToUint256(nextAmount);
16575
16582
  }
16583
+ } else if (err.message.includes("Residual tokens")) {
16584
+ logger.error("Residual tokens");
16585
+ if (sameErrorCount.error == "Residual tokens") {
16586
+ sameErrorCount.count++;
16587
+ } else {
16588
+ sameErrorCount.error = "Residual tokens";
16589
+ sameErrorCount.count = 1;
16590
+ }
16591
+ } else if (err.message.includes("Insufficient tokens received")) {
16592
+ logger.error("Insufficient tokens received");
16593
+ if (sameErrorCount.error == "Insufficient tokens received") {
16594
+ sameErrorCount.count++;
16595
+ } else {
16596
+ sameErrorCount.error = "Insufficient tokens received";
16597
+ sameErrorCount.count = 1;
16598
+ }
16599
+ } else if (err.message.includes("Could not reach the end of the program")) {
16600
+ logger.error("Could not reach the end of the program, may be the block is full (could be a temp/permanent gas issue)");
16601
+ if (sameErrorCount.error == "Could not reach the end of the program") {
16602
+ sameErrorCount.count++;
16603
+ } else {
16604
+ sameErrorCount.error = "Could not reach the end of the program";
16605
+ sameErrorCount.count = 1;
16606
+ }
16576
16607
  } else {
16577
16608
  logger.error("Unexpected error:", err);
16578
16609
  throw err;
@@ -16585,7 +16616,10 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
16585
16616
  isSellTokenToken0,
16586
16617
  retry + 1,
16587
16618
  lowerLimit,
16588
- upperLimit
16619
+ upperLimit,
16620
+ MAX_RETRIES,
16621
+ sameErrorCount,
16622
+ MAX_SAME_ERROR_COUNT
16589
16623
  );
16590
16624
  }
16591
16625
  }
package/dist/index.mjs CHANGED
@@ -16399,13 +16399,20 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
16399
16399
  * @param retry - Current retry attempt number (default 0)
16400
16400
  * @param adjustmentFactor - Percentage to adjust swap amount by (default 1)
16401
16401
  * @param isToken0Deficit - Whether token0 balance needs increasing (default true)
16402
+ * @param MAX_RETRIES - Maximum number of retries (default 40)
16403
+ * @param sameErrorCount - For certain errors, we just retry with same amount again. This is the count of such retries (default { count: 0, error: null })
16404
+ * @param MAX_SAME_ERROR_COUNT - For certain errors, we just retry with same amount again. This limits such retries (default 10)
16402
16405
  * @returns Array of contract calls needed for rebalancing
16403
- * @throws Error if max retries reached without successful rebalance
16406
+ * @throws Error if max retries reached without successful rebalance or max same errors reached
16404
16407
  */
16405
- async rebalanceIter(swapInfo, acc, estimateCall, isSellTokenToken0 = true, retry = 0, lowerLimit = 0n, upperLimit = 0n, MAX_RETRIES = 40) {
16408
+ async rebalanceIter(swapInfo, acc, estimateCall, isSellTokenToken0 = true, retry = 0, lowerLimit = 0n, upperLimit = 0n, MAX_RETRIES = 40, sameErrorCount = { count: 0, error: null }, MAX_SAME_ERROR_COUNT = 10) {
16406
16409
  logger.verbose(
16407
- `Rebalancing ${this.metadata.name}: retry=${retry}, lowerLimit=${lowerLimit}, upperLimit=${upperLimit}, isSellTokenToken0=${isSellTokenToken0}`
16410
+ `Rebalancing ${this.metadata.name}: retry=${retry}, lowerLimit=${lowerLimit}, upperLimit=${upperLimit}, isSellTokenToken0=${isSellTokenToken0}, MAX_RETRIES=${MAX_RETRIES}, sameErrorCount=${sameErrorCount.error} (${sameErrorCount.count})`
16408
16411
  );
16412
+ if (sameErrorCount.count >= MAX_SAME_ERROR_COUNT) {
16413
+ logger.error(`Rebalance failed after ${MAX_SAME_ERROR_COUNT} same errors`);
16414
+ throw new Error(`Rebalance failed after ${MAX_SAME_ERROR_COUNT} same errors`);
16415
+ }
16409
16416
  const fromAmount = uint2564.uint256ToBN(swapInfo.token_from_amount);
16410
16417
  logger.verbose(
16411
16418
  `Selling ${fromAmount.toString()} of token ${swapInfo.token_from_address}`
@@ -16424,7 +16431,7 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
16424
16431
  );
16425
16432
  const newSwapInfo = { ...swapInfo };
16426
16433
  const currentAmount = Web3Number.fromWei(fromAmount.toString(), 18);
16427
- logger.verbose(`Current amount: ${currentAmount.toString()}`);
16434
+ logger.verbose(`Current amount: ${currentAmount.toString()}, lowerLimit: ${lowerLimit.toString()}, upperLimit: ${upperLimit.toString()}`);
16428
16435
  if (err.message.includes("invalid token0 balance") || err.message.includes("invalid token0 amount")) {
16429
16436
  if (!isSellTokenToken0) {
16430
16437
  logger.verbose("Reducing swap amount - excess token0");
@@ -16471,6 +16478,30 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
16471
16478
  }
16472
16479
  newSwapInfo.token_from_amount = uint2564.bnToUint256(nextAmount);
16473
16480
  }
16481
+ } else if (err.message.includes("Residual tokens")) {
16482
+ logger.error("Residual tokens");
16483
+ if (sameErrorCount.error == "Residual tokens") {
16484
+ sameErrorCount.count++;
16485
+ } else {
16486
+ sameErrorCount.error = "Residual tokens";
16487
+ sameErrorCount.count = 1;
16488
+ }
16489
+ } else if (err.message.includes("Insufficient tokens received")) {
16490
+ logger.error("Insufficient tokens received");
16491
+ if (sameErrorCount.error == "Insufficient tokens received") {
16492
+ sameErrorCount.count++;
16493
+ } else {
16494
+ sameErrorCount.error = "Insufficient tokens received";
16495
+ sameErrorCount.count = 1;
16496
+ }
16497
+ } else if (err.message.includes("Could not reach the end of the program")) {
16498
+ logger.error("Could not reach the end of the program, may be the block is full (could be a temp/permanent gas issue)");
16499
+ if (sameErrorCount.error == "Could not reach the end of the program") {
16500
+ sameErrorCount.count++;
16501
+ } else {
16502
+ sameErrorCount.error = "Could not reach the end of the program";
16503
+ sameErrorCount.count = 1;
16504
+ }
16474
16505
  } else {
16475
16506
  logger.error("Unexpected error:", err);
16476
16507
  throw err;
@@ -16483,7 +16514,10 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
16483
16514
  isSellTokenToken0,
16484
16515
  retry + 1,
16485
16516
  lowerLimit,
16486
- upperLimit
16517
+ upperLimit,
16518
+ MAX_RETRIES,
16519
+ sameErrorCount,
16520
+ MAX_SAME_ERROR_COUNT
16487
16521
  );
16488
16522
  }
16489
16523
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strkfarm/sdk",
3
- "version": "1.1.60",
3
+ "version": "1.1.61",
4
4
  "description": "STRKFarm TS SDK (Meant for our internal use, but feel free to use it)",
5
5
  "typings": "dist/index.d.ts",
6
6
  "types": "dist/index.d.ts",
@@ -1402,8 +1402,11 @@ export class EkuboCLVault extends BaseStrategy<
1402
1402
  * @param retry - Current retry attempt number (default 0)
1403
1403
  * @param adjustmentFactor - Percentage to adjust swap amount by (default 1)
1404
1404
  * @param isToken0Deficit - Whether token0 balance needs increasing (default true)
1405
+ * @param MAX_RETRIES - Maximum number of retries (default 40)
1406
+ * @param sameErrorCount - For certain errors, we just retry with same amount again. This is the count of such retries (default { count: 0, error: null })
1407
+ * @param MAX_SAME_ERROR_COUNT - For certain errors, we just retry with same amount again. This limits such retries (default 10)
1405
1408
  * @returns Array of contract calls needed for rebalancing
1406
- * @throws Error if max retries reached without successful rebalance
1409
+ * @throws Error if max retries reached without successful rebalance or max same errors reached
1407
1410
  */
1408
1411
  async rebalanceIter(
1409
1412
  swapInfo: SwapInfo,
@@ -1413,14 +1416,21 @@ export class EkuboCLVault extends BaseStrategy<
1413
1416
  retry = 0,
1414
1417
  lowerLimit = 0n,
1415
1418
  upperLimit = 0n,
1416
- MAX_RETRIES = 40
1419
+ MAX_RETRIES = 40,
1420
+ sameErrorCount: { count: number, error: null | string } = { count: 0, error: null },
1421
+ MAX_SAME_ERROR_COUNT = 10
1417
1422
  ): Promise<Call[]> {
1418
1423
 
1419
1424
  logger.verbose(
1420
1425
  `Rebalancing ${this.metadata.name}: ` +
1421
- `retry=${retry}, lowerLimit=${lowerLimit}, upperLimit=${upperLimit}, isSellTokenToken0=${isSellTokenToken0}`
1426
+ `retry=${retry}, lowerLimit=${lowerLimit}, upperLimit=${upperLimit}, isSellTokenToken0=${isSellTokenToken0}, MAX_RETRIES=${MAX_RETRIES}, sameErrorCount=${sameErrorCount.error} (${sameErrorCount.count})`
1422
1427
  );
1423
1428
 
1429
+ if (sameErrorCount.count >= MAX_SAME_ERROR_COUNT) {
1430
+ logger.error(`Rebalance failed after ${MAX_SAME_ERROR_COUNT} same errors`);
1431
+ throw new Error(`Rebalance failed after ${MAX_SAME_ERROR_COUNT} same errors`);
1432
+ }
1433
+
1424
1434
  const fromAmount = uint256.uint256ToBN(swapInfo.token_from_amount);
1425
1435
  logger.verbose(
1426
1436
  `Selling ${fromAmount.toString()} of token ${swapInfo.token_from_address}`
@@ -1442,7 +1452,7 @@ export class EkuboCLVault extends BaseStrategy<
1442
1452
 
1443
1453
  const newSwapInfo = { ...swapInfo };
1444
1454
  const currentAmount = Web3Number.fromWei(fromAmount.toString(), 18); // 18 is ok, as its toWei eventually anyways
1445
- logger.verbose(`Current amount: ${currentAmount.toString()}`);
1455
+ logger.verbose(`Current amount: ${currentAmount.toString()}, lowerLimit: ${lowerLimit.toString()}, upperLimit: ${upperLimit.toString()}`);
1446
1456
  if (
1447
1457
  err.message.includes("invalid token0 balance") ||
1448
1458
  err.message.includes("invalid token0 amount")
@@ -1495,6 +1505,33 @@ export class EkuboCLVault extends BaseStrategy<
1495
1505
  }
1496
1506
  newSwapInfo.token_from_amount = uint256.bnToUint256(nextAmount);
1497
1507
  }
1508
+ } else if (err.message.includes("Residual tokens")) {
1509
+ logger.error("Residual tokens");
1510
+ if (sameErrorCount.error == "Residual tokens") {
1511
+ sameErrorCount.count++;
1512
+ } else {
1513
+ sameErrorCount.error = "Residual tokens";
1514
+ sameErrorCount.count = 1;
1515
+ }
1516
+ // dont do anything, just try again.
1517
+ } else if (err.message.includes("Insufficient tokens received")) {
1518
+ logger.error("Insufficient tokens received");
1519
+ if (sameErrorCount.error == "Insufficient tokens received") {
1520
+ sameErrorCount.count++;
1521
+ } else {
1522
+ sameErrorCount.error = "Insufficient tokens received";
1523
+ sameErrorCount.count = 1;
1524
+ }
1525
+ // dont do anything, just try again.
1526
+ } else if (err.message.includes("Could not reach the end of the program")) {
1527
+ logger.error("Could not reach the end of the program, may be the block is full (could be a temp/permanent gas issue)");
1528
+ if (sameErrorCount.error == "Could not reach the end of the program") {
1529
+ sameErrorCount.count++;
1530
+ } else {
1531
+ sameErrorCount.error = "Could not reach the end of the program";
1532
+ sameErrorCount.count = 1;
1533
+ }
1534
+ // just try again.
1498
1535
  } else {
1499
1536
  logger.error("Unexpected error:", err);
1500
1537
  throw err;
@@ -1507,7 +1544,10 @@ export class EkuboCLVault extends BaseStrategy<
1507
1544
  isSellTokenToken0,
1508
1545
  retry + 1,
1509
1546
  lowerLimit,
1510
- upperLimit
1547
+ upperLimit,
1548
+ MAX_RETRIES,
1549
+ sameErrorCount,
1550
+ MAX_SAME_ERROR_COUNT
1511
1551
  );
1512
1552
  }
1513
1553
  }