@defisaver/positions-sdk 2.1.5 → 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.
@@ -98,6 +98,7 @@ const getChainLinkPricesForTokens = (tokens, network, client) => __awaiter(void
98
98
  const btcPriceChainlink = new decimal_js_1.default(results[1].result).div(1e8).toString();
99
99
  let offset = 2; // wstETH and weETH has 3 calls, while others have only 1, so we need to keep track. First 2 are static calls for eth and btc prices
100
100
  return noDuplicateTokens.reduce((acc, token, i) => {
101
+ var _a;
101
102
  const assetInfo = (0, tokens_1.getAssetInfoByAddress)(token, network);
102
103
  switch (assetInfo.symbol) {
103
104
  case 'USDA':
@@ -142,7 +143,7 @@ const getChainLinkPricesForTokens = (tokens, network, client) => __awaiter(void
142
143
  }
143
144
  default:
144
145
  // @ts-ignore
145
- if (results[i + offset].result[1]) {
146
+ if ((_a = results[i + offset].result) === null || _a === void 0 ? void 0 : _a[1]) {
146
147
  // @ts-ignore
147
148
  acc[token] = new decimal_js_1.default(results[i + offset].result[1].toString()).div(1e8).toString();
148
149
  }
@@ -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;
@@ -92,6 +92,7 @@ const getChainLinkPricesForTokens = (tokens, network, client) => __awaiter(void
92
92
  const btcPriceChainlink = new Dec(results[1].result).div(1e8).toString();
93
93
  let offset = 2; // wstETH and weETH has 3 calls, while others have only 1, so we need to keep track. First 2 are static calls for eth and btc prices
94
94
  return noDuplicateTokens.reduce((acc, token, i) => {
95
+ var _a;
95
96
  const assetInfo = getAssetInfoByAddress(token, network);
96
97
  switch (assetInfo.symbol) {
97
98
  case 'USDA':
@@ -136,7 +137,7 @@ const getChainLinkPricesForTokens = (tokens, network, client) => __awaiter(void
136
137
  }
137
138
  default:
138
139
  // @ts-ignore
139
- if (results[i + offset].result[1]) {
140
+ if ((_a = results[i + offset].result) === null || _a === void 0 ? void 0 : _a[1]) {
140
141
  // @ts-ignore
141
142
  acc[token] = new Dec(results[i + offset].result[1].toString()).div(1e8).toString();
142
143
  }
@@ -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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defisaver/positions-sdk",
3
- "version": "2.1.5",
3
+ "version": "2.1.6-dev-debt-in-front",
4
4
  "description": "",
5
5
  "main": "./cjs/index.js",
6
6
  "module": "./esm/index.js",
@@ -21,7 +21,7 @@
21
21
  "author": "",
22
22
  "license": "ISC",
23
23
  "dependencies": {
24
- "@defisaver/tokens": "^1.7.1",
24
+ "@defisaver/tokens": "^1.7.5",
25
25
  "@types/lodash": "^4.17.15",
26
26
  "@types/memoizee": "^0.4.12",
27
27
  "decimal.js": "^10.6.0",
@@ -195,7 +195,7 @@ const getChainLinkPricesForTokens = async (
195
195
 
196
196
  default:
197
197
  // @ts-ignore
198
- if (results[i + offset].result[1]) {
198
+ if (results[i + offset].result?.[1]) {
199
199
  // @ts-ignore
200
200
  acc[token] = new Dec(results[i + offset].result[1]!.toString() as string).div(1e8).toString();
201
201
  } else if (results[i + offset].result) {
@@ -264,23 +264,69 @@ const getAllMarketsUnbackedDebts = async (markets: Record<LiquityV2Versions, Liq
264
264
  return Object.fromEntries(allMarketsUnbackedDebt) as Record<LiquityV2Versions, string>;
265
265
  };
266
266
 
267
- const calculateDebtInFrontLiquityV2 = (markets: Record<LiquityV2Versions, LiquityV2MarketData>, selectedMarket: LiquityV2Versions, allMarketsUnbackedDebts: Record<LiquityV2Versions, string>, interestRateDebtInFront: string): string => {
267
+ export const calculateDebtInFrontLiquityV2 = (markets: Record<LiquityV2Versions, LiquityV2MarketData>, selectedMarket: LiquityV2Versions, allMarketsUnbackedDebts: Record<LiquityV2Versions, string>, interestRateDebtInFront: string): string => {
268
+ // Sanity check to avoid division by 0. Very unlikely to ever happen.
269
+ const selectedMarketTotalBorrow = new Dec(markets[selectedMarket].assetsData[LiquityV2Markets(NetworkNumber.Eth)[selectedMarket].debtToken].totalBorrow);
270
+ if (selectedMarketTotalBorrow.eq(0)) return new Dec(0).toString();
271
+
268
272
  const selectedMarketUnbackedDebt = new Dec(allMarketsUnbackedDebts[selectedMarket]);
269
273
  const { isLegacy } = LiquityV2Markets(NetworkNumber.Eth)[selectedMarket];
270
- if (selectedMarketUnbackedDebt.eq(0)) return interestRateDebtInFront;
274
+ const totalUnbackedDebt = Object.values(allMarketsUnbackedDebts).reduce((acc, val) => acc.plus(new Dec(val)), new Dec(0));
275
+
276
+ // When totalUnbackedDebt is 0, redemptions will be proportional with the branch size and not to unbacked debt.
277
+ // 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:
278
+ // - 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
279
+ // - Perform split the same way as we would do when totalUnbackedDebt == 0, this would represent the second call to the redemption function
280
+ if (selectedMarketUnbackedDebt.eq(0)) {
281
+ // Special case if the branch debt in front is 0, it means that all debt in front is unbacked debt from other branches.
282
+ if (new Dec(interestRateDebtInFront).eq(0)) return totalUnbackedDebt.toString();
283
+
284
+ // Then calculate how much of that estimated amount would go to each branch
285
+ // Second redemption call - calculate proportional redemption based on updated total debt
286
+ const amountBeingRedeemedOnEachMarketByTotalBorrow = Object.entries(markets).map(([version, market]) => {
287
+ const { isLegacy: isLegacyMarket } = LiquityV2Markets(NetworkNumber.Eth)[version as LiquityV2Versions];
288
+ if (version === selectedMarket && isLegacyMarket !== isLegacy) return new Dec(interestRateDebtInFront);
289
+ const { assetsData } = market;
290
+ const { debtToken } = LiquityV2Markets(NetworkNumber.Eth)[version as LiquityV2Versions];
291
+ // For other markets, subtract their unbacked debt as it will be cleared in first redemption call
292
+ const marketUnbackedDebt = new Dec(allMarketsUnbackedDebts[version as LiquityV2Versions]);
293
+ const totalBorrow = new Dec(assetsData[debtToken].totalBorrow).sub(marketUnbackedDebt);
294
+ const amountToRedeem = new Dec(interestRateDebtInFront).mul(totalBorrow).div(selectedMarketTotalBorrow);
295
+ return Dec.min(amountToRedeem, totalBorrow);
296
+ });
271
297
 
272
- const amountBeingReedemedOnEachMarket = Object.entries(markets).map(([version, market]) => {
298
+ const redemptionAmount = amountBeingRedeemedOnEachMarketByTotalBorrow.reduce((acc, val) => acc.plus(val), new Dec(0));
299
+ return totalUnbackedDebt.plus(redemptionAmount).toString();
300
+ }
301
+
302
+ const amountBeingRedeemedOnEachMarketByUnbackedDebt = Object.entries(markets).map(([version, market]) => {
273
303
  const { isLegacy: isLegacyMarket } = LiquityV2Markets(NetworkNumber.Eth)[version as LiquityV2Versions];
274
304
  if (version === selectedMarket && isLegacyMarket !== isLegacy) return new Dec(interestRateDebtInFront);
275
305
  const { assetsData } = market;
276
306
  const { debtToken } = LiquityV2Markets(NetworkNumber.Eth)[version as LiquityV2Versions];
277
307
  const unbackedDebt = new Dec(allMarketsUnbackedDebts[version as LiquityV2Versions]);
278
308
  const totalBorrow = new Dec(assetsData[debtToken].totalBorrow);
279
- const amountToReedem = new Dec(interestRateDebtInFront).mul(unbackedDebt).div(selectedMarketUnbackedDebt);
280
- return Dec.min(amountToReedem, totalBorrow);
309
+ const amountToRedeem = new Dec(interestRateDebtInFront).mul(unbackedDebt).div(selectedMarketUnbackedDebt);
310
+ return Dec.min(amountToRedeem, totalBorrow);
281
311
  });
282
312
 
283
- return amountBeingReedemedOnEachMarket.reduce((acc, val) => acc.plus(val), new Dec(0)).toString();
313
+ return amountBeingRedeemedOnEachMarketByUnbackedDebt.reduce((acc, val) => acc.plus(val), new Dec(0)).toString();
314
+ };
315
+
316
+ // @dev The amount redeemed on each branch depends on the unbacked debt of every branch (the difference between total borrow and stability pool deposits).
317
+ // When new debt is generated on the selected market, the unbacked debt will increase, resulting in a higher redemption amount on that branch.
318
+ // 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.
319
+ export const getDebtInFrontForInterestRateIncludingNewDebtLiquityV2 = async (newDebt: string, markets: Record<LiquityV2Versions, LiquityV2MarketData>, selectedMarket: LiquityV2Versions, provider: Client, network: NetworkNumber, interestRate: string) => {
320
+ const marketsWithNewDebt = structuredClone(markets);
321
+ const selectedMarketDebtToken = LiquityV2Markets(network)[selectedMarket].debtToken;
322
+ const currentTotalBorrow = new Dec(marketsWithNewDebt[selectedMarket].assetsData[selectedMarketDebtToken].totalBorrow);
323
+ marketsWithNewDebt[selectedMarket].assetsData[selectedMarketDebtToken].totalBorrow = currentTotalBorrow.add(newDebt).toString();
324
+
325
+ const { isLegacy } = LiquityV2Markets(NetworkNumber.Eth)[selectedMarket];
326
+ const allMarketsUnbackedDebts = await getAllMarketsUnbackedDebts(marketsWithNewDebt, isLegacy, provider, network);
327
+ const interestRateDebtInFront = new Dec(await getDebtInFrontForInterestRateSingleMarketLiquityV2(provider, network, isLegacy, LiquityV2Markets(network)[selectedMarket].marketAddress, interestRate));
328
+
329
+ return calculateDebtInFrontLiquityV2(marketsWithNewDebt, selectedMarket, allMarketsUnbackedDebts, interestRateDebtInFront.toString());
284
330
  };
285
331
 
286
332
  const getDebtInFrontLiquityV2 = async (markets: Record<LiquityV2Versions, LiquityV2MarketData>, selectedMarket: LiquityV2Versions, provider: Client, network: NetworkNumber, viewContract: any, troveId: string) => {