@strkfarm/sdk 1.0.32 → 1.0.34

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.
@@ -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
  }