Skip to content

Commit f30b78b

Browse files
committed
feat: add dev subpath
1 parent df23351 commit f30b78b

File tree

108 files changed

+205
-12
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+205
-12
lines changed

package.json

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,22 @@
88
"./package.json": "./package.json",
99
".": {
1010
"import": {
11-
"types": "./dist/esm/index.d.mts",
12-
"default": "./dist/esm/index.mjs"
11+
"types": "./dist/esm/sdk/index.d.mts",
12+
"default": "./dist/esm/sdk/index.mjs"
1313
},
1414
"require": {
15-
"types": "./dist/cjs/index.d.ts",
16-
"default": "./dist/cjs/index.cjs"
15+
"types": "./dist/cjs/sdk/index.d.ts",
16+
"default": "./dist/cjs/sdk/index.cjs"
17+
}
18+
},
19+
"./dev": {
20+
"import": {
21+
"types": "./dist/esm/dev/index.d.mts",
22+
"default": "./dist/esm/dev/index.mjs"
23+
},
24+
"require": {
25+
"types": "./dist/cjs/dev/index.d.ts",
26+
"default": "./dist/cjs/dev/index.cjs"
1727
}
1828
}
1929
},

scripts/example.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import { writeFile } from "node:fs/promises";
22

33
import { pino } from "pino";
44

5-
import { GearboxSDK, json_stringify } from "../src";
5+
import { GearboxSDK, json_stringify } from "../src/sdk";
66

77
async function example(): Promise<void> {
88
const logger = pino({
99
level: process.env.LOG_LEVEL ?? "debug",
1010
});
1111

1212
const sdk = await GearboxSDK.attach({
13-
rpcURL: "http://127.0.0.1:8545",
13+
rpcURLs: ["http://127.0.0.1:8545"],
1414
timeout: 480_000,
1515
addressProvider: "0x81ED8e0325B17A266B2aF225570679cfd635d0bb",
1616
logger,

src/constants/math.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/dev/calcLiquidatableLTs.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import type { Address } from "viem";
2+
3+
import type { CreditAccountData, GearboxSDK } from "../sdk";
4+
import { PERCENTAGE_FACTOR, WAD } from "../sdk";
5+
6+
/**
7+
* Given credit accounts, calculates new liquidation thresholds that needs to be set to drop account health factor a bit to make it eligible for partial liquidation
8+
* @param ca
9+
*/
10+
export async function calcLiquidatableLTs(
11+
sdk: GearboxSDK,
12+
ca: CreditAccountData,
13+
factor = 9990n,
14+
): Promise<Record<Address, [ltOld: number, ltNew: number]>> {
15+
const cm = sdk.marketRegister.findCreditManager(ca.creditManager);
16+
const market = sdk.marketRegister.findByCreditManager(ca.creditManager);
17+
18+
// Sort account tokens by weighted value descending, but underlying token comes last
19+
const weightedBalances = ca.tokens
20+
.map(t => {
21+
const { token, balance } = t;
22+
const balanceU = market.priceOracle.convertToUnderlying(token, balance);
23+
const lt = BigInt(cm.collateralTokens[token]);
24+
return {
25+
token,
26+
weightedBalance: (balanceU * lt) / PERCENTAGE_FACTOR,
27+
lt,
28+
};
29+
})
30+
.sort((a, b) => {
31+
if (a.token === ca.underlying) return 1;
32+
if (b.token === ca.underlying) return -1;
33+
return b.weightedBalance > a.weightedBalance ? 1 : -1;
34+
});
35+
36+
// LTnew = LT * k, where
37+
//
38+
// totalDebt - B_underlying * LT_underlying
39+
// k = -------------------------------------------------------------
40+
// sum(p * b* LT)
41+
let divisor = 0n;
42+
let dividend =
43+
(factor * (ca.debt + ca.accruedInterest + ca.accruedFees)) /
44+
PERCENTAGE_FACTOR; // TODO: USDT fee
45+
for (const { token, weightedBalance } of weightedBalances) {
46+
if (token === ca.underlying) {
47+
dividend -= weightedBalance;
48+
} else {
49+
divisor += weightedBalance;
50+
}
51+
}
52+
if (divisor === 0n) {
53+
throw new Error("warning: assets have zero weighted value in underlying");
54+
}
55+
if (dividend <= 0n) {
56+
throw new Error(`warning: account balance in underlying covers debt`);
57+
}
58+
const k = (WAD * dividend) / divisor;
59+
60+
const result: Record<Address, [number, number]> = {};
61+
for (const { token, lt: oldLT } of weightedBalances) {
62+
if (token !== ca.underlying) {
63+
const newLT = (oldLT * k) / WAD;
64+
result[token] = [Number(oldLT), Number(newLT)];
65+
}
66+
}
67+
return result;
68+
}

src/dev/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./calcLiquidatableLTs";
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)