@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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Quartz Labs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,17 @@
1
+ <div align="center">
2
+ <img width="2500" alt="Quartz" src="https://cdn.prod.website-files.com/65707af0f4af991289bbd432/670e37661cdb2314fe8ba469_logo-glow-banner.jpg" />
3
+
4
+ <h1 style="margin-top:20px;">Quartz SDK</h1>
5
+ </div>
6
+
7
+ Typescript SDK for interacting with the Quartz Protocol.
8
+
9
+ ## Links
10
+
11
+ Website and waitlist: [quartzpay.io](https://quartzpay.io/)
12
+
13
+ Docs: [docs.quartzpay.io](https://docs.quartzpay.io/)
14
+
15
+ X: [@quartzpay](https://x.com/quartzpay)
16
+
17
+ Contact: [diego@quartzpay.io](mailto:diego@quartzpay.io)
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@quartz-labs/sdk",
3
+ "version": "0.0.1",
4
+ "description": "SDK for interacting with the Quartz Protocol",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "typesVersions": {
8
+ ">=4.7": { "*": ["ts/*"] }
9
+ },
10
+ "scripts": {
11
+ "build": "tsc",
12
+ "test": "jest"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/quartz-labs/sdk.git"
17
+ },
18
+ "keywords": [
19
+ "quartz",
20
+ "solana",
21
+ "sdk",
22
+ "crypto",
23
+ "protocol"
24
+ ],
25
+ "author": "Quartz Labs",
26
+ "license": "MIT",
27
+ "bugs": {
28
+ "email": "iarla@quartzpay.io"
29
+ },
30
+ "homepage": "https://quartzpay.io",
31
+ "devDependencies": {
32
+ "@types/jest": "^29.5.14",
33
+ "@types/node": "^22.10.1",
34
+ "jest": "^29.7.0",
35
+ "ts-jest": "^29.2.5",
36
+ "typescript": "^5.7.2"
37
+ },
38
+ "dependencies": {
39
+ "@coral-xyz/anchor": "^0.29.0",
40
+ "@drift-labs/sdk": "^2.103.0-beta.9",
41
+ "@pythnetwork/pyth-solana-receiver": "^0.8.2",
42
+ "@solana/web3.js": "^1.95.8"
43
+ }
44
+ }
package/src/client.ts ADDED
@@ -0,0 +1,117 @@
1
+ import { DriftClient, fetchUserAccountsUsingKeys as fetchDriftAccountsUsingKeys } from "@drift-labs/sdk";
2
+ import { QUARTZ_ADDRESS_TABLE, QUARTZ_PROGRAM_ID, SUPPORTED_DRIFT_MARKETS } from "./config/constants";
3
+ import quartzIdl from "./idl/quartz.json";
4
+ import { Quartz } from "./types/quartz.js";
5
+ import { AnchorProvider, Idl, Program, setProvider, Wallet } from "@coral-xyz/anchor";
6
+ import { Connection, AddressLookupTableAccount } from "@solana/web3.js";
7
+ import { PythSolanaReceiver } from "@pythnetwork/pyth-solana-receiver";
8
+ import { PublicKey } from "@solana/web3.js";
9
+ import { QuartzUser } from "./user";
10
+ import { getDriftUser, getVaultPubkey } from "./helpers";
11
+
12
+ export class QuartzClient {
13
+ private connection: Connection;
14
+ private wallet: Wallet;
15
+ private program: Program<Quartz>;
16
+ private quartzAddressTable: AddressLookupTableAccount;
17
+
18
+ private driftClient: DriftClient;
19
+ private oracles: Map<string, PublicKey>;
20
+
21
+ constructor(
22
+ connection: Connection,
23
+ wallet: Wallet,
24
+ program: Program<Quartz>,
25
+ quartzAddressTable: AddressLookupTableAccount,
26
+ driftClient: DriftClient,
27
+ oracles: Map<string, PublicKey>
28
+ ) {
29
+ this.connection = connection;
30
+ this.wallet = wallet;
31
+ this.program = program;
32
+ this.quartzAddressTable = quartzAddressTable;
33
+ this.driftClient = driftClient;
34
+ this.oracles = oracles;
35
+ }
36
+
37
+ static async initialize(
38
+ connection: Connection,
39
+ wallet: Wallet
40
+ ) {
41
+ const provider = new AnchorProvider(connection, wallet, { commitment: "confirmed" });
42
+ setProvider(provider);
43
+ const program = new Program(quartzIdl as Idl, QUARTZ_PROGRAM_ID, provider) as unknown as Program<Quartz>;
44
+
45
+ const quartzLookupTable = await connection.getAddressLookupTable(QUARTZ_ADDRESS_TABLE).then((res) => res.value);
46
+ if (!quartzLookupTable) throw Error("Address Lookup Table account not found");
47
+
48
+ const driftClient = new DriftClient({
49
+ connection,
50
+ wallet,
51
+ env: 'mainnet-beta',
52
+ userStats: false,
53
+ perpMarketIndexes: [],
54
+ spotMarketIndexes: SUPPORTED_DRIFT_MARKETS,
55
+ accountSubscription: {
56
+ type: 'websocket',
57
+ commitment: "confirmed"
58
+ }
59
+ });
60
+ await driftClient.subscribe();
61
+
62
+ const pythSolanaReceiver = new PythSolanaReceiver({ connection, wallet });
63
+ const oracles = new Map<string, PublicKey>([
64
+ ["SOL/USD", pythSolanaReceiver.getPriceFeedAccountAddress(0,
65
+ "0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d")],
66
+ ["USDC/USD", pythSolanaReceiver.getPriceFeedAccountAddress(0,
67
+ "0xeaa020c61cc479712813461ce153894a96a6c00b21ed0cfc2798d1f9a9e9c94a")]
68
+ ]);
69
+
70
+ return new QuartzClient(
71
+ connection,
72
+ wallet,
73
+ program,
74
+ quartzLookupTable,
75
+ driftClient,
76
+ oracles
77
+ );
78
+ }
79
+
80
+ public async getAllAccountPubkeys(): Promise<PublicKey[]> {
81
+ return (
82
+ await this.program.account.vault.all()
83
+ ).map((vault) => vault.publicKey);
84
+ }
85
+
86
+ public async getQuartzAccount(pubkey: PublicKey): Promise<QuartzUser> {
87
+ const vault = getVaultPubkey(pubkey);
88
+ await this.program.account.vault.fetch(vault); // Check account exists
89
+ return new QuartzUser(pubkey, this.connection, this.driftClient);
90
+ }
91
+
92
+ public async getMultipleQuartzAccounts(pubkeys: PublicKey[]): Promise<QuartzUser[]> {
93
+ if (pubkeys.length === 0) return [];
94
+
95
+ const vaults = pubkeys.map((pubkey) => getVaultPubkey(pubkey));
96
+ const accounts = await this.program.account.vault.fetchMultiple(vaults);
97
+
98
+ accounts.forEach((account, index) => {
99
+ if (account === null) throw Error(`Account not found for pubkey: ${pubkeys[index].toBase58()}`)
100
+ });
101
+
102
+ const driftUsers = await fetchDriftAccountsUsingKeys(
103
+ this.connection,
104
+ this.driftClient.program,
105
+ vaults.map((vault) => getDriftUser(vault))
106
+ )
107
+
108
+ const undefinedIndex = driftUsers.findIndex(user => !user);
109
+ if (undefinedIndex !== -1) {
110
+ throw new Error(`[${this.wallet?.publicKey}] Failed to fetch drift user for vault ${vaults[undefinedIndex].toBase58()}`);
111
+ }
112
+
113
+ return driftUsers.map((driftUser, index) => {
114
+ return new QuartzUser(pubkeys[index], this.connection, this.driftClient, driftUser)
115
+ });
116
+ }
117
+ }
@@ -0,0 +1,10 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+
3
+ export const QUARTZ_PROGRAM_ID = new PublicKey("6JjHXLheGSNvvexgzMthEcgjkcirDrGduc3HAKB2P1v2");
4
+ export const QUARTZ_ADDRESS_TABLE = new PublicKey("96BmeKKVGX3LKYSKo3FCEom1YpNY11kCnGscKq6ouxLx");
5
+
6
+ export const QUARTZ_HEALTH_BUFFER = 0.1;
7
+
8
+ export const DRIFT_MARKET_INDEX_USDC = 0;
9
+ export const DRIFT_MARKET_INDEX_SOL = 1;
10
+ export const SUPPORTED_DRIFT_MARKETS = [DRIFT_MARKET_INDEX_USDC, DRIFT_MARKET_INDEX_SOL];
package/src/helpers.ts ADDED
@@ -0,0 +1,33 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ import { QUARTZ_PROGRAM_ID } from "./config/constants";
3
+ import { BN } from "@coral-xyz/anchor";
4
+ import { DRIFT_PROGRAM_ID } from "@drift-labs/sdk";
5
+
6
+ export const getVaultPubkey = (owner: PublicKey) => {
7
+ const [vaultPda] = PublicKey.findProgramAddressSync(
8
+ [Buffer.from("vault"), owner.toBuffer()],
9
+ QUARTZ_PROGRAM_ID
10
+ )
11
+ return vaultPda;
12
+ }
13
+
14
+ export const getVaultSplPubkey = (owner: PublicKey, mint: PublicKey) => {
15
+ const vaultPda = getVaultPubkey(owner);
16
+ const [vaultSplPda] = PublicKey.findProgramAddressSync(
17
+ [vaultPda.toBuffer(), mint.toBuffer()],
18
+ QUARTZ_PROGRAM_ID
19
+ );
20
+ return vaultSplPda;
21
+ }
22
+
23
+ export const getDriftUser = (vaultPda: PublicKey) => {
24
+ const [userPda] = PublicKey.findProgramAddressSync(
25
+ [
26
+ Buffer.from("user"),
27
+ vaultPda.toBuffer(),
28
+ new BN(0).toArrayLike(Buffer, 'le', 2),
29
+ ],
30
+ new PublicKey(DRIFT_PROGRAM_ID)
31
+ );
32
+ return userPda;
33
+ }