@defisaver/positions-sdk 2.1.1-dev.0 → 2.1.2

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 (118) hide show
  1. package/CLAUDE.md +32 -0
  2. package/cjs/aaveV2/index.js +1 -0
  3. package/cjs/aaveV3/index.d.ts +12 -0
  4. package/cjs/aaveV3/index.js +93 -1
  5. package/cjs/claiming/aaveV3.d.ts +5 -0
  6. package/cjs/claiming/aaveV3.js +139 -0
  7. package/cjs/claiming/compV3.d.ts +15 -0
  8. package/cjs/claiming/compV3.js +34 -0
  9. package/cjs/claiming/index.d.ts +6 -0
  10. package/cjs/claiming/index.js +46 -0
  11. package/cjs/claiming/king.d.ts +4 -0
  12. package/cjs/claiming/king.js +72 -0
  13. package/cjs/claiming/morphoBlue.d.ts +5 -0
  14. package/cjs/claiming/morphoBlue.js +113 -0
  15. package/cjs/claiming/spark.d.ts +6 -0
  16. package/cjs/claiming/spark.js +188 -0
  17. package/cjs/config/contracts.d.ts +2681 -0
  18. package/cjs/config/contracts.js +103 -2
  19. package/cjs/constants/index.d.ts +4 -0
  20. package/cjs/constants/index.js +6 -2
  21. package/cjs/contracts.d.ts +3027 -23
  22. package/cjs/contracts.js +10 -1
  23. package/cjs/helpers/aaveHelpers/index.d.ts +4 -0
  24. package/cjs/helpers/aaveHelpers/index.js +17 -1
  25. package/cjs/helpers/morphoBlueHelpers/index.d.ts +1 -0
  26. package/cjs/helpers/morphoBlueHelpers/index.js +7 -1
  27. package/cjs/index.d.ts +2 -1
  28. package/cjs/index.js +3 -1
  29. package/cjs/liquity/index.d.ts +11 -0
  30. package/cjs/liquity/index.js +39 -1
  31. package/cjs/liquityV2/index.d.ts +35 -2
  32. package/cjs/liquityV2/index.js +150 -52
  33. package/cjs/morphoBlue/index.d.ts +5 -0
  34. package/cjs/morphoBlue/index.js +35 -0
  35. package/cjs/portfolio/index.d.ts +6 -1
  36. package/cjs/portfolio/index.js +293 -10
  37. package/cjs/services/utils.d.ts +5 -0
  38. package/cjs/services/utils.js +33 -1
  39. package/cjs/services/viem.d.ts +12 -12
  40. package/cjs/spark/index.js +1 -0
  41. package/cjs/staking/staking.js +2 -1
  42. package/cjs/types/claiming.d.ts +93 -0
  43. package/cjs/types/claiming.js +27 -0
  44. package/cjs/umbrella/index.d.ts +5 -0
  45. package/cjs/umbrella/index.js +50 -0
  46. package/cjs/umbrella/umbrellaUtils.d.ts +22 -0
  47. package/cjs/umbrella/umbrellaUtils.js +34 -0
  48. package/esm/aaveV2/index.js +1 -0
  49. package/esm/aaveV3/index.d.ts +12 -0
  50. package/esm/aaveV3/index.js +91 -1
  51. package/esm/claiming/aaveV3.d.ts +5 -0
  52. package/esm/claiming/aaveV3.js +132 -0
  53. package/esm/claiming/compV3.d.ts +15 -0
  54. package/esm/claiming/compV3.js +30 -0
  55. package/esm/claiming/index.d.ts +6 -0
  56. package/esm/claiming/index.js +6 -0
  57. package/esm/claiming/king.d.ts +4 -0
  58. package/esm/claiming/king.js +64 -0
  59. package/esm/claiming/morphoBlue.d.ts +5 -0
  60. package/esm/claiming/morphoBlue.js +105 -0
  61. package/esm/claiming/spark.d.ts +6 -0
  62. package/esm/claiming/spark.js +179 -0
  63. package/esm/config/contracts.d.ts +2681 -0
  64. package/esm/config/contracts.js +102 -1
  65. package/esm/constants/index.d.ts +4 -0
  66. package/esm/constants/index.js +5 -1
  67. package/esm/contracts.d.ts +3027 -23
  68. package/esm/contracts.js +9 -0
  69. package/esm/helpers/aaveHelpers/index.d.ts +4 -0
  70. package/esm/helpers/aaveHelpers/index.js +15 -0
  71. package/esm/helpers/morphoBlueHelpers/index.d.ts +1 -0
  72. package/esm/helpers/morphoBlueHelpers/index.js +6 -1
  73. package/esm/index.d.ts +2 -1
  74. package/esm/index.js +2 -1
  75. package/esm/liquity/index.d.ts +11 -0
  76. package/esm/liquity/index.js +38 -1
  77. package/esm/liquityV2/index.d.ts +35 -2
  78. package/esm/liquityV2/index.js +149 -51
  79. package/esm/morphoBlue/index.d.ts +5 -0
  80. package/esm/morphoBlue/index.js +35 -1
  81. package/esm/portfolio/index.d.ts +6 -1
  82. package/esm/portfolio/index.js +298 -15
  83. package/esm/services/utils.d.ts +5 -0
  84. package/esm/services/utils.js +31 -0
  85. package/esm/services/viem.d.ts +12 -12
  86. package/esm/spark/index.js +1 -0
  87. package/esm/staking/staking.js +2 -1
  88. package/esm/types/claiming.d.ts +93 -0
  89. package/esm/types/claiming.js +24 -0
  90. package/esm/umbrella/index.d.ts +5 -0
  91. package/esm/umbrella/index.js +46 -0
  92. package/esm/umbrella/umbrellaUtils.d.ts +22 -0
  93. package/esm/umbrella/umbrellaUtils.js +28 -0
  94. package/package.json +1 -1
  95. package/src/aaveV2/index.ts +2 -1
  96. package/src/aaveV3/index.ts +100 -2
  97. package/src/claiming/aaveV3.ts +155 -0
  98. package/src/claiming/compV3.ts +23 -0
  99. package/src/claiming/index.ts +13 -0
  100. package/src/claiming/king.ts +66 -0
  101. package/src/claiming/morphoBlue.ts +119 -0
  102. package/src/claiming/spark.ts +226 -0
  103. package/src/config/contracts.ts +104 -4
  104. package/src/constants/index.ts +5 -1
  105. package/src/contracts.ts +14 -1
  106. package/src/helpers/aaveHelpers/index.ts +15 -0
  107. package/src/helpers/morphoBlueHelpers/index.ts +7 -2
  108. package/src/index.ts +2 -0
  109. package/src/liquity/index.ts +57 -2
  110. package/src/liquityV2/index.ts +250 -56
  111. package/src/morphoBlue/index.ts +40 -2
  112. package/src/portfolio/index.ts +300 -15
  113. package/src/services/utils.ts +37 -1
  114. package/src/spark/index.ts +2 -1
  115. package/src/staking/staking.ts +2 -1
  116. package/src/types/claiming.ts +109 -0
  117. package/src/umbrella/index.ts +70 -0
  118. package/src/umbrella/umbrellaUtils.ts +30 -0
package/esm/contracts.js CHANGED
@@ -80,3 +80,12 @@ export const LiquityActivePoolContractViem = createViemContractFromConfigFunc('L
80
80
  export const LiquityV2ViewContractViem = createViemContractFromConfigFunc('LiquityV2View');
81
81
  export const LiquityV2LegacyViewContractViem = createViemContractFromConfigFunc('LiquityV2LegacyView');
82
82
  export const FluidViewContractViem = createViemContractFromConfigFunc('FluidView');
83
+ export const AaveIncentivesControllerViem = createViemContractFromConfigFunc('AaveIncentivesController');
84
+ export const AaveUmbrellaViewViem = createViemContractFromConfigFunc('AaveUmbrellaView');
85
+ export const LiquityLQTYStakingViem = createViemContractFromConfigFunc('LiquityLQTYStaking');
86
+ export const LiquityStabilityPoolViem = createViemContractFromConfigFunc('LiquityStabilityPool');
87
+ export const UUPSViem = createViemContractFromConfigFunc('UUPS');
88
+ export const SparkRewardsControllerViem = createViemContractFromConfigFunc('SparkRewardsController');
89
+ export const AaveRewardsControllerViem = createViemContractFromConfigFunc('AaveRewardsController');
90
+ export const LiquityV2sBoldVaultViem = createViemContractFromConfigFunc('LiquityV2sBoldVault');
91
+ export const StkAAVEViem = createViemContractFromConfigFunc('StkAAVE');
@@ -41,3 +41,7 @@ export declare const getApyAfterValuesEstimation: (selectedMarket: AaveMarketInf
41
41
  borrowRate: string;
42
42
  };
43
43
  }>;
44
+ /**
45
+ * won't cover all cases
46
+ */
47
+ export declare const getAaveUnderlyingSymbol: (_symbol?: string) => any;
@@ -140,3 +140,18 @@ const getApyAfterValuesEstimationInner = (selectedMarket, actions, client, netwo
140
140
  return rates;
141
141
  });
142
142
  export const getApyAfterValuesEstimation = (selectedMarket, actions, provider, network) => __awaiter(void 0, void 0, void 0, function* () { return getApyAfterValuesEstimationInner(selectedMarket, actions, getViemProvider(provider, network), network); });
143
+ /**
144
+ * won't cover all cases
145
+ */
146
+ export const getAaveUnderlyingSymbol = (_symbol = '') => {
147
+ let symbol = _symbol
148
+ .replace(/^aEthLido/, '')
149
+ .replace(/^aEthEtherFi/, '')
150
+ .replace(/^aEth/, '')
151
+ .replace(/^aArb/, '')
152
+ .replace(/^aOpt/, '')
153
+ .replace(/^aBas/, '');
154
+ if (symbol.startsWith('a'))
155
+ symbol = symbol.slice(1);
156
+ return wethToEth(symbol);
157
+ };
@@ -56,3 +56,4 @@ export declare const getRewardsForMarket: (marketId: string, network?: NetworkNu
56
56
  supplyApy: string;
57
57
  borrowApy: string;
58
58
  }>;
59
+ export declare const getMorphoUnderlyingSymbol: (_symbol: string) => any;
@@ -14,7 +14,7 @@ import { calculateNetApy } from '../../staking';
14
14
  import { NetworkNumber, } from '../../types/common';
15
15
  import { borrowOperations, SECONDS_PER_YEAR, WAD } from '../../constants';
16
16
  import { MorphoBlueViewContractViem } from '../../contracts';
17
- import { compareAddresses, DEFAULT_TIMEOUT } from '../../services/utils';
17
+ import { compareAddresses, DEFAULT_TIMEOUT, wethToEth } from '../../services/utils';
18
18
  import { getViemProvider } from '../../services/viem';
19
19
  export const getMorphoBlueAggregatedPositionData = ({ usedAssets, assetsData, marketInfo }) => {
20
20
  var _a, _b, _c, _d, _e, _f;
@@ -348,3 +348,8 @@ export const getRewardsForMarket = (marketId_1, ...args_1) => __awaiter(void 0,
348
348
  const borrowAprPercent = new Dec(borrowApr).mul(100).toString();
349
349
  return { supplyApy: aprToApy(supplyAprPercent), borrowApy: aprToApy(borrowAprPercent) };
350
350
  });
351
+ export const getMorphoUnderlyingSymbol = (_symbol) => {
352
+ if (_symbol === 'MORPHO Legacy')
353
+ return 'MORPHO';
354
+ return wethToEth(_symbol);
355
+ };
package/esm/index.d.ts CHANGED
@@ -18,5 +18,6 @@ import * as morphoBlue from './morphoBlue';
18
18
  import * as llamaLend from './llamaLend';
19
19
  import * as eulerV2 from './eulerV2';
20
20
  import * as portfolio from './portfolio';
21
+ import * as claiming from './claiming';
21
22
  export * from './types';
22
- export { aaveV2, aaveV3, compoundV2, compoundV3, spark, curveUsd, liquity, liquityV2, maker, exchange, staking, moneymarket, markets, helpers, morphoBlue, llamaLend, eulerV2, fluid, portfolio, };
23
+ export { aaveV2, aaveV3, compoundV2, compoundV3, spark, curveUsd, liquity, liquityV2, maker, exchange, staking, moneymarket, markets, helpers, morphoBlue, llamaLend, eulerV2, fluid, portfolio, claiming, };
package/esm/index.js CHANGED
@@ -18,5 +18,6 @@ import * as morphoBlue from './morphoBlue';
18
18
  import * as llamaLend from './llamaLend';
19
19
  import * as eulerV2 from './eulerV2';
20
20
  import * as portfolio from './portfolio';
21
+ import * as claiming from './claiming';
21
22
  export * from './types';
22
- export { aaveV2, aaveV3, compoundV2, compoundV3, spark, curveUsd, liquity, liquityV2, maker, exchange, staking, moneymarket, markets, helpers, morphoBlue, llamaLend, eulerV2, fluid, portfolio, };
23
+ export { aaveV2, aaveV3, compoundV2, compoundV3, spark, curveUsd, liquity, liquityV2, maker, exchange, staking, moneymarket, markets, helpers, morphoBlue, llamaLend, eulerV2, fluid, portfolio, claiming, };
@@ -8,3 +8,14 @@ export declare const getLiquityAccountBalances: (provider: EthereumProvider, net
8
8
  export declare const getDebtInFront: (provider: EthereumProvider, address: EthAddress) => Promise<string>;
9
9
  export declare const _getLiquityTroveInfo: (provider: Client, network: NetworkNumber, address: EthAddress) => Promise<LiquityTroveInfo>;
10
10
  export declare const getLiquityTroveInfo: (provider: EthereumProvider, network: NetworkNumber, address: EthAddress) => Promise<LiquityTroveInfo>;
11
+ export declare const getLiquityStakingData: (provider: Client, network: NetworkNumber, address: EthAddress) => Promise<{
12
+ totalLUSDDeposited: string;
13
+ totalLQTYStaked: string;
14
+ stakedLQTY: string;
15
+ stakedLUSDBalance: string;
16
+ rewardETH: string;
17
+ rewardLUSD: string;
18
+ stabilityRewardETH: string;
19
+ stabilityRewardLQTY: string;
20
+ showStakingBalances: boolean;
21
+ }>;
@@ -10,10 +10,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  import Dec from 'decimal.js';
11
11
  import { assetAmountInEth, getAssetInfo } from '@defisaver/tokens';
12
12
  import { NetworkNumber, } from '../types/common';
13
- import { LiquityActivePoolContractViem, LiquityCollSurplusPoolContractViem, LiquityPriceFeedContractViem, LiquityTroveManagerContractViem, LiquityViewContractViem, } from '../contracts';
13
+ import { LiquityActivePoolContractViem, LiquityCollSurplusPoolContractViem, LiquityLQTYStakingViem, LiquityPriceFeedContractViem, LiquityStabilityPoolViem, LiquityTroveManagerContractViem, LiquityViewContractViem, } from '../contracts';
14
14
  import { LIQUITY_TROVE_STATUS_ENUM } from '../types';
15
15
  import { ZERO_ADDRESS } from '../constants';
16
16
  import { getViemProvider, setViemBlockNumber } from '../services/viem';
17
+ import { getEthAmountForDecimals } from '../services/utils';
17
18
  export const LIQUITY_NORMAL_MODE_RATIO = 110; // MCR
18
19
  export const LIQUITY_RECOVERY_MODE_RATIO = 150; // CCR
19
20
  export const _getLiquityAccountBalances = (provider, network, block, addressMapping, address) => __awaiter(void 0, void 0, void 0, function* () {
@@ -85,3 +86,39 @@ export const _getLiquityTroveInfo = (provider, network, address) => __awaiter(vo
85
86
  return payload;
86
87
  });
87
88
  export const getLiquityTroveInfo = (provider, network, address) => __awaiter(void 0, void 0, void 0, function* () { return _getLiquityTroveInfo(getViemProvider(provider, network, { batch: { multicall: true } }), network, address); });
89
+ export const getLiquityStakingData = (provider, network, address) => __awaiter(void 0, void 0, void 0, function* () {
90
+ const lqtyStakingView = LiquityLQTYStakingViem(provider, network);
91
+ const stabilityPoolView = LiquityStabilityPoolViem(provider, network);
92
+ const [stakes, pendingETHGain, pendingLUSDGain, totalLQTYStakes, stabilityPoolETHGain, stabilityPoolLQTYGain, compoundedLUSDDeposit, totalLUSDDeposits,] = yield Promise.all([
93
+ lqtyStakingView.read.stakes([address]),
94
+ lqtyStakingView.read.getPendingETHGain([address]),
95
+ lqtyStakingView.read.getPendingLUSDGain([address]),
96
+ lqtyStakingView.read.totalLQTYStaked(),
97
+ stabilityPoolView.read.getDepositorETHGain([address]),
98
+ stabilityPoolView.read.getDepositorLQTYGain([address]),
99
+ stabilityPoolView.read.getCompoundedLUSDDeposit([address]),
100
+ stabilityPoolView.read.getTotalLUSDDeposits(),
101
+ ]);
102
+ const totalLUSDDeposited = getEthAmountForDecimals(totalLUSDDeposits, 18);
103
+ const totalLQTYStaked = getEthAmountForDecimals(totalLQTYStakes, 18);
104
+ const stakedLQTY = getEthAmountForDecimals(stakes, 18);
105
+ const stakedLUSDBalance = getEthAmountForDecimals(compoundedLUSDDeposit, 18);
106
+ const rewardETH = getEthAmountForDecimals(pendingETHGain, 18);
107
+ const rewardLUSD = getEthAmountForDecimals(pendingLUSDGain, 18);
108
+ const stabilityRewardETH = getEthAmountForDecimals(stabilityPoolETHGain, 18);
109
+ const stabilityRewardLQTY = getEthAmountForDecimals(stabilityPoolLQTYGain, 18);
110
+ const showStakingBalances = !!(+stakedLQTY || +stakedLUSDBalance
111
+ || +rewardETH || +rewardLUSD
112
+ || +stabilityRewardETH || +stabilityRewardLQTY);
113
+ return {
114
+ totalLUSDDeposited,
115
+ totalLQTYStaked,
116
+ stakedLQTY,
117
+ stakedLUSDBalance,
118
+ rewardETH,
119
+ rewardLUSD,
120
+ stabilityRewardETH,
121
+ stabilityRewardLQTY,
122
+ showStakingBalances,
123
+ };
124
+ });
@@ -1,6 +1,14 @@
1
1
  import { Client, PublicClient } from 'viem';
2
2
  import { EthAddress, EthereumProvider, NetworkNumber } from '../types/common';
3
3
  import { LiquityV2AssetsData, LiquityV2MarketData, LiquityV2MarketInfo, LiquityV2TroveData, LiquityV2Versions } from '../types';
4
+ export type SPYieldGainParameters = {
5
+ P: string;
6
+ aggWeightedDebtSum: string;
7
+ currentScale: string;
8
+ lastAggUpdateTime: string;
9
+ totalBoldDeposits: string;
10
+ yieldGainsPending: string;
11
+ };
4
12
  export declare const _getLiquityV2MarketData: (provider: Client, network: NetworkNumber, selectedMarket: LiquityV2MarketInfo) => Promise<LiquityV2MarketData>;
5
13
  export declare const getLiquityV2MarketData: (provider: EthereumProvider, network: NetworkNumber, selectedMarket: LiquityV2MarketInfo) => Promise<LiquityV2MarketData>;
6
14
  export declare const _getLiquityV2UserTroveIds: (provider: PublicClient, network: NetworkNumber, selectedMarket: LiquityV2MarketInfo, troveNFTAddress: EthAddress, limitBlocksForEventFetching: boolean, account: EthAddress) => Promise<{
@@ -15,8 +23,6 @@ export declare const getLiquityV2UserTroveIds: (provider: EthereumProvider, netw
15
23
  }[];
16
24
  nextFreeTroveIndex: string;
17
25
  }>;
18
- export declare const calculateDebtInFrontLiquityV2: (markets: Record<LiquityV2Versions, LiquityV2MarketData>, selectedMarket: LiquityV2Versions, allMarketsUnbackedDebts: Record<LiquityV2Versions, string>, interestRateDebtInFront: string) => string;
19
- export declare const getDebtInFrontForInterestRateIncludingNewDebtLiquityV2: (newDebt: string, markets: Record<LiquityV2Versions, LiquityV2MarketData>, selectedMarket: LiquityV2Versions, provider: Client, network: NetworkNumber, interestRate: string) => Promise<string>;
20
26
  export declare const getDebtInFrontForInterestRateLiquityV2: (markets: Record<LiquityV2Versions, LiquityV2MarketData>, selectedMarket: LiquityV2Versions, provider: EthereumProvider, network: NetworkNumber, isLegacy: boolean, interestRate: string, debtInFrontBeingMoved?: string) => Promise<string>;
21
27
  export declare const _getLiquityV2TroveData: (provider: Client, network: NetworkNumber, { selectedMarket, assetsData, troveId, allMarketsData, }: {
22
28
  selectedMarket: LiquityV2MarketInfo;
@@ -31,3 +37,30 @@ export declare const getLiquityV2TroveData: (provider: EthereumProvider, network
31
37
  allMarketsData: Record<LiquityV2Versions, LiquityV2MarketData>;
32
38
  }, fetchDebtInFront?: boolean) => Promise<LiquityV2TroveData>;
33
39
  export declare const getLiquityV2ClaimableCollateral: (collSurplusPoolAddress: EthAddress, account: EthAddress, provider: EthereumProvider, network: NetworkNumber) => Promise<string>;
40
+ export type sBoldYieldParameters = {
41
+ WETH: string;
42
+ wsETH: string;
43
+ rETH: string;
44
+ };
45
+ export declare const getLiquityV2Staking: (provider: Client, network: NetworkNumber, market: LiquityV2Versions, user: EthAddress) => Promise<{
46
+ totalBOLDDeposited: string;
47
+ stakedBOLDBalance: string;
48
+ stabilityRewardColl: string;
49
+ stabilityRewardYield: string;
50
+ showStakingBalances: boolean;
51
+ debtTokenBalance: string;
52
+ stabilityPoolApy: string;
53
+ }>;
54
+ export declare const getLiquitySAndYBold: (provider: Client, network: NetworkNumber, markets: any, user: EthAddress) => Promise<{
55
+ spApy: string;
56
+ yBoldApy: string;
57
+ totalBoldDepositedSBold: string;
58
+ boldRateSBold: string;
59
+ maxWithdrawSBold: string;
60
+ totalBoldDepositedYBold: string;
61
+ boldRateYBold: string;
62
+ maxWithdrawYBold: string;
63
+ sBoldBalance: string;
64
+ yBoldBalance: string;
65
+ stYBoldBalance: string;
66
+ }>;
@@ -9,13 +9,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  import Dec from 'decimal.js';
11
11
  import { assetAmountInEth, getAssetInfo } from '@defisaver/tokens';
12
- import { createViemContractFromConfigFunc, LiquityV2LegacyViewContractViem, LiquityV2ViewContractViem, } from '../contracts';
12
+ import { createViemContractFromConfigFunc, LiquityV2LegacyViewContractViem, LiquityV2sBoldVaultViem, LiquityV2ViewContractViem, } from '../contracts';
13
13
  import { IncentiveKind, NetworkNumber, } from '../types/common';
14
14
  import { LIQUITY_V2_TROVE_STATUS_ENUM, LiquityV2Versions, } from '../types';
15
15
  import { getStakingApy, STAKING_ASSETS } from '../staking';
16
16
  import { getLiquityV2AggregatedPositionData } from '../helpers/liquityV2Helpers';
17
- import { compareAddresses, ethToWeth, MAXUINT } from '../services/utils';
18
- import { ZERO_ADDRESS } from '../constants';
17
+ import { compareAddresses, ethToWeth, getEthAmountForDecimals, MAXUINT, } from '../services/utils';
18
+ import { SECONDS_PER_YEAR, ZERO_ADDRESS } from '../constants';
19
19
  import { LiquityV2Markets } from '../markets';
20
20
  import { getViemProvider } from '../services/viem';
21
21
  const getLiquityV2ViewContract = (provider, network, isLegacy) => {
@@ -190,40 +190,12 @@ const getAllMarketsUnbackedDebts = (markets, isLegacy, provider, network) => __a
190
190
  })));
191
191
  return Object.fromEntries(allMarketsUnbackedDebt);
192
192
  });
193
- export const calculateDebtInFrontLiquityV2 = (markets, selectedMarket, allMarketsUnbackedDebts, interestRateDebtInFront) => {
194
- // Sanity check to avoid division by 0. Very unlikely to ever happen.
195
- const selectedMarketTotalBorrow = new Dec(markets[selectedMarket].assetsData[LiquityV2Markets(NetworkNumber.Eth)[selectedMarket].debtToken].totalBorrow);
196
- if (selectedMarketTotalBorrow.eq(0))
197
- return new Dec(0).toString();
193
+ const calculateDebtInFrontLiquityV2 = (markets, selectedMarket, allMarketsUnbackedDebts, interestRateDebtInFront) => {
198
194
  const selectedMarketUnbackedDebt = new Dec(allMarketsUnbackedDebts[selectedMarket]);
199
195
  const { isLegacy } = LiquityV2Markets(NetworkNumber.Eth)[selectedMarket];
200
- const totalUnbackedDebt = Object.values(allMarketsUnbackedDebts).reduce((acc, val) => acc.plus(new Dec(val)), new Dec(0));
201
- // When totalUnbackedDebt is 0, redemptions will be proportional with the branch size and not to unbacked debt.
202
- // When unbacked debt is 0 for branch, next redemption call won't touch that branch, so in order to estimate total debt in front we will:
203
- // - First add up all the unbacked debt from other branches, as that will be the only debt that will be redeemed on the fist redemption call
204
- // - Perform split the same way as we would do when totalUnbackedDebt == 0, this would represent the second call to the redemption function
205
- if (selectedMarketUnbackedDebt.eq(0)) {
206
- // Special case if the branch debt in front is 0, it means that all debt in front is unbacked debt from other branches.
207
- if (new Dec(interestRateDebtInFront).eq(0))
208
- return totalUnbackedDebt.toString();
209
- // Then calculate how much of that estimated amount would go to each branch
210
- // Second redemption call - calculate proportional redemption based on updated total debt
211
- const amountBeingRedeemedOnEachMarketByTotalBorrow = Object.entries(markets).map(([version, market]) => {
212
- const { isLegacy: isLegacyMarket } = LiquityV2Markets(NetworkNumber.Eth)[version];
213
- if (version === selectedMarket && isLegacyMarket !== isLegacy)
214
- return new Dec(interestRateDebtInFront);
215
- const { assetsData } = market;
216
- const { debtToken } = LiquityV2Markets(NetworkNumber.Eth)[version];
217
- // For other markets, subtract their unbacked debt as it will be cleared in first redemption call
218
- const marketUnbackedDebt = new Dec(allMarketsUnbackedDebts[version]);
219
- const totalBorrow = new Dec(assetsData[debtToken].totalBorrow).sub(marketUnbackedDebt);
220
- const amountToRedeem = new Dec(interestRateDebtInFront).mul(totalBorrow).div(selectedMarketTotalBorrow);
221
- return Dec.min(amountToRedeem, totalBorrow);
222
- });
223
- const redemptionAmount = amountBeingRedeemedOnEachMarketByTotalBorrow.reduce((acc, val) => acc.plus(val), new Dec(0));
224
- return totalUnbackedDebt.plus(redemptionAmount).toString();
225
- }
226
- const amountBeingRedeemedOnEachMarketByUnbackedDebt = Object.entries(markets).map(([version, market]) => {
196
+ if (selectedMarketUnbackedDebt.eq(0))
197
+ return interestRateDebtInFront;
198
+ const amountBeingReedemedOnEachMarket = Object.entries(markets).map(([version, market]) => {
227
199
  const { isLegacy: isLegacyMarket } = LiquityV2Markets(NetworkNumber.Eth)[version];
228
200
  if (version === selectedMarket && isLegacyMarket !== isLegacy)
229
201
  return new Dec(interestRateDebtInFront);
@@ -231,24 +203,11 @@ export const calculateDebtInFrontLiquityV2 = (markets, selectedMarket, allMarket
231
203
  const { debtToken } = LiquityV2Markets(NetworkNumber.Eth)[version];
232
204
  const unbackedDebt = new Dec(allMarketsUnbackedDebts[version]);
233
205
  const totalBorrow = new Dec(assetsData[debtToken].totalBorrow);
234
- const amountToRedeem = new Dec(interestRateDebtInFront).mul(unbackedDebt).div(selectedMarketUnbackedDebt);
235
- return Dec.min(amountToRedeem, totalBorrow);
206
+ const amountToReedem = new Dec(interestRateDebtInFront).mul(unbackedDebt).div(selectedMarketUnbackedDebt);
207
+ return Dec.min(amountToReedem, totalBorrow);
236
208
  });
237
- return amountBeingRedeemedOnEachMarketByUnbackedDebt.reduce((acc, val) => acc.plus(val), new Dec(0)).toString();
209
+ return amountBeingReedemedOnEachMarket.reduce((acc, val) => acc.plus(val), new Dec(0)).toString();
238
210
  };
239
- // @dev The amount redeemed on each branch depends on the unbacked debt of every branch (the difference between total borrow and stability pool deposits).
240
- // When new debt is generated on the selected market, the unbacked debt will increase, resulting in a higher redemption amount on that branch.
241
- // This function accepts the new debt that's about to be generated (e.g., trove creation) and estimates the debt in front based on the new state.
242
- export const getDebtInFrontForInterestRateIncludingNewDebtLiquityV2 = (newDebt, markets, selectedMarket, provider, network, interestRate) => __awaiter(void 0, void 0, void 0, function* () {
243
- const marketsWithNewDebt = structuredClone(markets);
244
- const selectedMarketDebtToken = LiquityV2Markets(network)[selectedMarket].debtToken;
245
- const currentTotalBorrow = new Dec(marketsWithNewDebt[selectedMarket].assetsData[selectedMarketDebtToken].totalBorrow);
246
- marketsWithNewDebt[selectedMarket].assetsData[selectedMarketDebtToken].totalBorrow = currentTotalBorrow.add(newDebt).toString();
247
- const { isLegacy } = LiquityV2Markets(NetworkNumber.Eth)[selectedMarket];
248
- const allMarketsUnbackedDebts = yield getAllMarketsUnbackedDebts(marketsWithNewDebt, isLegacy, provider, network);
249
- const interestRateDebtInFront = new Dec(yield getDebtInFrontForInterestRateSingleMarketLiquityV2(provider, network, isLegacy, LiquityV2Markets(network)[selectedMarket].marketAddress, interestRate));
250
- return calculateDebtInFrontLiquityV2(marketsWithNewDebt, selectedMarket, allMarketsUnbackedDebts, interestRateDebtInFront.toString());
251
- });
252
211
  const getDebtInFrontLiquityV2 = (markets, selectedMarket, provider, network, viewContract, troveId) => __awaiter(void 0, void 0, void 0, function* () {
253
212
  const { isLegacy } = LiquityV2Markets(NetworkNumber.Eth)[selectedMarket];
254
213
  const allMarketsUnbackedDebts = yield getAllMarketsUnbackedDebts(markets, isLegacy, provider, network);
@@ -335,3 +294,142 @@ export const getLiquityV2ClaimableCollateral = (collSurplusPoolAddress, account,
335
294
  const claimableCollateral = yield collSurplusPoolContract.read.getCollateral([account]);
336
295
  return claimableCollateral.toString();
337
296
  });
297
+ const stabilityPoolAddrForMarket = {
298
+ [LiquityV2Versions.LiquityV2Eth]: '0x5721cbbd64fc7Ae3Ef44A0A3F9a790A9264Cf9BF',
299
+ [LiquityV2Versions.LiquityV2WstEth]: '0x9502b7c397e9aa22fe9db7ef7daf21cd2aebe56b',
300
+ [LiquityV2Versions.LiquityV2REth]: '0xd442e41019b7f5c4dd78f50dc03726c446148695',
301
+ [LiquityV2Versions.LiquityV2EthLegacy]: ZERO_ADDRESS,
302
+ [LiquityV2Versions.LiquityV2REthLegacy]: ZERO_ADDRESS,
303
+ [LiquityV2Versions.LiquityV2WstEthLegacy]: ZERO_ADDRESS,
304
+ };
305
+ const activePoolAddrForMarket = {
306
+ [LiquityV2Versions.LiquityV2Eth]: '0xeB5A8C825582965f1d84606E078620a84ab16AfE',
307
+ [LiquityV2Versions.LiquityV2WstEth]: '0x531a8f99c70d6a56a7cee02d6b4281650d7919a0',
308
+ [LiquityV2Versions.LiquityV2REth]: '0x9074d72cc82dad1e13e454755aa8f144c479532f',
309
+ [LiquityV2Versions.LiquityV2EthLegacy]: ZERO_ADDRESS,
310
+ [LiquityV2Versions.LiquityV2WstEthLegacy]: ZERO_ADDRESS,
311
+ [LiquityV2Versions.LiquityV2REthLegacy]: ZERO_ADDRESS,
312
+ };
313
+ function ceilDiv(a, b) {
314
+ return new Dec(a).add(new Dec(b).sub(1)).div(b).toString();
315
+ }
316
+ const SP_YIELD_SPLIT = new Dec(75).mul(Math.pow(10, 16)).toString(); // 75%
317
+ const calcPendingSPYield = (aggWeightedDebtSum, lastAggUpdateTime, currentTime) => {
318
+ const a = new Dec(aggWeightedDebtSum).mul(new Dec(currentTime).sub(new Dec(lastAggUpdateTime))).toString();
319
+ const b = new Dec(SECONDS_PER_YEAR).mul(1000).mul(1e18).toString();
320
+ return new Dec(ceilDiv(a, b)).mul(SP_YIELD_SPLIT).div(1e18).toString();
321
+ };
322
+ const calculateStabilityPoolApy = (spYieldGainParams) => {
323
+ const { aggWeightedDebtSum, totalBoldDeposits, lastAggUpdateTime, yieldGainsPending, } = spYieldGainParams;
324
+ if (new Dec(totalBoldDeposits).eq(0)) {
325
+ return '0';
326
+ }
327
+ const now = Date.now().toString();
328
+ const lastAggUpdateTimeScaled = new Dec(lastAggUpdateTime).mul(1000).toString();
329
+ const pendingSPYield = new Dec(calcPendingSPYield(aggWeightedDebtSum, lastAggUpdateTimeScaled, now)).add(yieldGainsPending).toString();
330
+ const annualizedYield = new Dec(pendingSPYield).mul(SECONDS_PER_YEAR).mul(1000).div(new Dec(now).minus(lastAggUpdateTimeScaled))
331
+ .toString();
332
+ return new Dec(annualizedYield).div(totalBoldDeposits).mul(100).toString();
333
+ };
334
+ const getYBoldApyApi = () => __awaiter(void 0, void 0, void 0, function* () {
335
+ try {
336
+ const url = 'https://ydaemon.yearn.fi/1/vaults/0x23346B04a7f55b8760E5860AA5A77383D63491cD?strategiesDetails=withDetails&strategiesCondition=inQueue';
337
+ const yBoldData = yield fetch(url)
338
+ .then(res => res.json())
339
+ .catch(console.error);
340
+ return new Dec(yBoldData.apr.netAPR).mul(100).toString();
341
+ }
342
+ catch (error) {
343
+ console.error('External API Failure: YBold ', error);
344
+ return '0';
345
+ }
346
+ });
347
+ const calculateSPApy = (spYieldGainParams, spAPYs) => {
348
+ const { WETH, wsETH, rETH, } = spYieldGainParams;
349
+ const apy = new Dec(WETH).mul(spAPYs.apyEth).add(new Dec(wsETH).mul(spAPYs.apyWstEth)).add(new Dec(rETH).mul(spAPYs.apyREth))
350
+ .toString();
351
+ return apy;
352
+ };
353
+ export const getLiquityV2Staking = (provider, network, market, user) => __awaiter(void 0, void 0, void 0, function* () {
354
+ const stabilityPoolView = createViemContractFromConfigFunc('LiquityV2StabilityPool', stabilityPoolAddrForMarket[market])(provider, network);
355
+ const activePoolView = createViemContractFromConfigFunc('LiquityV2ActivePool', activePoolAddrForMarket[market])(provider, network);
356
+ const debtTokenInfo = getAssetInfo(LiquityV2Markets(network)[market].debtToken, network);
357
+ const debtTokenContract = createViemContractFromConfigFunc('Erc20', debtTokenInfo.address)(provider, network);
358
+ const [stabilityRewardColl, stabilityRewardYield, compoundedBoldDeposit, totalBoldDeposits, P, currentScale, yieldGainsPending, debtTokenBalance, aggWeightedDebtSum, lastAggUpdateTime,] = yield Promise.all([
359
+ stabilityPoolView.read.getDepositorCollGain([user]),
360
+ stabilityPoolView.read.getDepositorYieldGain([user]),
361
+ stabilityPoolView.read.getCompoundedBoldDeposit([user]),
362
+ stabilityPoolView.read.getTotalBoldDeposits(),
363
+ stabilityPoolView.read.P(),
364
+ stabilityPoolView.read.currentScale(),
365
+ stabilityPoolView.read.getYieldGainsPending(),
366
+ debtTokenContract.read.balanceOf([user]),
367
+ activePoolView.read.aggWeightedDebtSum(),
368
+ activePoolView.read.lastAggUpdateTime(),
369
+ ]);
370
+ const stabilityPoolYieldParams = {
371
+ aggWeightedDebtSum: aggWeightedDebtSum.toString(),
372
+ lastAggUpdateTime: lastAggUpdateTime.toString(),
373
+ P: P.toString(),
374
+ currentScale: currentScale.toString(),
375
+ yieldGainsPending: yieldGainsPending.toString(),
376
+ totalBoldDeposits: totalBoldDeposits.toString(),
377
+ };
378
+ const stabilityPoolApy = calculateStabilityPoolApy(stabilityPoolYieldParams);
379
+ const stakedBOLDBalanceForUser = getEthAmountForDecimals(compoundedBoldDeposit.toString(), 18);
380
+ const stabilityRewardCollForUser = getEthAmountForDecimals(stabilityRewardColl.toString(), 18);
381
+ const stabilityRewardYieldForUser = getEthAmountForDecimals(stabilityRewardYield.toString(), 18);
382
+ return {
383
+ totalBOLDDeposited: getEthAmountForDecimals(totalBoldDeposits.toString(), 18),
384
+ stakedBOLDBalance: stakedBOLDBalanceForUser,
385
+ stabilityRewardColl: stabilityRewardCollForUser,
386
+ stabilityRewardYield: stabilityRewardYieldForUser,
387
+ showStakingBalances: !!(+stakedBOLDBalanceForUser || +stabilityRewardCollForUser || +stabilityRewardYieldForUser),
388
+ debtTokenBalance: getEthAmountForDecimals(debtTokenBalance.toString(), debtTokenInfo.decimals),
389
+ stabilityPoolApy,
390
+ };
391
+ });
392
+ export const getLiquitySAndYBold = (provider, network, markets, user) => __awaiter(void 0, void 0, void 0, function* () {
393
+ const sBold = LiquityV2sBoldVaultViem(provider, network);
394
+ const yBold = createViemContractFromConfigFunc('Erc4626', '0x9F4330700a36B29952869fac9b33f45EEdd8A3d8')(provider, network);
395
+ const stYBold = createViemContractFromConfigFunc('Erc4626', '0x23346B04a7f55b8760E5860AA5A77383D63491cD')(provider, network);
396
+ const spAPYs = {
397
+ apyEth: markets[LiquityV2Versions.LiquityV2Eth].data.stabilityPoolApy,
398
+ apyWstEth: markets[LiquityV2Versions.LiquityV2WstEth].data.stabilityPoolApy,
399
+ apyREth: markets[LiquityV2Versions.LiquityV2REth].data.stabilityPoolApy,
400
+ };
401
+ const [sBoldTotalAssets, sBoldConvertToShares, sBoldMaxWithdraw, wethApy, wsETHApy, rETHApy, yBoldTotalAssets, yBoldMaxWithdraw, stYBoldConvertToShares, yBoldApy, sBoldBalance, yBoldBalance, stYBoldBalance,] = yield Promise.all([
402
+ sBold.read.totalAssets(),
403
+ sBold.read.convertToShares([BigInt(1e18)]),
404
+ sBold.read.maxWithdraw([user]),
405
+ sBold.read.sps([BigInt(0)]),
406
+ sBold.read.sps([BigInt(1)]),
407
+ sBold.read.sps([BigInt(2)]),
408
+ yBold.read.totalAssets(),
409
+ yBold.read.maxWithdraw([user]),
410
+ stYBold.read.convertToShares([BigInt(1e18)]),
411
+ getYBoldApyApi(),
412
+ sBold.read.balanceOf([user]),
413
+ yBold.read.balanceOf([user]),
414
+ stYBold.read.balanceOf([user]),
415
+ ]);
416
+ const spMarketRes = {
417
+ WETH: new Dec(wethApy[1]).div(10000).toString(),
418
+ wsETH: new Dec(wsETHApy[1]).div(10000).toString(),
419
+ rETH: new Dec(rETHApy[1]).div(10000).toString(),
420
+ };
421
+ const spApy = calculateSPApy(spMarketRes, spAPYs);
422
+ return {
423
+ spApy,
424
+ yBoldApy,
425
+ totalBoldDepositedSBold: assetAmountInEth(sBoldTotalAssets.toString(), 'sBOLD'),
426
+ boldRateSBold: assetAmountInEth(sBoldConvertToShares.toString(), 'sBOLD'),
427
+ maxWithdrawSBold: assetAmountInEth(sBoldMaxWithdraw.toString(), 'sBOLD'),
428
+ totalBoldDepositedYBold: assetAmountInEth(yBoldTotalAssets.toString(), 'yBOLD'),
429
+ boldRateYBold: assetAmountInEth(stYBoldConvertToShares.toString(), 'yBOLD'),
430
+ maxWithdrawYBold: assetAmountInEth(yBoldMaxWithdraw.toString(), 'yBOLD'),
431
+ sBoldBalance: assetAmountInEth(sBoldBalance.toString(), 'sBOLD'),
432
+ yBoldBalance: assetAmountInEth(yBoldBalance.toString(), 'yBOLD'),
433
+ stYBoldBalance: assetAmountInEth(stYBoldBalance.toString(), 'yBOLD'),
434
+ };
435
+ });
@@ -7,3 +7,8 @@ export declare const _getMorphoBlueAccountBalances: (provider: Client, network:
7
7
  export declare const getMorphoBlueAccountBalances: (provider: EthereumProvider, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress, selectedMarket: MorphoBlueMarketData) => Promise<PositionBalances>;
8
8
  export declare function _getMorphoBlueAccountData(provider: Client, network: NetworkNumber, account: EthAddress, selectedMarket: MorphoBlueMarketData, marketInfo: MorphoBlueMarketInfo): Promise<MorphoBluePositionData>;
9
9
  export declare function getMorphoBlueAccountData(provider: EthereumProvider, network: NetworkNumber, account: EthAddress, selectedMarket: MorphoBlueMarketData, marketInfo: MorphoBlueMarketInfo): Promise<MorphoBluePositionData>;
10
+ export declare function getMorphoEarn(provider: Client, network: NetworkNumber, account: EthAddress, selectedMarket: MorphoBlueMarketData, marketInfo: MorphoBlueMarketInfo): Promise<{
11
+ apy: string;
12
+ amount: string;
13
+ amountUsd: string;
14
+ }>;
@@ -12,7 +12,7 @@ import { assetAmountInEth, getAssetInfoByAddress } from '@defisaver/tokens';
12
12
  import { IncentiveKind, NetworkNumber, } from '../types/common';
13
13
  import { DFSFeedRegistryContractViem, FeedRegistryContractViem, MorphoBlueViewContractViem, } from '../contracts';
14
14
  import { USD_QUOTE, WAD } from '../constants';
15
- import { getStakingApy, STAKING_ASSETS } from '../staking';
15
+ import { calculateNetApy, getStakingApy, STAKING_ASSETS } from '../staking';
16
16
  import { isMainnetNetwork, wethToEth } from '../services/utils';
17
17
  import { getBorrowRate, getMorphoBlueAggregatedPositionData, getRewardsForMarket, getSupplyRate, } from '../helpers/morphoBlueHelpers';
18
18
  import { getChainlinkAssetAddress } from '../services/priceService';
@@ -201,3 +201,37 @@ export function getMorphoBlueAccountData(provider, network, account, selectedMar
201
201
  return _getMorphoBlueAccountData(getViemProvider(provider, network), network, account, selectedMarket, marketInfo);
202
202
  });
203
203
  }
204
+ export function getMorphoEarn(provider, network, account, selectedMarket, marketInfo) {
205
+ return __awaiter(this, void 0, void 0, function* () {
206
+ const { loanToken, collateralToken, oracle, irm, lltv, } = selectedMarket;
207
+ const lltvInWei = new Dec(lltv).mul(WAD).toString();
208
+ const viewContract = MorphoBlueViewContractViem(provider, network);
209
+ const loanInfo = (yield viewContract.read.getUserInfo([
210
+ {
211
+ loanToken, collateralToken, oracle, irm, lltv: BigInt(lltvInWei),
212
+ },
213
+ account
214
+ ]));
215
+ const loanTokenInfo = marketInfo.assetsData[marketInfo.loanToken];
216
+ const loanTokenSupplied = assetAmountInEth(loanInfo.suppliedInAssets.toString(), marketInfo.loanToken);
217
+ const loanTokenSuppliedUsd = new Dec(loanTokenSupplied).mul(loanTokenInfo.price).toString();
218
+ const usedAssets = {
219
+ [marketInfo.loanToken]: {
220
+ symbol: loanTokenInfo.symbol,
221
+ supplied: loanTokenSupplied,
222
+ borrowed: '0',
223
+ isSupplied: new Dec(loanInfo.suppliedInAssets.toString()).gt(0),
224
+ isBorrowed: false,
225
+ collateral: false,
226
+ suppliedUsd: loanTokenSuppliedUsd,
227
+ borrowedUsd: '0',
228
+ },
229
+ };
230
+ const { netApy } = calculateNetApy({ usedAssets, assetsData: marketInfo.assetsData });
231
+ return {
232
+ apy: netApy,
233
+ amount: loanTokenSupplied,
234
+ amountUsd: loanTokenSuppliedUsd,
235
+ };
236
+ });
237
+ }
@@ -1,3 +1,8 @@
1
1
  import { EthAddress, EthereumProvider, NetworkNumber } from '../types/common';
2
2
  import { PortfolioPositionsData } from '../types';
3
- export declare function getPortfolioData(provider: EthereumProvider, network: NetworkNumber, defaultProvider: EthereumProvider, addresses: EthAddress[], summerFiAddresses: EthAddress[]): Promise<PortfolioPositionsData>;
3
+ export declare function getPortfolioData(provider: EthereumProvider, network: NetworkNumber, defaultProvider: EthereumProvider, addresses: EthAddress[], summerFiAddresses: EthAddress[]): Promise<{
4
+ positions: PortfolioPositionsData;
5
+ stakingPositions: any;
6
+ rewardsData: any;
7
+ markets: any;
8
+ }>;