@strkfarm/sdk 1.0.32 → 1.0.33

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.
@@ -18630,11 +18630,10 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
18630
18630
  * @returns Array of contract calls needed for rebalancing
18631
18631
  * @throws Error if max retries reached without successful rebalance
18632
18632
  */
18633
- async rebalanceIter(swapInfo, acc, estimateCall, retry = 0, adjustmentFactor = 1, isToken0Deficit = true) {
18634
- const MAX_RETRIES = 20;
18635
- const MIN_ADJUSTMENT = 1e-3;
18633
+ async rebalanceIter(swapInfo, acc, estimateCall, isSellTokenToken0 = true, retry = 0, lowerLimit = 0n, upperLimit = 0n) {
18634
+ const MAX_RETRIES = 40;
18636
18635
  logger.verbose(
18637
- `Rebalancing ${this.metadata.name}: retry=${retry}, adjustment=${adjustmentFactor}%, token0Deficit=${isToken0Deficit}`
18636
+ `Rebalancing ${this.metadata.name}: retry=${retry}, lowerLimit=${lowerLimit}, upperLimit=${upperLimit}, isSellTokenToken0=${isSellTokenToken0}`
18638
18637
  );
18639
18638
  const fromAmount = uint2564.uint256ToBN(swapInfo.token_from_amount);
18640
18639
  logger.verbose(
@@ -18649,38 +18648,69 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
18649
18648
  logger.error(`Rebalance failed after ${MAX_RETRIES} retries`);
18650
18649
  throw err;
18651
18650
  }
18652
- if (adjustmentFactor < MIN_ADJUSTMENT) {
18653
- logger.error("Adjustment factor too small, likely oscillating");
18654
- throw new Error("Failed to converge on valid swap amount");
18655
- }
18656
18651
  logger.error(`Rebalance attempt ${retry + 1} failed, adjusting swap amount...`);
18657
18652
  const newSwapInfo = { ...swapInfo };
18658
18653
  const currentAmount = Web3Number.fromWei(fromAmount.toString(), 18);
18654
+ logger.verbose(`Current amount: ${currentAmount.toString()}`);
18659
18655
  if (err.message.includes("invalid token0 balance") || err.message.includes("invalid token0 amount")) {
18660
- logger.verbose("Reducing swap amount - excess token0");
18661
- newSwapInfo.token_from_amount = uint2564.bnToUint256(
18662
- currentAmount.multipliedBy((100 - adjustmentFactor) / 100).toWei()
18663
- );
18664
- adjustmentFactor = isToken0Deficit ? adjustmentFactor * 2 : adjustmentFactor / 2;
18665
- isToken0Deficit = true;
18666
- } else if (err.message.includes("invalid token1 balance") || err.message.includes("invalid token1 amount")) {
18667
- logger.verbose("Increasing swap amount - excess token1");
18668
- newSwapInfo.token_from_amount = uint2564.bnToUint256(
18669
- currentAmount.multipliedBy((100 + adjustmentFactor) / 100).toWei()
18670
- );
18671
- adjustmentFactor = isToken0Deficit ? adjustmentFactor / 2 : adjustmentFactor * 2;
18672
- isToken0Deficit = false;
18656
+ if (!isSellTokenToken0) {
18657
+ logger.verbose("Reducing swap amount - excess token0");
18658
+ let nextAmount = (fromAmount + lowerLimit) / 2n;
18659
+ upperLimit = fromAmount;
18660
+ if (nextAmount <= lowerLimit) {
18661
+ logger.error("Convergence failed: nextAmount <= lowerLimit");
18662
+ throw err;
18663
+ }
18664
+ newSwapInfo.token_from_amount = uint2564.bnToUint256(nextAmount);
18665
+ } else {
18666
+ logger.verbose("Increasing swap amount - deficit token0");
18667
+ let nextAmount = (fromAmount + upperLimit) / 2n;
18668
+ if (upperLimit == 0n) {
18669
+ nextAmount = fromAmount * 2n;
18670
+ }
18671
+ lowerLimit = fromAmount;
18672
+ if (upperLimit != 0n && nextAmount >= upperLimit) {
18673
+ logger.error("Convergence failed: nextAmount >= upperLimit");
18674
+ throw err;
18675
+ }
18676
+ newSwapInfo.token_from_amount = uint2564.bnToUint256(nextAmount);
18677
+ }
18678
+ } else if (err.message.includes("invalid token1 amount") || err.message.includes("invalid token1 balance")) {
18679
+ if (isSellTokenToken0) {
18680
+ logger.verbose("Reducing swap amount - excess token1");
18681
+ let nextAmount = (fromAmount + lowerLimit) / 2n;
18682
+ upperLimit = fromAmount;
18683
+ if (nextAmount <= lowerLimit) {
18684
+ logger.error("Convergence failed: nextAmount <= lowerLimit");
18685
+ throw err;
18686
+ }
18687
+ newSwapInfo.token_from_amount = uint2564.bnToUint256(nextAmount);
18688
+ } else {
18689
+ logger.verbose("Increasing swap amount - deficit token1");
18690
+ let nextAmount = (fromAmount + upperLimit) / 2n;
18691
+ if (upperLimit == 0n) {
18692
+ nextAmount = fromAmount * 2n;
18693
+ }
18694
+ lowerLimit = fromAmount;
18695
+ if (upperLimit != 0n && nextAmount >= upperLimit) {
18696
+ logger.error("Convergence failed: nextAmount >= upperLimit");
18697
+ throw err;
18698
+ }
18699
+ newSwapInfo.token_from_amount = uint2564.bnToUint256(nextAmount);
18700
+ }
18673
18701
  } else {
18674
18702
  logger.error("Unexpected error:", err);
18703
+ throw err;
18675
18704
  }
18676
18705
  newSwapInfo.token_to_min_amount = uint2564.bnToUint256("0");
18677
18706
  return this.rebalanceIter(
18678
18707
  newSwapInfo,
18679
18708
  acc,
18680
18709
  estimateCall,
18710
+ isSellTokenToken0,
18681
18711
  retry + 1,
18682
- adjustmentFactor,
18683
- isToken0Deficit
18712
+ lowerLimit,
18713
+ upperLimit
18684
18714
  );
18685
18715
  }
18686
18716
  }
package/dist/index.d.ts CHANGED
@@ -717,7 +717,7 @@ declare class EkuboCLVault extends BaseStrategy<DualTokenInfo, DualActionAmount>
717
717
  * @returns Array of contract calls needed for rebalancing
718
718
  * @throws Error if max retries reached without successful rebalance
719
719
  */
720
- rebalanceIter(swapInfo: SwapInfo, acc: Account, estimateCall: (swapInfo: SwapInfo) => Promise<Call[]>, retry?: number, adjustmentFactor?: number, isToken0Deficit?: boolean): Promise<Call[]>;
720
+ rebalanceIter(swapInfo: SwapInfo, acc: Account, estimateCall: (swapInfo: SwapInfo) => Promise<Call[]>, isSellTokenToken0?: boolean, retry?: number, lowerLimit?: bigint, upperLimit?: bigint): Promise<Call[]>;
721
721
  static tickToi129(tick: number): {
722
722
  mag: number;
723
723
  sign: number;
package/dist/index.js CHANGED
@@ -18719,11 +18719,10 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
18719
18719
  * @returns Array of contract calls needed for rebalancing
18720
18720
  * @throws Error if max retries reached without successful rebalance
18721
18721
  */
18722
- async rebalanceIter(swapInfo, acc, estimateCall, retry = 0, adjustmentFactor = 1, isToken0Deficit = true) {
18723
- const MAX_RETRIES = 20;
18724
- const MIN_ADJUSTMENT = 1e-3;
18722
+ async rebalanceIter(swapInfo, acc, estimateCall, isSellTokenToken0 = true, retry = 0, lowerLimit = 0n, upperLimit = 0n) {
18723
+ const MAX_RETRIES = 40;
18725
18724
  logger.verbose(
18726
- `Rebalancing ${this.metadata.name}: retry=${retry}, adjustment=${adjustmentFactor}%, token0Deficit=${isToken0Deficit}`
18725
+ `Rebalancing ${this.metadata.name}: retry=${retry}, lowerLimit=${lowerLimit}, upperLimit=${upperLimit}, isSellTokenToken0=${isSellTokenToken0}`
18727
18726
  );
18728
18727
  const fromAmount = import_starknet9.uint256.uint256ToBN(swapInfo.token_from_amount);
18729
18728
  logger.verbose(
@@ -18738,38 +18737,69 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
18738
18737
  logger.error(`Rebalance failed after ${MAX_RETRIES} retries`);
18739
18738
  throw err;
18740
18739
  }
18741
- if (adjustmentFactor < MIN_ADJUSTMENT) {
18742
- logger.error("Adjustment factor too small, likely oscillating");
18743
- throw new Error("Failed to converge on valid swap amount");
18744
- }
18745
18740
  logger.error(`Rebalance attempt ${retry + 1} failed, adjusting swap amount...`);
18746
18741
  const newSwapInfo = { ...swapInfo };
18747
18742
  const currentAmount = Web3Number.fromWei(fromAmount.toString(), 18);
18743
+ logger.verbose(`Current amount: ${currentAmount.toString()}`);
18748
18744
  if (err.message.includes("invalid token0 balance") || err.message.includes("invalid token0 amount")) {
18749
- logger.verbose("Reducing swap amount - excess token0");
18750
- newSwapInfo.token_from_amount = import_starknet9.uint256.bnToUint256(
18751
- currentAmount.multipliedBy((100 - adjustmentFactor) / 100).toWei()
18752
- );
18753
- adjustmentFactor = isToken0Deficit ? adjustmentFactor * 2 : adjustmentFactor / 2;
18754
- isToken0Deficit = true;
18755
- } else if (err.message.includes("invalid token1 balance") || err.message.includes("invalid token1 amount")) {
18756
- logger.verbose("Increasing swap amount - excess token1");
18757
- newSwapInfo.token_from_amount = import_starknet9.uint256.bnToUint256(
18758
- currentAmount.multipliedBy((100 + adjustmentFactor) / 100).toWei()
18759
- );
18760
- adjustmentFactor = isToken0Deficit ? adjustmentFactor / 2 : adjustmentFactor * 2;
18761
- isToken0Deficit = false;
18745
+ if (!isSellTokenToken0) {
18746
+ logger.verbose("Reducing swap amount - excess token0");
18747
+ let nextAmount = (fromAmount + lowerLimit) / 2n;
18748
+ upperLimit = fromAmount;
18749
+ if (nextAmount <= lowerLimit) {
18750
+ logger.error("Convergence failed: nextAmount <= lowerLimit");
18751
+ throw err;
18752
+ }
18753
+ newSwapInfo.token_from_amount = import_starknet9.uint256.bnToUint256(nextAmount);
18754
+ } else {
18755
+ logger.verbose("Increasing swap amount - deficit token0");
18756
+ let nextAmount = (fromAmount + upperLimit) / 2n;
18757
+ if (upperLimit == 0n) {
18758
+ nextAmount = fromAmount * 2n;
18759
+ }
18760
+ lowerLimit = fromAmount;
18761
+ if (upperLimit != 0n && nextAmount >= upperLimit) {
18762
+ logger.error("Convergence failed: nextAmount >= upperLimit");
18763
+ throw err;
18764
+ }
18765
+ newSwapInfo.token_from_amount = import_starknet9.uint256.bnToUint256(nextAmount);
18766
+ }
18767
+ } else if (err.message.includes("invalid token1 amount") || err.message.includes("invalid token1 balance")) {
18768
+ if (isSellTokenToken0) {
18769
+ logger.verbose("Reducing swap amount - excess token1");
18770
+ let nextAmount = (fromAmount + lowerLimit) / 2n;
18771
+ upperLimit = fromAmount;
18772
+ if (nextAmount <= lowerLimit) {
18773
+ logger.error("Convergence failed: nextAmount <= lowerLimit");
18774
+ throw err;
18775
+ }
18776
+ newSwapInfo.token_from_amount = import_starknet9.uint256.bnToUint256(nextAmount);
18777
+ } else {
18778
+ logger.verbose("Increasing swap amount - deficit token1");
18779
+ let nextAmount = (fromAmount + upperLimit) / 2n;
18780
+ if (upperLimit == 0n) {
18781
+ nextAmount = fromAmount * 2n;
18782
+ }
18783
+ lowerLimit = fromAmount;
18784
+ if (upperLimit != 0n && nextAmount >= upperLimit) {
18785
+ logger.error("Convergence failed: nextAmount >= upperLimit");
18786
+ throw err;
18787
+ }
18788
+ newSwapInfo.token_from_amount = import_starknet9.uint256.bnToUint256(nextAmount);
18789
+ }
18762
18790
  } else {
18763
18791
  logger.error("Unexpected error:", err);
18792
+ throw err;
18764
18793
  }
18765
18794
  newSwapInfo.token_to_min_amount = import_starknet9.uint256.bnToUint256("0");
18766
18795
  return this.rebalanceIter(
18767
18796
  newSwapInfo,
18768
18797
  acc,
18769
18798
  estimateCall,
18799
+ isSellTokenToken0,
18770
18800
  retry + 1,
18771
- adjustmentFactor,
18772
- isToken0Deficit
18801
+ lowerLimit,
18802
+ upperLimit
18773
18803
  );
18774
18804
  }
18775
18805
  }
package/dist/index.mjs CHANGED
@@ -18650,11 +18650,10 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
18650
18650
  * @returns Array of contract calls needed for rebalancing
18651
18651
  * @throws Error if max retries reached without successful rebalance
18652
18652
  */
18653
- async rebalanceIter(swapInfo, acc, estimateCall, retry = 0, adjustmentFactor = 1, isToken0Deficit = true) {
18654
- const MAX_RETRIES = 20;
18655
- const MIN_ADJUSTMENT = 1e-3;
18653
+ async rebalanceIter(swapInfo, acc, estimateCall, isSellTokenToken0 = true, retry = 0, lowerLimit = 0n, upperLimit = 0n) {
18654
+ const MAX_RETRIES = 40;
18656
18655
  logger.verbose(
18657
- `Rebalancing ${this.metadata.name}: retry=${retry}, adjustment=${adjustmentFactor}%, token0Deficit=${isToken0Deficit}`
18656
+ `Rebalancing ${this.metadata.name}: retry=${retry}, lowerLimit=${lowerLimit}, upperLimit=${upperLimit}, isSellTokenToken0=${isSellTokenToken0}`
18658
18657
  );
18659
18658
  const fromAmount = uint2564.uint256ToBN(swapInfo.token_from_amount);
18660
18659
  logger.verbose(
@@ -18669,38 +18668,69 @@ var EkuboCLVault = class _EkuboCLVault extends BaseStrategy {
18669
18668
  logger.error(`Rebalance failed after ${MAX_RETRIES} retries`);
18670
18669
  throw err;
18671
18670
  }
18672
- if (adjustmentFactor < MIN_ADJUSTMENT) {
18673
- logger.error("Adjustment factor too small, likely oscillating");
18674
- throw new Error("Failed to converge on valid swap amount");
18675
- }
18676
18671
  logger.error(`Rebalance attempt ${retry + 1} failed, adjusting swap amount...`);
18677
18672
  const newSwapInfo = { ...swapInfo };
18678
18673
  const currentAmount = Web3Number.fromWei(fromAmount.toString(), 18);
18674
+ logger.verbose(`Current amount: ${currentAmount.toString()}`);
18679
18675
  if (err.message.includes("invalid token0 balance") || err.message.includes("invalid token0 amount")) {
18680
- logger.verbose("Reducing swap amount - excess token0");
18681
- newSwapInfo.token_from_amount = uint2564.bnToUint256(
18682
- currentAmount.multipliedBy((100 - adjustmentFactor) / 100).toWei()
18683
- );
18684
- adjustmentFactor = isToken0Deficit ? adjustmentFactor * 2 : adjustmentFactor / 2;
18685
- isToken0Deficit = true;
18686
- } else if (err.message.includes("invalid token1 balance") || err.message.includes("invalid token1 amount")) {
18687
- logger.verbose("Increasing swap amount - excess token1");
18688
- newSwapInfo.token_from_amount = uint2564.bnToUint256(
18689
- currentAmount.multipliedBy((100 + adjustmentFactor) / 100).toWei()
18690
- );
18691
- adjustmentFactor = isToken0Deficit ? adjustmentFactor / 2 : adjustmentFactor * 2;
18692
- isToken0Deficit = false;
18676
+ if (!isSellTokenToken0) {
18677
+ logger.verbose("Reducing swap amount - excess token0");
18678
+ let nextAmount = (fromAmount + lowerLimit) / 2n;
18679
+ upperLimit = fromAmount;
18680
+ if (nextAmount <= lowerLimit) {
18681
+ logger.error("Convergence failed: nextAmount <= lowerLimit");
18682
+ throw err;
18683
+ }
18684
+ newSwapInfo.token_from_amount = uint2564.bnToUint256(nextAmount);
18685
+ } else {
18686
+ logger.verbose("Increasing swap amount - deficit token0");
18687
+ let nextAmount = (fromAmount + upperLimit) / 2n;
18688
+ if (upperLimit == 0n) {
18689
+ nextAmount = fromAmount * 2n;
18690
+ }
18691
+ lowerLimit = fromAmount;
18692
+ if (upperLimit != 0n && nextAmount >= upperLimit) {
18693
+ logger.error("Convergence failed: nextAmount >= upperLimit");
18694
+ throw err;
18695
+ }
18696
+ newSwapInfo.token_from_amount = uint2564.bnToUint256(nextAmount);
18697
+ }
18698
+ } else if (err.message.includes("invalid token1 amount") || err.message.includes("invalid token1 balance")) {
18699
+ if (isSellTokenToken0) {
18700
+ logger.verbose("Reducing swap amount - excess token1");
18701
+ let nextAmount = (fromAmount + lowerLimit) / 2n;
18702
+ upperLimit = fromAmount;
18703
+ if (nextAmount <= lowerLimit) {
18704
+ logger.error("Convergence failed: nextAmount <= lowerLimit");
18705
+ throw err;
18706
+ }
18707
+ newSwapInfo.token_from_amount = uint2564.bnToUint256(nextAmount);
18708
+ } else {
18709
+ logger.verbose("Increasing swap amount - deficit token1");
18710
+ let nextAmount = (fromAmount + upperLimit) / 2n;
18711
+ if (upperLimit == 0n) {
18712
+ nextAmount = fromAmount * 2n;
18713
+ }
18714
+ lowerLimit = fromAmount;
18715
+ if (upperLimit != 0n && nextAmount >= upperLimit) {
18716
+ logger.error("Convergence failed: nextAmount >= upperLimit");
18717
+ throw err;
18718
+ }
18719
+ newSwapInfo.token_from_amount = uint2564.bnToUint256(nextAmount);
18720
+ }
18693
18721
  } else {
18694
18722
  logger.error("Unexpected error:", err);
18723
+ throw err;
18695
18724
  }
18696
18725
  newSwapInfo.token_to_min_amount = uint2564.bnToUint256("0");
18697
18726
  return this.rebalanceIter(
18698
18727
  newSwapInfo,
18699
18728
  acc,
18700
18729
  estimateCall,
18730
+ isSellTokenToken0,
18701
18731
  retry + 1,
18702
- adjustmentFactor,
18703
- isToken0Deficit
18732
+ lowerLimit,
18733
+ upperLimit
18704
18734
  );
18705
18735
  }
18706
18736
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strkfarm/sdk",
3
- "version": "1.0.32",
3
+ "version": "1.0.33",
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",
@@ -702,16 +702,16 @@ export class EkuboCLVault extends BaseStrategy<DualTokenInfo, DualActionAmount>
702
702
  swapInfo: SwapInfo,
703
703
  acc: Account,
704
704
  estimateCall: (swapInfo: SwapInfo) => Promise<Call[]>,
705
+ isSellTokenToken0 = true,
705
706
  retry = 0,
706
- adjustmentFactor = 1,
707
- isToken0Deficit = true
707
+ lowerLimit = 0n,
708
+ upperLimit = 0n,
708
709
  ): Promise<Call[]> {
709
- const MAX_RETRIES = 20;
710
- const MIN_ADJUSTMENT = 0.001; // Minimum adjustment factor
710
+ const MAX_RETRIES = 40;
711
711
 
712
712
  logger.verbose(
713
713
  `Rebalancing ${this.metadata.name}: ` +
714
- `retry=${retry}, adjustment=${adjustmentFactor}%, token0Deficit=${isToken0Deficit}`
714
+ `retry=${retry}, lowerLimit=${lowerLimit}, upperLimit=${upperLimit}, isSellTokenToken0=${isSellTokenToken0}`
715
715
  );
716
716
 
717
717
  const fromAmount = uint256.uint256ToBN(swapInfo.token_from_amount);
@@ -729,47 +729,70 @@ export class EkuboCLVault extends BaseStrategy<DualTokenInfo, DualActionAmount>
729
729
  throw err;
730
730
  }
731
731
 
732
- if (adjustmentFactor < MIN_ADJUSTMENT) {
733
- logger.error('Adjustment factor too small, likely oscillating');
734
- throw new Error('Failed to converge on valid swap amount');
735
- }
736
-
737
732
  logger.error(`Rebalance attempt ${retry + 1} failed, adjusting swap amount...`);
738
733
 
739
734
  const newSwapInfo = { ...swapInfo };
740
735
  const currentAmount = Web3Number.fromWei(fromAmount.toString(), 18); // 18 is ok, as its toWei eventually anyways
741
-
736
+ logger.verbose(`Current amount: ${currentAmount.toString()}`);
742
737
  if (err.message.includes('invalid token0 balance') || err.message.includes('invalid token0 amount')) {
743
- // Too much token0, decrease swap amount
744
- logger.verbose('Reducing swap amount - excess token0');
745
- newSwapInfo.token_from_amount = uint256.bnToUint256(
746
- currentAmount.multipliedBy((100 - adjustmentFactor)/100).toWei()
747
- );
748
- adjustmentFactor = isToken0Deficit ? adjustmentFactor * 2: adjustmentFactor / 2;
749
- isToken0Deficit = true;
750
-
751
- } else if (err.message.includes('invalid token1 balance') || err.message.includes('invalid token1 amount')) {
752
- // Too much token1, increase swap amount
753
- logger.verbose('Increasing swap amount - excess token1');
754
- newSwapInfo.token_from_amount = uint256.bnToUint256(
755
- currentAmount.multipliedBy((100 + adjustmentFactor)/100).toWei()
756
- );
757
- adjustmentFactor = isToken0Deficit ? adjustmentFactor / 2 : adjustmentFactor * 2;
758
- isToken0Deficit = false;
759
-
738
+ if (!isSellTokenToken0) {
739
+ logger.verbose('Reducing swap amount - excess token0');
740
+ let nextAmount = (fromAmount + lowerLimit) / 2n;
741
+ upperLimit = fromAmount;
742
+ if (nextAmount <= lowerLimit) {
743
+ logger.error('Convergence failed: nextAmount <= lowerLimit');
744
+ throw err;
745
+ }
746
+ newSwapInfo.token_from_amount = uint256.bnToUint256(nextAmount);
747
+ } else {
748
+ logger.verbose('Increasing swap amount - deficit token0');
749
+ let nextAmount = (fromAmount + upperLimit) / 2n;
750
+ if (upperLimit == 0n) {
751
+ nextAmount = fromAmount * 2n;
752
+ }
753
+ lowerLimit = fromAmount;
754
+ if (upperLimit != 0n && nextAmount >= upperLimit) {
755
+ logger.error('Convergence failed: nextAmount >= upperLimit');
756
+ throw err;
757
+ }
758
+ newSwapInfo.token_from_amount = uint256.bnToUint256(nextAmount);
759
+ }
760
+ } else if (err.message.includes('invalid token1 amount') || err.message.includes('invalid token1 balance')) {
761
+ if (isSellTokenToken0) {
762
+ logger.verbose('Reducing swap amount - excess token1');
763
+ let nextAmount = (fromAmount + lowerLimit) / 2n;
764
+ upperLimit = fromAmount;
765
+ if (nextAmount <= lowerLimit) {
766
+ logger.error('Convergence failed: nextAmount <= lowerLimit');
767
+ throw err;
768
+ }
769
+ newSwapInfo.token_from_amount = uint256.bnToUint256(nextAmount);
770
+ } else {
771
+ logger.verbose('Increasing swap amount - deficit token1');
772
+ let nextAmount = (fromAmount + upperLimit) / 2n;
773
+ if (upperLimit == 0n) {
774
+ nextAmount = fromAmount * 2n;
775
+ }
776
+ lowerLimit = fromAmount;
777
+ if (upperLimit != 0n && nextAmount >= upperLimit) {
778
+ logger.error('Convergence failed: nextAmount >= upperLimit');
779
+ throw err;
780
+ }
781
+ newSwapInfo.token_from_amount = uint256.bnToUint256(nextAmount);
782
+ }
760
783
  } else {
761
784
  logger.error('Unexpected error:', err);
785
+ throw err;
762
786
  }
763
-
764
787
  newSwapInfo.token_to_min_amount = uint256.bnToUint256('0');
765
-
766
788
  return this.rebalanceIter(
767
789
  newSwapInfo,
768
790
  acc,
769
791
  estimateCall,
792
+ isSellTokenToken0,
770
793
  retry + 1,
771
- adjustmentFactor,
772
- isToken0Deficit
794
+ lowerLimit,
795
+ upperLimit,
773
796
  );
774
797
  }
775
798
  }