@defisaver/positions-sdk 2.1.15 → 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;
@@ -18,7 +18,7 @@ exports.aaveV3AssetsDefaultMarketOpt = [
18
18
  exports.aaveV3AssetsDefaultMarketArb = ['DAI', 'LINK', 'USDC.e', 'WBTC', 'ETH', 'USDT', 'AAVE', 'EURS', 'wstETH', 'MAI', 'rETH', 'LUSD', 'USDC', 'FRAX', 'ARB', 'weETH', 'GHO', 'ezETH', 'rsETH', 'tBTC'];
19
19
  exports.aaveV3AssetsDefaultMarketBase = ['ETH', 'cbETH', 'USDbC', 'wstETH', 'USDC', 'weETH', 'cbBTC', 'ezETH', 'GHO', 'wrsETH', 'LBTC', 'EURC', 'AAVE', 'tBTC'];
20
20
  exports.aaveV3AssetsDefaultMarketLinea = ['ETH', 'USDC', 'weETH', 'ezETH', 'USDT', 'wstETH', 'wrsETH', 'WBTC'];
21
- exports.aaveV3AssetsDefaultMarketPlasma = ['ETH', 'USDT', 'sUSDe', 'USDe', 'weETH', 'XAUt', 'PT USDe Jan', 'PT sUSDe Jan', 'wrsETH', 'wstETH'];
21
+ exports.aaveV3AssetsDefaultMarketPlasma = ['ETH', 'USDT', 'sUSDe', 'USDe', 'weETH', 'XAUt', 'PT USDe Jan', 'PT sUSDe Jan', 'wrsETH', 'wstETH', 'syrupUSDT'];
22
22
  // @dev Keep assets in array, do not assign directly, so we can parse it and edit it programmatically with `scripts/updateMarkets`
23
23
  exports.aaveV3AssetsDefaultMarket = {
24
24
  [common_1.NetworkNumber.Eth]: exports.aaveV3AssetsDefaultMarketEth,
@@ -27,7 +27,11 @@ const calcLeverageLiqPrice = (leverageType, assetPrice, borrowedUsd, borrowLimit
27
27
  exports.calcLeverageLiqPrice = calcLeverageLiqPrice;
28
28
  const calculateBorrowingAssetLimit = (assetBorrowedUsd, borrowLimitUsd) => new decimal_js_1.default(assetBorrowedUsd).div(borrowLimitUsd).times(100).toString();
29
29
  exports.calculateBorrowingAssetLimit = calculateBorrowingAssetLimit;
30
- exports.STABLE_ASSETS = ['DAI', 'USDC', 'USDT', 'TUSD', 'USDP', 'GUSD', 'BUSD', 'SUSD', 'FRAX', 'LUSD', 'USDC.e', 'GHO', 'sDAI', 'crvUSD', 'BOLD'];
30
+ exports.STABLE_ASSETS = [
31
+ 'DAI', 'USDC', 'USDT', 'TUSD', 'USDP', 'GUSD', 'BUSD', 'SUSD', 'FRAX', 'LUSD', 'USDC.e', 'GHO', 'sDAI', 'USDA',
32
+ 'USDe', 'sUSDe', 'USDS', 'sUSDS', 'USR', 'EURC', 'BOLD', 'BOLD Legacy', 'RLUSD', 'PT sUSDe July', 'PT eUSDe May',
33
+ 'USDtb', 'eUSDe', 'PT USDe July', 'PT eUSDe Aug', 'PT sUSDe Sep', 'PT USDe Sep', 'PT sUSDe Nov', 'PT USDe Nov', 'PT sUSDe Jan', 'PT USDe Jan',
34
+ ];
31
35
  const isLeveragedPos = (usedAssets, dustLimit = 5) => {
32
36
  let borrowUnstable = 0;
33
37
  let supplyStable = 0;
@@ -57,7 +57,7 @@ const getSuperOETHApy = () => __awaiter(void 0, void 0, void 0, function* () {
57
57
  const getApyFromDfsApi = (asset_1, ...args_1) => __awaiter(void 0, [asset_1, ...args_1], void 0, function* (asset, network = common_1.NetworkNumber.Eth) {
58
58
  var _a;
59
59
  try {
60
- const res = yield fetch(`http://localhost:8888/api/staking/apy?asset=${asset}&network=${network}`, { signal: AbortSignal.timeout(utils_1.DEFAULT_TIMEOUT) });
60
+ const res = yield fetch(`https://fe.defisaver.com/api/staking/apy?asset=${asset}&network=${network}`, { signal: AbortSignal.timeout(utils_1.DEFAULT_TIMEOUT) });
61
61
  if (!res.ok)
62
62
  throw new Error(`Failed to fetch APY for ${asset}`);
63
63
  const data = yield res.json();
@@ -68,7 +68,7 @@ const getApyFromDfsApi = (asset_1, ...args_1) => __awaiter(void 0, [asset_1, ...
68
68
  return '0';
69
69
  }
70
70
  });
71
- exports.STAKING_ASSETS = ['cbETH', 'wstETH', 'cbETH', 'rETH', 'sDAI', 'weETH', 'sUSDe', 'osETH', 'ezETH', 'ETHx', 'rsETH', 'pufETH', 'wrsETH', 'wsuperOETHb', 'sUSDS', 'tETH', 'PT sUSDe Sep', 'PT USDe Sep', 'PT sUSDe Nov', 'PT USDe Nov', 'PT USDe Jan', 'PT sUSDe Jan', 'wrsETH', 'wstETH'];
71
+ exports.STAKING_ASSETS = ['cbETH', 'wstETH', 'cbETH', 'rETH', 'sDAI', 'weETH', 'sUSDe', 'osETH', 'ezETH', 'ETHx', 'rsETH', 'pufETH', 'wrsETH', 'wsuperOETHb', 'sUSDS', 'tETH', 'PT sUSDe Sep', 'PT USDe Sep', 'PT sUSDe Nov', 'PT USDe Nov', 'PT USDe Jan', 'PT sUSDe Jan', 'wrsETH', 'wstETH', 'syrupUSDT'];
72
72
  exports.getStakingApy = (0, memoizee_1.default)((asset_1, ...args_1) => __awaiter(void 0, [asset_1, ...args_1], void 0, function* (asset, network = common_1.NetworkNumber.Eth) {
73
73
  try {
74
74
  if (asset === 'stETH' || asset === 'wstETH')
@@ -121,6 +121,8 @@ exports.getStakingApy = (0, memoizee_1.default)((asset_1, ...args_1) => __awaite
121
121
  return yield getApyFromDfsApi('PT USDe Jan', network);
122
122
  if (asset === 'PT sUSDe Jan')
123
123
  return yield getApyFromDfsApi('PT sUSDe Jan', network);
124
+ if (asset === 'syrupUSDT')
125
+ return yield getApyFromDfsApi('syrupUSDT');
124
126
  }
125
127
  catch (e) {
126
128
  console.error(`Failed to fetch APY for ${asset}`);
@@ -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);
@@ -15,7 +15,7 @@ export const aaveV3AssetsDefaultMarketOpt = [
15
15
  export const aaveV3AssetsDefaultMarketArb = ['DAI', 'LINK', 'USDC.e', 'WBTC', 'ETH', 'USDT', 'AAVE', 'EURS', 'wstETH', 'MAI', 'rETH', 'LUSD', 'USDC', 'FRAX', 'ARB', 'weETH', 'GHO', 'ezETH', 'rsETH', 'tBTC'];
16
16
  export const aaveV3AssetsDefaultMarketBase = ['ETH', 'cbETH', 'USDbC', 'wstETH', 'USDC', 'weETH', 'cbBTC', 'ezETH', 'GHO', 'wrsETH', 'LBTC', 'EURC', 'AAVE', 'tBTC'];
17
17
  export const aaveV3AssetsDefaultMarketLinea = ['ETH', 'USDC', 'weETH', 'ezETH', 'USDT', 'wstETH', 'wrsETH', 'WBTC'];
18
- export const aaveV3AssetsDefaultMarketPlasma = ['ETH', 'USDT', 'sUSDe', 'USDe', 'weETH', 'XAUt', 'PT USDe Jan', 'PT sUSDe Jan', 'wrsETH', 'wstETH'];
18
+ export const aaveV3AssetsDefaultMarketPlasma = ['ETH', 'USDT', 'sUSDe', 'USDe', 'weETH', 'XAUt', 'PT USDe Jan', 'PT sUSDe Jan', 'wrsETH', 'wstETH', 'syrupUSDT'];
19
19
  // @dev Keep assets in array, do not assign directly, so we can parse it and edit it programmatically with `scripts/updateMarkets`
20
20
  export const aaveV3AssetsDefaultMarket = {
21
21
  [NetworkNumber.Eth]: aaveV3AssetsDefaultMarketEth,
@@ -16,7 +16,11 @@ export const calcLeverageLiqPrice = (leverageType, assetPrice, borrowedUsd, borr
16
16
  return '0';
17
17
  };
18
18
  export const calculateBorrowingAssetLimit = (assetBorrowedUsd, borrowLimitUsd) => new Dec(assetBorrowedUsd).div(borrowLimitUsd).times(100).toString();
19
- export const STABLE_ASSETS = ['DAI', 'USDC', 'USDT', 'TUSD', 'USDP', 'GUSD', 'BUSD', 'SUSD', 'FRAX', 'LUSD', 'USDC.e', 'GHO', 'sDAI', 'crvUSD', 'BOLD'];
19
+ export const STABLE_ASSETS = [
20
+ 'DAI', 'USDC', 'USDT', 'TUSD', 'USDP', 'GUSD', 'BUSD', 'SUSD', 'FRAX', 'LUSD', 'USDC.e', 'GHO', 'sDAI', 'USDA',
21
+ 'USDe', 'sUSDe', 'USDS', 'sUSDS', 'USR', 'EURC', 'BOLD', 'BOLD Legacy', 'RLUSD', 'PT sUSDe July', 'PT eUSDe May',
22
+ 'USDtb', 'eUSDe', 'PT USDe July', 'PT eUSDe Aug', 'PT sUSDe Sep', 'PT USDe Sep', 'PT sUSDe Nov', 'PT USDe Nov', 'PT sUSDe Jan', 'PT USDe Jan',
23
+ ];
20
24
  export const isLeveragedPos = (usedAssets, dustLimit = 5) => {
21
25
  let borrowUnstable = 0;
22
26
  let supplyStable = 0;
@@ -51,7 +51,7 @@ const getSuperOETHApy = () => __awaiter(void 0, void 0, void 0, function* () {
51
51
  const getApyFromDfsApi = (asset_1, ...args_1) => __awaiter(void 0, [asset_1, ...args_1], void 0, function* (asset, network = NetworkNumber.Eth) {
52
52
  var _a;
53
53
  try {
54
- const res = yield fetch(`http://localhost:8888/api/staking/apy?asset=${asset}&network=${network}`, { signal: AbortSignal.timeout(DEFAULT_TIMEOUT) });
54
+ const res = yield fetch(`https://fe.defisaver.com/api/staking/apy?asset=${asset}&network=${network}`, { signal: AbortSignal.timeout(DEFAULT_TIMEOUT) });
55
55
  if (!res.ok)
56
56
  throw new Error(`Failed to fetch APY for ${asset}`);
57
57
  const data = yield res.json();
@@ -62,7 +62,7 @@ const getApyFromDfsApi = (asset_1, ...args_1) => __awaiter(void 0, [asset_1, ...
62
62
  return '0';
63
63
  }
64
64
  });
65
- export const STAKING_ASSETS = ['cbETH', 'wstETH', 'cbETH', 'rETH', 'sDAI', 'weETH', 'sUSDe', 'osETH', 'ezETH', 'ETHx', 'rsETH', 'pufETH', 'wrsETH', 'wsuperOETHb', 'sUSDS', 'tETH', 'PT sUSDe Sep', 'PT USDe Sep', 'PT sUSDe Nov', 'PT USDe Nov', 'PT USDe Jan', 'PT sUSDe Jan', 'wrsETH', 'wstETH'];
65
+ export const STAKING_ASSETS = ['cbETH', 'wstETH', 'cbETH', 'rETH', 'sDAI', 'weETH', 'sUSDe', 'osETH', 'ezETH', 'ETHx', 'rsETH', 'pufETH', 'wrsETH', 'wsuperOETHb', 'sUSDS', 'tETH', 'PT sUSDe Sep', 'PT USDe Sep', 'PT sUSDe Nov', 'PT USDe Nov', 'PT USDe Jan', 'PT sUSDe Jan', 'wrsETH', 'wstETH', 'syrupUSDT'];
66
66
  export const getStakingApy = memoize((asset_1, ...args_1) => __awaiter(void 0, [asset_1, ...args_1], void 0, function* (asset, network = NetworkNumber.Eth) {
67
67
  try {
68
68
  if (asset === 'stETH' || asset === 'wstETH')
@@ -115,6 +115,8 @@ export const getStakingApy = memoize((asset_1, ...args_1) => __awaiter(void 0, [
115
115
  return yield getApyFromDfsApi('PT USDe Jan', network);
116
116
  if (asset === 'PT sUSDe Jan')
117
117
  return yield getApyFromDfsApi('PT sUSDe Jan', network);
118
+ if (asset === 'syrupUSDT')
119
+ return yield getApyFromDfsApi('syrupUSDT');
118
120
  }
119
121
  catch (e) {
120
122
  console.error(`Failed to fetch APY for ${asset}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defisaver/positions-sdk",
3
- "version": "2.1.15",
3
+ "version": "2.1.16",
4
4
  "description": "",
5
5
  "main": "./cjs/index.js",
6
6
  "module": "./esm/index.js",