@gearbox-protocol/sdk 3.0.0-next.210 → 3.0.0-next.211

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.
@@ -84,6 +84,10 @@ interface LiquidationPriceProps {
84
84
  targetToken: Address;
85
85
  assets: Record<Address, Asset>;
86
86
  }
87
+ export interface TimeToLiquidationProps {
88
+ totalBorrowRate_debt: bigint;
89
+ healthFactor: number;
90
+ }
87
91
  export declare class CreditAccountData {
88
92
  readonly isSuccessful: boolean;
89
93
  readonly priceFeedsNeeded: Address[];
@@ -140,5 +144,10 @@ export declare class CreditAccountData {
140
144
  static calcQuotaBorrowRate({ quotas, quotaRates }: CalcQuotaBorrowRateProps): bigint;
141
145
  static calcRelativeBaseBorrowRate({ debt, baseRateWithFee, assetAmountInUnderlying, }: CalcRelativeBaseBorrowRateProps): bigint;
142
146
  static liquidationPrice({ liquidationThresholds, debt, underlyingToken, targetToken, assets, }: LiquidationPriceProps): bigint;
147
+ /**
148
+ * Calculates the time remaining until liquidation for a credit account.
149
+ * @returns The time remaining until liquidation in milliseconds.
150
+ */
151
+ static getTimeToLiquidation({ healthFactor, totalBorrowRate_debt, }: TimeToLiquidationProps): bigint;
143
152
  }
144
153
  export {};
@@ -399,5 +399,18 @@ class CreditAccountData {
399
399
  return ((effectiveDebt * sdk_gov_1.PRICE_DECIMALS * sdk_gov_1.PERCENTAGE_FACTOR) /
400
400
  (effectiveTargetBalance * lpLT));
401
401
  }
402
+ /**
403
+ * Calculates the time remaining until liquidation for a credit account.
404
+ * @returns The time remaining until liquidation in milliseconds.
405
+ */
406
+ static getTimeToLiquidation({ healthFactor, totalBorrowRate_debt, }) {
407
+ if (healthFactor <= sdk_gov_1.PERCENTAGE_FACTOR || totalBorrowRate_debt === 0n)
408
+ return 0n;
409
+ // (HF - 1) / (br_D / year) or (HF - 1) * (year / br_D)
410
+ const HF_1 = BigInt(healthFactor) - sdk_gov_1.PERCENTAGE_FACTOR;
411
+ const brPerYear = (BigInt(sdk_gov_1.SECONDS_PER_YEAR) * sdk_gov_1.PERCENTAGE_FACTOR * sdk_gov_1.PERCENTAGE_DECIMALS) /
412
+ totalBorrowRate_debt;
413
+ return (HF_1 * brPerYear * 1000n) / sdk_gov_1.PERCENTAGE_FACTOR;
414
+ }
402
415
  }
403
416
  exports.CreditAccountData = CreditAccountData;
@@ -1619,3 +1619,27 @@ describe("CreditAccount calcRelativeBaseBorrowRate test", () => {
1619
1619
  (0, chai_1.expect)(result).to.be.eq(250n);
1620
1620
  });
1621
1621
  });
1622
+ describe("CreditAccount getTimeToLiquidation test", () => {
1623
+ it("should return 0 when HF < 1", () => {
1624
+ const result = creditAccount_1.CreditAccountData.getTimeToLiquidation({
1625
+ healthFactor: 9000,
1626
+ totalBorrowRate_debt: 250n,
1627
+ });
1628
+ (0, chai_1.expect)(result).to.be.eq(0n);
1629
+ });
1630
+ it("should return 0 when br_debt === 0", () => {
1631
+ const result = creditAccount_1.CreditAccountData.getTimeToLiquidation({
1632
+ healthFactor: 9000,
1633
+ totalBorrowRate_debt: 0n,
1634
+ });
1635
+ (0, chai_1.expect)(result).to.be.eq(0n);
1636
+ });
1637
+ it("should calculate time to liquidation correctly", () => {
1638
+ const result = creditAccount_1.CreditAccountData.getTimeToLiquidation({
1639
+ healthFactor: 13750,
1640
+ totalBorrowRate_debt: 20n * 10000n,
1641
+ });
1642
+ // 59_130_000
1643
+ (0, chai_1.expect)(result).to.be.eq(59130000n * 1000n);
1644
+ });
1645
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "3.0.0-next.210",
3
+ "version": "3.0.0-next.211",
4
4
  "description": "Gearbox SDK",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",