@quartz-labs/sdk 0.0.1
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/LICENSE +21 -0
- package/README.md +17 -0
- package/package.json +44 -0
- package/src/client.ts +117 -0
- package/src/config/constants.ts +10 -0
- package/src/helpers.ts +33 -0
- package/src/idl/quartz.json +646 -0
- package/src/index.ts +4 -0
- package/src/model/driftUser.ts +1056 -0
- package/src/types/quartz.ts +1293 -0
- package/src/user.ts +85 -0
- package/tsconfig.json +17 -0
package/src/user.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { PublicKey } from "@solana/web3.js";
|
|
2
|
+
import { DriftUser } from "./model/driftUser";
|
|
3
|
+
import { DriftClient, UserAccount } from "@drift-labs/sdk";
|
|
4
|
+
import { getVaultPubkey } from "./helpers";
|
|
5
|
+
import { Connection } from "@solana/web3.js";
|
|
6
|
+
import { QUARTZ_HEALTH_BUFFER } from "./config/constants";
|
|
7
|
+
|
|
8
|
+
export class QuartzUser {
|
|
9
|
+
private pubkey: PublicKey;
|
|
10
|
+
private vaultPubkey: PublicKey;
|
|
11
|
+
|
|
12
|
+
private connection: Connection;
|
|
13
|
+
private driftClient: DriftClient;
|
|
14
|
+
private driftUser: DriftUser;
|
|
15
|
+
|
|
16
|
+
constructor(
|
|
17
|
+
pubkey: PublicKey,
|
|
18
|
+
connection: Connection,
|
|
19
|
+
driftClient: DriftClient,
|
|
20
|
+
driftUserAccount?: UserAccount
|
|
21
|
+
) {
|
|
22
|
+
this.pubkey = pubkey;
|
|
23
|
+
this.vaultPubkey = getVaultPubkey(pubkey);
|
|
24
|
+
this.connection = connection;
|
|
25
|
+
this.driftClient = driftClient;
|
|
26
|
+
this.driftUser = new DriftUser(this.vaultPubkey, connection, driftClient, driftUserAccount);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
private convertToQuartzHealth(protocolHealth: number): number {
|
|
30
|
+
if (protocolHealth <= 0) return 0;
|
|
31
|
+
if (protocolHealth >= 100) return 100;
|
|
32
|
+
|
|
33
|
+
return Math.floor(
|
|
34
|
+
Math.min(
|
|
35
|
+
100,
|
|
36
|
+
Math.max(
|
|
37
|
+
0,
|
|
38
|
+
(protocolHealth - QUARTZ_HEALTH_BUFFER) / (1 - QUARTZ_HEALTH_BUFFER)
|
|
39
|
+
)
|
|
40
|
+
)
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public getHealth(): number {
|
|
45
|
+
const driftHealth = this.driftUser.getHealth();
|
|
46
|
+
return this.convertToQuartzHealth(driftHealth);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public getRepayAmountForTargetHealth(
|
|
50
|
+
targetHealth: number,
|
|
51
|
+
repayCollateralWeight: number
|
|
52
|
+
): number {
|
|
53
|
+
// New Quartz health after repayment is given as:
|
|
54
|
+
//
|
|
55
|
+
// loanValue - repayAmount
|
|
56
|
+
// 1 - ----------------------------------------------------------------- - quartzHealthBuffer
|
|
57
|
+
// currentWeightedCollateral - (repayAmount * repayCollateralWeight)
|
|
58
|
+
// targetHealth = -------------------------------------------------------------------------------------------
|
|
59
|
+
// 1 - quartzHealthBuffer
|
|
60
|
+
//
|
|
61
|
+
// The following is an algebraicly simplified expression of the above formula, in terms of repayAmount
|
|
62
|
+
|
|
63
|
+
if (targetHealth <= 0 || targetHealth >= 100) throw Error(`Target health must be between 0 and 100`);
|
|
64
|
+
if (targetHealth <= this.getHealth()) throw Error(`Target health must be greater than current health`);
|
|
65
|
+
|
|
66
|
+
const currentWeightedCollateral = this.getTotalWeightedCollateral();
|
|
67
|
+
const loanValue = this.getMaintenanceMarginRequirement();
|
|
68
|
+
|
|
69
|
+
return Math.round(
|
|
70
|
+
(
|
|
71
|
+
loanValue - currentWeightedCollateral * (1 - QUARTZ_HEALTH_BUFFER) * (1 - targetHealth)
|
|
72
|
+
) / (
|
|
73
|
+
1 - repayCollateralWeight * (1 - QUARTZ_HEALTH_BUFFER) * (1 - targetHealth)
|
|
74
|
+
)
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public getTotalWeightedCollateral(): number {
|
|
79
|
+
return this.driftUser.getTotalCollateral('Maintenance').toNumber();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public getMaintenanceMarginRequirement(): number {
|
|
83
|
+
return this.driftUser.getMaintenanceMarginRequirement().toNumber();
|
|
84
|
+
}
|
|
85
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2020",
|
|
4
|
+
"module": "es2015",
|
|
5
|
+
"rootDir": "./src",
|
|
6
|
+
"outDir": "./build",
|
|
7
|
+
"declaration": true,
|
|
8
|
+
"strict": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"forceConsistentCasingInFileNames": true,
|
|
12
|
+
"moduleResolution": "node",
|
|
13
|
+
"resolveJsonModule": true
|
|
14
|
+
},
|
|
15
|
+
"include": ["src/**/*"],
|
|
16
|
+
"exclude": ["node_modules", "build", "**/*.test.ts"]
|
|
17
|
+
}
|