@defisaver/positions-sdk 2.1.13-dev-plasma-fluid6 → 2.1.13-dev-plasma-fluid8
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.
- package/cjs/fluid/index.js +9 -3
- package/cjs/liquityV2/index.d.ts +2 -0
- package/cjs/liquityV2/index.js +52 -9
- package/cjs/moneymarket/moneymarketCommonService.js +5 -1
- package/esm/fluid/index.js +9 -3
- package/esm/liquityV2/index.d.ts +2 -0
- package/esm/liquityV2/index.js +48 -7
- package/esm/moneymarket/moneymarketCommonService.js +5 -1
- package/package.json +2 -2
- package/src/fluid/index.ts +6 -3
- package/src/liquityV2/index.ts +52 -6
- package/src/moneymarket/moneymarketCommonService.ts +5 -1
- package/src/staking/staking.ts +4 -1
package/cjs/fluid/index.js
CHANGED
|
@@ -119,7 +119,9 @@ const getChainLinkPricesForTokens = (tokens, network, client) => __awaiter(void
|
|
|
119
119
|
break;
|
|
120
120
|
}
|
|
121
121
|
case 'wrsETH': {
|
|
122
|
-
const { ethPrice, wrsETHRate, } = (0, priceService_1.parseWrsETHPriceCalls)(
|
|
122
|
+
const { ethPrice, wrsETHRate, } = (0, priceService_1.parseWrsETHPriceCalls)(
|
|
123
|
+
// @ts-ignore
|
|
124
|
+
results[i + offset].result[1].toString(),
|
|
123
125
|
// @ts-ignore
|
|
124
126
|
results[i + offset + 1].result[1].toString());
|
|
125
127
|
offset += 1;
|
|
@@ -127,7 +129,9 @@ const getChainLinkPricesForTokens = (tokens, network, client) => __awaiter(void
|
|
|
127
129
|
break;
|
|
128
130
|
}
|
|
129
131
|
case 'syrupUSDT': {
|
|
130
|
-
const { syrupUSDTRate, USDTRate, } = (0, priceService_1.parseSyrupUSDTPriceCalls)(
|
|
132
|
+
const { syrupUSDTRate, USDTRate, } = (0, priceService_1.parseSyrupUSDTPriceCalls)(
|
|
133
|
+
// @ts-ignore
|
|
134
|
+
results[i + offset].result[1].toString(),
|
|
131
135
|
// @ts-ignore
|
|
132
136
|
results[i + offset + 1].result[1].toString());
|
|
133
137
|
offset += 1;
|
|
@@ -135,7 +139,9 @@ const getChainLinkPricesForTokens = (tokens, network, client) => __awaiter(void
|
|
|
135
139
|
break;
|
|
136
140
|
}
|
|
137
141
|
case 'wstUSR': {
|
|
138
|
-
const { wstUSRRate, USRRate, } = (0, priceService_1.parseWstUSRPriceCalls)(
|
|
142
|
+
const { wstUSRRate, USRRate, } = (0, priceService_1.parseWstUSRPriceCalls)(
|
|
143
|
+
// @ts-ignore
|
|
144
|
+
results[i + offset].result[1].toString(),
|
|
139
145
|
// @ts-ignore
|
|
140
146
|
results[i + offset + 1].result[1].toString());
|
|
141
147
|
offset += 1;
|
package/cjs/liquityV2/index.d.ts
CHANGED
|
@@ -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;
|
package/cjs/liquityV2/index.js
CHANGED
|
@@ -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
|
-
|
|
207
|
-
|
|
208
|
-
|
|
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
|
|
217
|
-
return decimal_js_1.default.min(
|
|
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
|
|
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;
|
|
@@ -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 = [
|
|
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;
|
package/esm/fluid/index.js
CHANGED
|
@@ -113,7 +113,9 @@ const getChainLinkPricesForTokens = (tokens, network, client) => __awaiter(void
|
|
|
113
113
|
break;
|
|
114
114
|
}
|
|
115
115
|
case 'wrsETH': {
|
|
116
|
-
const { ethPrice, wrsETHRate, } = parseWrsETHPriceCalls(
|
|
116
|
+
const { ethPrice, wrsETHRate, } = parseWrsETHPriceCalls(
|
|
117
|
+
// @ts-ignore
|
|
118
|
+
results[i + offset].result[1].toString(),
|
|
117
119
|
// @ts-ignore
|
|
118
120
|
results[i + offset + 1].result[1].toString());
|
|
119
121
|
offset += 1;
|
|
@@ -121,7 +123,9 @@ const getChainLinkPricesForTokens = (tokens, network, client) => __awaiter(void
|
|
|
121
123
|
break;
|
|
122
124
|
}
|
|
123
125
|
case 'syrupUSDT': {
|
|
124
|
-
const { syrupUSDTRate, USDTRate, } = parseSyrupUSDTPriceCalls(
|
|
126
|
+
const { syrupUSDTRate, USDTRate, } = parseSyrupUSDTPriceCalls(
|
|
127
|
+
// @ts-ignore
|
|
128
|
+
results[i + offset].result[1].toString(),
|
|
125
129
|
// @ts-ignore
|
|
126
130
|
results[i + offset + 1].result[1].toString());
|
|
127
131
|
offset += 1;
|
|
@@ -129,7 +133,9 @@ const getChainLinkPricesForTokens = (tokens, network, client) => __awaiter(void
|
|
|
129
133
|
break;
|
|
130
134
|
}
|
|
131
135
|
case 'wstUSR': {
|
|
132
|
-
const { wstUSRRate, USRRate, } = parseWstUSRPriceCalls(
|
|
136
|
+
const { wstUSRRate, USRRate, } = parseWstUSRPriceCalls(
|
|
137
|
+
// @ts-ignore
|
|
138
|
+
results[i + offset].result[1].toString(),
|
|
133
139
|
// @ts-ignore
|
|
134
140
|
results[i + offset + 1].result[1].toString());
|
|
135
141
|
offset += 1;
|
package/esm/liquityV2/index.d.ts
CHANGED
|
@@ -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;
|
package/esm/liquityV2/index.js
CHANGED
|
@@ -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
|
-
|
|
197
|
-
|
|
198
|
-
|
|
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
|
|
207
|
-
return Dec.min(
|
|
234
|
+
const amountToRedeem = new Dec(interestRateDebtInFront).mul(unbackedDebt).div(selectedMarketUnbackedDebt);
|
|
235
|
+
return Dec.min(amountToRedeem, totalBorrow);
|
|
208
236
|
});
|
|
209
|
-
return
|
|
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);
|
|
@@ -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 = [
|
|
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;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@defisaver/positions-sdk",
|
|
3
|
-
"version": "2.1.13-dev-plasma-
|
|
3
|
+
"version": "2.1.13-dev-plasma-fluid8",
|
|
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.
|
|
24
|
+
"@defisaver/tokens": "^1.7.9",
|
|
25
25
|
"@types/lodash": "^4.17.15",
|
|
26
26
|
"@types/memoizee": "^0.4.12",
|
|
27
27
|
"decimal.js": "^10.6.0",
|
package/src/fluid/index.ts
CHANGED
|
@@ -182,7 +182,8 @@ const getChainLinkPricesForTokens = async (
|
|
|
182
182
|
ethPrice,
|
|
183
183
|
wrsETHRate,
|
|
184
184
|
} = parseWrsETHPriceCalls(
|
|
185
|
-
|
|
185
|
+
// @ts-ignore
|
|
186
|
+
results[i + offset].result[1]!.toString(),
|
|
186
187
|
// @ts-ignore
|
|
187
188
|
results[i + offset + 1].result[1]!.toString(),
|
|
188
189
|
);
|
|
@@ -196,7 +197,8 @@ const getChainLinkPricesForTokens = async (
|
|
|
196
197
|
syrupUSDTRate,
|
|
197
198
|
USDTRate,
|
|
198
199
|
} = parseSyrupUSDTPriceCalls(
|
|
199
|
-
|
|
200
|
+
// @ts-ignore
|
|
201
|
+
results[i + offset].result[1]!.toString(),
|
|
200
202
|
// @ts-ignore
|
|
201
203
|
results[i + offset + 1].result[1]!.toString(),
|
|
202
204
|
);
|
|
@@ -209,7 +211,8 @@ const getChainLinkPricesForTokens = async (
|
|
|
209
211
|
wstUSRRate,
|
|
210
212
|
USRRate,
|
|
211
213
|
} = parseWstUSRPriceCalls(
|
|
212
|
-
|
|
214
|
+
// @ts-ignore
|
|
215
|
+
results[i + offset].result[1]!.toString(),
|
|
213
216
|
// @ts-ignore
|
|
214
217
|
results[i + offset + 1].result[1]!.toString(),
|
|
215
218
|
);
|
package/src/liquityV2/index.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
|
280
|
-
return Dec.min(
|
|
309
|
+
const amountToRedeem = new Dec(interestRateDebtInFront).mul(unbackedDebt).div(selectedMarketUnbackedDebt);
|
|
310
|
+
return Dec.min(amountToRedeem, totalBorrow);
|
|
281
311
|
});
|
|
282
312
|
|
|
283
|
-
return
|
|
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) => {
|
|
@@ -20,7 +20,11 @@ export const calcLeverageLiqPrice = (leverageType: string, assetPrice: string, b
|
|
|
20
20
|
|
|
21
21
|
export const calculateBorrowingAssetLimit = (assetBorrowedUsd: string, borrowLimitUsd: string) => new Dec(assetBorrowedUsd).div(borrowLimitUsd).times(100).toString();
|
|
22
22
|
|
|
23
|
-
export const STABLE_ASSETS = [
|
|
23
|
+
export const STABLE_ASSETS = [
|
|
24
|
+
'DAI', 'USDC', 'USDT', 'TUSD', 'USDP', 'GUSD', 'BUSD', 'SUSD', 'FRAX', 'LUSD', 'USDC.e', 'GHO', 'sDAI', 'USDA',
|
|
25
|
+
'USDe', 'sUSDe', 'USDS', 'sUSDS', 'USR', 'EURC', 'BOLD', 'BOLD Legacy', 'RLUSD', 'PT sUSDe July', 'PT eUSDe May',
|
|
26
|
+
'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',
|
|
27
|
+
];
|
|
24
28
|
|
|
25
29
|
export const isLeveragedPos = (usedAssets: MMUsedAssets, dustLimit = 5) => {
|
|
26
30
|
let borrowUnstable = 0;
|
package/src/staking/staking.ts
CHANGED
|
@@ -60,7 +60,10 @@ const getApyFromDfsApi = async (asset: string, network: number = NetworkNumber.E
|
|
|
60
60
|
}
|
|
61
61
|
};
|
|
62
62
|
|
|
63
|
-
export const STAKING_ASSETS = [
|
|
63
|
+
export const STAKING_ASSETS = [
|
|
64
|
+
'cbETH', 'wstETH', 'cbETH', 'rETH', 'sDAI', 'weETH', 'sUSDe', 'osETH', 'ezETH', 'ETHx', 'rsETH', 'pufETH', 'wrsETH',
|
|
65
|
+
'wsuperOETHb', 'sUSDS', 'tETH', 'PT sUSDe Sep', 'PT USDe Sep', 'PT sUSDe Nov', 'PT USDe Nov', 'PT USDe Jan', 'PT sUSDe Jan',
|
|
66
|
+
'wrsETH', 'wstETH', 'syrupUSDT'];
|
|
64
67
|
|
|
65
68
|
export const getStakingApy = memoize(async (asset: string, network: number = NetworkNumber.Eth) => {
|
|
66
69
|
try {
|