@quartz-labs/sdk 0.0.1 → 0.0.2

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/README.md CHANGED
@@ -14,4 +14,4 @@ Docs: [docs.quartzpay.io](https://docs.quartzpay.io/)
14
14
 
15
15
  X: [@quartzpay](https://x.com/quartzpay)
16
16
 
17
- Contact: [diego@quartzpay.io](mailto:diego@quartzpay.io)
17
+ Contact: [iarla@quartzpay.io](mailto:diego@quartzpay.io)
@@ -0,0 +1,19 @@
1
+ import { DriftClient } from "@drift-labs/sdk";
2
+ import { Quartz } from "./types/quartz.js";
3
+ import { Program, Wallet } from "@coral-xyz/anchor";
4
+ import { Connection, AddressLookupTableAccount } from "@solana/web3.js";
5
+ import { PublicKey } from "@solana/web3.js";
6
+ import { QuartzUser } from "./user";
7
+ export declare class QuartzClient {
8
+ private connection;
9
+ private wallet;
10
+ private program;
11
+ private quartzAddressTable;
12
+ private driftClient;
13
+ private oracles;
14
+ constructor(connection: Connection, wallet: Wallet, program: Program<Quartz>, quartzAddressTable: AddressLookupTableAccount, driftClient: DriftClient, oracles: Map<string, PublicKey>);
15
+ static initialize(connection: Connection, wallet: Wallet): Promise<QuartzClient>;
16
+ getAllQuartzAccountPubkeys(): Promise<PublicKey[]>;
17
+ getQuartzAccount(vault: PublicKey): Promise<QuartzUser>;
18
+ getMultipleQuartzAccounts(vaults: PublicKey[]): Promise<(QuartzUser | null)[]>;
19
+ }
@@ -0,0 +1,60 @@
1
+ import { fetchUserAccountsUsingKeys as fetchDriftAccountsUsingKeys } from "@drift-labs/sdk";
2
+ import { QUARTZ_ADDRESS_TABLE, QUARTZ_PROGRAM_ID } from "./config/constants";
3
+ import quartzIdl from "./idl/quartz.json";
4
+ import { AnchorProvider, Program, setProvider } from "@coral-xyz/anchor";
5
+ import { PythSolanaReceiver } from "@pythnetwork/pyth-solana-receiver";
6
+ import { QuartzUser } from "./user";
7
+ import { getDriftUser } from "./helpers";
8
+ import { DriftClientService } from "./services/driftClientService";
9
+ export class QuartzClient {
10
+ constructor(connection, wallet, program, quartzAddressTable, driftClient, oracles) {
11
+ this.connection = connection;
12
+ this.wallet = wallet;
13
+ this.program = program;
14
+ this.quartzAddressTable = quartzAddressTable;
15
+ this.driftClient = driftClient;
16
+ this.oracles = oracles;
17
+ }
18
+ static async initialize(connection, wallet) {
19
+ const provider = new AnchorProvider(connection, wallet, { commitment: "confirmed" });
20
+ setProvider(provider);
21
+ const program = new Program(quartzIdl, QUARTZ_PROGRAM_ID, provider);
22
+ const quartzLookupTable = await connection.getAddressLookupTable(QUARTZ_ADDRESS_TABLE).then((res) => res.value);
23
+ if (!quartzLookupTable)
24
+ throw Error("Address Lookup Table account not found");
25
+ const driftClient = await DriftClientService.getDriftClient(connection);
26
+ const pythSolanaReceiver = new PythSolanaReceiver({ connection, wallet });
27
+ const oracles = new Map([
28
+ ["SOL/USD", pythSolanaReceiver.getPriceFeedAccountAddress(0, "0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d")],
29
+ ["USDC/USD", pythSolanaReceiver.getPriceFeedAccountAddress(0, "0xeaa020c61cc479712813461ce153894a96a6c00b21ed0cfc2798d1f9a9e9c94a")]
30
+ ]);
31
+ return new QuartzClient(connection, wallet, program, quartzLookupTable, driftClient, oracles);
32
+ }
33
+ async getAllQuartzAccountPubkeys() {
34
+ return (await this.program.account.vault.all()).map((vault) => vault.publicKey);
35
+ }
36
+ async getQuartzAccount(vault) {
37
+ await this.program.account.vault.fetch(vault); // Check account exists
38
+ return new QuartzUser(vault, this.connection, this.driftClient);
39
+ }
40
+ async getMultipleQuartzAccounts(vaults) {
41
+ if (vaults.length === 0)
42
+ return [];
43
+ const accounts = await this.program.account.vault.fetchMultiple(vaults);
44
+ accounts.forEach((account, index) => {
45
+ if (account === null)
46
+ throw Error(`Account not found for pubkey: ${vaults[index].toBase58()}`);
47
+ });
48
+ const driftUsers = await fetchDriftAccountsUsingKeys(this.connection, this.driftClient.program, vaults.map((vault) => getDriftUser(vault)));
49
+ // TODO: Uncomment once Drift accounts are guaranteed
50
+ // const undefinedIndex = driftUsers.findIndex(user => !user);
51
+ // if (undefinedIndex !== -1) {
52
+ // throw new Error(`[${this.wallet?.publicKey}] Failed to fetch drift user for vault ${vaults[undefinedIndex].toBase58()}`);
53
+ // }
54
+ return driftUsers.map((driftUser, index) => {
55
+ if (driftUser === undefined)
56
+ return null;
57
+ return new QuartzUser(vaults[index], this.connection, this.driftClient, driftUser);
58
+ });
59
+ }
60
+ }
@@ -0,0 +1,7 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ export declare const QUARTZ_PROGRAM_ID: PublicKey;
3
+ export declare const QUARTZ_ADDRESS_TABLE: PublicKey;
4
+ export declare const QUARTZ_HEALTH_BUFFER = 0.1;
5
+ export declare const DRIFT_MARKET_INDEX_USDC = 0;
6
+ export declare const DRIFT_MARKET_INDEX_SOL = 1;
7
+ export declare const SUPPORTED_DRIFT_MARKETS: number[];
@@ -0,0 +1,7 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ export const QUARTZ_PROGRAM_ID = new PublicKey("6JjHXLheGSNvvexgzMthEcgjkcirDrGduc3HAKB2P1v2");
3
+ export const QUARTZ_ADDRESS_TABLE = new PublicKey("96BmeKKVGX3LKYSKo3FCEom1YpNY11kCnGscKq6ouxLx");
4
+ export const QUARTZ_HEALTH_BUFFER = 0.1;
5
+ export const DRIFT_MARKET_INDEX_USDC = 0;
6
+ export const DRIFT_MARKET_INDEX_SOL = 1;
7
+ export const SUPPORTED_DRIFT_MARKETS = [DRIFT_MARKET_INDEX_USDC, DRIFT_MARKET_INDEX_SOL];
@@ -0,0 +1,12 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ export declare const getVaultPubkey: (user: PublicKey) => PublicKey;
3
+ export declare const getVaultSplPubkey: (user: PublicKey, mint: PublicKey) => PublicKey;
4
+ export declare const getDriftUser: (vaultPda: PublicKey) => PublicKey;
5
+ export declare const getDriftUserStats: (vaultPda: PublicKey) => PublicKey;
6
+ export declare const getDriftState: () => PublicKey;
7
+ export declare const getDriftSpotMarketVault: (marketIndex: number) => PublicKey;
8
+ export declare const toRemainingAccount: (pubkey: PublicKey, isWritable: boolean, isSigner: boolean) => {
9
+ pubkey: PublicKey;
10
+ isWritable: boolean;
11
+ isSigner: boolean;
12
+ };
@@ -0,0 +1,39 @@
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
+ export const getVaultPubkey = (user) => {
6
+ const [vaultPda] = PublicKey.findProgramAddressSync([Buffer.from("vault"), user.toBuffer()], QUARTZ_PROGRAM_ID);
7
+ return vaultPda;
8
+ };
9
+ export const getVaultSplPubkey = (user, mint) => {
10
+ const vaultPda = getVaultPubkey(user);
11
+ const [vaultSplPda] = PublicKey.findProgramAddressSync([vaultPda.toBuffer(), mint.toBuffer()], QUARTZ_PROGRAM_ID);
12
+ return vaultSplPda;
13
+ };
14
+ export const getDriftUser = (vaultPda) => {
15
+ const [userPda] = PublicKey.findProgramAddressSync([
16
+ Buffer.from("user"),
17
+ vaultPda.toBuffer(),
18
+ new BN(0).toArrayLike(Buffer, 'le', 2),
19
+ ], new PublicKey(DRIFT_PROGRAM_ID));
20
+ return userPda;
21
+ };
22
+ export const getDriftUserStats = (vaultPda) => {
23
+ const [userStatsPda] = PublicKey.findProgramAddressSync([Buffer.from("user_stats"), vaultPda.toBuffer()], new PublicKey(DRIFT_PROGRAM_ID));
24
+ return userStatsPda;
25
+ };
26
+ export const getDriftState = () => {
27
+ const [statePda] = PublicKey.findProgramAddressSync([Buffer.from("drift_state")], new PublicKey(DRIFT_PROGRAM_ID));
28
+ return statePda;
29
+ };
30
+ export const getDriftSpotMarketVault = (marketIndex) => {
31
+ const [spotMarketVaultPda] = PublicKey.findProgramAddressSync([
32
+ Buffer.from("spot_market_vault"),
33
+ new BN(marketIndex).toArrayLike(Buffer, 'le', 2)
34
+ ], new PublicKey(DRIFT_PROGRAM_ID));
35
+ return spotMarketVaultPda;
36
+ };
37
+ export const toRemainingAccount = (pubkey, isWritable, isSigner) => {
38
+ return { pubkey, isWritable, isSigner };
39
+ };