@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
@@ -7,6 +7,7 @@ import {
7
7
  CrvUsdMarkets,
8
8
  LiquityV2Markets,
9
9
  LlamaLendMarkets,
10
+ MakerActiveIlks,
10
11
  MorphoBlueMarkets,
11
12
  MorphoMidnightMarkets,
12
13
  SparkMarkets,
@@ -27,6 +28,7 @@ import {
27
28
  LlamaLendGlobalMarketData,
28
29
  MorphoBlueMarketInfo,
29
30
  MorphoMidnightMarketInfo,
31
+ PortfolioMarketsData,
30
32
  PortfolioPositionsData,
31
33
  SparkMarketsData,
32
34
  } from '../types';
@@ -36,13 +38,13 @@ import { _getCurveUsdGlobalData, _getCurveUsdUserData } from '../curveUsd';
36
38
  import { _getLlamaLendGlobalData, _getLlamaLendUserData } from '../llamaLend';
37
39
  import { _getAaveV3AccountData, _getAaveV3MarketData, getStakeAaveData } from '../aaveV3';
38
40
  import { ZERO_ADDRESS } from '../constants';
39
- import { _getMakerCdpData, _getUserCdps } from '../maker';
41
+ import { _getMakerCdpData, _getMakerIlksData, _getUserCdps } from '../maker';
40
42
  import { _getAaveV2AccountData, _getAaveV2MarketsData } from '../aaveV2';
41
43
  import { _getCompoundV2AccountData, _getCompoundV2MarketsData } from '../compoundV2';
42
44
  import { getViemProvider } from '../services/viem';
43
45
  import { _getLiquityTroveInfo, getLiquityStakingData } from '../liquity';
44
46
  import { _getLiquityV2MarketData, getLiquitySAndYBold, getLiquityV2Staking } from '../liquityV2';
45
- import { _getAllUserEarnPositionsWithFTokens, _getUserPositionsPortfolio } from '../fluid';
47
+ import { _getAllFluidMarketDataPortfolio, _getAllUserEarnPositionsWithFTokens, _getUserPositionsPortfolio } from '../fluid';
46
48
  import { getUmbrellaData } from '../umbrella';
47
49
  import { getMerklUnclaimedRewards, getUnclaimedRewardsForAllMarkets } from '../claiming/aaveV3';
48
50
  import { fetchSparkAirdropRewards, fetchSparkRewards } from '../claiming/spark';
@@ -506,7 +508,7 @@ export async function getPortfolioData(provider: EthereumProvider, network: Netw
506
508
  positions[address.toLowerCase() as EthAddress].spark[market.value] = { error: `Error fetching Spark account data for address ${address} on market ${market.value}`, data: null };
507
509
  }
508
510
  })).flat(),
509
- ...addresses.map(async (address) => makerCdps[address.toLowerCase() as EthAddress]?.map(async (cdpInfo) => {
511
+ ...addresses.map((address) => (makerCdps[address.toLowerCase() as EthAddress] || []).map(async (cdpInfo) => {
510
512
  try {
511
513
  const cdpData = await _getMakerCdpData(client, network, cdpInfo);
512
514
  if (cdpData) {
@@ -547,9 +549,9 @@ export async function getPortfolioData(provider: EthereumProvider, network: Netw
547
549
  }),
548
550
  ...crvUsdMarkets.map((market) => addresses.map(async (address) => {
549
551
  try {
550
- const accData = await _getCurveUsdUserData(client, network, address, market, crvUsdMarketsData[market.value].activeBand);
552
+ const accData = await _getCurveUsdUserData(client, network, address, market, crvUsdMarketsData[market.value].activeBand, crvUsdMarketsData[market.value].borrowRate);
551
553
  if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
552
- positions[address.toLowerCase() as EthAddress].crvUsd[market.value] = { error: '', data: { ...accData, borrowRate: crvUsdMarketsData[market.value].borrowRate } };
554
+ positions[address.toLowerCase() as EthAddress].crvUsd[market.value] = { error: '', data: accData };
553
555
  }
554
556
  } catch (error) {
555
557
  console.error(`Error fetching Curve USD account data for address ${address} on market ${market.value}:`, error);
@@ -591,4 +593,259 @@ export async function getPortfolioData(provider: EthereumProvider, network: Netw
591
593
  };
592
594
  }
593
595
 
596
+
597
+ export async function getShifterPortfolioData(provider: EthereumProvider, network: NetworkNumber, defaultProvider: EthereumProvider, addresses: EthAddress[], isSim = false): Promise<{
598
+ positions: PortfolioPositionsData;
599
+ markets: PortfolioMarketsData;
600
+ }> {
601
+ const isMainnet = network === NetworkNumber.Eth;
602
+ const isFluidSupported = [NetworkNumber.Eth, NetworkNumber.Arb, NetworkNumber.Base, NetworkNumber.Plasma].includes(network);
603
+
604
+ const morphoMarkets = Object.values(MorphoBlueMarkets(network)).filter((market) => market.chainIds.includes(network));
605
+ const compoundV3Markets = Object.values(CompoundMarkets(network)).filter((market) => market.chainIds.includes(network) && market.value !== CompoundVersions.CompoundV2);
606
+ const sparkMarkets = Object.values(SparkMarkets(network)).filter((market) => market.chainIds.includes(network));
607
+ const aaveV3Markets = [AaveVersions.AaveV3, AaveVersions.AaveV3Lido, AaveVersions.AaveV3Etherfi].map((version) => AaveMarkets(network)[version]).filter((market) => market.chainIds.includes(network));
608
+ const aaveV2Markets = [AaveVersions.AaveV2].map((version) => AaveMarkets(network)[version]).filter((market) => market.chainIds.includes(network));
609
+ const compoundV2Markets = [CompoundVersions.CompoundV2].map((version) => CompoundMarkets(network)[version]).filter((market) => market.chainIds.includes(network));
610
+ const crvUsdMarkets = Object.values(CrvUsdMarkets(network)).filter((market) => market.chainIds.includes(network));
611
+ const llamaLendMarkets = [NetworkNumber.Eth, NetworkNumber.Arb].includes(network) ? Object.values(LlamaLendMarkets(network)).filter((market) => market.chainIds.includes(network)) : [];
612
+ const liquityV2Markets = [NetworkNumber.Eth].includes(network) ? Object.values(LiquityV2Markets(network)) : [];
613
+ const aaveV4Spokes = Object.values(AaveV4Spokes(network)).filter((market) => market.chainIds.includes(network));
614
+
615
+ const args: [NetworkNumber, any?] = [network, { batch: { multicall: { batchSize: isSim ? 2_000 : 2_500_000 } } }];
616
+ const client = getViemProvider(provider, ...args);
617
+ const defaultClient = getViemProvider(defaultProvider, ...args);
618
+
619
+ const markets: PortfolioMarketsData = {
620
+ morphoMarketsData: {},
621
+ morphoMidnightMarketsData: {}, // Morpho Midnight is not fetched for the shifter; key kept so the payload shape is stable
622
+ compoundV3MarketsData: {},
623
+ sparkMarketsData: {},
624
+ aaveV3MarketsData: {},
625
+ aaveV2MarketsData: {},
626
+ compoundV2MarketsData: {},
627
+ crvUsdMarketsData: {},
628
+ llamaLendMarketsData: {},
629
+ liquityV2MarketsData: {},
630
+ aaveV4SpokesData: {},
631
+ fluidMarketsData: {},
632
+ makerMarketsData: {},
633
+ };
634
+ const makerCdps: Record<string, CdpInfo[]> = {};
635
+
636
+ const positions: PortfolioPositionsData = {};
637
+
638
+ for (const address of addresses) {
639
+ positions[address.toLowerCase() as EthAddress] = {
640
+ aaveV3: {},
641
+ aaveV4: {},
642
+ morphoBlue: {},
643
+ morphoMidnight: {}, // not fetched for the shifter (see markets above)
644
+ compoundV3: {},
645
+ spark: {},
646
+ maker: {},
647
+ aaveV2: {},
648
+ compoundV2: {},
649
+ liquity: {},
650
+ crvUsd: {},
651
+ llamaLend: {},
652
+ fluid: {
653
+ error: '',
654
+ data: {},
655
+ },
656
+ };
657
+ }
658
+
659
+ await Promise.allSettled([
660
+ // === MARKET DATA (needs to be fetched first) ===
661
+ ...morphoMarkets.map(async (market) => {
662
+ markets.morphoMarketsData[market.value] = await _getMorphoBluePortfolioMarketData(client, network, market);
663
+ }),
664
+ ...compoundV3Markets.map(async (market) => {
665
+ markets.compoundV3MarketsData[market.value] = await _getCompoundV3MarketsData(client, network, market, defaultClient);
666
+ }),
667
+ ...sparkMarkets.map(async (market) => {
668
+ markets.sparkMarketsData[market.value] = await _getSparkMarketsData(client, network, market);
669
+ }),
670
+ ...aaveV3Markets.map(async (market) => {
671
+ markets.aaveV3MarketsData[market.value] = await _getAaveV3MarketData(client, network, market);
672
+ }),
673
+ ...aaveV4Spokes.map(async (spoke) => {
674
+ markets.aaveV4SpokesData[spoke.value] = await _getAaveV4SpokeData(client, network, spoke);
675
+ }),
676
+ ...aaveV2Markets.map(async (market) => {
677
+ markets.aaveV2MarketsData[market.value] = await _getAaveV2MarketsData(client, network, market);
678
+ }),
679
+ ...compoundV2Markets.map(async (market) => {
680
+ markets.compoundV2MarketsData[market.value] = await _getCompoundV2MarketsData(client, network);
681
+ }),
682
+ ...crvUsdMarkets.map(async (market) => {
683
+ markets.crvUsdMarketsData[market.value] = await _getCurveUsdGlobalData(client, network, market);
684
+ }),
685
+ ...llamaLendMarkets.map(async (market) => {
686
+ markets.llamaLendMarketsData[market.value] = await _getLlamaLendGlobalData(client, network, market);
687
+ }),
688
+ ...liquityV2Markets.map(async (market) => {
689
+ markets.liquityV2MarketsData[market.value] = await _getLiquityV2MarketData(client, network, market);
690
+ }),
691
+ (async () => {
692
+ if (!isFluidSupported) return;
693
+ try {
694
+ markets.fluidMarketsData = await _getAllFluidMarketDataPortfolio(client, network);
695
+ } catch (error) {
696
+ console.error('Error fetching Fluid markets data:', error);
697
+ }
698
+ })(),
699
+ (async () => {
700
+ if (!isMainnet) return; // Maker CDPs are only available on mainnet
701
+ try {
702
+ markets.makerMarketsData = await _getMakerIlksData(client, network, MakerActiveIlks);
703
+ } catch (error) {
704
+ console.error('Error fetching Maker ilks data:', error);
705
+ }
706
+ })(),
707
+
708
+ // === INDEPENDENT USER DATA (doesn't depend on market data) ===
709
+ ...addresses.map(async (address) => {
710
+ if (!isMainnet) return; // Maker CDPs are only available on mainnet
711
+ const makerCdp = await _getUserCdps(client, network, address);
712
+ makerCdps[address.toLowerCase() as EthAddress] = makerCdp;
713
+ }),
714
+ ...addresses.map(async (address) => {
715
+ try {
716
+ if (!isFluidSupported) return; // Fluid is not available on Optimism
717
+ const userPositions = (await _getUserPositionsPortfolio(client, network, address));
718
+ for (const position of userPositions) {
719
+ if (position.userData && new Dec(position.userData.suppliedUsd).gt(0)) {
720
+ positions[address.toLowerCase() as EthAddress].fluid.data[position.userData.nftId] = position.userData;
721
+ }
722
+ }
723
+ } catch (error) {
724
+ console.error(`Error fetching Fluid positions for address ${address}:`, error);
725
+ positions[address.toLowerCase() as EthAddress].fluid = {
726
+ error: `Error fetching Fluid positions for address ${address}`,
727
+ data: {},
728
+ };
729
+ }
730
+ }),
731
+ ]);
732
+
733
+ await Promise.all([
734
+ ...aaveV3Markets.map((market) => addresses.map(async (address) => {
735
+ try {
736
+ const accData = await _getAaveV3AccountData(client, network, address, { selectedMarket: market, ...markets.aaveV3MarketsData[market.value] });
737
+ // Always store the position (even with no supply) so the eMode category is available to the shifter
738
+ positions[address.toLowerCase() as EthAddress].aaveV3[market.value] = { error: '', data: accData };
739
+ } catch (error) {
740
+ console.error(`Error fetching AaveV3 account data for address ${address} on market ${market.value}:`, error);
741
+ positions[address.toLowerCase() as EthAddress].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(async (address) => {
745
+ try {
746
+ const accData = await _getAaveV4AccountData(client, network, markets.aaveV4SpokesData[spoke.value], address);
747
+ if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].aaveV4[spoke.value] = { error: '', data: accData };
748
+ } catch (error) {
749
+ console.error(`Error fetching AaveV4 account data for address ${address} on spoke ${spoke.value}:`, error);
750
+ positions[address.toLowerCase() as EthAddress].aaveV4[spoke.value] = { error: `Error fetching AaveV4 account data for address ${address} on spoke ${spoke.value}`, data: null };
751
+ }
752
+ })).flat(),
753
+ ...morphoMarkets.map((market) => addresses.map(async (address) => {
754
+ try {
755
+ const accData = await _getMorphoBlueAccountData(client, network, address, market, markets.morphoMarketsData[market.value]);
756
+ if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = { error: '', data: accData };
757
+ } catch (error) {
758
+ console.error(`Error fetching MorphoBlue account data for address ${address} on market ${market.value}:`, error);
759
+ positions[address.toLowerCase() as EthAddress].morphoBlue[market.value] = { error: `Error fetching MorphoBlue account data for address ${address} on market ${market.value}`, data: null };
760
+ }
761
+ })).flat(),
762
+ ...compoundV3Markets.map((market) => addresses.map(async (address) => {
763
+ try {
764
+ const accData = await _getCompoundV3AccountData(client, network, address, ZERO_ADDRESS, { selectedMarket: market, assetsData: markets.compoundV3MarketsData[market.value].assetsData });
765
+ if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].compoundV3[market.value] = { error: '', data: accData };
766
+ } catch (error) {
767
+ console.error(`Error fetching CompoundV3 account data for address ${address} on market ${market.value}:`, error);
768
+ positions[address.toLowerCase() as EthAddress].compoundV3[market.value] = { error: `Error fetching CompoundV3 account data for address ${address} on market ${market.value}`, data: null };
769
+ }
770
+ })).flat(),
771
+ ...sparkMarkets.map((market) => addresses.map(async (address) => {
772
+ try {
773
+ const accData = await _getSparkAccountData(client, network, address, { selectedMarket: market, assetsData: markets.sparkMarketsData[market.value].assetsData, eModeCategoriesData: markets.sparkMarketsData[market.value].eModeCategoriesData });
774
+ // Always store the position (even with no supply) so the eMode category is available to the shifter
775
+ positions[address.toLowerCase() as EthAddress].spark[market.value] = { error: '', data: accData };
776
+ } catch (error) {
777
+ console.error(`Error fetching Spark account data for address ${address} on market ${market.value}:`, error);
778
+ positions[address.toLowerCase() as EthAddress].spark[market.value] = { error: `Error fetching Spark account data for address ${address} on market ${market.value}`, data: null };
779
+ }
780
+ })).flat(),
781
+ ...addresses.map((address) => (makerCdps[address.toLowerCase() as EthAddress] || []).map(async (cdpInfo) => {
782
+ try {
783
+ // reuse ilk data fetched for the markets payload; ilks outside the active set are fetched on demand
784
+ const cdpData = await _getMakerCdpData(client, network, cdpInfo, markets.makerMarketsData[cdpInfo.ilkLabel]);
785
+ if (cdpData) {
786
+ positions[address.toLowerCase() as EthAddress].maker[cdpInfo.id] = { error: '', data: cdpData };
787
+ }
788
+ } catch (error) {
789
+ console.error(`Error fetching Maker CDP data for address ${address} with ID ${cdpInfo.id}:`, error);
790
+ positions[address.toLowerCase() as EthAddress].maker[cdpInfo.id] = { error: `Error fetching Maker CDP data for address ${address} with ID ${cdpInfo.id}`, data: null };
791
+ }
792
+ })).flat(),
793
+ ...aaveV2Markets.map((market) => addresses.map(async (address) => {
794
+ try {
795
+ const accData = await _getAaveV2AccountData(client, network, address, markets.aaveV2MarketsData[market.value].assetsData, market);
796
+ if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].aaveV2[market.value] = { error: '', data: accData };
797
+ } catch (error) {
798
+ console.error(`Error fetching AaveV2 account data for address ${address}:`, error);
799
+ positions[address.toLowerCase() as EthAddress].aaveV2[market.value] = { error: `Error fetching AaveV2 account data for address ${address}`, data: null };
800
+ }
801
+ })).flat(),
802
+ ...compoundV2Markets.map((market) => addresses.map(async (address) => {
803
+ try {
804
+ const accData = await _getCompoundV2AccountData(client, network, address, markets.compoundV2MarketsData[market.value].assetsData);
805
+ if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].compoundV2[market.value] = { error: '', data: accData };
806
+ } catch (error) {
807
+ console.error(`Error fetching CompoundV2 account data for address ${address}:`, error);
808
+ positions[address.toLowerCase() as EthAddress].compoundV2[market.value] = { error: `Error fetching CompoundV2 account data for address ${address}`, data: null };
809
+ }
810
+ })).flat(),
811
+ ...addresses.map(async (address) => {
812
+ try {
813
+ if (!isMainnet) return; // Liquity trove info is only available on mainnet
814
+ const troveInfo = await _getLiquityTroveInfo(client, network, address);
815
+ if (new Dec(troveInfo.collateral).gt(0)) positions[address.toLowerCase() as EthAddress].liquity = { error: '', data: troveInfo };
816
+ } catch (error) {
817
+ console.error(`Error fetching Liquity trove info for address ${address}:`, error);
818
+ positions[address.toLowerCase() as EthAddress].liquity = { error: `Error fetching Liquity trove info for address ${address}`, data: null };
819
+ }
820
+ }),
821
+ ...crvUsdMarkets.map((market) => addresses.map(async (address) => {
822
+ try {
823
+ const accData = await _getCurveUsdUserData(client, network, address, market, markets.crvUsdMarketsData[market.value].activeBand, markets.crvUsdMarketsData[market.value].borrowRate);
824
+ if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
825
+ positions[address.toLowerCase() as EthAddress].crvUsd[market.value] = { error: '', data: accData };
826
+ }
827
+ } catch (error) {
828
+ console.error(`Error fetching Curve USD account data for address ${address} on market ${market.value}:`, error);
829
+ positions[address.toLowerCase() as EthAddress].crvUsd[market.value] = { error: `Error fetching Curve USD account data for address ${address} on market ${market.value}`, data: null };
830
+ }
831
+ })).flat(),
832
+ ...llamaLendMarkets.map((market) => addresses.map(async (address) => {
833
+ try {
834
+ const accData = await _getLlamaLendUserData(client, network, address, market, markets.llamaLendMarketsData[market.value]);
835
+ if (new Dec(accData.suppliedUsd).gt(0) || new Dec(accData.borrowedUsd).gt(0)) {
836
+ positions[address.toLowerCase() as EthAddress].llamaLend[market.value] = { error: '', data: { ...accData, borrowRate: markets.llamaLendMarketsData[market.value].borrowRate } };
837
+ }
838
+ } catch (error) {
839
+ console.error(`Error fetching LlamaLend account data for address ${address} on market ${market.value}:`, error);
840
+ positions[address.toLowerCase() as EthAddress].llamaLend[market.value] = { error: `Error fetching LlamaLend account data for address ${address} on market ${market.value}`, data: null };
841
+ }
842
+ })).flat(),
843
+ ]);
844
+
845
+ return {
846
+ positions,
847
+ markets,
848
+ };
849
+ }
850
+
594
851
  export * from './discovery';
@@ -137,6 +137,34 @@ export const calculateInterestEarned = (principal: string, interest: string, typ
137
137
  return (+principal * (((1 + (+interest / 100) / BLOCKS_IN_A_YEAR)) ** (BLOCKS_IN_A_YEAR * interval))) - +principal; // eslint-disable-line
138
138
  };
139
139
 
140
+ /**
141
+ * Net APY for positions that have a single known borrow rate (and optionally a single supply rate)
142
+ * instead of per-asset `assetsData` to feed into `calculateNetApy` (Maker, crvUSD, Liquity v1).
143
+ * All rates are yearly percentages that are already compounded. `incentives` are extra yields
144
+ * (e.g. the native staking yield of the collateral) applied to their own USD base and summed into `incentiveUsd`.
145
+ */
146
+ export const calculateNetApyFromRates = ({
147
+ suppliedUsd, borrowedUsd, supplyRate = '0', borrowRate = '0', incentives = [],
148
+ }: {
149
+ suppliedUsd: string,
150
+ borrowedUsd: string,
151
+ supplyRate?: string,
152
+ borrowRate?: string,
153
+ incentives?: { apy: string, amountUsd: string }[],
154
+ }) => {
155
+ const supplyInterest = new Dec(calculateInterestEarned(suppliedUsd, supplyRate, 'year', true));
156
+ const borrowInterest = new Dec(calculateInterestEarned(borrowedUsd, borrowRate, 'year', true));
157
+ const incentiveUsd = incentives
158
+ .reduce((acc, { apy, amountUsd }) => acc.add(calculateInterestEarned(amountUsd, apy, 'year', true)), new Dec(0))
159
+ .toString();
160
+
161
+ const totalInterestUsd = supplyInterest.sub(borrowInterest).add(incentiveUsd).toString();
162
+ const balance = new Dec(suppliedUsd).sub(borrowedUsd);
163
+ const netApy = balance.isZero() ? '0' : new Dec(totalInterestUsd).div(balance).times(100).toString();
164
+
165
+ return { netApy, totalInterestUsd, incentiveUsd };
166
+ };
167
+
140
168
  export const calculateNetApy = ({
141
169
  usedAssets, assetsData, optionalData,
142
170
  }: { usedAssets: MMUsedAssets, assetsData: MMAssetsData, optionalData?: any }) => {
package/src/types/aave.ts CHANGED
@@ -144,6 +144,8 @@ export interface AavePositionData extends MMPositionData {
144
144
  collRatio: string,
145
145
  // Safety ratio as evaluated by automation bots (LTV-0 collateral credited at LLTV - 5%). Aave v3 only.
146
146
  safetyRatioWithLtvZeroFallback?: string,
147
+ // Normalised safety ratio (100 = liquidation on every protocol). Aave v3: the LTV-0 fallback ratio, Aave v2: `ratio`.
148
+ safetyRatio?: string,
147
149
  suppliedUsd: string,
148
150
  borrowedUsd: string,
149
151
  borrowLimitUsd: string,
@@ -178,6 +180,7 @@ export interface AaveV3AggregatedPositionData {
178
180
  collRatio: string,
179
181
  borrowLimitWithLtvZeroFallbackUsd: string,
180
182
  safetyRatioWithLtvZeroFallback: string,
183
+ safetyRatio: string,
181
184
  netApy: string,
182
185
  incentiveUsd: string,
183
186
  totalInterestUsd: string,
@@ -183,6 +183,7 @@ export interface AaveV4AggregatedPositionData {
183
183
  leftToBorrowUsd: string,
184
184
  ratio: string,
185
185
  collRatio: string,
186
+ safetyRatio: string,
186
187
  liqRatio: string,
187
188
  liqPercent: string,
188
189
  leveragedType: LeverageType,
@@ -112,6 +112,7 @@ export interface CompoundAggregatedPositionData {
112
112
  leftToBorrowUsd: string,
113
113
  ratio: string,
114
114
  collRatio: string,
115
+ safetyRatio: string,
115
116
  netApy: string,
116
117
  incentiveUsd: string,
117
118
  totalInterestUsd: string,
@@ -132,6 +133,7 @@ export interface CompoundAggregatedPositionData {
132
133
  export interface CompoundPositionData extends MMPositionData {
133
134
  ratio: string,
134
135
  minRatio: string,
136
+ safetyRatio?: string,
135
137
  suppliedUsd: string,
136
138
  borrowedUsd: string,
137
139
  borrowLimitUsd: string,
@@ -80,6 +80,10 @@ export interface CrvUSDAggregatedPositionData {
80
80
  collFactor: string,
81
81
  leveragedType: LeverageType,
82
82
  exposure: string,
83
+ // Yearly net return (%) on the position's equity: market borrow rate on crvUSD debt vs native yield of staked collateral.
84
+ netApy: string,
85
+ totalInterestUsd: string,
86
+ incentiveUsd: string,
83
87
  leveragedAsset?: string,
84
88
  liquidationPrice?: string,
85
89
  }
@@ -123,4 +127,7 @@ export interface CrvUSDUserData {
123
127
  collateralPrice: string,
124
128
  collRatio: string,
125
129
  exposure: string,
130
+ netApy: string,
131
+ totalInterestUsd: string,
132
+ incentiveUsd: string,
126
133
  }
@@ -344,6 +344,7 @@ export interface FluidAggregatedVaultData {
344
344
  merklBorrowIncentives: IncentiveData[],
345
345
  ratio: string,
346
346
  collRatio: string,
347
+ safetyRatio: string,
347
348
  minRatio: string,
348
349
  totalInterestUsd: string,
349
350
  leveragedType?: LeverageType,
@@ -26,7 +26,13 @@ export interface LiquityTroveInfo {
26
26
  totalETH: string,
27
27
  totalLUSD: string,
28
28
  minCollateralRatio: number,
29
+ // Collateral ratio rebased so 100 sits on `minCollateralRatio` (normalised safety ratio).
30
+ safetyRatio: string,
29
31
  priceForRecovery: string,
30
32
  debtInFront: string,
31
33
  exposure: string,
34
+ // Always '0' for Liquity v1: LUSD debt is interest free (one-off borrowing fee only) and ETH collateral earns no yield.
35
+ netApy: string,
36
+ totalInterestUsd: string,
37
+ incentiveUsd: string,
32
38
  }
@@ -104,6 +104,7 @@ export interface LiquityV2AggregatedTroveData {
104
104
  liquidationPrice: string,
105
105
  ratio: string,
106
106
  collRatio: string,
107
+ safetyRatio: string,
107
108
  exposure: string,
108
109
  }
109
110
 
@@ -112,6 +113,7 @@ export interface LiquityV2TroveData {
112
113
  troveId: string,
113
114
  ratio: string,
114
115
  collRatio: string,
116
+ safetyRatio?: string,
115
117
  liqRatio: string,
116
118
  borrowLimitRatio: string,
117
119
  interestRate: string,
@@ -47,6 +47,8 @@ export interface CdpData {
47
47
  debtAssetMarketPrice: string,
48
48
  liquidationPrice: string,
49
49
  ratio: string,
50
+ // Collateral ratio rebased so 100 sits on `liqPercent` (normalised safety ratio).
51
+ safetyRatio: string,
50
52
  liqRatio: string,
51
53
  liqPercent: number,
52
54
  assetPrice: string,
@@ -62,4 +64,8 @@ export interface CdpData {
62
64
  liquidationFee: string,
63
65
  lastUpdated: number,
64
66
  exposure: string,
67
+ // Yearly net return (%) on the vault's equity: stability fee on DAI debt vs native yield of staked collateral.
68
+ netApy: string,
69
+ totalInterestUsd: string,
70
+ incentiveUsd: string,
65
71
  }
@@ -206,6 +206,7 @@ export interface MorphoBlueAggregatedPositionData {
206
206
  totalInterestUsd: string,
207
207
  ltv: string,
208
208
  ratio: string,
209
+ safetyRatio: string, // borrowLimitUsd / borrowedUsd as a percentage (100 = liquidation)
209
210
  leveragedType: LeverageType,
210
211
  leveragedAsset?: string,
211
212
  currentVolatilePairRatio?: string,
@@ -229,6 +230,7 @@ export interface MorphoBluePositionData {
229
230
  totalInterestUsd: string,
230
231
  ltv: string,
231
232
  ratio: string,
233
+ safetyRatio?: string,
232
234
  leveragedType: LeverageType,
233
235
  leveragedAsset?: string,
234
236
  currentVolatilePairRatio?: string,
@@ -191,6 +191,7 @@ export interface MorphoMidnightAggregatedPositionData {
191
191
  ltv: string,
192
192
  ratio: string, // health ratio as a percentage (from MidnightView.ratio, 1e18-scaled)
193
193
  healthRatio: string, // liquidationLimitUsd / borrowedUsd
194
+ safetyRatio: string, // borrowLimitUsd / borrowedUsd as a percentage (100 = liquidation)
194
195
  leveragedType: LeverageType,
195
196
  leveragedAsset?: string,
196
197
  currentVolatilePairRatio?: string,
@@ -1,16 +1,20 @@
1
- import { AaveV2PositionData, AaveV3PositionData, AaveVersions } from './aave';
2
- import { AaveV4AccountData, AaveV4SpokesType } from './aaveV4';
1
+ import {
2
+ AaveV2MarketData, AaveV2PositionData, AaveV3MarketData, AaveV3PositionData, AaveVersions,
3
+ } from './aave';
4
+ import { AaveV4AccountData, AaveV4SpokeData, AaveV4SpokesType } from './aaveV4';
3
5
  import { EthAddress } from './common';
4
- import { CompoundV2PositionData, CompoundV3PositionData, CompoundVersions } from './compound';
5
- import { CrvUSDUserData, CrvUSDVersions } from './curveUsd';
6
- import { FluidVaultData } from './fluid';
6
+ import {
7
+ CompoundV2MarketsData, CompoundV2PositionData, CompoundV3MarketsData, CompoundV3PositionData, CompoundVersions,
8
+ } from './compound';
9
+ import { CrvUSDGlobalMarketData, CrvUSDUserData, CrvUSDVersions } from './curveUsd';
10
+ import { FluidMarketData, FluidVaultData } from './fluid';
7
11
  import { LiquityTroveInfo } from './liquity';
8
- import { LiquityV2TroveData, LiquityV2Versions } from './liquityV2';
9
- import { LlamaLendUserData, LlamaLendVersionsType } from './llamaLend';
10
- import { CdpData } from './maker';
11
- import { MorphoBluePositionData, MorphoBlueVersions } from './morphoBlue';
12
- import { MorphoMidnightPositionData, MorphoMidnightVersions } from './morphoMidnight';
13
- import { SparkPositionData, SparkVersions } from './spark';
12
+ import { LiquityV2MarketData, LiquityV2TroveData, LiquityV2Versions } from './liquityV2';
13
+ import { LlamaLendGlobalMarketData, LlamaLendUserData, LlamaLendVersionsType } from './llamaLend';
14
+ import { CdpData, IlkInfo } from './maker';
15
+ import { MorphoBlueMarketInfo, MorphoBluePositionData, MorphoBlueVersions } from './morphoBlue';
16
+ import { MorphoMidnightMarketInfo, MorphoMidnightPositionData, MorphoMidnightVersions } from './morphoMidnight';
17
+ import { SparkMarketsData, SparkPositionData, SparkVersions } from './spark';
14
18
 
15
19
  export interface PortfolioProtocolData<T> {
16
20
  error: string,
@@ -62,4 +66,19 @@ export interface PortfolioPositionsDataForAddress {
62
66
 
63
67
  export interface PortfolioPositionsData {
64
68
  [key: EthAddress]: PortfolioPositionsDataForAddress;
65
- }
69
+ }
70
+ export interface PortfolioMarketsData {
71
+ morphoMarketsData: Record<string, MorphoBlueMarketInfo>;
72
+ morphoMidnightMarketsData: Record<string, MorphoMidnightMarketInfo>;
73
+ compoundV3MarketsData: Record<string, CompoundV3MarketsData>;
74
+ sparkMarketsData: Record<string, SparkMarketsData>;
75
+ aaveV3MarketsData: Record<string, AaveV3MarketData>;
76
+ aaveV2MarketsData: Record<string, AaveV2MarketData>;
77
+ compoundV2MarketsData: Record<string, CompoundV2MarketsData>;
78
+ crvUsdMarketsData: Record<string, CrvUSDGlobalMarketData>;
79
+ llamaLendMarketsData: Record<string, LlamaLendGlobalMarketData>;
80
+ liquityV2MarketsData: Record<string, LiquityV2MarketData>;
81
+ aaveV4SpokesData: Record<string, AaveV4SpokeData>;
82
+ fluidMarketsData: Record<string, FluidMarketData>;
83
+ makerMarketsData: Record<string, IlkInfo>;
84
+ }
@@ -99,6 +99,7 @@ export interface SparkAggregatedPositionData {
99
99
  leftToBorrowUsd: string,
100
100
  ratio: string,
101
101
  collRatio: string,
102
+ safetyRatio: string,
102
103
  netApy: string,
103
104
  incentiveUsd: string,
104
105
  totalInterestUsd: string,
@@ -119,6 +120,7 @@ export interface SparkPositionData extends MMPositionData {
119
120
  ratio: string,
120
121
  minRatio: string,
121
122
  collRatio: string,
123
+ safetyRatio?: string,
122
124
  suppliedUsd: string,
123
125
  borrowedUsd: string,
124
126
  borrowLimitUsd: string,