@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.
- package/README.md +416 -0
- package/dist/accounts/index.cjs +258 -0
- package/dist/accounts/index.cjs.map +1 -0
- package/dist/accounts/index.d.cts +115 -0
- package/dist/accounts/index.d.ts +115 -0
- package/dist/accounts/index.js +245 -0
- package/dist/accounts/index.js.map +1 -0
- package/dist/constants/index.cjs +174 -0
- package/dist/constants/index.cjs.map +1 -0
- package/dist/constants/index.d.cts +143 -0
- package/dist/constants/index.d.ts +143 -0
- package/dist/constants/index.js +158 -0
- package/dist/constants/index.js.map +1 -0
- package/dist/errors/index.cjs +177 -0
- package/dist/errors/index.cjs.map +1 -0
- package/dist/errors/index.d.cts +83 -0
- package/dist/errors/index.d.ts +83 -0
- package/dist/errors/index.js +170 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/index.cjs +2063 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +680 -0
- package/dist/index.d.ts +680 -0
- package/dist/index.js +1926 -0
- package/dist/index.js.map +1 -0
- package/dist/instructions/index.cjs +852 -0
- package/dist/instructions/index.cjs.map +1 -0
- package/dist/instructions/index.d.cts +452 -0
- package/dist/instructions/index.d.ts +452 -0
- package/dist/instructions/index.js +809 -0
- package/dist/instructions/index.js.map +1 -0
- package/dist/pdas/index.cjs +241 -0
- package/dist/pdas/index.cjs.map +1 -0
- package/dist/pdas/index.d.cts +171 -0
- package/dist/pdas/index.d.ts +171 -0
- package/dist/pdas/index.js +217 -0
- package/dist/pdas/index.js.map +1 -0
- package/dist/types/index.cjs +44 -0
- package/dist/types/index.cjs.map +1 -0
- package/dist/types/index.d.cts +229 -0
- package/dist/types/index.d.ts +229 -0
- package/dist/types/index.js +39 -0
- package/dist/types/index.js.map +1 -0
- package/package.json +130 -0
- package/src/accounts/index.ts +329 -0
- package/src/client.ts +715 -0
- package/src/constants/index.ts +205 -0
- package/src/errors/index.ts +222 -0
- package/src/events/index.ts +256 -0
- package/src/index.ts +253 -0
- package/src/instructions/index.ts +1504 -0
- package/src/pdas/index.ts +404 -0
- package/src/types/index.ts +267 -0
- package/src/utils/index.ts +277 -0
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Raise Utilities
|
|
3
|
+
*
|
|
4
|
+
* Helper functions for common operations.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { BN } from '@coral-xyz/anchor';
|
|
8
|
+
import { PublicKey, Connection, TransactionSignature } from '@solana/web3.js';
|
|
9
|
+
|
|
10
|
+
// =============================================================================
|
|
11
|
+
// Transaction Utilities
|
|
12
|
+
// =============================================================================
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Wait for transaction confirmation
|
|
16
|
+
*
|
|
17
|
+
* @param connection - Solana connection
|
|
18
|
+
* @param signature - Transaction signature
|
|
19
|
+
* @param commitment - Confirmation commitment level
|
|
20
|
+
* @returns Confirmation result
|
|
21
|
+
*/
|
|
22
|
+
export async function confirmTransaction(
|
|
23
|
+
connection: Connection,
|
|
24
|
+
signature: TransactionSignature,
|
|
25
|
+
commitment: 'confirmed' | 'finalized' = 'confirmed'
|
|
26
|
+
) {
|
|
27
|
+
const latestBlockhash = await connection.getLatestBlockhash();
|
|
28
|
+
|
|
29
|
+
return connection.confirmTransaction(
|
|
30
|
+
{
|
|
31
|
+
signature,
|
|
32
|
+
blockhash: latestBlockhash.blockhash,
|
|
33
|
+
lastValidBlockHeight: latestBlockhash.lastValidBlockHeight,
|
|
34
|
+
},
|
|
35
|
+
commitment
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Get transaction details with retry
|
|
41
|
+
*
|
|
42
|
+
* @param connection - Solana connection
|
|
43
|
+
* @param signature - Transaction signature
|
|
44
|
+
* @param maxRetries - Maximum number of retries
|
|
45
|
+
* @returns Transaction details
|
|
46
|
+
*/
|
|
47
|
+
export async function getTransactionWithRetry(
|
|
48
|
+
connection: Connection,
|
|
49
|
+
signature: TransactionSignature,
|
|
50
|
+
maxRetries: number = 3
|
|
51
|
+
) {
|
|
52
|
+
for (let i = 0; i < maxRetries; i++) {
|
|
53
|
+
const tx = await connection.getTransaction(signature, {
|
|
54
|
+
maxSupportedTransactionVersion: 0,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
if (tx) {
|
|
58
|
+
return tx;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Wait before retrying
|
|
62
|
+
await new Promise((resolve) => setTimeout(resolve, 1000 * (i + 1)));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
throw new Error(`Transaction ${signature} not found after ${maxRetries} retries`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// =============================================================================
|
|
69
|
+
// BN Utilities
|
|
70
|
+
// =============================================================================
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Convert BN to number safely
|
|
74
|
+
*
|
|
75
|
+
* @param bn - BN value
|
|
76
|
+
* @returns Number value
|
|
77
|
+
* @throws If value is too large for safe integer
|
|
78
|
+
*/
|
|
79
|
+
export function bnToNumber(bn: BN): number {
|
|
80
|
+
const num = bn.toNumber();
|
|
81
|
+
if (!Number.isSafeInteger(num)) {
|
|
82
|
+
throw new Error(`BN value ${bn.toString()} is too large to convert to number`);
|
|
83
|
+
}
|
|
84
|
+
return num;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Convert BN to bigint
|
|
89
|
+
*
|
|
90
|
+
* @param bn - BN value
|
|
91
|
+
* @returns BigInt value
|
|
92
|
+
*/
|
|
93
|
+
export function bnToBigInt(bn: BN): bigint {
|
|
94
|
+
return BigInt(bn.toString());
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Convert bigint to BN
|
|
99
|
+
*
|
|
100
|
+
* @param value - BigInt value
|
|
101
|
+
* @returns BN value
|
|
102
|
+
*/
|
|
103
|
+
export function bigIntToBN(value: bigint): BN {
|
|
104
|
+
return new BN(value.toString());
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// =============================================================================
|
|
108
|
+
// Time Utilities
|
|
109
|
+
// =============================================================================
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Get current Unix timestamp in seconds
|
|
113
|
+
*
|
|
114
|
+
* @returns Current timestamp
|
|
115
|
+
*/
|
|
116
|
+
export function getCurrentTimestamp(): number {
|
|
117
|
+
return Math.floor(Date.now() / 1000);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Convert Unix timestamp to Date
|
|
122
|
+
*
|
|
123
|
+
* @param timestamp - Unix timestamp in seconds
|
|
124
|
+
* @returns Date object
|
|
125
|
+
*/
|
|
126
|
+
export function timestampToDate(timestamp: number | BN): Date {
|
|
127
|
+
const ts = typeof timestamp === 'number' ? timestamp : timestamp.toNumber();
|
|
128
|
+
return new Date(ts * 1000);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Check if a timestamp has passed
|
|
133
|
+
*
|
|
134
|
+
* @param timestamp - Unix timestamp in seconds
|
|
135
|
+
* @returns True if timestamp has passed
|
|
136
|
+
*/
|
|
137
|
+
export function hasTimestampPassed(timestamp: number | BN): boolean {
|
|
138
|
+
const ts = typeof timestamp === 'number' ? timestamp : timestamp.toNumber();
|
|
139
|
+
return getCurrentTimestamp() > ts;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Calculate time remaining until timestamp
|
|
144
|
+
*
|
|
145
|
+
* @param timestamp - Unix timestamp in seconds
|
|
146
|
+
* @returns Seconds remaining (0 if passed)
|
|
147
|
+
*/
|
|
148
|
+
export function timeRemaining(timestamp: number | BN): number {
|
|
149
|
+
const ts = typeof timestamp === 'number' ? timestamp : timestamp.toNumber();
|
|
150
|
+
const remaining = ts - getCurrentTimestamp();
|
|
151
|
+
return remaining > 0 ? remaining : 0;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Format duration in human-readable format
|
|
156
|
+
*
|
|
157
|
+
* @param seconds - Duration in seconds
|
|
158
|
+
* @returns Formatted string (e.g., "2d 5h 30m")
|
|
159
|
+
*/
|
|
160
|
+
export function formatDuration(seconds: number): string {
|
|
161
|
+
if (seconds <= 0) return '0s';
|
|
162
|
+
|
|
163
|
+
const days = Math.floor(seconds / 86400);
|
|
164
|
+
const hours = Math.floor((seconds % 86400) / 3600);
|
|
165
|
+
const minutes = Math.floor((seconds % 3600) / 60);
|
|
166
|
+
const secs = seconds % 60;
|
|
167
|
+
|
|
168
|
+
const parts: string[] = [];
|
|
169
|
+
if (days > 0) parts.push(`${days}d`);
|
|
170
|
+
if (hours > 0) parts.push(`${hours}h`);
|
|
171
|
+
if (minutes > 0) parts.push(`${minutes}m`);
|
|
172
|
+
if (secs > 0 && days === 0) parts.push(`${secs}s`);
|
|
173
|
+
|
|
174
|
+
return parts.join(' ') || '0s';
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// =============================================================================
|
|
178
|
+
// Percentage Utilities
|
|
179
|
+
// =============================================================================
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Calculate percentage from basis points
|
|
183
|
+
*
|
|
184
|
+
* @param bps - Basis points (1% = 100 bps)
|
|
185
|
+
* @returns Percentage as decimal
|
|
186
|
+
*/
|
|
187
|
+
export function bpsToPercent(bps: number): number {
|
|
188
|
+
return bps / 10000;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Calculate basis points from percentage
|
|
193
|
+
*
|
|
194
|
+
* @param percent - Percentage as decimal
|
|
195
|
+
* @returns Basis points
|
|
196
|
+
*/
|
|
197
|
+
export function percentToBps(percent: number): number {
|
|
198
|
+
return Math.floor(percent * 10000);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Calculate percentage of amount
|
|
203
|
+
*
|
|
204
|
+
* @param amount - Total amount
|
|
205
|
+
* @param percentage - Percentage (0-100)
|
|
206
|
+
* @returns Calculated amount
|
|
207
|
+
*/
|
|
208
|
+
export function percentageOf(amount: bigint, percentage: number): bigint {
|
|
209
|
+
return (amount * BigInt(Math.floor(percentage * 100))) / 10000n;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// =============================================================================
|
|
213
|
+
// Validation Utilities
|
|
214
|
+
// =============================================================================
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Validate milestone percentages sum to 100
|
|
218
|
+
*
|
|
219
|
+
* @param percentages - Array of percentage values
|
|
220
|
+
* @returns True if valid
|
|
221
|
+
*/
|
|
222
|
+
export function validateMilestonePercentages(percentages: number[]): boolean {
|
|
223
|
+
const sum = percentages.reduce((acc, p) => acc + p, 0);
|
|
224
|
+
return sum === 100;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Validate metadata URI format
|
|
229
|
+
*
|
|
230
|
+
* @param uri - URI string
|
|
231
|
+
* @param maxLength - Maximum allowed length
|
|
232
|
+
* @returns True if valid
|
|
233
|
+
*/
|
|
234
|
+
export function validateMetadataUri(uri: string, maxLength: number = 200): boolean {
|
|
235
|
+
if (uri.length > maxLength) return false;
|
|
236
|
+
|
|
237
|
+
try {
|
|
238
|
+
new URL(uri);
|
|
239
|
+
return true;
|
|
240
|
+
} catch {
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// =============================================================================
|
|
246
|
+
// Account Utilities
|
|
247
|
+
// =============================================================================
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Check if a public key is valid
|
|
251
|
+
*
|
|
252
|
+
* @param pubkey - String or PublicKey
|
|
253
|
+
* @returns True if valid
|
|
254
|
+
*/
|
|
255
|
+
export function isValidPublicKey(pubkey: string | PublicKey): boolean {
|
|
256
|
+
try {
|
|
257
|
+
if (typeof pubkey === 'string') {
|
|
258
|
+
new PublicKey(pubkey);
|
|
259
|
+
}
|
|
260
|
+
return true;
|
|
261
|
+
} catch {
|
|
262
|
+
return false;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Shorten a public key for display
|
|
268
|
+
*
|
|
269
|
+
* @param pubkey - Public key
|
|
270
|
+
* @param chars - Number of characters to show on each end
|
|
271
|
+
* @returns Shortened string (e.g., "ABC...XYZ")
|
|
272
|
+
*/
|
|
273
|
+
export function shortenPublicKey(pubkey: PublicKey | string, chars: number = 4): string {
|
|
274
|
+
const str = pubkey.toString();
|
|
275
|
+
if (str.length <= chars * 2 + 3) return str;
|
|
276
|
+
return `${str.slice(0, chars)}...${str.slice(-chars)}`;
|
|
277
|
+
}
|