@defisaver/positions-sdk 2.1.4 → 2.1.6-dev-debt-in-front

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 (96) hide show
  1. package/.mocharc.json +4 -4
  2. package/.nvmrc +1 -1
  3. package/CLAUDE.md +32 -0
  4. package/README.md +64 -64
  5. package/cjs/curveUsd/index.js +2 -1
  6. package/cjs/fluid/index.js +2 -1
  7. package/cjs/helpers/morphoBlueHelpers/index.js +66 -66
  8. package/cjs/liquityV2/index.d.ts +2 -0
  9. package/cjs/liquityV2/index.js +52 -9
  10. package/cjs/types/curveUsd.d.ts +2 -0
  11. package/esm/curveUsd/index.js +2 -1
  12. package/esm/fluid/index.js +2 -1
  13. package/esm/helpers/morphoBlueHelpers/index.js +66 -66
  14. package/esm/liquityV2/index.d.ts +2 -0
  15. package/esm/liquityV2/index.js +48 -7
  16. package/esm/types/curveUsd.d.ts +2 -0
  17. package/package.json +47 -47
  18. package/src/aaveV2/index.ts +240 -240
  19. package/src/aaveV3/index.ts +614 -614
  20. package/src/aaveV3/merit.ts +94 -94
  21. package/src/aaveV3/merkl.ts +74 -74
  22. package/src/claiming/aaveV3.ts +154 -154
  23. package/src/claiming/compV3.ts +22 -22
  24. package/src/claiming/index.ts +12 -12
  25. package/src/claiming/king.ts +66 -66
  26. package/src/claiming/morphoBlue.ts +118 -118
  27. package/src/claiming/spark.ts +225 -225
  28. package/src/compoundV2/index.ts +244 -244
  29. package/src/compoundV3/index.ts +274 -274
  30. package/src/config/contracts.ts +1228 -1228
  31. package/src/constants/index.ts +10 -10
  32. package/src/contracts.ts +120 -120
  33. package/src/curveUsd/index.ts +254 -250
  34. package/src/eulerV2/index.ts +324 -324
  35. package/src/exchange/index.ts +25 -25
  36. package/src/fluid/index.ts +1638 -1638
  37. package/src/helpers/aaveHelpers/index.ts +185 -185
  38. package/src/helpers/compoundHelpers/index.ts +283 -283
  39. package/src/helpers/curveUsdHelpers/index.ts +40 -40
  40. package/src/helpers/eulerHelpers/index.ts +222 -222
  41. package/src/helpers/fluidHelpers/index.ts +326 -326
  42. package/src/helpers/index.ts +10 -10
  43. package/src/helpers/liquityV2Helpers/index.ts +82 -82
  44. package/src/helpers/llamaLendHelpers/index.ts +53 -53
  45. package/src/helpers/makerHelpers/index.ts +52 -52
  46. package/src/helpers/morphoBlueHelpers/index.ts +396 -396
  47. package/src/helpers/sparkHelpers/index.ts +155 -155
  48. package/src/index.ts +47 -47
  49. package/src/liquity/index.ts +159 -159
  50. package/src/liquityV2/index.ts +703 -657
  51. package/src/llamaLend/index.ts +305 -305
  52. package/src/maker/index.ts +223 -223
  53. package/src/markets/aave/index.ts +116 -116
  54. package/src/markets/aave/marketAssets.ts +49 -49
  55. package/src/markets/compound/index.ts +227 -227
  56. package/src/markets/compound/marketsAssets.ts +90 -90
  57. package/src/markets/curveUsd/index.ts +69 -69
  58. package/src/markets/euler/index.ts +26 -26
  59. package/src/markets/fluid/index.ts +2456 -2456
  60. package/src/markets/index.ts +25 -25
  61. package/src/markets/liquityV2/index.ts +102 -102
  62. package/src/markets/llamaLend/contractAddresses.ts +141 -141
  63. package/src/markets/llamaLend/index.ts +235 -235
  64. package/src/markets/morphoBlue/index.ts +895 -895
  65. package/src/markets/spark/index.ts +29 -29
  66. package/src/markets/spark/marketAssets.ts +11 -11
  67. package/src/moneymarket/moneymarketCommonService.ts +80 -80
  68. package/src/morphoBlue/index.ts +274 -274
  69. package/src/portfolio/index.ts +570 -570
  70. package/src/services/priceService.ts +159 -159
  71. package/src/services/utils.ts +99 -99
  72. package/src/services/viem.ts +32 -32
  73. package/src/setup.ts +8 -8
  74. package/src/spark/index.ts +445 -445
  75. package/src/staking/eligibility.ts +59 -59
  76. package/src/staking/index.ts +1 -1
  77. package/src/staking/staking.ts +170 -170
  78. package/src/types/aave.ts +189 -189
  79. package/src/types/claiming.ts +109 -109
  80. package/src/types/common.ts +105 -105
  81. package/src/types/compound.ts +136 -136
  82. package/src/types/curveUsd.ts +123 -121
  83. package/src/types/euler.ts +175 -175
  84. package/src/types/fluid.ts +448 -448
  85. package/src/types/index.ts +13 -13
  86. package/src/types/liquity.ts +30 -30
  87. package/src/types/liquityV2.ts +126 -126
  88. package/src/types/llamaLend.ts +159 -159
  89. package/src/types/maker.ts +63 -63
  90. package/src/types/merit.ts +1 -1
  91. package/src/types/merkl.ts +70 -70
  92. package/src/types/morphoBlue.ts +194 -194
  93. package/src/types/portfolio.ts +60 -60
  94. package/src/types/spark.ts +135 -135
  95. package/src/umbrella/index.ts +69 -69
  96. package/src/umbrella/umbrellaUtils.ts +29 -29
@@ -23,6 +23,8 @@ export declare const getLiquityV2UserTroveIds: (provider: EthereumProvider, netw
23
23
  }[];
24
24
  nextFreeTroveIndex: string;
25
25
  }>;
26
+ export declare const calculateDebtInFrontLiquityV2: (markets: Record<LiquityV2Versions, LiquityV2MarketData>, selectedMarket: LiquityV2Versions, allMarketsUnbackedDebts: Record<LiquityV2Versions, string>, interestRateDebtInFront: string) => string;
27
+ export declare const getDebtInFrontForInterestRateIncludingNewDebtLiquityV2: (newDebt: string, markets: Record<LiquityV2Versions, LiquityV2MarketData>, selectedMarket: LiquityV2Versions, provider: Client, network: NetworkNumber, interestRate: string) => Promise<string>;
26
28
  export declare const getDebtInFrontForInterestRateLiquityV2: (markets: Record<LiquityV2Versions, LiquityV2MarketData>, selectedMarket: LiquityV2Versions, provider: EthereumProvider, network: NetworkNumber, isLegacy: boolean, interestRate: string, debtInFrontBeingMoved?: string) => Promise<string>;
27
29
  export declare const _getLiquityV2TroveData: (provider: Client, network: NetworkNumber, { selectedMarket, assetsData, troveId, allMarketsData, }: {
28
30
  selectedMarket: LiquityV2MarketInfo;
@@ -190,12 +190,40 @@ const getAllMarketsUnbackedDebts = (markets, isLegacy, provider, network) => __a
190
190
  })));
191
191
  return Object.fromEntries(allMarketsUnbackedDebt);
192
192
  });
193
- const calculateDebtInFrontLiquityV2 = (markets, selectedMarket, allMarketsUnbackedDebts, interestRateDebtInFront) => {
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();
194
198
  const selectedMarketUnbackedDebt = new Dec(allMarketsUnbackedDebts[selectedMarket]);
195
199
  const { isLegacy } = LiquityV2Markets(NetworkNumber.Eth)[selectedMarket];
196
- if (selectedMarketUnbackedDebt.eq(0))
197
- return interestRateDebtInFront;
198
- const amountBeingReedemedOnEachMarket = Object.entries(markets).map(([version, market]) => {
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]) => {
199
227
  const { isLegacy: isLegacyMarket } = LiquityV2Markets(NetworkNumber.Eth)[version];
200
228
  if (version === selectedMarket && isLegacyMarket !== isLegacy)
201
229
  return new Dec(interestRateDebtInFront);
@@ -203,11 +231,24 @@ const calculateDebtInFrontLiquityV2 = (markets, selectedMarket, allMarketsUnback
203
231
  const { debtToken } = LiquityV2Markets(NetworkNumber.Eth)[version];
204
232
  const unbackedDebt = new Dec(allMarketsUnbackedDebts[version]);
205
233
  const totalBorrow = new Dec(assetsData[debtToken].totalBorrow);
206
- const amountToReedem = new Dec(interestRateDebtInFront).mul(unbackedDebt).div(selectedMarketUnbackedDebt);
207
- return Dec.min(amountToReedem, totalBorrow);
234
+ const amountToRedeem = new Dec(interestRateDebtInFront).mul(unbackedDebt).div(selectedMarketUnbackedDebt);
235
+ return Dec.min(amountToRedeem, totalBorrow);
208
236
  });
209
- return amountBeingReedemedOnEachMarket.reduce((acc, val) => acc.plus(val), new Dec(0)).toString();
237
+ return amountBeingRedeemedOnEachMarketByUnbackedDebt.reduce((acc, val) => acc.plus(val), new Dec(0)).toString();
210
238
  };
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
+ });
211
252
  const getDebtInFrontLiquityV2 = (markets, selectedMarket, provider, network, viewContract, troveId) => __awaiter(void 0, void 0, void 0, function* () {
212
253
  const { isLegacy } = LiquityV2Markets(NetworkNumber.Eth)[selectedMarket];
213
254
  const allMarketsUnbackedDebts = yield getAllMarketsUnbackedDebts(markets, isLegacy, provider, network);
@@ -109,4 +109,6 @@ export interface CrvUSDUserData {
109
109
  userBands: UserBandData[];
110
110
  loanExists: boolean;
111
111
  borrowRate?: string;
112
+ collateralPrice: string;
113
+ collRatio: string;
112
114
  }
package/package.json CHANGED
@@ -1,47 +1,47 @@
1
- {
2
- "name": "@defisaver/positions-sdk",
3
- "version": "2.1.4",
4
- "description": "",
5
- "main": "./cjs/index.js",
6
- "module": "./esm/index.js",
7
- "types": "./esm/index.d.ts",
8
- "scripts": {
9
- "build:esm": "rm -rf esm && tsc -p tsconfig.esm.json",
10
- "build:cjs": "rm -rf cjs && tsc -p tsconfig.cjs.json",
11
- "build": "npm run lint && npm run build:cjs && npm run build:esm",
12
- "dev": "tsc -p tsconfig.json --watch",
13
- "lint": "eslint src/ --fix",
14
- "lint-check": "eslint src/",
15
- "test": "mocha tests/*",
16
- "test-single": "mocha ./tests/$npm_config_name.ts",
17
- "test:debugger": "mocha --inspect-brk tests/*",
18
- "version-bump": "git commit -am \"Version bump to $(npm version patch | cut -c 2-)\""
19
- },
20
- "keywords": [],
21
- "author": "",
22
- "license": "ISC",
23
- "dependencies": {
24
- "@defisaver/tokens": "^1.7.1",
25
- "@types/lodash": "^4.17.15",
26
- "@types/memoizee": "^0.4.12",
27
- "decimal.js": "^10.6.0",
28
- "lodash": "^4.17.21",
29
- "memoizee": "^0.4.17",
30
- "viem": "^2.31.4"
31
- },
32
- "devDependencies": {
33
- "@defisaver/eslint-config": "^1.0.1",
34
- "@metamask/eth-json-rpc-middleware": "^15.0.1",
35
- "@metamask/eth-json-rpc-provider": "^4.1.6",
36
- "@types/chai": "^5.0.0",
37
- "@types/mocha": "^10.0.9",
38
- "chai": "^4.3.8",
39
- "dotenv": "^16.3.1",
40
- "eslint": "^8.49.0",
41
- "eslint-plugin-import": "^2.31.0",
42
- "mocha": "^10.2.0",
43
- "nock": "^14.0.0",
44
- "ts-node": "^10.9.2",
45
- "typescript": "^5.2.2"
46
- }
47
- }
1
+ {
2
+ "name": "@defisaver/positions-sdk",
3
+ "version": "2.1.6-dev-debt-in-front",
4
+ "description": "",
5
+ "main": "./cjs/index.js",
6
+ "module": "./esm/index.js",
7
+ "types": "./esm/index.d.ts",
8
+ "scripts": {
9
+ "build:esm": "rm -rf esm && tsc -p tsconfig.esm.json",
10
+ "build:cjs": "rm -rf cjs && tsc -p tsconfig.cjs.json",
11
+ "build": "npm run lint && npm run build:cjs && npm run build:esm",
12
+ "dev": "tsc -p tsconfig.json --watch",
13
+ "lint": "eslint src/ --fix",
14
+ "lint-check": "eslint src/",
15
+ "test": "mocha tests/*",
16
+ "test-single": "mocha ./tests/$npm_config_name.ts",
17
+ "test:debugger": "mocha --inspect-brk tests/*",
18
+ "version-bump": "git commit -am \"Version bump to $(npm version patch | cut -c 2-)\""
19
+ },
20
+ "keywords": [],
21
+ "author": "",
22
+ "license": "ISC",
23
+ "dependencies": {
24
+ "@defisaver/tokens": "^1.7.5",
25
+ "@types/lodash": "^4.17.15",
26
+ "@types/memoizee": "^0.4.12",
27
+ "decimal.js": "^10.6.0",
28
+ "lodash": "^4.17.21",
29
+ "memoizee": "^0.4.17",
30
+ "viem": "^2.31.4"
31
+ },
32
+ "devDependencies": {
33
+ "@defisaver/eslint-config": "^1.0.1",
34
+ "@metamask/eth-json-rpc-middleware": "^15.0.1",
35
+ "@metamask/eth-json-rpc-provider": "^4.1.6",
36
+ "@types/chai": "^5.0.0",
37
+ "@types/mocha": "^10.0.9",
38
+ "chai": "^4.3.8",
39
+ "dotenv": "^16.3.1",
40
+ "eslint": "^8.49.0",
41
+ "eslint-plugin-import": "^2.31.0",
42
+ "mocha": "^10.2.0",
43
+ "nock": "^14.0.0",
44
+ "ts-node": "^10.9.2",
45
+ "typescript": "^5.2.2"
46
+ }
47
+ }