@zemyth/raise-sdk 0.1.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 (54) hide show
  1. package/README.md +416 -0
  2. package/dist/accounts/index.cjs +258 -0
  3. package/dist/accounts/index.cjs.map +1 -0
  4. package/dist/accounts/index.d.cts +115 -0
  5. package/dist/accounts/index.d.ts +115 -0
  6. package/dist/accounts/index.js +245 -0
  7. package/dist/accounts/index.js.map +1 -0
  8. package/dist/constants/index.cjs +174 -0
  9. package/dist/constants/index.cjs.map +1 -0
  10. package/dist/constants/index.d.cts +143 -0
  11. package/dist/constants/index.d.ts +143 -0
  12. package/dist/constants/index.js +158 -0
  13. package/dist/constants/index.js.map +1 -0
  14. package/dist/errors/index.cjs +177 -0
  15. package/dist/errors/index.cjs.map +1 -0
  16. package/dist/errors/index.d.cts +83 -0
  17. package/dist/errors/index.d.ts +83 -0
  18. package/dist/errors/index.js +170 -0
  19. package/dist/errors/index.js.map +1 -0
  20. package/dist/index.cjs +2063 -0
  21. package/dist/index.cjs.map +1 -0
  22. package/dist/index.d.cts +680 -0
  23. package/dist/index.d.ts +680 -0
  24. package/dist/index.js +1926 -0
  25. package/dist/index.js.map +1 -0
  26. package/dist/instructions/index.cjs +852 -0
  27. package/dist/instructions/index.cjs.map +1 -0
  28. package/dist/instructions/index.d.cts +452 -0
  29. package/dist/instructions/index.d.ts +452 -0
  30. package/dist/instructions/index.js +809 -0
  31. package/dist/instructions/index.js.map +1 -0
  32. package/dist/pdas/index.cjs +241 -0
  33. package/dist/pdas/index.cjs.map +1 -0
  34. package/dist/pdas/index.d.cts +171 -0
  35. package/dist/pdas/index.d.ts +171 -0
  36. package/dist/pdas/index.js +217 -0
  37. package/dist/pdas/index.js.map +1 -0
  38. package/dist/types/index.cjs +44 -0
  39. package/dist/types/index.cjs.map +1 -0
  40. package/dist/types/index.d.cts +229 -0
  41. package/dist/types/index.d.ts +229 -0
  42. package/dist/types/index.js +39 -0
  43. package/dist/types/index.js.map +1 -0
  44. package/package.json +130 -0
  45. package/src/accounts/index.ts +329 -0
  46. package/src/client.ts +715 -0
  47. package/src/constants/index.ts +205 -0
  48. package/src/errors/index.ts +222 -0
  49. package/src/events/index.ts +256 -0
  50. package/src/index.ts +253 -0
  51. package/src/instructions/index.ts +1504 -0
  52. package/src/pdas/index.ts +404 -0
  53. package/src/types/index.ts +267 -0
  54. package/src/utils/index.ts +277 -0
@@ -0,0 +1,329 @@
1
+ /**
2
+ * Raise Account Fetchers
3
+ *
4
+ * Functions to fetch and decode program accounts.
5
+ */
6
+
7
+ import { Program, BN } from '@coral-xyz/anchor';
8
+ import { PublicKey } from '@solana/web3.js';
9
+ import {
10
+ getProjectPDA,
11
+ getMilestonePDA,
12
+ getInvestmentPDA,
13
+ getVotePDA,
14
+ getPivotProposalPDA,
15
+ getTgeEscrowPDA,
16
+ getAdminConfigPDA,
17
+ } from '../pdas/index.js';
18
+ import type { InvestmentWithKey, MilestoneWithKey, VoteWithKey } from '../types/index.js';
19
+
20
+ // Generic type for any Anchor program
21
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
22
+ type AnyProgram = Program<any>;
23
+
24
+ // Helper to access account namespace dynamically
25
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
26
+ function getAccountNamespace(program: AnyProgram): any {
27
+ return program.account;
28
+ }
29
+
30
+ /**
31
+ * Fetch project account data
32
+ *
33
+ * @param program - Anchor program instance
34
+ * @param projectId - Project identifier
35
+ * @returns Project account data or null if not found
36
+ */
37
+ export async function fetchProject(
38
+ program: AnyProgram,
39
+ projectId: BN
40
+ ) {
41
+ try {
42
+ const projectPda = getProjectPDA(projectId, program.programId);
43
+ return await getAccountNamespace(program).project.fetch(projectPda);
44
+ } catch (error) {
45
+ if (error instanceof Error && error.message?.includes('Account does not exist')) {
46
+ return null;
47
+ }
48
+ throw error;
49
+ }
50
+ }
51
+
52
+ /**
53
+ * Fetch project account by PDA
54
+ *
55
+ * @param program - Anchor program instance
56
+ * @param projectPda - Project account PDA
57
+ * @returns Project account data or null if not found
58
+ */
59
+ export async function fetchProjectByPda(
60
+ program: AnyProgram,
61
+ projectPda: PublicKey
62
+ ) {
63
+ try {
64
+ return await getAccountNamespace(program).project.fetch(projectPda);
65
+ } catch (error) {
66
+ if (error instanceof Error && error.message?.includes('Account does not exist')) {
67
+ return null;
68
+ }
69
+ throw error;
70
+ }
71
+ }
72
+
73
+ /**
74
+ * Fetch milestone account data
75
+ *
76
+ * @param program - Anchor program instance
77
+ * @param projectId - Project identifier
78
+ * @param milestoneIndex - Milestone index
79
+ * @returns Milestone account data or null if not found
80
+ */
81
+ export async function fetchMilestone(
82
+ program: AnyProgram,
83
+ projectId: BN,
84
+ milestoneIndex: number
85
+ ) {
86
+ try {
87
+ const projectPda = getProjectPDA(projectId, program.programId);
88
+ const milestonePda = getMilestonePDA(projectPda, milestoneIndex, program.programId);
89
+ return await getAccountNamespace(program).milestone.fetch(milestonePda);
90
+ } catch (error) {
91
+ if (error instanceof Error && error.message?.includes('Account does not exist')) {
92
+ return null;
93
+ }
94
+ throw error;
95
+ }
96
+ }
97
+
98
+ /**
99
+ * Fetch all milestones for a project
100
+ *
101
+ * @param program - Anchor program instance
102
+ * @param projectId - Project identifier
103
+ * @returns Array of milestone accounts with their public keys
104
+ */
105
+ export async function fetchAllMilestones(
106
+ program: AnyProgram,
107
+ projectId: BN
108
+ ): Promise<MilestoneWithKey[]> {
109
+ const projectPda = getProjectPDA(projectId, program.programId);
110
+
111
+ const milestones = await getAccountNamespace(program).milestone.all([
112
+ {
113
+ memcmp: {
114
+ offset: 8, // Skip discriminator
115
+ bytes: projectPda.toBase58(),
116
+ },
117
+ },
118
+ ]);
119
+
120
+ return milestones.map((m: { publicKey: PublicKey; account: unknown }) => ({
121
+ publicKey: m.publicKey,
122
+ account: m.account,
123
+ })) as MilestoneWithKey[];
124
+ }
125
+
126
+ /**
127
+ * Fetch investment account data
128
+ *
129
+ * @param program - Anchor program instance
130
+ * @param projectId - Project identifier
131
+ * @param nftMint - Investment NFT mint address
132
+ * @returns Investment account data or null if not found
133
+ */
134
+ export async function fetchInvestment(
135
+ program: AnyProgram,
136
+ projectId: BN,
137
+ nftMint: PublicKey
138
+ ) {
139
+ try {
140
+ const projectPda = getProjectPDA(projectId, program.programId);
141
+ const investmentPda = getInvestmentPDA(projectPda, nftMint, program.programId);
142
+ return await getAccountNamespace(program).investment.fetch(investmentPda);
143
+ } catch (error) {
144
+ if (error instanceof Error && error.message?.includes('Account does not exist')) {
145
+ return null;
146
+ }
147
+ throw error;
148
+ }
149
+ }
150
+
151
+ /**
152
+ * Fetch all investments for a project
153
+ *
154
+ * @param program - Anchor program instance
155
+ * @param projectId - Project identifier
156
+ * @returns Array of investment accounts with their public keys
157
+ */
158
+ export async function fetchAllInvestments(
159
+ program: AnyProgram,
160
+ projectId: BN
161
+ ): Promise<InvestmentWithKey[]> {
162
+ const projectPda = getProjectPDA(projectId, program.programId);
163
+
164
+ const investments = await getAccountNamespace(program).investment.all([
165
+ {
166
+ memcmp: {
167
+ offset: 8, // Skip discriminator
168
+ bytes: projectPda.toBase58(),
169
+ },
170
+ },
171
+ ]);
172
+
173
+ return investments.map((inv: { publicKey: PublicKey; account: unknown }) => ({
174
+ publicKey: inv.publicKey,
175
+ account: inv.account,
176
+ })) as InvestmentWithKey[];
177
+ }
178
+
179
+ /**
180
+ * Fetch vote account data
181
+ *
182
+ * @param program - Anchor program instance
183
+ * @param projectId - Project identifier
184
+ * @param milestoneIndex - Milestone index
185
+ * @param voterKey - Voter's public key
186
+ * @param votingRound - Current voting round (0 initially, incremented on resubmit)
187
+ * @returns Vote account data or null if not found
188
+ */
189
+ export async function fetchVote(
190
+ program: AnyProgram,
191
+ projectId: BN,
192
+ milestoneIndex: number,
193
+ voterKey: PublicKey,
194
+ votingRound: number
195
+ ) {
196
+ try {
197
+ const projectPda = getProjectPDA(projectId, program.programId);
198
+ const milestonePda = getMilestonePDA(projectPda, milestoneIndex, program.programId);
199
+ const votePda = getVotePDA(milestonePda, voterKey, votingRound, program.programId);
200
+ return await getAccountNamespace(program).vote.fetch(votePda);
201
+ } catch (error) {
202
+ if (error instanceof Error && error.message?.includes('Account does not exist')) {
203
+ return null;
204
+ }
205
+ throw error;
206
+ }
207
+ }
208
+
209
+ /**
210
+ * Fetch all votes for a milestone
211
+ *
212
+ * @param program - Anchor program instance
213
+ * @param projectId - Project identifier
214
+ * @param milestoneIndex - Milestone index
215
+ * @returns Array of vote accounts with their public keys
216
+ */
217
+ export async function fetchAllVotes(
218
+ program: AnyProgram,
219
+ projectId: BN,
220
+ milestoneIndex: number
221
+ ): Promise<VoteWithKey[]> {
222
+ const projectPda = getProjectPDA(projectId, program.programId);
223
+ const milestonePda = getMilestonePDA(projectPda, milestoneIndex, program.programId);
224
+
225
+ const votes = await getAccountNamespace(program).vote.all([
226
+ {
227
+ memcmp: {
228
+ offset: 8, // Skip discriminator
229
+ bytes: milestonePda.toBase58(),
230
+ },
231
+ },
232
+ ]);
233
+
234
+ return votes.map((v: { publicKey: PublicKey; account: unknown }) => ({
235
+ publicKey: v.publicKey,
236
+ account: v.account,
237
+ })) as VoteWithKey[];
238
+ }
239
+
240
+ /**
241
+ * Fetch pivot proposal account data
242
+ *
243
+ * @param program - Anchor program instance
244
+ * @param projectId - Project identifier
245
+ * @returns PivotProposal account data or null if not found
246
+ */
247
+ export async function fetchPivotProposal(
248
+ program: AnyProgram,
249
+ projectId: BN
250
+ ) {
251
+ try {
252
+ const projectPda = getProjectPDA(projectId, program.programId);
253
+
254
+ // First fetch the project to get the active pivot or pivot_count
255
+ const projectAccount = await getAccountNamespace(program).project.fetch(projectPda);
256
+
257
+ // Use active_pivot if available, otherwise derive from pivot_count
258
+ let pivotPda;
259
+ if (projectAccount.activePivot) {
260
+ pivotPda = projectAccount.activePivot;
261
+ } else {
262
+ const pivotCount = projectAccount.pivotCount || 0;
263
+ pivotPda = getPivotProposalPDA(projectPda, pivotCount, program.programId);
264
+ }
265
+
266
+ return await getAccountNamespace(program).pivotProposal.fetch(pivotPda);
267
+ } catch (error) {
268
+ if (error instanceof Error && error.message?.includes('Account does not exist')) {
269
+ return null;
270
+ }
271
+ throw error;
272
+ }
273
+ }
274
+
275
+ /**
276
+ * Fetch TGE escrow account data
277
+ *
278
+ * @param program - Anchor program instance
279
+ * @param projectId - Project identifier
280
+ * @returns TgeEscrow account data or null if not found
281
+ */
282
+ export async function fetchTgeEscrow(
283
+ program: AnyProgram,
284
+ projectId: BN
285
+ ) {
286
+ try {
287
+ const projectPda = getProjectPDA(projectId, program.programId);
288
+ const tgeEscrowPda = getTgeEscrowPDA(projectPda, program.programId);
289
+ return await getAccountNamespace(program).tgeEscrow.fetch(tgeEscrowPda);
290
+ } catch (error) {
291
+ if (error instanceof Error && error.message?.includes('Account does not exist')) {
292
+ return null;
293
+ }
294
+ throw error;
295
+ }
296
+ }
297
+
298
+ /**
299
+ * Fetch admin config account data
300
+ *
301
+ * @param program - Anchor program instance
302
+ * @returns AdminConfig account data
303
+ */
304
+ export async function fetchAdminConfig(program: AnyProgram) {
305
+ const adminConfigPda = getAdminConfigPDA(program.programId);
306
+ return await getAccountNamespace(program).adminConfig.fetch(adminConfigPda);
307
+ }
308
+
309
+ /**
310
+ * Check if an account exists
311
+ *
312
+ * @param program - Anchor program instance
313
+ * @param accountType - Account type name
314
+ * @param pda - Account PDA
315
+ * @returns True if account exists
316
+ */
317
+ export async function accountExists(
318
+ program: AnyProgram,
319
+ accountType: string,
320
+ pda: PublicKey
321
+ ): Promise<boolean> {
322
+ try {
323
+ // @ts-expect-error - dynamic account access
324
+ await program.account[accountType].fetch(pda);
325
+ return true;
326
+ } catch {
327
+ return false;
328
+ }
329
+ }