@zcomb/programs-sdk 1.7.0 → 1.8.0

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.
Files changed (44) hide show
  1. package/dist/futarchy/client.d.ts +113 -1
  2. package/dist/futarchy/client.js +13 -3
  3. package/dist/futarchy/instructions.d.ts +109 -1
  4. package/dist/futarchy/instructions.js +17 -3
  5. package/dist/generated/idls/futarchy.json +112 -0
  6. package/dist/generated/types/futarchy.d.ts +112 -0
  7. package/dist/generated/types/futarchy.js +1 -1
  8. package/package.json +1 -2
  9. package/src/amm/client.ts +0 -485
  10. package/src/amm/constants.ts +0 -31
  11. package/src/amm/index.ts +0 -5
  12. package/src/amm/instructions.ts +0 -139
  13. package/src/amm/types.ts +0 -62
  14. package/src/amm/utils.ts +0 -263
  15. package/src/futarchy/client.ts +0 -1100
  16. package/src/futarchy/constants.ts +0 -28
  17. package/src/futarchy/index.ts +0 -5
  18. package/src/futarchy/instructions.ts +0 -235
  19. package/src/futarchy/types.ts +0 -54
  20. package/src/futarchy/utils.ts +0 -108
  21. package/src/generated/idls/amm.json +0 -1252
  22. package/src/generated/idls/futarchy.json +0 -1763
  23. package/src/generated/idls/index.ts +0 -4
  24. package/src/generated/idls/svault.json +0 -2228
  25. package/src/generated/idls/vault.json +0 -1501
  26. package/src/generated/types/amm.ts +0 -1258
  27. package/src/generated/types/futarchy.ts +0 -1769
  28. package/src/generated/types/index.ts +0 -4
  29. package/src/generated/types/svault.ts +0 -2234
  30. package/src/generated/types/vault.ts +0 -1507
  31. package/src/index.ts +0 -163
  32. package/src/svault/client.ts +0 -401
  33. package/src/svault/constants.ts +0 -23
  34. package/src/svault/index.ts +0 -5
  35. package/src/svault/instructions.ts +0 -258
  36. package/src/svault/types.ts +0 -45
  37. package/src/svault/utils.ts +0 -145
  38. package/src/utils.ts +0 -41
  39. package/src/vault/client.ts +0 -333
  40. package/src/vault/constants.ts +0 -23
  41. package/src/vault/index.ts +0 -5
  42. package/src/vault/instructions.ts +0 -170
  43. package/src/vault/types.ts +0 -54
  44. package/src/vault/utils.ts +0 -70
@@ -1,333 +0,0 @@
1
- /*
2
- * High-level client for the Vault program.
3
- * Provides ergonomic methods for vault operations with automatic PDA derivation,
4
- * native SOL wrapping/unwrapping, and compute budget management.
5
- */
6
-
7
- import { Program, AnchorProvider, BN } from "@coral-xyz/anchor";
8
- import { PublicKey, ComputeBudgetProgram, SystemProgram } from "@solana/web3.js";
9
- import {
10
- getAccount,
11
- getAssociatedTokenAddressSync,
12
- TokenAccountNotFoundError,
13
- NATIVE_MINT,
14
- createSyncNativeInstruction,
15
- createCloseAccountInstruction,
16
- createAssociatedTokenAccountIdempotentInstruction,
17
- } from "@solana/spl-token";
18
- import { PROGRAM_ID } from "./constants";
19
- import { Vault, VaultType, VaultAccount, VaultActionOptions } from "./types";
20
- import {
21
- deriveVaultPDA,
22
- deriveConditionalMint,
23
- fetchVaultAccount,
24
- } from "./utils";
25
- import {
26
- initialize,
27
- addOption,
28
- activate,
29
- deposit,
30
- withdraw,
31
- finalize,
32
- redeemWinnings,
33
- } from "./instructions";
34
-
35
- import { VaultIDL } from "../generated/idls";
36
-
37
- const DEFAULT_COMPUTE_UNITS = 450_000;
38
-
39
- export class VaultClient {
40
- public program: Program<Vault>;
41
- public programId: PublicKey;
42
- public computeUnits: number;
43
-
44
- constructor(
45
- provider: AnchorProvider,
46
- programId?: PublicKey,
47
- computeUnits?: number
48
- ) {
49
- this.programId = programId ?? PROGRAM_ID;
50
- this.computeUnits = computeUnits ?? DEFAULT_COMPUTE_UNITS;
51
- this.program = new Program(VaultIDL as Vault, provider);
52
- }
53
-
54
- /* PDA Helpers */
55
-
56
- deriveVaultPDA(
57
- owner: PublicKey,
58
- nonce: number
59
- ): [PublicKey, number] {
60
- return deriveVaultPDA(owner, nonce, this.programId);
61
- }
62
-
63
- deriveConditionalMint(
64
- vaultPda: PublicKey,
65
- vaultType: VaultType,
66
- index: number
67
- ): [PublicKey, number] {
68
- return deriveConditionalMint(vaultPda, vaultType, index, this.programId);
69
- }
70
-
71
- /* State Fetching */
72
-
73
- async fetchVault(vaultPda: PublicKey): Promise<VaultAccount> {
74
- return fetchVaultAccount(this.program, vaultPda);
75
- }
76
-
77
- async fetchUserATAs(vaultPda: PublicKey, user: PublicKey, vaultType: VaultType) {
78
- const vault = await this.fetchVault(vaultPda);
79
- const mint = vaultType === VaultType.Base ? vault.baseMint.address : vault.quoteMint.address;
80
- const condMints = (vaultType === VaultType.Base ? vault.condBaseMints : vault.condQuoteMints)
81
- .slice(0, vault.numOptions);
82
- return {
83
- userAta: getAssociatedTokenAddressSync(mint, user),
84
- userCondATAs: condMints.map((m) => getAssociatedTokenAddressSync(m, user)),
85
- };
86
- }
87
-
88
- async fetchVaultATA(vaultPda: PublicKey, vaultType: VaultType) {
89
- const vault = await this.fetchVault(vaultPda);
90
- const mint = vaultType === VaultType.Base ? vault.baseMint.address : vault.quoteMint.address;
91
- return getAssociatedTokenAddressSync(mint, vaultPda, true);
92
- }
93
-
94
- async fetchUserBalances(vaultPda: PublicKey, user: PublicKey, vaultType: VaultType) {
95
- const { userAta, userCondATAs } = await this.fetchUserATAs(vaultPda, user, vaultType);
96
- const connection = this.program.provider.connection;
97
-
98
- const getBalanceSafe = async (ata: PublicKey) => {
99
- try {
100
- const acc = await getAccount(connection, ata);
101
- return new BN(acc.amount.toString());
102
- } catch (e) {
103
- if (e instanceof TokenAccountNotFoundError) {
104
- return new BN(0);
105
- }
106
- throw e;
107
- }
108
- };
109
-
110
- const [userBalance, ...condBalances] = await Promise.all([
111
- getBalanceSafe(userAta),
112
- ...userCondATAs.map(getBalanceSafe),
113
- ]);
114
-
115
- return { userBalance, condBalances };
116
- }
117
-
118
- async fetchVaultBalance(vaultPda: PublicKey, vaultType: VaultType): Promise<BN> {
119
- const vaultAta = await this.fetchVaultATA(vaultPda, vaultType);
120
- try {
121
- const acc = await getAccount(this.program.provider.connection, vaultAta);
122
- return new BN(acc.amount.toString());
123
- } catch (e) {
124
- if (e instanceof TokenAccountNotFoundError) {
125
- return new BN(0);
126
- }
127
- throw e;
128
- }
129
- }
130
-
131
- /* Instruction Builders */
132
-
133
- initialize(
134
- payer: PublicKey,
135
- baseMint: PublicKey,
136
- quoteMint: PublicKey,
137
- nonce: number,
138
- owner?: PublicKey
139
- ) {
140
- const vaultOwner = owner ?? payer;
141
- const [vaultPda] = this.deriveVaultPDA(vaultOwner, nonce);
142
- const [condBaseMint0] = this.deriveConditionalMint(vaultPda, VaultType.Base, 0);
143
- const [condBaseMint1] = this.deriveConditionalMint(vaultPda, VaultType.Base, 1);
144
- const [condQuoteMint0] = this.deriveConditionalMint(vaultPda, VaultType.Quote, 0);
145
- const [condQuoteMint1] = this.deriveConditionalMint(vaultPda, VaultType.Quote, 1);
146
-
147
- const builder = initialize(
148
- this.program,
149
- payer,
150
- vaultOwner,
151
- vaultPda,
152
- baseMint,
153
- quoteMint,
154
- condBaseMint0,
155
- condBaseMint1,
156
- condQuoteMint0,
157
- condQuoteMint1,
158
- nonce
159
- );
160
-
161
- return {
162
- builder,
163
- vaultPda,
164
- condBaseMint0,
165
- condBaseMint1,
166
- condQuoteMint0,
167
- condQuoteMint1,
168
- };
169
- }
170
-
171
- async addOption(payer: PublicKey, owner: PublicKey, vaultPda: PublicKey) {
172
- const vault = await this.fetchVault(vaultPda);
173
- const [condBaseMint] = this.deriveConditionalMint(vaultPda, VaultType.Base, vault.numOptions);
174
- const [condQuoteMint] = this.deriveConditionalMint(vaultPda, VaultType.Quote, vault.numOptions);
175
-
176
- const builder = addOption(
177
- this.program,
178
- payer,
179
- owner,
180
- vaultPda,
181
- condBaseMint,
182
- condQuoteMint
183
- );
184
-
185
- return { builder, condBaseMint, condQuoteMint };
186
- }
187
-
188
- activate(payer: PublicKey, owner: PublicKey, vaultPda: PublicKey) {
189
- return activate(this.program, payer, owner, vaultPda);
190
- }
191
-
192
- async deposit(
193
- signer: PublicKey,
194
- vaultPda: PublicKey,
195
- vaultType: VaultType,
196
- amount: BN | number,
197
- options?: VaultActionOptions
198
- ) {
199
- const { autoWrapUnwrap = true, includeCuBudget = true, computeUnits } = options ?? {};
200
-
201
- const vault = await this.fetchVault(vaultPda);
202
- const mint = vaultType === VaultType.Base ? vault.baseMint.address : vault.quoteMint.address;
203
- const condMints = (vaultType === VaultType.Base ? vault.condBaseMints : vault.condQuoteMints)
204
- .slice(0, vault.numOptions);
205
-
206
- const builder = deposit(
207
- this.program,
208
- signer,
209
- vaultPda,
210
- mint,
211
- condMints,
212
- vaultType,
213
- amount
214
- );
215
-
216
- const preIxs: ReturnType<typeof ComputeBudgetProgram.setComputeUnitLimit>[] = [];
217
-
218
- if (includeCuBudget) {
219
- preIxs.push(
220
- ComputeBudgetProgram.setComputeUnitLimit({
221
- units: computeUnits ?? this.computeUnits,
222
- })
223
- );
224
- }
225
-
226
- if (autoWrapUnwrap && mint.equals(NATIVE_MINT)) {
227
- const amountBN = typeof amount === "number" ? new BN(amount) : amount;
228
- const wsolAta = getAssociatedTokenAddressSync(NATIVE_MINT, signer);
229
- preIxs.push(
230
- createAssociatedTokenAccountIdempotentInstruction(
231
- signer,
232
- wsolAta,
233
- signer,
234
- NATIVE_MINT
235
- ),
236
- SystemProgram.transfer({
237
- fromPubkey: signer,
238
- toPubkey: wsolAta,
239
- lamports: BigInt(amountBN.toString()),
240
- }),
241
- createSyncNativeInstruction(wsolAta)
242
- );
243
- }
244
-
245
- return preIxs.length > 0 ? builder.preInstructions(preIxs) : builder;
246
- }
247
-
248
- async withdraw(
249
- signer: PublicKey,
250
- vaultPda: PublicKey,
251
- vaultType: VaultType,
252
- amount: BN | number,
253
- options?: VaultActionOptions
254
- ) {
255
- const { autoWrapUnwrap = true, includeCuBudget = true, computeUnits } = options ?? {};
256
-
257
- const vault = await this.fetchVault(vaultPda);
258
- const mint = vaultType === VaultType.Base ? vault.baseMint.address : vault.quoteMint.address;
259
- const condMints = (vaultType === VaultType.Base ? vault.condBaseMints : vault.condQuoteMints)
260
- .slice(0, vault.numOptions);
261
-
262
- let builder = withdraw(
263
- this.program,
264
- signer,
265
- vaultPda,
266
- mint,
267
- condMints,
268
- vaultType,
269
- amount
270
- );
271
-
272
- if (includeCuBudget) {
273
- builder = builder.preInstructions([
274
- ComputeBudgetProgram.setComputeUnitLimit({
275
- units: computeUnits ?? this.computeUnits,
276
- }),
277
- ]);
278
- }
279
-
280
- if (autoWrapUnwrap && mint.equals(NATIVE_MINT)) {
281
- const wsolAta = getAssociatedTokenAddressSync(NATIVE_MINT, signer);
282
- builder = builder.postInstructions([
283
- createCloseAccountInstruction(wsolAta, signer, signer),
284
- ]);
285
- }
286
-
287
- return builder;
288
- }
289
-
290
- finalize(payer: PublicKey, owner: PublicKey, vaultPda: PublicKey, winningIdx: number) {
291
- return finalize(this.program, payer, owner, vaultPda, winningIdx);
292
- }
293
-
294
- async redeemWinnings(
295
- signer: PublicKey,
296
- vaultPda: PublicKey,
297
- vaultType: VaultType,
298
- options?: VaultActionOptions
299
- ) {
300
- const { autoWrapUnwrap = true, includeCuBudget = true, computeUnits } = options ?? {};
301
-
302
- const vault = await this.fetchVault(vaultPda);
303
- const mint = vaultType === VaultType.Base ? vault.baseMint.address : vault.quoteMint.address;
304
- const condMints = (vaultType === VaultType.Base ? vault.condBaseMints : vault.condQuoteMints)
305
- .slice(0, vault.numOptions);
306
-
307
- let builder = redeemWinnings(
308
- this.program,
309
- signer,
310
- vaultPda,
311
- mint,
312
- condMints,
313
- vaultType
314
- );
315
-
316
- if (includeCuBudget) {
317
- builder = builder.preInstructions([
318
- ComputeBudgetProgram.setComputeUnitLimit({
319
- units: computeUnits ?? this.computeUnits,
320
- }),
321
- ]);
322
- }
323
-
324
- if (autoWrapUnwrap && mint.equals(NATIVE_MINT)) {
325
- const wsolAta = getAssociatedTokenAddressSync(NATIVE_MINT, signer);
326
- builder = builder.postInstructions([
327
- createCloseAccountInstruction(wsolAta, signer, signer),
328
- ]);
329
- }
330
-
331
- return builder;
332
- }
333
- }
@@ -1,23 +0,0 @@
1
- /*
2
- * Constants for the Vault program.
3
- * Parsed from the generated IDL to stay in sync with the Rust program.
4
- */
5
-
6
- import { PublicKey } from "@solana/web3.js";
7
- import { VaultIDL } from "../generated/idls";
8
- import { parseIdlBytes, getIdlConstant } from "../utils";
9
-
10
- /* Program ID */
11
-
12
- export const PROGRAM_ID = new PublicKey(VaultIDL.address);
13
-
14
- /* PDA Seeds */
15
-
16
- export const VAULT_SEED = parseIdlBytes(getIdlConstant(VaultIDL, "VAULT_SEED"));
17
- export const CONDITIONAL_MINT_SEED = parseIdlBytes(getIdlConstant(VaultIDL, "CONDITIONAL_MINT_SEED"));
18
-
19
- /* Numeric Constants */
20
-
21
- export const MAX_OPTIONS = Number(getIdlConstant(VaultIDL, "MAX_OPTIONS"));
22
- export const MIN_OPTIONS = Number(getIdlConstant(VaultIDL, "MIN_OPTIONS"));
23
- export const VAULT_VERSION = Number(getIdlConstant(VaultIDL, "VAULT_VERSION"));
@@ -1,5 +0,0 @@
1
- export * from "./constants";
2
- export * from "./types";
3
- export * from "./utils";
4
- export * from "./instructions";
5
- export * from "./client";
@@ -1,170 +0,0 @@
1
- /*
2
- * Low-level instruction builders for the Vault program.
3
- * These are thin wrappers around the program methods - use VaultClient for higher-level operations.
4
- */
5
-
6
- import { Program, BN } from "@coral-xyz/anchor";
7
- import { PublicKey } from "@solana/web3.js";
8
- import { getAssociatedTokenAddressSync } from "@solana/spl-token";
9
- import { Vault, VaultType } from "./types";
10
-
11
- export function initialize(
12
- program: Program<Vault>,
13
- payer: PublicKey,
14
- owner: PublicKey,
15
- vaultPda: PublicKey,
16
- baseMint: PublicKey,
17
- quoteMint: PublicKey,
18
- condBaseMint0: PublicKey,
19
- condBaseMint1: PublicKey,
20
- condQuoteMint0: PublicKey,
21
- condQuoteMint1: PublicKey,
22
- nonce: number
23
- ) {
24
- return program.methods.initialize(nonce).accountsPartial({
25
- payer,
26
- owner,
27
- vault: vaultPda,
28
- baseMint,
29
- quoteMint,
30
- condBaseMint0,
31
- condBaseMint1,
32
- condQuoteMint0,
33
- condQuoteMint1,
34
- });
35
- }
36
-
37
- export function addOption(
38
- program: Program<Vault>,
39
- payer: PublicKey,
40
- owner: PublicKey,
41
- vaultPda: PublicKey,
42
- condBaseMint: PublicKey,
43
- condQuoteMint: PublicKey
44
- ) {
45
- return program.methods.addOption().accountsPartial({
46
- payer,
47
- owner,
48
- vault: vaultPda,
49
- condBaseMint,
50
- condQuoteMint,
51
- });
52
- }
53
-
54
- export function activate(
55
- program: Program<Vault>,
56
- payer: PublicKey,
57
- owner: PublicKey,
58
- vaultPda: PublicKey
59
- ) {
60
- return program.methods.activate().accountsPartial({
61
- payer,
62
- owner,
63
- vault: vaultPda,
64
- });
65
- }
66
-
67
- export function deposit(
68
- program: Program<Vault>,
69
- signer: PublicKey,
70
- vaultPda: PublicKey,
71
- mint: PublicKey,
72
- condMints: PublicKey[],
73
- vaultType: VaultType,
74
- amount: BN | number
75
- ) {
76
- const amountBN = typeof amount === "number" ? new BN(amount) : amount;
77
- const vaultTypeArg = vaultType === VaultType.Base ? { base: {} } : { quote: {} };
78
-
79
- return program.methods
80
- .deposit(vaultTypeArg, amountBN)
81
- .accountsPartial({
82
- signer,
83
- vault: vaultPda,
84
- mint,
85
- })
86
- .remainingAccounts(
87
- condMints.flatMap((condMint) => [
88
- { pubkey: condMint, isSigner: false, isWritable: true },
89
- {
90
- pubkey: getAssociatedTokenAddressSync(condMint, signer),
91
- isSigner: false,
92
- isWritable: true,
93
- },
94
- ])
95
- );
96
- }
97
-
98
- export function withdraw(
99
- program: Program<Vault>,
100
- signer: PublicKey,
101
- vaultPda: PublicKey,
102
- mint: PublicKey,
103
- condMints: PublicKey[],
104
- vaultType: VaultType,
105
- amount: BN | number
106
- ) {
107
- const amountBN = typeof amount === "number" ? new BN(amount) : amount;
108
- const vaultTypeArg = vaultType === VaultType.Base ? { base: {} } : { quote: {} };
109
-
110
- return program.methods
111
- .withdraw(vaultTypeArg, amountBN)
112
- .accountsPartial({
113
- signer,
114
- vault: vaultPda,
115
- mint,
116
- })
117
- .remainingAccounts(
118
- condMints.flatMap((condMint) => [
119
- { pubkey: condMint, isSigner: false, isWritable: true },
120
- {
121
- pubkey: getAssociatedTokenAddressSync(condMint, signer),
122
- isSigner: false,
123
- isWritable: true,
124
- },
125
- ])
126
- );
127
- }
128
-
129
- export function finalize(
130
- program: Program<Vault>,
131
- payer: PublicKey,
132
- owner: PublicKey,
133
- vaultPda: PublicKey,
134
- winningIdx: number
135
- ) {
136
- return program.methods.finalize(winningIdx).accountsPartial({
137
- payer,
138
- owner,
139
- vault: vaultPda,
140
- });
141
- }
142
-
143
- export function redeemWinnings(
144
- program: Program<Vault>,
145
- signer: PublicKey,
146
- vaultPda: PublicKey,
147
- mint: PublicKey,
148
- condMints: PublicKey[],
149
- vaultType: VaultType
150
- ) {
151
- const vaultTypeArg = vaultType === VaultType.Base ? { base: {} } : { quote: {} };
152
-
153
- return program.methods
154
- .redeemWinnings(vaultTypeArg)
155
- .accountsPartial({
156
- signer,
157
- vault: vaultPda,
158
- mint,
159
- })
160
- .remainingAccounts(
161
- condMints.flatMap((condMint) => [
162
- { pubkey: condMint, isSigner: false, isWritable: true },
163
- {
164
- pubkey: getAssociatedTokenAddressSync(condMint, signer),
165
- isSigner: false,
166
- isWritable: true,
167
- },
168
- ])
169
- );
170
- }
@@ -1,54 +0,0 @@
1
- /*
2
- * Type definitions for the Vault program.
3
- * Exports IDL-derived types and SDK-friendly enums.
4
- */
5
-
6
- import { IdlAccounts, IdlEvents, IdlTypes } from "@coral-xyz/anchor";
7
- import { TxOptions } from "../utils";
8
-
9
- export { Vault } from "../generated/types";
10
- import type { Vault } from "../generated/types";
11
-
12
- /* IDL-derived Types */
13
-
14
- export type VaultAccount = IdlAccounts<Vault>["vaultAccount"];
15
- export type VaultStateRaw = IdlTypes<Vault>["vaultState"];
16
- export type VaultTypeRaw = IdlTypes<Vault>["vaultType"];
17
-
18
- export type VaultInitializedEvent = IdlEvents<Vault>["vaultInitialized"];
19
- export type VaultActivatedEvent = IdlEvents<Vault>["vaultActivated"];
20
- export type VaultDepositEvent = IdlEvents<Vault>["vaultDeposit"];
21
- export type VaultWithdrawalEvent = IdlEvents<Vault>["vaultWithdrawal"];
22
- export type VaultFinalizedEvent = IdlEvents<Vault>["vaultFinalized"];
23
- export type OptionAddedEvent = IdlEvents<Vault>["optionAdded"];
24
- export type WinningsRedeemedEvent = IdlEvents<Vault>["winningsRedeemed"];
25
-
26
- /* SDK Enums */
27
-
28
- export enum VaultType {
29
- Base = 0,
30
- Quote = 1,
31
- }
32
-
33
- export enum VaultState {
34
- Setup = "setup",
35
- Active = "active",
36
- Finalized = "finalized",
37
- }
38
-
39
- /* Event Union Type */
40
-
41
- export type VaultEvent =
42
- | { name: "VaultInitialized"; data: VaultInitializedEvent }
43
- | { name: "VaultActivated"; data: VaultActivatedEvent }
44
- | { name: "VaultDeposit"; data: VaultDepositEvent }
45
- | { name: "VaultWithdrawal"; data: VaultWithdrawalEvent }
46
- | { name: "VaultFinalized"; data: VaultFinalizedEvent }
47
- | { name: "OptionAdded"; data: OptionAddedEvent }
48
- | { name: "WinningsRedeemed"; data: WinningsRedeemedEvent };
49
-
50
- /* Client Options */
51
-
52
- export interface VaultActionOptions extends TxOptions {
53
- autoWrapUnwrap?: boolean; // Auto wrap/unwrap native SOL (default: true)
54
- }
@@ -1,70 +0,0 @@
1
- /*
2
- * Utility functions for the Vault program.
3
- * PDA derivation, state parsing, and account fetching.
4
- */
5
-
6
- import { Program } from "@coral-xyz/anchor";
7
- import { PublicKey } from "@solana/web3.js";
8
- import { VAULT_SEED, CONDITIONAL_MINT_SEED, PROGRAM_ID } from "./constants";
9
- import { Vault, VaultType, VaultState, VaultAccount } from "./types";
10
-
11
- /* PDA Derivation */
12
-
13
- export function deriveVaultPDA(
14
- owner: PublicKey,
15
- nonce: number,
16
- programId: PublicKey = PROGRAM_ID
17
- ): [PublicKey, number] {
18
- const nonceBuffer = Buffer.alloc(2);
19
- nonceBuffer.writeUInt16LE(nonce);
20
- return PublicKey.findProgramAddressSync(
21
- [
22
- VAULT_SEED,
23
- owner.toBuffer(),
24
- nonceBuffer,
25
- ],
26
- programId
27
- );
28
- }
29
-
30
- export function deriveConditionalMint(
31
- vaultPda: PublicKey,
32
- vaultType: VaultType,
33
- index: number,
34
- programId: PublicKey = PROGRAM_ID
35
- ): [PublicKey, number] {
36
- return PublicKey.findProgramAddressSync(
37
- [
38
- CONDITIONAL_MINT_SEED,
39
- vaultPda.toBuffer(),
40
- Buffer.from([vaultType]),
41
- Buffer.from([index]),
42
- ],
43
- programId
44
- );
45
- }
46
-
47
- /* Parsers */
48
-
49
- export function parseVaultState(state: any): { state: VaultState; winningIdx: number | null } {
50
- if ("setup" in state) {
51
- return { state: VaultState.Setup, winningIdx: null };
52
- }
53
- if ("active" in state) {
54
- return { state: VaultState.Active, winningIdx: null };
55
- }
56
- if ("finalized" in state) {
57
- const winningIdx = state.finalized[0] ?? state.finalized;
58
- return { state: VaultState.Finalized, winningIdx };
59
- }
60
- throw new Error("Unknown vault state");
61
- }
62
-
63
- /* Fetch */
64
-
65
- export async function fetchVaultAccount(
66
- program: Program<Vault>,
67
- vaultPda: PublicKey
68
- ): Promise<VaultAccount> {
69
- return program.account.vaultAccount.fetch(vaultPda);
70
- }