@strkfarm/sdk 2.0.0-dev.26 → 2.0.0-dev.28

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.
Files changed (70) hide show
  1. package/dist/cli.js +190 -36
  2. package/dist/cli.mjs +188 -34
  3. package/dist/index.browser.global.js +79130 -49354
  4. package/dist/index.browser.mjs +18039 -11431
  5. package/dist/index.d.ts +2869 -898
  6. package/dist/index.js +19036 -12207
  7. package/dist/index.mjs +18942 -12158
  8. package/package.json +1 -1
  9. package/src/data/avnu.abi.json +840 -0
  10. package/src/data/ekubo-price-fethcer.abi.json +265 -0
  11. package/src/dataTypes/_bignumber.ts +13 -4
  12. package/src/dataTypes/index.ts +3 -2
  13. package/src/dataTypes/mynumber.ts +141 -0
  14. package/src/global.ts +76 -41
  15. package/src/index.browser.ts +2 -1
  16. package/src/interfaces/common.tsx +167 -2
  17. package/src/modules/ExtendedWrapperSDk/types.ts +26 -4
  18. package/src/modules/ExtendedWrapperSDk/wrapper.ts +110 -67
  19. package/src/modules/apollo-client-config.ts +28 -0
  20. package/src/modules/avnu.ts +4 -4
  21. package/src/modules/ekubo-pricer.ts +79 -0
  22. package/src/modules/ekubo-quoter.ts +46 -30
  23. package/src/modules/erc20.ts +17 -0
  24. package/src/modules/harvests.ts +43 -29
  25. package/src/modules/pragma.ts +23 -8
  26. package/src/modules/pricer-from-api.ts +156 -15
  27. package/src/modules/pricer-lst.ts +1 -1
  28. package/src/modules/pricer.ts +40 -4
  29. package/src/modules/pricerBase.ts +2 -1
  30. package/src/node/deployer.ts +36 -1
  31. package/src/node/pricer-redis.ts +2 -1
  32. package/src/strategies/base-strategy.ts +78 -10
  33. package/src/strategies/ekubo-cl-vault.tsx +906 -347
  34. package/src/strategies/factory.ts +159 -0
  35. package/src/strategies/index.ts +6 -1
  36. package/src/strategies/registry.ts +239 -0
  37. package/src/strategies/sensei.ts +335 -7
  38. package/src/strategies/svk-strategy.ts +97 -27
  39. package/src/strategies/types.ts +4 -0
  40. package/src/strategies/universal-adapters/adapter-utils.ts +2 -1
  41. package/src/strategies/universal-adapters/avnu-adapter.ts +177 -268
  42. package/src/strategies/universal-adapters/baseAdapter.ts +263 -251
  43. package/src/strategies/universal-adapters/common-adapter.ts +206 -203
  44. package/src/strategies/universal-adapters/extended-adapter.ts +155 -336
  45. package/src/strategies/universal-adapters/index.ts +9 -8
  46. package/src/strategies/universal-adapters/token-transfer-adapter.ts +200 -0
  47. package/src/strategies/universal-adapters/usdc<>usdce-adapter.ts +200 -0
  48. package/src/strategies/universal-adapters/vesu-adapter.ts +110 -75
  49. package/src/strategies/universal-adapters/vesu-modify-position-adapter.ts +476 -0
  50. package/src/strategies/universal-adapters/vesu-multiply-adapter.ts +762 -844
  51. package/src/strategies/universal-adapters/vesu-position-common.ts +251 -0
  52. package/src/strategies/universal-adapters/vesu-supply-only-adapter.ts +18 -3
  53. package/src/strategies/universal-lst-muliplier-strategy.tsx +396 -204
  54. package/src/strategies/universal-strategy.tsx +1426 -1178
  55. package/src/strategies/vesu-extended-strategy/services/executionService.ts +2251 -0
  56. package/src/strategies/vesu-extended-strategy/services/extended-vesu-state-manager.ts +2941 -0
  57. package/src/strategies/vesu-extended-strategy/services/operationService.ts +12 -1
  58. package/src/strategies/vesu-extended-strategy/types/transaction-metadata.ts +52 -0
  59. package/src/strategies/vesu-extended-strategy/utils/config.runtime.ts +1 -0
  60. package/src/strategies/vesu-extended-strategy/utils/constants.ts +3 -1
  61. package/src/strategies/vesu-extended-strategy/utils/helper.ts +158 -124
  62. package/src/strategies/vesu-extended-strategy/vesu-extended-strategy.tsx +377 -1781
  63. package/src/strategies/vesu-rebalance.tsx +255 -152
  64. package/src/utils/health-factor-math.ts +4 -1
  65. package/src/utils/index.ts +2 -1
  66. package/src/utils/logger.browser.ts +22 -4
  67. package/src/utils/logger.node.ts +259 -24
  68. package/src/utils/starknet-call-parser.ts +1036 -0
  69. package/src/utils/strategy-utils.ts +61 -0
  70. package/src/strategies/universal-adapters/unused-balance-adapter.ts +0 -109
@@ -16,6 +16,7 @@ import { EkuboPoolKey } from "../ekubo-cl-vault";
16
16
  import VesuPoolV2Abi from '@/data/vesu-pool-v2.abi.json';
17
17
  import VesuExtensionAbi from '@/data/vesu-extension.abi.json';
18
18
  import { CacheClass } from "@/utils/cacheClass";
19
+ import { PricerFromApi } from "@/modules";
19
20
 
20
21
  interface VesuPoolsInfo { pools: any[]; isErrorPoolsAPI: boolean };
21
22
 
@@ -178,7 +179,7 @@ function getVesuMultiplyParams(isIncrease: boolean, params: IncreaseLeverParams
178
179
  }
179
180
  }
180
181
 
181
- const _params = params as DecreaseLeverParams;
182
+ const _params = params as DecreaseLeverParams;
182
183
  return {
183
184
  action: new CairoCustomEnum({ DecreaseLever: {
184
185
  pool_id: _params.pool_id.toBigInt(),
@@ -235,6 +236,29 @@ export const VesuPools = {
235
236
  Re7xSTRK: ContractAddr.from('0x052fb52363939c3aa848f8f4ac28f0a51379f8d1b971d8444de25fbd77d8f161'),
236
237
  Re7xBTC: ContractAddr.from('0x3a8416bf20d036df5b1cf3447630a2e1cb04685f6b0c3a70ed7fb1473548ecf'),
237
238
  Re7USDCPrime: ContractAddr.from('0x02eef0c13b10b487ea5916b54c0a7f98ec43fb3048f60fdeedaf5b08f6f88aaf'),
239
+ Prime: ContractAddr.from('0x451fe483d5921a2919ddd81d0de6696669bccdacd859f72a4fba7656b97c3b5'),
240
+ Re7STRK: ContractAddr.from('0x01fcdacc1d8184eca7b472b5acbaf1500cec9d5683ca95fede8128b46c8f9cc2'),
241
+ }
242
+
243
+ export const VesuPoolMetadata = {
244
+ [VesuPools.Genesis.address]: {
245
+ name: 'Genesis',
246
+ },
247
+ [VesuPools.Re7xSTRK.address]: {
248
+ name: 'Re7 xSTRK',
249
+ },
250
+ [VesuPools.Re7xBTC.address]: {
251
+ name: 'Re7 xBTC',
252
+ },
253
+ [VesuPools.Prime.address]: {
254
+ name: 'Prime',
255
+ },
256
+ [VesuPools.Re7STRK.address]: {
257
+ name: 'Re7 STRK',
258
+ },
259
+ [VesuPools.Re7USDCPrime.address]: {
260
+ name: 'Re7 USDC Prime',
261
+ },
238
262
  }
239
263
 
240
264
  export const extensionMap: {[key: string]: ContractAddr} = {};
@@ -251,6 +275,7 @@ export function getVesuSingletonAddress(vesuPool: ContractAddr) {
251
275
  export class VesuAdapter extends CacheClass {
252
276
  VESU_MULTIPLY_V1 = ContractAddr.from('0x3630f1f8e5b8f5c4c4ae9b6620f8a570ae55cddebc0276c37550e7c118edf67');
253
277
  VESU_MULTIPLY = ContractAddr.from('0x027fef272d0a9a3844767c851a64b36fe4f0115141d81134baade95d2b27b781');
278
+ VESU_WITHDRAW_SWAP_FIXED_MULTIPLIER = ContractAddr.from('0xd2b0006de39992e78d457b7607a606c00bd6b601e9d9a77a8e1870c5e19e73');
254
279
  config: VesuAdapterConfig;
255
280
  networkConfig: IConfig | undefined;
256
281
  pricer: PricerBase | undefined;
@@ -286,31 +311,31 @@ export class VesuAdapter extends CacheClass {
286
311
  // return { leaf: output, callConstructor: this.getModifyPositionCall.bind(this) };
287
312
  // }
288
313
 
289
- // static getDefaultModifyPositionCallParams(params: {
290
- // collateralAmount: Web3Number,
291
- // isAddCollateral: boolean,
292
- // debtAmount: Web3Number,
293
- // isBorrow: boolean
294
- // }) {
295
- // return {
296
- // collateralAmount: {
297
- // amount_type: VesuAmountType.Delta,
298
- // denomination: VesuAmountDenomination.Assets,
299
- // value: {
300
- // abs: params.collateralAmount,
301
- // is_negative: !params.isAddCollateral
302
- // }
303
- // },
304
- // debtAmount: {
305
- // amount_type: VesuAmountType.Delta,
306
- // denomination: VesuAmountDenomination.Assets,
307
- // value: {
308
- // abs: params.debtAmount,
309
- // is_negative: !params.isBorrow
310
- // }
311
- // }
312
- // }
313
- // }
314
+ static getDefaultModifyPositionCallParams(params: {
315
+ collateralAmount: Web3Number,
316
+ isAddCollateral: boolean,
317
+ debtAmount: Web3Number,
318
+ isBorrow: boolean
319
+ }) {
320
+ return {
321
+ collateralAmount: {
322
+ amount_type: VesuAmountType.Delta,
323
+ denomination: VesuAmountDenomination.Assets,
324
+ value: {
325
+ abs: params.collateralAmount,
326
+ is_negative: !params.isAddCollateral
327
+ }
328
+ },
329
+ debtAmount: {
330
+ amount_type: VesuAmountType.Delta,
331
+ denomination: VesuAmountDenomination.Assets,
332
+ value: {
333
+ abs: params.debtAmount,
334
+ is_negative: !params.isBorrow
335
+ }
336
+ }
337
+ }
338
+ }
314
339
 
315
340
  // getModifyPositionCall = (params: VesuModifyPositionCallParams): ManageCall => {
316
341
  // // pub pool_id: felt252,
@@ -439,29 +464,31 @@ export class VesuAdapter extends CacheClass {
439
464
  // }
440
465
  // }
441
466
 
442
- getVesuModifyDelegationCall = (params: VesuModifyDelegationCallParams): ManageCall => {
443
- const VESU_SINGLETON = getVesuSingletonAddress(this.config.poolId).addr;
444
- const { contract, isV2 } = this.getVesuSingletonContract(getMainnetConfig(), this.config.poolId);
445
- const call = contract.populate('modify_delegation', isV2 ? {
446
- delegatee: this.VESU_MULTIPLY.toBigInt(),
447
- delegation: params.delegation,
448
- } : {
449
- pool_id: this.config.poolId.toBigInt(),
450
- delegatee: this.VESU_MULTIPLY_V1.toBigInt(),
451
- delegation: params.delegation,
452
- });
453
- return {
454
- sanitizer: isV2 ? SIMPLE_SANITIZER_V2 : SIMPLE_SANITIZER_VESU_V1_DELEGATIONS,
455
- call: {
456
- contractAddress: VESU_SINGLETON,
457
- selector: hash.getSelectorFromName('modify_delegation'),
458
- calldata: [
459
- ...call.calldata as bigint[]
460
- ]
467
+ getVesuModifyDelegationCall = (delegatee: ContractAddr) => {
468
+ return (params: VesuModifyDelegationCallParams) => {
469
+ const VESU_SINGLETON = getVesuSingletonAddress(this.config.poolId).addr;
470
+ const { contract, isV2 } = this.getVesuSingletonContract(getMainnetConfig(), this.config.poolId);
471
+ const call = contract.populate('modify_delegation', isV2 ? {
472
+ delegatee: delegatee.toBigInt(),
473
+ delegation: params.delegation,
474
+ } : {
475
+ pool_id: this.config.poolId.toBigInt(),
476
+ delegatee: delegatee.toBigInt(),
477
+ delegation: params.delegation,
478
+ });
479
+ return {
480
+ sanitizer: isV2 ? SIMPLE_SANITIZER_V2 : SIMPLE_SANITIZER_VESU_V1_DELEGATIONS,
481
+ call: {
482
+ contractAddress: VESU_SINGLETON,
483
+ selector: hash.getSelectorFromName('modify_delegation'),
484
+ calldata: [
485
+ ...call.calldata as bigint[]
486
+ ]
487
+ }
461
488
  }
462
489
  }
463
490
  }
464
-
491
+
465
492
  // getDefispringRewardsAdapter = (id: string): () => AdapterLeafType<VesuDefiSpringRewardsCallParams> => {
466
493
  // return () => {
467
494
  // const packedArguments: bigint[] = [];
@@ -538,7 +565,7 @@ export class VesuAdapter extends CacheClass {
538
565
  }
539
566
  const output: any = await contract.call('pair_config', [this.config.collateral.address.address, this.config.debt.address.address]);
540
567
  logger.verbose(`${this.config.debt.symbol}::VesuAdapter::getDebtCap debt_cap: ${output.debt_cap.toString()}`);
541
-
568
+
542
569
  if (!isV2) {
543
570
  throw new Error('getDebtCap is not supported for v1');
544
571
  }
@@ -568,7 +595,7 @@ export class VesuAdapter extends CacheClass {
568
595
 
569
596
  }
570
597
  const _interestRateConfig: any = await interestRateConfigContract.call(
571
- 'interest_rate_config',
598
+ 'interest_rate_config',
572
599
  isV2 ? [this.config.debt.address.address] : [this.config.poolId.address, this.config.debt.address.address]
573
600
  );
574
601
  const interestRateConfig: InterestRateConfig = {
@@ -581,24 +608,25 @@ export class VesuAdapter extends CacheClass {
581
608
  min_full_utilization_rate: _interestRateConfig.min_full_utilization_rate,
582
609
  max_full_utilization_rate: _interestRateConfig.max_full_utilization_rate,
583
610
  };
584
-
611
+
585
612
  const _assetConfig: any = await contract.call(
586
- isV2 ? 'asset_config' : 'asset_config_unsafe',
613
+ isV2 ? 'asset_config' : 'asset_config_unsafe',
587
614
  isV2 ? [asset.address.address] : [this.config.poolId.address, asset.address.address]
588
615
  );
589
616
  const assetConfig = isV2 ? _assetConfig : _assetConfig['0'];
590
617
  const timeDelta = assetConfig.last_updated;
591
618
  const lastFullUtilizationRate = assetConfig.last_full_utilization_rate;
592
- const currentDebt = new Web3Number((Number(assetConfig.total_nominal_debt) / 1e18).toFixed(9), asset.decimals);
619
+ const debtSharePrice = Web3Number.fromWei(assetConfig.last_rate_accumulator, 18);
620
+ const currentDebt = (new Web3Number((Number(assetConfig.total_nominal_debt) / 1e18).toFixed(9), asset.decimals)).multipliedBy(debtSharePrice);
593
621
  const totalSupply = currentDebt.plus(Web3Number.fromWei(assetConfig.reserve, asset.decimals));
594
622
 
595
623
  const ratePerSecond = BigInt(Math.round(maxBorrowAPY / 365 / 24 / 60 / 60 * Number(SCALE)));
596
624
  const maxUtilisation = this.getMaxUtilizationGivenRatePerSecond(interestRateConfig, ratePerSecond, timeDelta, lastFullUtilizationRate);
597
625
  logger.verbose(`${asset.symbol}::VesuAdapter::getMaxBorrowableByInterestRate maxUtilisation: ${Number(maxUtilisation) / 1e18}, totalSupply: ${totalSupply.toString()}`);
598
-
626
+
599
627
  const maxDebtToHave = totalSupply.multipliedBy(Number(maxUtilisation) / 1e18);
600
628
  logger.verbose(`${asset.symbol}::VesuAdapter::getMaxBorrowableByInterestRate currentDebt: ${currentDebt.toString()}, maxDebtToHave: ${maxDebtToHave.toString()}`);
601
- return maxDebtToHave.minus(currentDebt);
629
+ return {maxDebtToHave: maxDebtToHave.minus(currentDebt), currentDebt: currentDebt, totalSupply: totalSupply};
602
630
  }
603
631
 
604
632
  async getLTVConfig(config: IConfig, blockNumber: BlockIdentifier = 'latest') {
@@ -636,30 +664,29 @@ export class VesuAdapter extends CacheClass {
636
664
 
637
665
  const { contract, isV2} = this.getVesuSingletonContract(config, this.config.poolId);
638
666
  const output: any = await contract
639
- .call(isV2 ? 'position' : 'position_unsafe', [...(isV2 ?
667
+ .call(isV2 ? 'position' : 'position_unsafe', [...(isV2 ?
640
668
  []: [this.config.poolId.address]), // exclude pool id in v2
641
669
  this.config.collateral.address.address,
642
670
  this.config.debt.address.address,
643
671
  this.config.vaultAllocator.address
644
672
  ], { blockIdentifier: blockNumber });
645
673
 
646
- console.log(output)
647
674
  const token1Price = await this.pricer.getPrice(this.config.collateral.symbol);
648
675
  const token2Price = await this.pricer.getPrice(this.config.debt.symbol);
649
676
  logger.verbose(`VesuAdapter::getPositions token1Price: ${token1Price.price}, token2Price: ${token2Price.price}`);
650
-
677
+
651
678
  const collateralAmount = Web3Number.fromWei(output['1'].toString(), this.config.collateral.decimals);
652
679
  const debtAmount = Web3Number.fromWei(output['2'].toString(), this.config.debt.decimals);
653
680
  const value = [{
654
681
  amount: collateralAmount,
655
682
  token: this.config.collateral,
656
683
  usdValue: collateralAmount.multipliedBy(token1Price.price).toNumber(),
657
- remarks: "Collateral"
684
+ remarks: `Collateral - ${VesuPoolMetadata[this.config.poolId.address].name} pool`
658
685
  }, {
659
686
  amount: debtAmount,
660
687
  token: this.config.debt,
661
688
  usdValue: debtAmount.multipliedBy(token2Price.price).toNumber(),
662
- remarks: "Debt"
689
+ remarks: `Debt - ${VesuPoolMetadata[this.config.poolId.address].name} pool`
663
690
  }];
664
691
  // this.setCache(CACHE_KEY, value, 60000); // ttl: 1min
665
692
  return value.map(v => ({ ...v, protocol: Protocols.VESU }));
@@ -678,7 +705,7 @@ export class VesuAdapter extends CacheClass {
678
705
  const { contract, isV2 } = this.getVesuSingletonContract(config, this.config.poolId);
679
706
  const output: any = await contract
680
707
  .call(isV2 ? 'check_collateralization' : 'check_collateralization_unsafe', [
681
- ...(isV2 ?
708
+ ...(isV2 ?
682
709
  []: [this.config.poolId.address]), // exclude pool id in v2
683
710
  this.config.collateral.address.address,
684
711
  this.config.debt.address.address,
@@ -711,12 +738,20 @@ export class VesuAdapter extends CacheClass {
711
738
 
712
739
  const collateralTokenAmount = positions[0].amount;
713
740
  const collateralUSDAmount = collateralization[0].usdValue;
714
- const collateralPrice = collateralUSDAmount / collateralTokenAmount.toNumber();
741
+ let collateralPrice = collateralUSDAmount / collateralTokenAmount.toNumber();
715
742
 
716
743
  const debtTokenAmount = positions[1].amount;
717
744
  const debtUSDAmount = collateralization[1].usdValue;
718
- const debtPrice = debtUSDAmount / debtTokenAmount.toNumber();
719
-
745
+ let debtPrice = debtUSDAmount / debtTokenAmount.toNumber();
746
+ if (isNaN(collateralPrice)) {
747
+ const pricer = new PricerFromApi(this.networkConfig!, Global.getDefaultTokens())
748
+ collateralPrice = (await pricer.getPrice(this.config.collateral.priceProxySymbol || this.config.collateral.symbol)).price;
749
+ }
750
+ if (isNaN(debtPrice)) {
751
+ const pricer = new PricerFromApi(this.networkConfig!, Global.getDefaultTokens())
752
+ debtPrice = (await pricer.getPrice(this.config.debt.priceProxySymbol || this.config.debt.symbol)).price;
753
+ }
754
+ logger.verbose(`VesuAdapter::getAssetPrices collateralPrice: ${collateralPrice}, debtPrice: ${debtPrice}`);
720
755
  return {
721
756
  collateralTokenAmount,
722
757
  collateralUSDAmount,
@@ -747,7 +782,7 @@ export class VesuAdapter extends CacheClass {
747
782
  let pools: any[] = [];
748
783
  try {
749
784
  const data = await getAPIUsingHeadlessBrowser(
750
- `${ENDPOINTS.VESU_BASE_STAGING}/pools`
785
+ `${ENDPOINTS.VESU_BASE}/pools`
751
786
  );
752
787
  pools = data.data;
753
788
 
@@ -792,11 +827,11 @@ export class VesuAdapter extends CacheClass {
792
827
  min_full_utilization_rate,
793
828
  max_full_utilization_rate,
794
829
  } = interestRateConfig;
795
-
830
+
796
831
  const halfLifeScaled = rate_half_life * SCALE;
797
-
832
+
798
833
  let nextFullUtilizationRate: bigint;
799
-
834
+
800
835
  if (utilization < min_target_utilization) {
801
836
  const utilizationDelta =
802
837
  ((min_target_utilization - utilization) * SCALE) / min_target_utilization;
@@ -811,7 +846,7 @@ export class VesuAdapter extends CacheClass {
811
846
  } else {
812
847
  nextFullUtilizationRate = fullUtilizationRate;
813
848
  }
814
-
849
+
815
850
  if (nextFullUtilizationRate > max_full_utilization_rate) {
816
851
  return max_full_utilization_rate;
817
852
  } else if (nextFullUtilizationRate < min_full_utilization_rate) {
@@ -831,26 +866,26 @@ export class VesuAdapter extends CacheClass {
831
866
  lastFullUtilizationRate: bigint
832
867
  ): { newRatePerSecond: bigint; nextFullUtilizationRate: bigint } {
833
868
  const scaledUtilization = utilization / UTILIZATION_SCALE_TO_SCALE;
834
-
869
+
835
870
  const {
836
871
  target_utilization,
837
872
  zero_utilization_rate,
838
873
  target_rate_percent,
839
874
  } = interestRateConfig;
840
-
875
+
841
876
  const nextFullUtilizationRate = this.fullUtilizationRate(
842
877
  interestRateConfig,
843
878
  timeDelta,
844
879
  scaledUtilization,
845
880
  lastFullUtilizationRate
846
881
  );
847
-
882
+
848
883
  const targetRate =
849
884
  (((nextFullUtilizationRate - zero_utilization_rate) * target_rate_percent) / SCALE) +
850
885
  zero_utilization_rate;
851
-
886
+
852
887
  let newRatePerSecond: bigint;
853
-
888
+
854
889
  if (scaledUtilization < target_utilization) {
855
890
  newRatePerSecond =
856
891
  zero_utilization_rate +
@@ -863,10 +898,10 @@ export class VesuAdapter extends CacheClass {
863
898
  (nextFullUtilizationRate - targetRate)) /
864
899
  (UTILIZATION_SCALE - target_utilization);
865
900
  }
866
-
901
+
867
902
  return { newRatePerSecond, nextFullUtilizationRate };
868
903
  }
869
-
904
+
870
905
  /**
871
906
  * Calculates utilization given a specific rate per second.
872
907
  * This is an inverse function of the piecewise interest rate formula above.
@@ -893,4 +928,4 @@ export class VesuAdapter extends CacheClass {
893
928
  }
894
929
  throw new Error('Max utilization not found');
895
930
  }
896
- }
931
+ }