@defisaver/positions-sdk 2.1.162 → 2.1.164

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 (120) hide show
  1. package/cjs/aaveV2/index.js +2 -0
  2. package/cjs/curveUsd/index.d.ts +5 -2
  3. package/cjs/curveUsd/index.js +30 -15
  4. package/cjs/fluid/index.d.ts +5 -0
  5. package/cjs/fluid/index.js +36 -1
  6. package/cjs/helpers/aaveHelpers/index.js +3 -0
  7. package/cjs/helpers/aaveV4Helpers/index.js +1 -0
  8. package/cjs/helpers/compoundHelpers/index.js +2 -0
  9. package/cjs/helpers/curveUsdHelpers/index.d.ts +3 -1
  10. package/cjs/helpers/curveUsdHelpers/index.js +15 -2
  11. package/cjs/helpers/fluidHelpers/index.js +1 -0
  12. package/cjs/helpers/liquityV2Helpers/index.js +1 -0
  13. package/cjs/helpers/llamaLendHelpers/index.js +1 -0
  14. package/cjs/helpers/makerHelpers/index.d.ts +1 -1
  15. package/cjs/helpers/makerHelpers/index.js +2 -2
  16. package/cjs/helpers/morphoBlueHelpers/index.js +2 -0
  17. package/cjs/helpers/morphoMidnightHelpers/index.js +2 -0
  18. package/cjs/helpers/sparkHelpers/index.js +1 -0
  19. package/cjs/liquity/index.js +15 -2
  20. package/cjs/maker/index.d.ts +7 -2
  21. package/cjs/maker/index.js +44 -12
  22. package/cjs/markets/index.d.ts +1 -0
  23. package/cjs/markets/index.js +3 -1
  24. package/cjs/markets/maker/index.d.ts +1 -0
  25. package/cjs/markets/maker/index.js +13 -0
  26. package/cjs/moneymarket/moneymarketCommonService.d.ts +9 -0
  27. package/cjs/moneymarket/moneymarketCommonService.js +13 -1
  28. package/cjs/portfolio/index.d.ts +5 -1
  29. package/cjs/portfolio/index.js +282 -16
  30. package/cjs/staking/staking.d.ts +20 -0
  31. package/cjs/staking/staking.js +19 -1
  32. package/cjs/types/aave.d.ts +2 -0
  33. package/cjs/types/aaveV4.d.ts +1 -0
  34. package/cjs/types/compound.d.ts +2 -0
  35. package/cjs/types/curveUsd.d.ts +6 -0
  36. package/cjs/types/fluid.d.ts +1 -0
  37. package/cjs/types/liquity.d.ts +4 -0
  38. package/cjs/types/liquityV2.d.ts +2 -0
  39. package/cjs/types/maker.d.ts +4 -0
  40. package/cjs/types/morphoBlue.d.ts +2 -0
  41. package/cjs/types/morphoMidnight.d.ts +1 -0
  42. package/cjs/types/portfolio.d.ts +26 -10
  43. package/cjs/types/spark.d.ts +2 -0
  44. package/esm/aaveV2/index.js +2 -0
  45. package/esm/curveUsd/index.d.ts +5 -2
  46. package/esm/curveUsd/index.js +30 -15
  47. package/esm/fluid/index.d.ts +5 -0
  48. package/esm/fluid/index.js +33 -0
  49. package/esm/helpers/aaveHelpers/index.js +3 -0
  50. package/esm/helpers/aaveV4Helpers/index.js +1 -0
  51. package/esm/helpers/compoundHelpers/index.js +2 -0
  52. package/esm/helpers/curveUsdHelpers/index.d.ts +3 -1
  53. package/esm/helpers/curveUsdHelpers/index.js +16 -3
  54. package/esm/helpers/fluidHelpers/index.js +1 -0
  55. package/esm/helpers/liquityV2Helpers/index.js +1 -0
  56. package/esm/helpers/llamaLendHelpers/index.js +2 -1
  57. package/esm/helpers/makerHelpers/index.d.ts +1 -1
  58. package/esm/helpers/makerHelpers/index.js +2 -2
  59. package/esm/helpers/morphoBlueHelpers/index.js +2 -0
  60. package/esm/helpers/morphoMidnightHelpers/index.js +2 -0
  61. package/esm/helpers/sparkHelpers/index.js +1 -0
  62. package/esm/liquity/index.js +16 -3
  63. package/esm/maker/index.d.ts +7 -2
  64. package/esm/maker/index.js +44 -14
  65. package/esm/markets/index.d.ts +1 -0
  66. package/esm/markets/index.js +1 -0
  67. package/esm/markets/maker/index.d.ts +1 -0
  68. package/esm/markets/maker/index.js +10 -0
  69. package/esm/moneymarket/moneymarketCommonService.d.ts +9 -0
  70. package/esm/moneymarket/moneymarketCommonService.js +11 -0
  71. package/esm/portfolio/index.d.ts +5 -1
  72. package/esm/portfolio/index.js +284 -19
  73. package/esm/staking/staking.d.ts +20 -0
  74. package/esm/staking/staking.js +17 -0
  75. package/esm/types/aave.d.ts +2 -0
  76. package/esm/types/aaveV4.d.ts +1 -0
  77. package/esm/types/compound.d.ts +2 -0
  78. package/esm/types/curveUsd.d.ts +6 -0
  79. package/esm/types/fluid.d.ts +1 -0
  80. package/esm/types/liquity.d.ts +4 -0
  81. package/esm/types/liquityV2.d.ts +2 -0
  82. package/esm/types/maker.d.ts +4 -0
  83. package/esm/types/morphoBlue.d.ts +2 -0
  84. package/esm/types/morphoMidnight.d.ts +1 -0
  85. package/esm/types/portfolio.d.ts +26 -10
  86. package/esm/types/spark.d.ts +2 -0
  87. package/package.json +1 -1
  88. package/src/aaveV2/index.ts +2 -0
  89. package/src/curveUsd/index.ts +33 -16
  90. package/src/fluid/index.ts +40 -0
  91. package/src/helpers/aaveHelpers/index.ts +3 -0
  92. package/src/helpers/aaveV4Helpers/index.ts +1 -0
  93. package/src/helpers/compoundHelpers/index.ts +2 -0
  94. package/src/helpers/curveUsdHelpers/index.ts +23 -3
  95. package/src/helpers/fluidHelpers/index.ts +1 -0
  96. package/src/helpers/liquityV2Helpers/index.ts +1 -0
  97. package/src/helpers/llamaLendHelpers/index.ts +2 -1
  98. package/src/helpers/makerHelpers/index.ts +2 -1
  99. package/src/helpers/morphoBlueHelpers/index.ts +2 -0
  100. package/src/helpers/morphoMidnightHelpers/index.ts +2 -0
  101. package/src/helpers/sparkHelpers/index.ts +1 -0
  102. package/src/liquity/index.ts +17 -3
  103. package/src/maker/index.ts +77 -30
  104. package/src/markets/index.ts +2 -1
  105. package/src/markets/maker/index.ts +10 -0
  106. package/src/moneymarket/moneymarketCommonService.ts +15 -0
  107. package/src/portfolio/index.ts +262 -5
  108. package/src/staking/staking.ts +28 -0
  109. package/src/types/aave.ts +3 -0
  110. package/src/types/aaveV4.ts +1 -0
  111. package/src/types/compound.ts +2 -0
  112. package/src/types/curveUsd.ts +7 -0
  113. package/src/types/fluid.ts +1 -0
  114. package/src/types/liquity.ts +6 -0
  115. package/src/types/liquityV2.ts +2 -0
  116. package/src/types/maker.ts +6 -0
  117. package/src/types/morphoBlue.ts +2 -0
  118. package/src/types/morphoMidnight.ts +1 -0
  119. package/src/types/portfolio.ts +31 -12
  120. package/src/types/spark.ts +2 -0
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  import Dec from 'decimal.js';
11
11
  import { NetworkNumber } from '../types/common';
12
- import { AaveMarkets, AaveV4Spokes, CompoundMarkets, CrvUsdMarkets, LiquityV2Markets, LlamaLendMarkets, MorphoBlueMarkets, MorphoMidnightMarkets, SparkMarkets, } from '../markets';
12
+ import { AaveMarkets, AaveV4Spokes, CompoundMarkets, CrvUsdMarkets, LiquityV2Markets, LlamaLendMarkets, MakerActiveIlks, MorphoBlueMarkets, MorphoMidnightMarkets, SparkMarkets, } from '../markets';
13
13
  import { _getMorphoBlueAccountData, _getMorphoBluePortfolioMarketData, getMorphoEarn } from '../morphoBlue';
14
14
  import { _getMorphoMidnightAccountData, _getMorphoMidnightMarketData } from '../morphoMidnight';
15
15
  import { AaveVersions, CompoundVersions, } from '../types';
@@ -19,13 +19,13 @@ import { _getCurveUsdGlobalData, _getCurveUsdUserData } from '../curveUsd';
19
19
  import { _getLlamaLendGlobalData, _getLlamaLendUserData } from '../llamaLend';
20
20
  import { _getAaveV3AccountData, _getAaveV3MarketData, getStakeAaveData } from '../aaveV3';
21
21
  import { ZERO_ADDRESS } from '../constants';
22
- import { _getMakerCdpData, _getUserCdps } from '../maker';
22
+ import { _getMakerCdpData, _getMakerIlksData, _getUserCdps } from '../maker';
23
23
  import { _getAaveV2AccountData, _getAaveV2MarketsData } from '../aaveV2';
24
24
  import { _getCompoundV2AccountData, _getCompoundV2MarketsData } from '../compoundV2';
25
25
  import { getViemProvider } from '../services/viem';
26
26
  import { _getLiquityTroveInfo, getLiquityStakingData } from '../liquity';
27
27
  import { _getLiquityV2MarketData, getLiquitySAndYBold, getLiquityV2Staking } from '../liquityV2';
28
- import { _getAllUserEarnPositionsWithFTokens, _getUserPositionsPortfolio } from '../fluid';
28
+ import { _getAllFluidMarketDataPortfolio, _getAllUserEarnPositionsWithFTokens, _getUserPositionsPortfolio } from '../fluid';
29
29
  import { getUmbrellaData } from '../umbrella';
30
30
  import { getMerklUnclaimedRewards, getUnclaimedRewardsForAllMarkets } from '../claiming/aaveV3';
31
31
  import { fetchSparkAirdropRewards, fetchSparkRewards } from '../claiming/spark';
@@ -501,21 +501,18 @@ export function getPortfolioData(provider_1, network_1, defaultProvider_1, addre
501
501
  positions[address.toLowerCase()].spark[market.value] = { error: `Error fetching Spark account data for address ${address} on market ${market.value}`, data: null };
502
502
  }
503
503
  }))).flat(),
504
- ...addresses.map((address) => __awaiter(this, void 0, void 0, function* () {
505
- var _a;
506
- return (_a = makerCdps[address.toLowerCase()]) === null || _a === void 0 ? void 0 : _a.map((cdpInfo) => __awaiter(this, void 0, void 0, function* () {
507
- try {
508
- const cdpData = yield _getMakerCdpData(client, network, cdpInfo);
509
- if (cdpData) {
510
- positions[address.toLowerCase()].maker[cdpInfo.id] = { error: '', data: cdpData };
511
- }
512
- }
513
- catch (error) {
514
- console.error(`Error fetching Maker CDP data for address ${address} with ID ${cdpInfo.id}:`, error);
515
- positions[address.toLowerCase()].maker[cdpInfo.id] = { error: `Error fetching Maker CDP data for address ${address} with ID ${cdpInfo.id}`, data: null };
504
+ ...addresses.map((address) => (makerCdps[address.toLowerCase()] || []).map((cdpInfo) => __awaiter(this, void 0, void 0, function* () {
505
+ try {
506
+ const cdpData = yield _getMakerCdpData(client, network, cdpInfo);
507
+ if (cdpData) {
508
+ positions[address.toLowerCase()].maker[cdpInfo.id] = { error: '', data: cdpData };
516
509
  }
517
- }));
518
- })).flat(),
510
+ }
511
+ catch (error) {
512
+ console.error(`Error fetching Maker CDP data for address ${address} with ID ${cdpInfo.id}:`, error);
513
+ positions[address.toLowerCase()].maker[cdpInfo.id] = { error: `Error fetching Maker CDP data for address ${address} with ID ${cdpInfo.id}`, data: null };
514
+ }
515
+ }))).flat(),
519
516
  ...aaveV2Markets.map((market) => addresses.map((address) => __awaiter(this, void 0, void 0, function* () {
520
517
  try {
521
518
  const accData = yield _getAaveV2AccountData(client, network, address, aaveV2MarketsData[market.value].assetsData, market);
@@ -553,9 +550,9 @@ export function getPortfolioData(provider_1, network_1, defaultProvider_1, addre
553
550
  })),
554
551
  ...crvUsdMarkets.map((market) => addresses.map((address) => __awaiter(this, void 0, void 0, function* () {
555
552
  try {
556
- const accData = yield _getCurveUsdUserData(client, network, address, market, crvUsdMarketsData[market.value].activeBand);
553
+ const accData = yield _getCurveUsdUserData(client, network, address, market, crvUsdMarketsData[market.value].activeBand, crvUsdMarketsData[market.value].borrowRate);
557
554
  if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
558
- positions[address.toLowerCase()].crvUsd[market.value] = { error: '', data: Object.assign(Object.assign({}, accData), { borrowRate: crvUsdMarketsData[market.value].borrowRate }) };
555
+ positions[address.toLowerCase()].crvUsd[market.value] = { error: '', data: accData };
559
556
  }
560
557
  }
561
558
  catch (error) {
@@ -599,4 +596,272 @@ export function getPortfolioData(provider_1, network_1, defaultProvider_1, addre
599
596
  };
600
597
  });
601
598
  }
599
+ export function getShifterPortfolioData(provider_1, network_1, defaultProvider_1, addresses_1) {
600
+ return __awaiter(this, arguments, void 0, function* (provider, network, defaultProvider, addresses, isSim = false) {
601
+ const isMainnet = network === NetworkNumber.Eth;
602
+ const isFluidSupported = [NetworkNumber.Eth, NetworkNumber.Arb, NetworkNumber.Base, NetworkNumber.Plasma].includes(network);
603
+ const morphoMarkets = Object.values(MorphoBlueMarkets(network)).filter((market) => market.chainIds.includes(network));
604
+ const compoundV3Markets = Object.values(CompoundMarkets(network)).filter((market) => market.chainIds.includes(network) && market.value !== CompoundVersions.CompoundV2);
605
+ const sparkMarkets = Object.values(SparkMarkets(network)).filter((market) => market.chainIds.includes(network));
606
+ const aaveV3Markets = [AaveVersions.AaveV3, AaveVersions.AaveV3Lido, AaveVersions.AaveV3Etherfi].map((version) => AaveMarkets(network)[version]).filter((market) => market.chainIds.includes(network));
607
+ const aaveV2Markets = [AaveVersions.AaveV2].map((version) => AaveMarkets(network)[version]).filter((market) => market.chainIds.includes(network));
608
+ const compoundV2Markets = [CompoundVersions.CompoundV2].map((version) => CompoundMarkets(network)[version]).filter((market) => market.chainIds.includes(network));
609
+ const crvUsdMarkets = Object.values(CrvUsdMarkets(network)).filter((market) => market.chainIds.includes(network));
610
+ const llamaLendMarkets = [NetworkNumber.Eth, NetworkNumber.Arb].includes(network) ? Object.values(LlamaLendMarkets(network)).filter((market) => market.chainIds.includes(network)) : [];
611
+ const liquityV2Markets = [NetworkNumber.Eth].includes(network) ? Object.values(LiquityV2Markets(network)) : [];
612
+ const aaveV4Spokes = Object.values(AaveV4Spokes(network)).filter((market) => market.chainIds.includes(network));
613
+ const args = [network, { batch: { multicall: { batchSize: isSim ? 2000 : 2500000 } } }];
614
+ const client = getViemProvider(provider, ...args);
615
+ const defaultClient = getViemProvider(defaultProvider, ...args);
616
+ const markets = {
617
+ morphoMarketsData: {},
618
+ morphoMidnightMarketsData: {}, // Morpho Midnight is not fetched for the shifter; key kept so the payload shape is stable
619
+ compoundV3MarketsData: {},
620
+ sparkMarketsData: {},
621
+ aaveV3MarketsData: {},
622
+ aaveV2MarketsData: {},
623
+ compoundV2MarketsData: {},
624
+ crvUsdMarketsData: {},
625
+ llamaLendMarketsData: {},
626
+ liquityV2MarketsData: {},
627
+ aaveV4SpokesData: {},
628
+ fluidMarketsData: {},
629
+ makerMarketsData: {},
630
+ };
631
+ const makerCdps = {};
632
+ const positions = {};
633
+ for (const address of addresses) {
634
+ positions[address.toLowerCase()] = {
635
+ aaveV3: {},
636
+ aaveV4: {},
637
+ morphoBlue: {},
638
+ morphoMidnight: {}, // not fetched for the shifter (see markets above)
639
+ compoundV3: {},
640
+ spark: {},
641
+ maker: {},
642
+ aaveV2: {},
643
+ compoundV2: {},
644
+ liquity: {},
645
+ crvUsd: {},
646
+ llamaLend: {},
647
+ fluid: {
648
+ error: '',
649
+ data: {},
650
+ },
651
+ };
652
+ }
653
+ yield Promise.allSettled([
654
+ // === MARKET DATA (needs to be fetched first) ===
655
+ ...morphoMarkets.map((market) => __awaiter(this, void 0, void 0, function* () {
656
+ markets.morphoMarketsData[market.value] = yield _getMorphoBluePortfolioMarketData(client, network, market);
657
+ })),
658
+ ...compoundV3Markets.map((market) => __awaiter(this, void 0, void 0, function* () {
659
+ markets.compoundV3MarketsData[market.value] = yield _getCompoundV3MarketsData(client, network, market, defaultClient);
660
+ })),
661
+ ...sparkMarkets.map((market) => __awaiter(this, void 0, void 0, function* () {
662
+ markets.sparkMarketsData[market.value] = yield _getSparkMarketsData(client, network, market);
663
+ })),
664
+ ...aaveV3Markets.map((market) => __awaiter(this, void 0, void 0, function* () {
665
+ markets.aaveV3MarketsData[market.value] = yield _getAaveV3MarketData(client, network, market);
666
+ })),
667
+ ...aaveV4Spokes.map((spoke) => __awaiter(this, void 0, void 0, function* () {
668
+ markets.aaveV4SpokesData[spoke.value] = yield _getAaveV4SpokeData(client, network, spoke);
669
+ })),
670
+ ...aaveV2Markets.map((market) => __awaiter(this, void 0, void 0, function* () {
671
+ markets.aaveV2MarketsData[market.value] = yield _getAaveV2MarketsData(client, network, market);
672
+ })),
673
+ ...compoundV2Markets.map((market) => __awaiter(this, void 0, void 0, function* () {
674
+ markets.compoundV2MarketsData[market.value] = yield _getCompoundV2MarketsData(client, network);
675
+ })),
676
+ ...crvUsdMarkets.map((market) => __awaiter(this, void 0, void 0, function* () {
677
+ markets.crvUsdMarketsData[market.value] = yield _getCurveUsdGlobalData(client, network, market);
678
+ })),
679
+ ...llamaLendMarkets.map((market) => __awaiter(this, void 0, void 0, function* () {
680
+ markets.llamaLendMarketsData[market.value] = yield _getLlamaLendGlobalData(client, network, market);
681
+ })),
682
+ ...liquityV2Markets.map((market) => __awaiter(this, void 0, void 0, function* () {
683
+ markets.liquityV2MarketsData[market.value] = yield _getLiquityV2MarketData(client, network, market);
684
+ })),
685
+ (() => __awaiter(this, void 0, void 0, function* () {
686
+ if (!isFluidSupported)
687
+ return;
688
+ try {
689
+ markets.fluidMarketsData = yield _getAllFluidMarketDataPortfolio(client, network);
690
+ }
691
+ catch (error) {
692
+ console.error('Error fetching Fluid markets data:', error);
693
+ }
694
+ }))(),
695
+ (() => __awaiter(this, void 0, void 0, function* () {
696
+ if (!isMainnet)
697
+ return; // Maker CDPs are only available on mainnet
698
+ try {
699
+ markets.makerMarketsData = yield _getMakerIlksData(client, network, MakerActiveIlks);
700
+ }
701
+ catch (error) {
702
+ console.error('Error fetching Maker ilks data:', error);
703
+ }
704
+ }))(),
705
+ // === INDEPENDENT USER DATA (doesn't depend on market data) ===
706
+ ...addresses.map((address) => __awaiter(this, void 0, void 0, function* () {
707
+ if (!isMainnet)
708
+ return; // Maker CDPs are only available on mainnet
709
+ const makerCdp = yield _getUserCdps(client, network, address);
710
+ makerCdps[address.toLowerCase()] = makerCdp;
711
+ })),
712
+ ...addresses.map((address) => __awaiter(this, void 0, void 0, function* () {
713
+ try {
714
+ if (!isFluidSupported)
715
+ return; // Fluid is not available on Optimism
716
+ const userPositions = (yield _getUserPositionsPortfolio(client, network, address));
717
+ for (const position of userPositions) {
718
+ if (position.userData && new Dec(position.userData.suppliedUsd).gt(0)) {
719
+ positions[address.toLowerCase()].fluid.data[position.userData.nftId] = position.userData;
720
+ }
721
+ }
722
+ }
723
+ catch (error) {
724
+ console.error(`Error fetching Fluid positions for address ${address}:`, error);
725
+ positions[address.toLowerCase()].fluid = {
726
+ error: `Error fetching Fluid positions for address ${address}`,
727
+ data: {},
728
+ };
729
+ }
730
+ })),
731
+ ]);
732
+ yield Promise.all([
733
+ ...aaveV3Markets.map((market) => addresses.map((address) => __awaiter(this, void 0, void 0, function* () {
734
+ try {
735
+ const accData = yield _getAaveV3AccountData(client, network, address, Object.assign({ selectedMarket: market }, markets.aaveV3MarketsData[market.value]));
736
+ // Always store the position (even with no supply) so the eMode category is available to the shifter
737
+ positions[address.toLowerCase()].aaveV3[market.value] = { error: '', data: accData };
738
+ }
739
+ catch (error) {
740
+ console.error(`Error fetching AaveV3 account data for address ${address} on market ${market.value}:`, error);
741
+ positions[address.toLowerCase()].aaveV3[market.value] = { error: `Error fetching AaveV3 account data for address ${address} on market ${market.value}`, data: null };
742
+ }
743
+ }))).flat(),
744
+ ...aaveV4Spokes.map((spoke) => addresses.map((address) => __awaiter(this, void 0, void 0, function* () {
745
+ try {
746
+ const accData = yield _getAaveV4AccountData(client, network, markets.aaveV4SpokesData[spoke.value], address);
747
+ if (new Dec(accData.suppliedUsd).gt(0))
748
+ positions[address.toLowerCase()].aaveV4[spoke.value] = { error: '', data: accData };
749
+ }
750
+ catch (error) {
751
+ console.error(`Error fetching AaveV4 account data for address ${address} on spoke ${spoke.value}:`, error);
752
+ positions[address.toLowerCase()].aaveV4[spoke.value] = { error: `Error fetching AaveV4 account data for address ${address} on spoke ${spoke.value}`, data: null };
753
+ }
754
+ }))).flat(),
755
+ ...morphoMarkets.map((market) => addresses.map((address) => __awaiter(this, void 0, void 0, function* () {
756
+ try {
757
+ const accData = yield _getMorphoBlueAccountData(client, network, address, market, markets.morphoMarketsData[market.value]);
758
+ if (new Dec(accData.suppliedUsd).gt(0))
759
+ positions[address.toLowerCase()].morphoBlue[market.value] = { error: '', data: accData };
760
+ }
761
+ catch (error) {
762
+ console.error(`Error fetching MorphoBlue account data for address ${address} on market ${market.value}:`, error);
763
+ positions[address.toLowerCase()].morphoBlue[market.value] = { error: `Error fetching MorphoBlue account data for address ${address} on market ${market.value}`, data: null };
764
+ }
765
+ }))).flat(),
766
+ ...compoundV3Markets.map((market) => addresses.map((address) => __awaiter(this, void 0, void 0, function* () {
767
+ try {
768
+ const accData = yield _getCompoundV3AccountData(client, network, address, ZERO_ADDRESS, { selectedMarket: market, assetsData: markets.compoundV3MarketsData[market.value].assetsData });
769
+ if (new Dec(accData.suppliedUsd).gt(0))
770
+ positions[address.toLowerCase()].compoundV3[market.value] = { error: '', data: accData };
771
+ }
772
+ catch (error) {
773
+ console.error(`Error fetching CompoundV3 account data for address ${address} on market ${market.value}:`, error);
774
+ positions[address.toLowerCase()].compoundV3[market.value] = { error: `Error fetching CompoundV3 account data for address ${address} on market ${market.value}`, data: null };
775
+ }
776
+ }))).flat(),
777
+ ...sparkMarkets.map((market) => addresses.map((address) => __awaiter(this, void 0, void 0, function* () {
778
+ try {
779
+ const accData = yield _getSparkAccountData(client, network, address, { selectedMarket: market, assetsData: markets.sparkMarketsData[market.value].assetsData, eModeCategoriesData: markets.sparkMarketsData[market.value].eModeCategoriesData });
780
+ // Always store the position (even with no supply) so the eMode category is available to the shifter
781
+ positions[address.toLowerCase()].spark[market.value] = { error: '', data: accData };
782
+ }
783
+ catch (error) {
784
+ console.error(`Error fetching Spark account data for address ${address} on market ${market.value}:`, error);
785
+ positions[address.toLowerCase()].spark[market.value] = { error: `Error fetching Spark account data for address ${address} on market ${market.value}`, data: null };
786
+ }
787
+ }))).flat(),
788
+ ...addresses.map((address) => (makerCdps[address.toLowerCase()] || []).map((cdpInfo) => __awaiter(this, void 0, void 0, function* () {
789
+ try {
790
+ // reuse ilk data fetched for the markets payload; ilks outside the active set are fetched on demand
791
+ const cdpData = yield _getMakerCdpData(client, network, cdpInfo, markets.makerMarketsData[cdpInfo.ilkLabel]);
792
+ if (cdpData) {
793
+ positions[address.toLowerCase()].maker[cdpInfo.id] = { error: '', data: cdpData };
794
+ }
795
+ }
796
+ catch (error) {
797
+ console.error(`Error fetching Maker CDP data for address ${address} with ID ${cdpInfo.id}:`, error);
798
+ positions[address.toLowerCase()].maker[cdpInfo.id] = { error: `Error fetching Maker CDP data for address ${address} with ID ${cdpInfo.id}`, data: null };
799
+ }
800
+ }))).flat(),
801
+ ...aaveV2Markets.map((market) => addresses.map((address) => __awaiter(this, void 0, void 0, function* () {
802
+ try {
803
+ const accData = yield _getAaveV2AccountData(client, network, address, markets.aaveV2MarketsData[market.value].assetsData, market);
804
+ if (new Dec(accData.suppliedUsd).gt(0))
805
+ positions[address.toLowerCase()].aaveV2[market.value] = { error: '', data: accData };
806
+ }
807
+ catch (error) {
808
+ console.error(`Error fetching AaveV2 account data for address ${address}:`, error);
809
+ positions[address.toLowerCase()].aaveV2[market.value] = { error: `Error fetching AaveV2 account data for address ${address}`, data: null };
810
+ }
811
+ }))).flat(),
812
+ ...compoundV2Markets.map((market) => addresses.map((address) => __awaiter(this, void 0, void 0, function* () {
813
+ try {
814
+ const accData = yield _getCompoundV2AccountData(client, network, address, markets.compoundV2MarketsData[market.value].assetsData);
815
+ if (new Dec(accData.suppliedUsd).gt(0))
816
+ positions[address.toLowerCase()].compoundV2[market.value] = { error: '', data: accData };
817
+ }
818
+ catch (error) {
819
+ console.error(`Error fetching CompoundV2 account data for address ${address}:`, error);
820
+ positions[address.toLowerCase()].compoundV2[market.value] = { error: `Error fetching CompoundV2 account data for address ${address}`, data: null };
821
+ }
822
+ }))).flat(),
823
+ ...addresses.map((address) => __awaiter(this, void 0, void 0, function* () {
824
+ try {
825
+ if (!isMainnet)
826
+ return; // Liquity trove info is only available on mainnet
827
+ const troveInfo = yield _getLiquityTroveInfo(client, network, address);
828
+ if (new Dec(troveInfo.collateral).gt(0))
829
+ positions[address.toLowerCase()].liquity = { error: '', data: troveInfo };
830
+ }
831
+ catch (error) {
832
+ console.error(`Error fetching Liquity trove info for address ${address}:`, error);
833
+ positions[address.toLowerCase()].liquity = { error: `Error fetching Liquity trove info for address ${address}`, data: null };
834
+ }
835
+ })),
836
+ ...crvUsdMarkets.map((market) => addresses.map((address) => __awaiter(this, void 0, void 0, function* () {
837
+ try {
838
+ const accData = yield _getCurveUsdUserData(client, network, address, market, markets.crvUsdMarketsData[market.value].activeBand, markets.crvUsdMarketsData[market.value].borrowRate);
839
+ if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
840
+ positions[address.toLowerCase()].crvUsd[market.value] = { error: '', data: accData };
841
+ }
842
+ }
843
+ catch (error) {
844
+ console.error(`Error fetching Curve USD account data for address ${address} on market ${market.value}:`, error);
845
+ positions[address.toLowerCase()].crvUsd[market.value] = { error: `Error fetching Curve USD account data for address ${address} on market ${market.value}`, data: null };
846
+ }
847
+ }))).flat(),
848
+ ...llamaLendMarkets.map((market) => addresses.map((address) => __awaiter(this, void 0, void 0, function* () {
849
+ try {
850
+ const accData = yield _getLlamaLendUserData(client, network, address, market, markets.llamaLendMarketsData[market.value]);
851
+ if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
852
+ positions[address.toLowerCase()].llamaLend[market.value] = { error: '', data: Object.assign(Object.assign({}, accData), { borrowRate: markets.llamaLendMarketsData[market.value].borrowRate }) };
853
+ }
854
+ }
855
+ catch (error) {
856
+ console.error(`Error fetching LlamaLend account data for address ${address} on market ${market.value}:`, error);
857
+ positions[address.toLowerCase()].llamaLend[market.value] = { error: `Error fetching LlamaLend account data for address ${address} on market ${market.value}`, data: null };
858
+ }
859
+ }))).flat(),
860
+ ]);
861
+ return {
862
+ positions,
863
+ markets,
864
+ };
865
+ });
866
+ }
602
867
  export * from './discovery';
@@ -3,6 +3,26 @@ import { MMAssetsData, MMUsedAssets } from '../types/common';
3
3
  export declare const STAKING_ASSETS: string[];
4
4
  export declare const getStakingApy: ((asset: string, network?: number) => Promise<string>) & memoize.Memoized<(asset: string, network?: number) => Promise<string>>;
5
5
  export declare const calculateInterestEarned: (principal: string, interest: string, type: string, apy?: boolean) => number;
6
+ /**
7
+ * Net APY for positions that have a single known borrow rate (and optionally a single supply rate)
8
+ * instead of per-asset `assetsData` to feed into `calculateNetApy` (Maker, crvUSD, Liquity v1).
9
+ * All rates are yearly percentages that are already compounded. `incentives` are extra yields
10
+ * (e.g. the native staking yield of the collateral) applied to their own USD base and summed into `incentiveUsd`.
11
+ */
12
+ export declare const calculateNetApyFromRates: ({ suppliedUsd, borrowedUsd, supplyRate, borrowRate, incentives, }: {
13
+ suppliedUsd: string;
14
+ borrowedUsd: string;
15
+ supplyRate?: string;
16
+ borrowRate?: string;
17
+ incentives?: {
18
+ apy: string;
19
+ amountUsd: string;
20
+ }[];
21
+ }) => {
22
+ netApy: string;
23
+ totalInterestUsd: string;
24
+ incentiveUsd: string;
25
+ };
6
26
  export declare const calculateNetApy: ({ usedAssets, assetsData, optionalData, }: {
7
27
  usedAssets: MMUsedAssets;
8
28
  assetsData: MMAssetsData;
@@ -182,6 +182,23 @@ export const calculateInterestEarned = (principal, interest, type, apy = false)
182
182
  }
183
183
  return (+principal * (Math.pow(((1 + (+interest / 100) / BLOCKS_IN_A_YEAR)), (BLOCKS_IN_A_YEAR * interval)))) - +principal; // eslint-disable-line
184
184
  };
185
+ /**
186
+ * Net APY for positions that have a single known borrow rate (and optionally a single supply rate)
187
+ * instead of per-asset `assetsData` to feed into `calculateNetApy` (Maker, crvUSD, Liquity v1).
188
+ * All rates are yearly percentages that are already compounded. `incentives` are extra yields
189
+ * (e.g. the native staking yield of the collateral) applied to their own USD base and summed into `incentiveUsd`.
190
+ */
191
+ export const calculateNetApyFromRates = ({ suppliedUsd, borrowedUsd, supplyRate = '0', borrowRate = '0', incentives = [], }) => {
192
+ const supplyInterest = new Dec(calculateInterestEarned(suppliedUsd, supplyRate, 'year', true));
193
+ const borrowInterest = new Dec(calculateInterestEarned(borrowedUsd, borrowRate, 'year', true));
194
+ const incentiveUsd = incentives
195
+ .reduce((acc, { apy, amountUsd }) => acc.add(calculateInterestEarned(amountUsd, apy, 'year', true)), new Dec(0))
196
+ .toString();
197
+ const totalInterestUsd = supplyInterest.sub(borrowInterest).add(incentiveUsd).toString();
198
+ const balance = new Dec(suppliedUsd).sub(borrowedUsd);
199
+ const netApy = balance.isZero() ? '0' : new Dec(totalInterestUsd).div(balance).times(100).toString();
200
+ return { netApy, totalInterestUsd, incentiveUsd };
201
+ };
185
202
  export const calculateNetApy = ({ usedAssets, assetsData, optionalData, }) => {
186
203
  const sumValues = Object.values(usedAssets).reduce((_acc, usedAsset) => {
187
204
  const acc = Object.assign({}, _acc);
@@ -127,6 +127,7 @@ export interface AavePositionData extends MMPositionData {
127
127
  minRatio: string;
128
128
  collRatio: string;
129
129
  safetyRatioWithLtvZeroFallback?: string;
130
+ safetyRatio?: string;
130
131
  suppliedUsd: string;
131
132
  borrowedUsd: string;
132
133
  borrowLimitUsd: string;
@@ -160,6 +161,7 @@ export interface AaveV3AggregatedPositionData {
160
161
  collRatio: string;
161
162
  borrowLimitWithLtvZeroFallbackUsd: string;
162
163
  safetyRatioWithLtvZeroFallback: string;
164
+ safetyRatio: string;
163
165
  netApy: string;
164
166
  incentiveUsd: string;
165
167
  totalInterestUsd: string;
@@ -169,6 +169,7 @@ export interface AaveV4AggregatedPositionData {
169
169
  leftToBorrowUsd: string;
170
170
  ratio: string;
171
171
  collRatio: string;
172
+ safetyRatio: string;
172
173
  liqRatio: string;
173
174
  liqPercent: string;
174
175
  leveragedType: LeverageType;
@@ -96,6 +96,7 @@ export interface CompoundAggregatedPositionData {
96
96
  leftToBorrowUsd: string;
97
97
  ratio: string;
98
98
  collRatio: string;
99
+ safetyRatio: string;
99
100
  netApy: string;
100
101
  incentiveUsd: string;
101
102
  totalInterestUsd: string;
@@ -115,6 +116,7 @@ export interface CompoundAggregatedPositionData {
115
116
  export interface CompoundPositionData extends MMPositionData {
116
117
  ratio: string;
117
118
  minRatio: string;
119
+ safetyRatio?: string;
118
120
  suppliedUsd: string;
119
121
  borrowedUsd: string;
120
122
  borrowLimitUsd: string;
@@ -73,6 +73,9 @@ export interface CrvUSDAggregatedPositionData {
73
73
  collFactor: string;
74
74
  leveragedType: LeverageType;
75
75
  exposure: string;
76
+ netApy: string;
77
+ totalInterestUsd: string;
78
+ incentiveUsd: string;
76
79
  leveragedAsset?: string;
77
80
  liquidationPrice?: string;
78
81
  }
@@ -113,4 +116,7 @@ export interface CrvUSDUserData {
113
116
  collateralPrice: string;
114
117
  collRatio: string;
115
118
  exposure: string;
119
+ netApy: string;
120
+ totalInterestUsd: string;
121
+ incentiveUsd: string;
116
122
  }
@@ -323,6 +323,7 @@ export interface FluidAggregatedVaultData {
323
323
  merklBorrowIncentives: IncentiveData[];
324
324
  ratio: string;
325
325
  collRatio: string;
326
+ safetyRatio: string;
326
327
  minRatio: string;
327
328
  totalInterestUsd: string;
328
329
  leveragedType?: LeverageType;
@@ -24,7 +24,11 @@ export interface LiquityTroveInfo {
24
24
  totalETH: string;
25
25
  totalLUSD: string;
26
26
  minCollateralRatio: number;
27
+ safetyRatio: string;
27
28
  priceForRecovery: string;
28
29
  debtInFront: string;
29
30
  exposure: string;
31
+ netApy: string;
32
+ totalInterestUsd: string;
33
+ incentiveUsd: string;
30
34
  }
@@ -94,6 +94,7 @@ export interface LiquityV2AggregatedTroveData {
94
94
  liquidationPrice: string;
95
95
  ratio: string;
96
96
  collRatio: string;
97
+ safetyRatio: string;
97
98
  exposure: string;
98
99
  }
99
100
  export interface LiquityV2TroveData {
@@ -101,6 +102,7 @@ export interface LiquityV2TroveData {
101
102
  troveId: string;
102
103
  ratio: string;
103
104
  collRatio: string;
105
+ safetyRatio?: string;
104
106
  liqRatio: string;
105
107
  borrowLimitRatio: string;
106
108
  interestRate: string;
@@ -43,6 +43,7 @@ export interface CdpData {
43
43
  debtAssetMarketPrice: string;
44
44
  liquidationPrice: string;
45
45
  ratio: string;
46
+ safetyRatio: string;
46
47
  liqRatio: string;
47
48
  liqPercent: number;
48
49
  assetPrice: string;
@@ -58,4 +59,7 @@ export interface CdpData {
58
59
  liquidationFee: string;
59
60
  lastUpdated: number;
60
61
  exposure: string;
62
+ netApy: string;
63
+ totalInterestUsd: string;
64
+ incentiveUsd: string;
61
65
  }
@@ -185,6 +185,7 @@ export interface MorphoBlueAggregatedPositionData {
185
185
  totalInterestUsd: string;
186
186
  ltv: string;
187
187
  ratio: string;
188
+ safetyRatio: string;
188
189
  leveragedType: LeverageType;
189
190
  leveragedAsset?: string;
190
191
  currentVolatilePairRatio?: string;
@@ -207,6 +208,7 @@ export interface MorphoBluePositionData {
207
208
  totalInterestUsd: string;
208
209
  ltv: string;
209
210
  ratio: string;
211
+ safetyRatio?: string;
210
212
  leveragedType: LeverageType;
211
213
  leveragedAsset?: string;
212
214
  currentVolatilePairRatio?: string;
@@ -170,6 +170,7 @@ export interface MorphoMidnightAggregatedPositionData {
170
170
  ltv: string;
171
171
  ratio: string;
172
172
  healthRatio: string;
173
+ safetyRatio: string;
173
174
  leveragedType: LeverageType;
174
175
  leveragedAsset?: string;
175
176
  currentVolatilePairRatio?: string;
@@ -1,15 +1,16 @@
1
- import { AaveV2PositionData, AaveV3PositionData, AaveVersions } from './aave';
2
- import { AaveV4AccountData, AaveV4SpokesType } from './aaveV4';
1
+ import { AaveV2MarketData, AaveV2PositionData, AaveV3MarketData, AaveV3PositionData, AaveVersions } from './aave';
2
+ import { AaveV4AccountData, AaveV4SpokeData, AaveV4SpokesType } from './aaveV4';
3
3
  import { EthAddress } from './common';
4
- import { CompoundV2PositionData, CompoundV3PositionData, CompoundVersions } from './compound';
5
- import { CrvUSDUserData, CrvUSDVersions } from './curveUsd';
6
- import { FluidVaultData } from './fluid';
4
+ import { CompoundV2MarketsData, CompoundV2PositionData, CompoundV3MarketsData, CompoundV3PositionData, CompoundVersions } from './compound';
5
+ import { CrvUSDGlobalMarketData, CrvUSDUserData, CrvUSDVersions } from './curveUsd';
6
+ import { FluidMarketData, FluidVaultData } from './fluid';
7
7
  import { LiquityTroveInfo } from './liquity';
8
- import { LlamaLendUserData, LlamaLendVersionsType } from './llamaLend';
9
- import { CdpData } from './maker';
10
- import { MorphoBluePositionData, MorphoBlueVersions } from './morphoBlue';
11
- import { MorphoMidnightPositionData, MorphoMidnightVersions } from './morphoMidnight';
12
- import { SparkPositionData, SparkVersions } from './spark';
8
+ import { LiquityV2MarketData } from './liquityV2';
9
+ import { LlamaLendGlobalMarketData, LlamaLendUserData, LlamaLendVersionsType } from './llamaLend';
10
+ import { CdpData, IlkInfo } from './maker';
11
+ import { MorphoBlueMarketInfo, MorphoBluePositionData, MorphoBlueVersions } from './morphoBlue';
12
+ import { MorphoMidnightMarketInfo, MorphoMidnightPositionData, MorphoMidnightVersions } from './morphoMidnight';
13
+ import { SparkMarketsData, SparkPositionData, SparkVersions } from './spark';
13
14
  export interface PortfolioProtocolData<T> {
14
15
  error: string;
15
16
  data: T | null;
@@ -59,3 +60,18 @@ export interface PortfolioPositionsDataForAddress {
59
60
  export interface PortfolioPositionsData {
60
61
  [key: EthAddress]: PortfolioPositionsDataForAddress;
61
62
  }
63
+ export interface PortfolioMarketsData {
64
+ morphoMarketsData: Record<string, MorphoBlueMarketInfo>;
65
+ morphoMidnightMarketsData: Record<string, MorphoMidnightMarketInfo>;
66
+ compoundV3MarketsData: Record<string, CompoundV3MarketsData>;
67
+ sparkMarketsData: Record<string, SparkMarketsData>;
68
+ aaveV3MarketsData: Record<string, AaveV3MarketData>;
69
+ aaveV2MarketsData: Record<string, AaveV2MarketData>;
70
+ compoundV2MarketsData: Record<string, CompoundV2MarketsData>;
71
+ crvUsdMarketsData: Record<string, CrvUSDGlobalMarketData>;
72
+ llamaLendMarketsData: Record<string, LlamaLendGlobalMarketData>;
73
+ liquityV2MarketsData: Record<string, LiquityV2MarketData>;
74
+ aaveV4SpokesData: Record<string, AaveV4SpokeData>;
75
+ fluidMarketsData: Record<string, FluidMarketData>;
76
+ makerMarketsData: Record<string, IlkInfo>;
77
+ }
@@ -85,6 +85,7 @@ export interface SparkAggregatedPositionData {
85
85
  leftToBorrowUsd: string;
86
86
  ratio: string;
87
87
  collRatio: string;
88
+ safetyRatio: string;
88
89
  netApy: string;
89
90
  incentiveUsd: string;
90
91
  totalInterestUsd: string;
@@ -104,6 +105,7 @@ export interface SparkPositionData extends MMPositionData {
104
105
  ratio: string;
105
106
  minRatio: string;
106
107
  collRatio: string;
108
+ safetyRatio?: string;
107
109
  suppliedUsd: string;
108
110
  borrowedUsd: string;
109
111
  borrowLimitUsd: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defisaver/positions-sdk",
3
- "version": "2.1.162",
3
+ "version": "2.1.164",
4
4
  "description": "",
5
5
  "main": "./cjs/index.js",
6
6
  "module": "./esm/index.js",
@@ -210,6 +210,8 @@ export const _getAaveV2AccountData = async (provider: Client, network: NetworkNu
210
210
  payload.collRatio = payload.borrowedUsd && payload.borrowedUsd !== '0'
211
211
  ? new Dec(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString()
212
212
  : '0';
213
+ // Aave v2 has no LTV-0 fallback (AaveV3View only), so its safety ratio is the plain ratio.
214
+ payload.safetyRatio = payload.ratio;
213
215
 
214
216
  // Calculate borrow limits per asset
215
217
  Object.values(payload.usedAssets).forEach((item) => {