@defisaver/positions-sdk 2.1.14 → 2.1.16

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.
@@ -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;
@@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.getLiquitySAndYBold = exports.getLiquityV2Staking = exports.getLiquityV2ClaimableCollateral = exports.getLiquityV2TroveData = exports._getLiquityV2TroveData = exports.getDebtInFrontForInterestRateLiquityV2 = exports.getLiquityV2UserTroveIds = exports._getLiquityV2UserTroveIds = exports.getLiquityV2MarketData = exports._getLiquityV2MarketData = void 0;
15
+ exports.getLiquitySAndYBold = exports.getLiquityV2Staking = exports.getLiquityV2ClaimableCollateral = exports.getLiquityV2TroveData = exports._getLiquityV2TroveData = exports.getDebtInFrontForInterestRateLiquityV2 = exports.getDebtInFrontForInterestRateIncludingNewDebtLiquityV2 = exports.calculateDebtInFrontLiquityV2 = exports.getLiquityV2UserTroveIds = exports._getLiquityV2UserTroveIds = exports.getLiquityV2MarketData = exports._getLiquityV2MarketData = void 0;
16
16
  const decimal_js_1 = __importDefault(require("decimal.js"));
17
17
  const tokens_1 = require("@defisaver/tokens");
18
18
  const contracts_1 = require("../contracts");
@@ -201,11 +201,39 @@ const getAllMarketsUnbackedDebts = (markets, isLegacy, provider, network) => __a
201
201
  return Object.fromEntries(allMarketsUnbackedDebt);
202
202
  });
203
203
  const calculateDebtInFrontLiquityV2 = (markets, selectedMarket, allMarketsUnbackedDebts, interestRateDebtInFront) => {
204
+ // Sanity check to avoid division by 0. Very unlikely to ever happen.
205
+ const selectedMarketTotalBorrow = new decimal_js_1.default(markets[selectedMarket].assetsData[(0, markets_1.LiquityV2Markets)(common_1.NetworkNumber.Eth)[selectedMarket].debtToken].totalBorrow);
206
+ if (selectedMarketTotalBorrow.eq(0))
207
+ return new decimal_js_1.default(0).toString();
204
208
  const selectedMarketUnbackedDebt = new decimal_js_1.default(allMarketsUnbackedDebts[selectedMarket]);
205
209
  const { isLegacy } = (0, markets_1.LiquityV2Markets)(common_1.NetworkNumber.Eth)[selectedMarket];
206
- if (selectedMarketUnbackedDebt.eq(0))
207
- return interestRateDebtInFront;
208
- const amountBeingReedemedOnEachMarket = Object.entries(markets).map(([version, market]) => {
210
+ const totalUnbackedDebt = Object.values(allMarketsUnbackedDebts).reduce((acc, val) => acc.plus(new decimal_js_1.default(val)), new decimal_js_1.default(0));
211
+ // When totalUnbackedDebt is 0, redemptions will be proportional with the branch size and not to unbacked debt.
212
+ // 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:
213
+ // - 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
214
+ // - Perform split the same way as we would do when totalUnbackedDebt == 0, this would represent the second call to the redemption function
215
+ if (selectedMarketUnbackedDebt.eq(0)) {
216
+ // Special case if the branch debt in front is 0, it means that all debt in front is unbacked debt from other branches.
217
+ if (new decimal_js_1.default(interestRateDebtInFront).eq(0))
218
+ return totalUnbackedDebt.toString();
219
+ // Then calculate how much of that estimated amount would go to each branch
220
+ // Second redemption call - calculate proportional redemption based on updated total debt
221
+ const amountBeingRedeemedOnEachMarketByTotalBorrow = Object.entries(markets).map(([version, market]) => {
222
+ const { isLegacy: isLegacyMarket } = (0, markets_1.LiquityV2Markets)(common_1.NetworkNumber.Eth)[version];
223
+ if (version === selectedMarket && isLegacyMarket !== isLegacy)
224
+ return new decimal_js_1.default(interestRateDebtInFront);
225
+ const { assetsData } = market;
226
+ const { debtToken } = (0, markets_1.LiquityV2Markets)(common_1.NetworkNumber.Eth)[version];
227
+ // For other markets, subtract their unbacked debt as it will be cleared in first redemption call
228
+ const marketUnbackedDebt = new decimal_js_1.default(allMarketsUnbackedDebts[version]);
229
+ const totalBorrow = new decimal_js_1.default(assetsData[debtToken].totalBorrow).sub(marketUnbackedDebt);
230
+ const amountToRedeem = new decimal_js_1.default(interestRateDebtInFront).mul(totalBorrow).div(selectedMarketTotalBorrow);
231
+ return decimal_js_1.default.min(amountToRedeem, totalBorrow);
232
+ });
233
+ const redemptionAmount = amountBeingRedeemedOnEachMarketByTotalBorrow.reduce((acc, val) => acc.plus(val), new decimal_js_1.default(0));
234
+ return totalUnbackedDebt.plus(redemptionAmount).toString();
235
+ }
236
+ const amountBeingRedeemedOnEachMarketByUnbackedDebt = Object.entries(markets).map(([version, market]) => {
209
237
  const { isLegacy: isLegacyMarket } = (0, markets_1.LiquityV2Markets)(common_1.NetworkNumber.Eth)[version];
210
238
  if (version === selectedMarket && isLegacyMarket !== isLegacy)
211
239
  return new decimal_js_1.default(interestRateDebtInFront);
@@ -213,16 +241,31 @@ const calculateDebtInFrontLiquityV2 = (markets, selectedMarket, allMarketsUnback
213
241
  const { debtToken } = (0, markets_1.LiquityV2Markets)(common_1.NetworkNumber.Eth)[version];
214
242
  const unbackedDebt = new decimal_js_1.default(allMarketsUnbackedDebts[version]);
215
243
  const totalBorrow = new decimal_js_1.default(assetsData[debtToken].totalBorrow);
216
- const amountToReedem = new decimal_js_1.default(interestRateDebtInFront).mul(unbackedDebt).div(selectedMarketUnbackedDebt);
217
- return decimal_js_1.default.min(amountToReedem, totalBorrow);
244
+ const amountToRedeem = new decimal_js_1.default(interestRateDebtInFront).mul(unbackedDebt).div(selectedMarketUnbackedDebt);
245
+ return decimal_js_1.default.min(amountToRedeem, totalBorrow);
218
246
  });
219
- return amountBeingReedemedOnEachMarket.reduce((acc, val) => acc.plus(val), new decimal_js_1.default(0)).toString();
247
+ return amountBeingRedeemedOnEachMarketByUnbackedDebt.reduce((acc, val) => acc.plus(val), new decimal_js_1.default(0)).toString();
220
248
  };
249
+ exports.calculateDebtInFrontLiquityV2 = calculateDebtInFrontLiquityV2;
250
+ // @dev The amount redeemed on each branch depends on the unbacked debt of every branch (the difference between total borrow and stability pool deposits).
251
+ // When new debt is generated on the selected market, the unbacked debt will increase, resulting in a higher redemption amount on that branch.
252
+ // 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.
253
+ const getDebtInFrontForInterestRateIncludingNewDebtLiquityV2 = (newDebt, markets, selectedMarket, provider, network, interestRate) => __awaiter(void 0, void 0, void 0, function* () {
254
+ const marketsWithNewDebt = structuredClone(markets);
255
+ const selectedMarketDebtToken = (0, markets_1.LiquityV2Markets)(network)[selectedMarket].debtToken;
256
+ const currentTotalBorrow = new decimal_js_1.default(marketsWithNewDebt[selectedMarket].assetsData[selectedMarketDebtToken].totalBorrow);
257
+ marketsWithNewDebt[selectedMarket].assetsData[selectedMarketDebtToken].totalBorrow = currentTotalBorrow.add(newDebt).toString();
258
+ const { isLegacy } = (0, markets_1.LiquityV2Markets)(common_1.NetworkNumber.Eth)[selectedMarket];
259
+ const allMarketsUnbackedDebts = yield getAllMarketsUnbackedDebts(marketsWithNewDebt, isLegacy, provider, network);
260
+ const interestRateDebtInFront = new decimal_js_1.default(yield getDebtInFrontForInterestRateSingleMarketLiquityV2(provider, network, isLegacy, (0, markets_1.LiquityV2Markets)(network)[selectedMarket].marketAddress, interestRate));
261
+ return (0, exports.calculateDebtInFrontLiquityV2)(marketsWithNewDebt, selectedMarket, allMarketsUnbackedDebts, interestRateDebtInFront.toString());
262
+ });
263
+ exports.getDebtInFrontForInterestRateIncludingNewDebtLiquityV2 = getDebtInFrontForInterestRateIncludingNewDebtLiquityV2;
221
264
  const getDebtInFrontLiquityV2 = (markets, selectedMarket, provider, network, viewContract, troveId) => __awaiter(void 0, void 0, void 0, function* () {
222
265
  const { isLegacy } = (0, markets_1.LiquityV2Markets)(common_1.NetworkNumber.Eth)[selectedMarket];
223
266
  const allMarketsUnbackedDebts = yield getAllMarketsUnbackedDebts(markets, isLegacy, provider, network);
224
267
  const interestRateDebtInFront = yield getDebtInFrontForSingleMarketLiquityV2(provider, network, isLegacy, (0, markets_1.LiquityV2Markets)(network)[selectedMarket].marketAddress, troveId);
225
- return calculateDebtInFrontLiquityV2(markets, selectedMarket, allMarketsUnbackedDebts, interestRateDebtInFront.toString());
268
+ return (0, exports.calculateDebtInFrontLiquityV2)(markets, selectedMarket, allMarketsUnbackedDebts, interestRateDebtInFront.toString());
226
269
  });
227
270
  /**
228
271
  * @param markets
@@ -237,7 +280,7 @@ const _getDebtInFrontForInterestRateLiquityV2 = (markets_2, selectedMarket_1, pr
237
280
  const allMarketsUnbackedDebts = yield getAllMarketsUnbackedDebts(markets, isLegacy, provider, network);
238
281
  const interestRateDebtInFront = new decimal_js_1.default(yield getDebtInFrontForInterestRateSingleMarketLiquityV2(provider, network, isLegacy, (0, markets_1.LiquityV2Markets)(network)[selectedMarket].marketAddress, interestRate))
239
282
  .sub(debtInFrontBeingMoved);
240
- return calculateDebtInFrontLiquityV2(markets, selectedMarket, allMarketsUnbackedDebts, interestRateDebtInFront.toString());
283
+ return (0, exports.calculateDebtInFrontLiquityV2)(markets, selectedMarket, allMarketsUnbackedDebts, interestRateDebtInFront.toString());
241
284
  });
242
285
  const getDebtInFrontForInterestRateLiquityV2 = (markets_2, selectedMarket_1, provider_1, network_1, isLegacy_1, interestRate_1, ...args_1) => __awaiter(void 0, [markets_2, selectedMarket_1, provider_1, network_1, isLegacy_1, interestRate_1, ...args_1], void 0, function* (markets, selectedMarket, provider, network, isLegacy, interestRate, debtInFrontBeingMoved = '0') { return _getDebtInFrontForInterestRateLiquityV2(markets, selectedMarket, (0, viem_1.getViemProvider)(provider, network), network, isLegacy, interestRate, debtInFrontBeingMoved); });
243
286
  exports.getDebtInFrontForInterestRateLiquityV2 = getDebtInFrontForInterestRateLiquityV2;