@qorechain/svm 0.3.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 +118 -0
- package/dist/index.cjs +299 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +396 -0
- package/dist/index.d.ts +396 -0
- package/dist/index.js +274 -0
- package/dist/index.js.map +1 -0
- package/package.json +51 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
import { Commitment, Connection, PublicKey, AccountInfo, GetLatestBlockhashConfig, TokenAccountsFilter, Keypair, Transaction, ConfirmOptions, RpcResponseAndContext, SimulatedTransactionResponse, TransactionInstruction, AccountMeta, VersionedTransaction, AccountChangeCallback, LogsFilter, LogsCallback, SlotChangeCallback } from '@solana/web3.js';
|
|
2
|
+
export { SignatureResult } from '@solana/web3.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Client factory for QoreChain's Solana-compatible JSON-RPC.
|
|
6
|
+
*
|
|
7
|
+
* This is a thin convenience layer over `@solana/web3.js`: it builds (or accepts)
|
|
8
|
+
* a `Connection` bound to the network's SVM RPC endpoint and exposes typed
|
|
9
|
+
* wrappers over the documented Solana-compatible JSON-RPC method set plus
|
|
10
|
+
* transaction build/sign/send helpers.
|
|
11
|
+
*
|
|
12
|
+
* `@solana/web3.js` is a peer dependency; nothing here reimplements it.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** Subset of a qorechain-sdk network's endpoints relevant to the SVM runtime. */
|
|
16
|
+
interface SvmEndpoints {
|
|
17
|
+
/** Solana-compatible JSON-RPC HTTP endpoint. */
|
|
18
|
+
svmRpc: string;
|
|
19
|
+
}
|
|
20
|
+
/** Default testnet SVM RPC endpoint (Solana-compatible JSON-RPC, port 8899). */
|
|
21
|
+
declare const DEFAULT_SVM_RPC_URL = "http://localhost:8899";
|
|
22
|
+
/** Options for {@link createSvmClient}. */
|
|
23
|
+
interface CreateSvmClientOptions {
|
|
24
|
+
/** Solana-compatible JSON-RPC HTTP URL. Mutually exclusive with `endpoints`. */
|
|
25
|
+
rpcUrl?: string;
|
|
26
|
+
/** A qorechain-sdk network endpoints object (uses `svmRpc`). */
|
|
27
|
+
endpoints?: SvmEndpoints;
|
|
28
|
+
/** Default commitment level for the connection. */
|
|
29
|
+
commitment?: Commitment;
|
|
30
|
+
/**
|
|
31
|
+
* A preconstructed `Connection`. When provided it is used as-is and `rpcUrl`/
|
|
32
|
+
* `endpoints`/`commitment` are ignored — primarily for testing.
|
|
33
|
+
*/
|
|
34
|
+
connection?: Connection;
|
|
35
|
+
}
|
|
36
|
+
/** Arguments for building/sending a SOL transfer. */
|
|
37
|
+
interface TransferSolArgs {
|
|
38
|
+
/** Funding/source keypair (signer). */
|
|
39
|
+
from: Keypair;
|
|
40
|
+
/** Destination address. */
|
|
41
|
+
to: PublicKey;
|
|
42
|
+
/** Amount in lamports. */
|
|
43
|
+
lamports: number | bigint;
|
|
44
|
+
}
|
|
45
|
+
/** A configured QoreChain SVM client bundle. */
|
|
46
|
+
interface SvmClient {
|
|
47
|
+
/** The underlying `@solana/web3.js` `Connection`. */
|
|
48
|
+
connection: Connection;
|
|
49
|
+
/** Lamport balance for an account. */
|
|
50
|
+
getBalance(pubkey: PublicKey, commitment?: Commitment): Promise<number>;
|
|
51
|
+
/** Raw account info, or `null` if the account does not exist. */
|
|
52
|
+
getAccountInfo(pubkey: PublicKey, commitment?: Commitment): Promise<AccountInfo<Buffer> | null>;
|
|
53
|
+
/** Latest blockhash and last valid block height. */
|
|
54
|
+
getLatestBlockhash(commitmentOrConfig?: Commitment | GetLatestBlockhashConfig): Promise<{
|
|
55
|
+
blockhash: string;
|
|
56
|
+
lastValidBlockHeight: number;
|
|
57
|
+
}>;
|
|
58
|
+
/** Token accounts owned by `owner`, filtered by mint or program id. */
|
|
59
|
+
getTokenAccountsByOwner(owner: PublicKey, filter: TokenAccountsFilter, commitment?: Commitment): ReturnType<Connection["getTokenAccountsByOwner"]>;
|
|
60
|
+
/** Confirmed signatures for transactions involving `address`. */
|
|
61
|
+
getSignaturesForAddress(address: PublicKey, options?: Parameters<Connection["getSignaturesForAddress"]>[1]): ReturnType<Connection["getSignaturesForAddress"]>;
|
|
62
|
+
/** Request an airdrop of `lamports` to `pubkey` (testnet/devnet only). */
|
|
63
|
+
requestAirdrop(pubkey: PublicKey, lamports: number): Promise<string>;
|
|
64
|
+
/** Build (unsigned, no blockhash) a SOL transfer transaction. */
|
|
65
|
+
buildTransferSol(args: TransferSolArgs): Transaction;
|
|
66
|
+
/** Build, sign, send, and confirm a SOL transfer. */
|
|
67
|
+
transferSol(args: TransferSolArgs, options?: ConfirmOptions): Promise<string>;
|
|
68
|
+
/** Sign, send, and confirm an arbitrary transaction. */
|
|
69
|
+
sendTransaction(tx: Transaction, signers: Keypair[], options?: ConfirmOptions): Promise<string>;
|
|
70
|
+
/** Simulate a transaction without submitting it. */
|
|
71
|
+
simulateTransaction(tx: Transaction): Promise<RpcResponseAndContext<SimulatedTransactionResponse>>;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Create a QoreChain SVM client bundle.
|
|
75
|
+
*
|
|
76
|
+
* With no options it targets the testnet localhost endpoint
|
|
77
|
+
* (`http://localhost:8899`) at `confirmed` commitment.
|
|
78
|
+
*/
|
|
79
|
+
declare function createSvmClient(opts?: CreateSvmClientOptions): SvmClient;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Key helpers for QoreChain's Solana-compatible runtime.
|
|
83
|
+
*
|
|
84
|
+
* These are thin wrappers over `@solana/web3.js` so callers don't need a second
|
|
85
|
+
* import path. `@qorechain/sdk`'s `deriveSvmAccount(mnemonic)` provides the
|
|
86
|
+
* 64-byte `secretKey` you can pass straight into {@link svmKeypairFromSecretKey}.
|
|
87
|
+
*/
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Reconstruct a `@solana/web3.js` `Keypair` from a 64-byte ed25519 secret key
|
|
91
|
+
* (the standard Solana secret-key encoding: 32-byte seed + 32-byte public key).
|
|
92
|
+
*
|
|
93
|
+
* Pair with `@qorechain/sdk`'s `deriveSvmAccount`, which returns this exact
|
|
94
|
+
* 64-byte `secretKey`.
|
|
95
|
+
*/
|
|
96
|
+
declare function svmKeypairFromSecretKey(secretKey: Uint8Array): Keypair;
|
|
97
|
+
/** Return the base58 address for a `Keypair` or a `PublicKey`. */
|
|
98
|
+
declare function svmAddress(key: Keypair | PublicKey): string;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Well-known native program ids for QoreChain's Solana-compatible runtime, plus
|
|
102
|
+
* thin instruction builders for the Memo and SPL-Token programs and the
|
|
103
|
+
* Associated Token Account program.
|
|
104
|
+
*
|
|
105
|
+
* The program ids are the exact canonical ones used by Solana-compatible
|
|
106
|
+
* runtimes, so standard wallets, explorers, and tooling interoperate directly.
|
|
107
|
+
* The SPL-Token / ATA / Memo instructions are built minimally here (by hand)
|
|
108
|
+
* to keep the dependency surface to a single peer — `@solana/web3.js` — with no
|
|
109
|
+
* `@solana/spl-token` requirement.
|
|
110
|
+
*/
|
|
111
|
+
|
|
112
|
+
/** System program (account creation, SOL transfers). */
|
|
113
|
+
declare const SYSTEM_PROGRAM_ID: PublicKey;
|
|
114
|
+
/** SPL-Token program. */
|
|
115
|
+
declare const TOKEN_PROGRAM_ID: PublicKey;
|
|
116
|
+
/** Associated Token Account program. */
|
|
117
|
+
declare const ASSOCIATED_TOKEN_PROGRAM_ID: PublicKey;
|
|
118
|
+
/** SPL Memo program. */
|
|
119
|
+
declare const MEMO_PROGRAM_ID: PublicKey;
|
|
120
|
+
/** Convenience grouping of the well-known program ids. */
|
|
121
|
+
declare const PROGRAM_IDS: {
|
|
122
|
+
readonly system: PublicKey;
|
|
123
|
+
readonly token: PublicKey;
|
|
124
|
+
readonly associatedToken: PublicKey;
|
|
125
|
+
readonly memo: PublicKey;
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* Build a Memo program instruction carrying `text` (encoded UTF-8).
|
|
129
|
+
*
|
|
130
|
+
* @param text Memo contents.
|
|
131
|
+
* @param signers Optional accounts that must sign the memo; each is included as
|
|
132
|
+
* a read-only signer (the Memo program's convention).
|
|
133
|
+
*/
|
|
134
|
+
declare function createMemoInstruction(text: string, signers?: PublicKey[]): TransactionInstruction;
|
|
135
|
+
/** Arguments for {@link createTransferTokenInstruction}. */
|
|
136
|
+
interface TransferTokenInstructionArgs {
|
|
137
|
+
/** Source SPL-Token account. */
|
|
138
|
+
source: PublicKey;
|
|
139
|
+
/** Destination SPL-Token account. */
|
|
140
|
+
destination: PublicKey;
|
|
141
|
+
/** Owner/authority of the source account (signer). */
|
|
142
|
+
owner: PublicKey;
|
|
143
|
+
/** Amount in the token's base units. */
|
|
144
|
+
amount: bigint;
|
|
145
|
+
/** Additional multisig signers, if `owner` is a multisig. */
|
|
146
|
+
multiSigners?: PublicKey[];
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Build an SPL-Token `Transfer` instruction.
|
|
150
|
+
*
|
|
151
|
+
* Layout: 1 byte instruction discriminator (`3` = Transfer) followed by a
|
|
152
|
+
* little-endian u64 amount.
|
|
153
|
+
*/
|
|
154
|
+
declare function createTransferTokenInstruction(args: TransferTokenInstructionArgs): TransactionInstruction;
|
|
155
|
+
/**
|
|
156
|
+
* Derive the deterministic Associated Token Account (ATA) address for an owner
|
|
157
|
+
* and mint under the SPL-Token program.
|
|
158
|
+
*/
|
|
159
|
+
declare function getAssociatedTokenAddress(mint: PublicKey, owner: PublicKey, tokenProgramId?: PublicKey): PublicKey;
|
|
160
|
+
/** Arguments for {@link createAssociatedTokenAccountInstruction}. */
|
|
161
|
+
interface CreateAssociatedTokenAccountInstructionArgs {
|
|
162
|
+
/** Account funding the rent for the new ATA (signer, writable). */
|
|
163
|
+
payer: PublicKey;
|
|
164
|
+
/** Wallet that will own the new ATA. */
|
|
165
|
+
owner: PublicKey;
|
|
166
|
+
/** Token mint. */
|
|
167
|
+
mint: PublicKey;
|
|
168
|
+
/** SPL-Token program id (defaults to the canonical SPL-Token program). */
|
|
169
|
+
tokenProgramId?: PublicKey;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Build an instruction that creates the Associated Token Account for
|
|
173
|
+
* `(owner, mint)`. The ATA address is derived deterministically.
|
|
174
|
+
*/
|
|
175
|
+
declare function createAssociatedTokenAccountInstruction(args: CreateAssociatedTokenAccountInstructionArgs): TransactionInstruction;
|
|
176
|
+
/**
|
|
177
|
+
* Generic instruction builder for invoking an arbitrary on-chain program.
|
|
178
|
+
*
|
|
179
|
+
* Use this with {@link createSvmClient}'s `sendTransaction` to call a deployed
|
|
180
|
+
* BPF program. Deploy programs with standard Solana tooling (e.g.
|
|
181
|
+
* `solana program deploy`); this package intentionally does not wrap the loader.
|
|
182
|
+
*/
|
|
183
|
+
declare function createInvokeInstruction(programId: PublicKey, keys: AccountMeta[], data: Uint8Array): TransactionInstruction;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* SVM browser-wallet integration for QoreChain (Phantom and Wallet-Standard).
|
|
187
|
+
*
|
|
188
|
+
* Adapts an injected Solana provider (Phantom's `window.solana` is the baseline)
|
|
189
|
+
* into a shape usable with `@solana/web3.js`, so apps can sign and send via the
|
|
190
|
+
* existing SVM client.
|
|
191
|
+
*
|
|
192
|
+
* ```ts
|
|
193
|
+
* import { getSvmWallet } from "@qorechain/svm";
|
|
194
|
+
* import { createSvmClient } from "@qorechain/svm";
|
|
195
|
+
*
|
|
196
|
+
* const wallet = await getSvmWallet();
|
|
197
|
+
* const signed = await wallet.signTransaction(tx);
|
|
198
|
+
* // ...then send with createSvmClient(...).sendRawTransaction(...)
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
|
|
202
|
+
/** A transaction the wallet can sign (legacy or versioned). */
|
|
203
|
+
type SvmSignableTransaction = Transaction | VersionedTransaction;
|
|
204
|
+
/**
|
|
205
|
+
* The minimal injected Solana-wallet surface used here. Phantom (and most
|
|
206
|
+
* Wallet-Standard providers) implement at least these. Declared structurally so
|
|
207
|
+
* no wallet types package is required.
|
|
208
|
+
*/
|
|
209
|
+
interface InjectedSvmWallet {
|
|
210
|
+
/** Phantom marker; present on the Phantom provider. */
|
|
211
|
+
isPhantom?: boolean;
|
|
212
|
+
/** Connect and return the account public key. */
|
|
213
|
+
connect(opts?: {
|
|
214
|
+
onlyIfTrusted?: boolean;
|
|
215
|
+
}): Promise<{
|
|
216
|
+
publicKey: PublicKey;
|
|
217
|
+
}>;
|
|
218
|
+
/** Disconnect the wallet. */
|
|
219
|
+
disconnect?(): Promise<void>;
|
|
220
|
+
/** The connected account public key (populated after `connect`). */
|
|
221
|
+
publicKey?: PublicKey | null;
|
|
222
|
+
/** Sign a single transaction. */
|
|
223
|
+
signTransaction<T extends SvmSignableTransaction>(tx: T): Promise<T>;
|
|
224
|
+
/** Sign multiple transactions. */
|
|
225
|
+
signAllTransactions?<T extends SvmSignableTransaction>(txs: T[]): Promise<T[]>;
|
|
226
|
+
}
|
|
227
|
+
/** Options for {@link getSvmWallet}. */
|
|
228
|
+
interface GetSvmWalletOptions {
|
|
229
|
+
/** The injected provider to use. Defaults to `window.solana` (Phantom). */
|
|
230
|
+
provider?: InjectedSvmWallet;
|
|
231
|
+
/** Pass through to the provider's `connect` (e.g. silent reconnect). */
|
|
232
|
+
onlyIfTrusted?: boolean;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* A connected SVM wallet adapted for use with `@solana/web3.js`.
|
|
236
|
+
*
|
|
237
|
+
* `signTransaction` / `signAllTransactions` accept and return web3.js
|
|
238
|
+
* `Transaction` / `VersionedTransaction` objects, so they drop into the existing
|
|
239
|
+
* SVM client's send path.
|
|
240
|
+
*/
|
|
241
|
+
interface SvmWalletConnection {
|
|
242
|
+
/** The connected account public key. */
|
|
243
|
+
publicKey: PublicKey;
|
|
244
|
+
/** Sign a single transaction. */
|
|
245
|
+
signTransaction<T extends SvmSignableTransaction>(tx: T): Promise<T>;
|
|
246
|
+
/** Sign multiple transactions (falls back to sequential signing). */
|
|
247
|
+
signAllTransactions<T extends SvmSignableTransaction>(txs: T[]): Promise<T[]>;
|
|
248
|
+
/** The underlying injected provider. */
|
|
249
|
+
provider: InjectedSvmWallet;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Connect to an injected Solana wallet (Phantom by default) and return an
|
|
253
|
+
* adapter usable with `@solana/web3.js`.
|
|
254
|
+
*
|
|
255
|
+
* Browser-only: throws a clear error if no `window` or no injected provider.
|
|
256
|
+
*/
|
|
257
|
+
declare function getSvmWallet(opts?: GetSvmWalletOptions): Promise<SvmWalletConnection>;
|
|
258
|
+
/**
|
|
259
|
+
* Detect a Wallet-Standard / Phantom Solana provider on `window`.
|
|
260
|
+
*
|
|
261
|
+
* Returns the injected provider if present, else `undefined`. Phantom's
|
|
262
|
+
* `window.solana` is the baseline; this is a light convenience for apps that
|
|
263
|
+
* want to feature-detect before prompting a connection.
|
|
264
|
+
*/
|
|
265
|
+
declare function detectSvmProvider(): InjectedSvmWallet | undefined;
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Human-readable decoding of SVM (Solana-compatible) transaction errors.
|
|
269
|
+
*
|
|
270
|
+
* `@solana/web3.js` throws a `SendTransactionError` (and related shapes) whose
|
|
271
|
+
* useful detail — the failing instruction index, a program's custom error code,
|
|
272
|
+
* and the program logs — is spread across several fields that differ slightly
|
|
273
|
+
* between library versions. {@link decodeSvmError} reads those fields
|
|
274
|
+
* structurally (so it works across versions) and produces a flat summary with
|
|
275
|
+
* the program logs and, where present, the failing instruction index.
|
|
276
|
+
*/
|
|
277
|
+
/** A decoded SVM error. */
|
|
278
|
+
interface DecodedSvmError {
|
|
279
|
+
/** A readable summary message. */
|
|
280
|
+
message: string;
|
|
281
|
+
/** A short, stable kind discriminator. */
|
|
282
|
+
kind: "instruction_error" | "simulation" | "send" | "unknown";
|
|
283
|
+
/** Zero-based index of the failing instruction, when determinable. */
|
|
284
|
+
instructionIndex?: number;
|
|
285
|
+
/** A program's custom error code, when present in the logs. */
|
|
286
|
+
customErrorCode?: number;
|
|
287
|
+
/** The program logs, when available. */
|
|
288
|
+
logs?: string[];
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Decode an SVM error thrown by `@solana/web3.js` into a readable, structured
|
|
292
|
+
* form.
|
|
293
|
+
*
|
|
294
|
+
* @param error - The thrown error (a `SendTransactionError` or any value).
|
|
295
|
+
*/
|
|
296
|
+
declare function decodeSvmError(error: unknown): DecodedSvmError;
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Real-time SVM subscriptions over the `@solana/web3.js` `Connection`.
|
|
300
|
+
*
|
|
301
|
+
* The `Connection` owns the websocket and its lifecycle; these helpers are thin
|
|
302
|
+
* wrappers that register a listener and return both the numeric subscription id
|
|
303
|
+
* and an `off()` function that removes it via the matching
|
|
304
|
+
* `remove*Listener` call. This keeps callers from having to remember which
|
|
305
|
+
* removal method pairs with which subscription.
|
|
306
|
+
*/
|
|
307
|
+
|
|
308
|
+
/** A registered subscription: its id and a function to remove it. */
|
|
309
|
+
interface SvmSubscription {
|
|
310
|
+
/** The numeric subscription id returned by `@solana/web3.js`. */
|
|
311
|
+
id: number;
|
|
312
|
+
/** Remove the listener (idempotent best-effort). */
|
|
313
|
+
off(): Promise<void>;
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Subscribe to transaction logs matching `filter`.
|
|
317
|
+
*
|
|
318
|
+
* @param connection - A `@solana/web3.js` `Connection`.
|
|
319
|
+
* @param filter - A `LogsFilter` (`"all"`, `"allWithVotes"`, or a `PublicKey`).
|
|
320
|
+
* @param callback - Invoked with each matching logs notification.
|
|
321
|
+
* @param commitment - Optional commitment level.
|
|
322
|
+
*/
|
|
323
|
+
declare function onLogs(connection: Connection, filter: LogsFilter, callback: LogsCallback, commitment?: Commitment): SvmSubscription;
|
|
324
|
+
/**
|
|
325
|
+
* Subscribe to changes of an account's data.
|
|
326
|
+
*
|
|
327
|
+
* @param connection - A `@solana/web3.js` `Connection`.
|
|
328
|
+
* @param pubkey - The account to watch.
|
|
329
|
+
* @param callback - Invoked with each account change.
|
|
330
|
+
* @param commitment - Optional commitment level.
|
|
331
|
+
*/
|
|
332
|
+
declare function onAccountChange(connection: Connection, pubkey: PublicKey, callback: AccountChangeCallback, commitment?: Commitment): SvmSubscription;
|
|
333
|
+
/**
|
|
334
|
+
* Subscribe to slot changes (new slots as they are processed).
|
|
335
|
+
*
|
|
336
|
+
* @param connection - A `@solana/web3.js` `Connection`.
|
|
337
|
+
* @param callback - Invoked with each slot change.
|
|
338
|
+
*/
|
|
339
|
+
declare function onSlotChange(connection: Connection, callback: SlotChangeCallback): SvmSubscription;
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Compute-budget and priority-fee conveniences over `@solana/web3.js`.
|
|
343
|
+
*
|
|
344
|
+
* {@link withComputeBudget} prepends `ComputeBudgetProgram` instructions to a
|
|
345
|
+
* legacy `Transaction` so it requests a compute-unit limit and pays a per-CU
|
|
346
|
+
* priority price; {@link estimatePriorityFee} reads recent prioritization fees
|
|
347
|
+
* from the RPC to suggest a `microLamports` price.
|
|
348
|
+
*/
|
|
349
|
+
|
|
350
|
+
/** Compute-budget options for {@link withComputeBudget}. */
|
|
351
|
+
interface ComputeBudgetOptions {
|
|
352
|
+
/** Compute-unit limit to request (`setComputeUnitLimit`). */
|
|
353
|
+
units?: number;
|
|
354
|
+
/** Per-compute-unit price in micro-lamports (`setComputeUnitPrice`). */
|
|
355
|
+
microLamports?: number | bigint;
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Prepend `ComputeBudgetProgram` instructions to `tx` (mutating it in place and
|
|
359
|
+
* returning it). A unit-limit instruction is added when `units` is given, and a
|
|
360
|
+
* unit-price instruction when `microLamports` is given; both are inserted at the
|
|
361
|
+
* front of the instruction list, as required by the runtime.
|
|
362
|
+
*
|
|
363
|
+
* @returns The same `tx`, for chaining.
|
|
364
|
+
*/
|
|
365
|
+
declare function withComputeBudget(tx: Transaction, options: ComputeBudgetOptions): Transaction;
|
|
366
|
+
/**
|
|
367
|
+
* Estimate a priority fee (`microLamports` per CU) from the RPC's
|
|
368
|
+
* `getRecentPrioritizationFees`. Returns the maximum recently observed fee,
|
|
369
|
+
* which is a conservative choice for landing a transaction quickly. Returns `0`
|
|
370
|
+
* when the RPC reports no recent fees.
|
|
371
|
+
*
|
|
372
|
+
* @param connection - SVM RPC connection.
|
|
373
|
+
* @param accounts - Optional accounts the tx will lock, scoping the query.
|
|
374
|
+
*/
|
|
375
|
+
declare function estimatePriorityFee(connection: Connection, accounts?: readonly PublicKey[]): Promise<number>;
|
|
376
|
+
/** Namespaced compute-budget / fee helpers. */
|
|
377
|
+
declare const fees: {
|
|
378
|
+
readonly withComputeBudget: typeof withComputeBudget;
|
|
379
|
+
readonly estimatePriorityFee: typeof estimatePriorityFee;
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* `@qorechain/svm` — a thin, type-safe adapter over
|
|
384
|
+
* [`@solana/web3.js`](https://solana-labs.github.io/solana-web3.js/) for
|
|
385
|
+
* QoreChain's Solana-compatible JSON-RPC.
|
|
386
|
+
*
|
|
387
|
+
* It does not reimplement an SVM client; `@solana/web3.js` is a peer dependency.
|
|
388
|
+
* This package adds QoreChain-specific conveniences: a client factory targeting
|
|
389
|
+
* the SVM RPC endpoint, key helpers, typed read wrappers, SOL transfer
|
|
390
|
+
* build/sign/send, and minimal native-program instruction builders (Memo,
|
|
391
|
+
* SPL-Token, Associated Token Account) plus a generic program-invoke builder.
|
|
392
|
+
*/
|
|
393
|
+
/** Package version. */
|
|
394
|
+
declare const VERSION = "0.3.0";
|
|
395
|
+
|
|
396
|
+
export { ASSOCIATED_TOKEN_PROGRAM_ID, type ComputeBudgetOptions, type CreateAssociatedTokenAccountInstructionArgs, type CreateSvmClientOptions, DEFAULT_SVM_RPC_URL, type DecodedSvmError, type GetSvmWalletOptions, type InjectedSvmWallet, MEMO_PROGRAM_ID, PROGRAM_IDS, SYSTEM_PROGRAM_ID, type SvmClient, type SvmEndpoints, type SvmSignableTransaction, type SvmSubscription, type SvmWalletConnection, TOKEN_PROGRAM_ID, type TransferSolArgs, type TransferTokenInstructionArgs, VERSION, createAssociatedTokenAccountInstruction, createInvokeInstruction, createMemoInstruction, createSvmClient, createTransferTokenInstruction, decodeSvmError, detectSvmProvider, estimatePriorityFee, fees, getAssociatedTokenAddress, getSvmWallet, onAccountChange, onLogs, onSlotChange, svmAddress, svmKeypairFromSecretKey, withComputeBudget };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import { PublicKey, Keypair, TransactionInstruction, ComputeBudgetProgram, Transaction, SystemProgram, sendAndConfirmTransaction, Connection } from '@solana/web3.js';
|
|
2
|
+
|
|
3
|
+
// src/client.ts
|
|
4
|
+
var DEFAULT_SVM_RPC_URL = "http://localhost:8899";
|
|
5
|
+
function resolveConnection(opts) {
|
|
6
|
+
if (opts.connection) return opts.connection;
|
|
7
|
+
const url = opts.rpcUrl ?? opts.endpoints?.svmRpc ?? DEFAULT_SVM_RPC_URL;
|
|
8
|
+
return new Connection(url, opts.commitment ?? "confirmed");
|
|
9
|
+
}
|
|
10
|
+
function createSvmClient(opts = {}) {
|
|
11
|
+
const connection = resolveConnection(opts);
|
|
12
|
+
const buildTransferSol = (args) => {
|
|
13
|
+
const tx = new Transaction();
|
|
14
|
+
tx.add(
|
|
15
|
+
SystemProgram.transfer({
|
|
16
|
+
fromPubkey: args.from.publicKey,
|
|
17
|
+
toPubkey: args.to,
|
|
18
|
+
lamports: args.lamports
|
|
19
|
+
})
|
|
20
|
+
);
|
|
21
|
+
return tx;
|
|
22
|
+
};
|
|
23
|
+
const sendTransaction = (tx, signers, options) => sendAndConfirmTransaction(connection, tx, signers, options);
|
|
24
|
+
return {
|
|
25
|
+
connection,
|
|
26
|
+
getBalance: (pubkey, commitment) => connection.getBalance(pubkey, commitment),
|
|
27
|
+
getAccountInfo: (pubkey, commitment) => connection.getAccountInfo(pubkey, commitment),
|
|
28
|
+
getLatestBlockhash: (commitmentOrConfig) => connection.getLatestBlockhash(commitmentOrConfig),
|
|
29
|
+
getTokenAccountsByOwner: (owner, filter, commitment) => connection.getTokenAccountsByOwner(owner, filter, commitment),
|
|
30
|
+
getSignaturesForAddress: (address, options) => connection.getSignaturesForAddress(address, options),
|
|
31
|
+
requestAirdrop: (pubkey, lamports) => connection.requestAirdrop(pubkey, lamports),
|
|
32
|
+
buildTransferSol,
|
|
33
|
+
transferSol: (args, options) => sendTransaction(buildTransferSol(args), [args.from], options),
|
|
34
|
+
sendTransaction,
|
|
35
|
+
simulateTransaction: (tx) => (
|
|
36
|
+
// The single-arg overload signs/derives blockhash as needed for v1
|
|
37
|
+
// legacy transactions.
|
|
38
|
+
connection.simulateTransaction(tx)
|
|
39
|
+
)
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function svmKeypairFromSecretKey(secretKey) {
|
|
43
|
+
if (secretKey.length !== 64) {
|
|
44
|
+
throw new Error(
|
|
45
|
+
`svmKeypairFromSecretKey: expected a 64-byte secret key, got ${secretKey.length}`
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
return Keypair.fromSecretKey(secretKey);
|
|
49
|
+
}
|
|
50
|
+
function svmAddress(key) {
|
|
51
|
+
const pubkey = key instanceof PublicKey ? key : key.publicKey;
|
|
52
|
+
return pubkey.toBase58();
|
|
53
|
+
}
|
|
54
|
+
var SYSTEM_PROGRAM_ID = new PublicKey("11111111111111111111111111111111");
|
|
55
|
+
var TOKEN_PROGRAM_ID = new PublicKey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
|
|
56
|
+
var ASSOCIATED_TOKEN_PROGRAM_ID = new PublicKey(
|
|
57
|
+
"ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"
|
|
58
|
+
);
|
|
59
|
+
var MEMO_PROGRAM_ID = new PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr");
|
|
60
|
+
var PROGRAM_IDS = {
|
|
61
|
+
system: SYSTEM_PROGRAM_ID,
|
|
62
|
+
token: TOKEN_PROGRAM_ID,
|
|
63
|
+
associatedToken: ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
64
|
+
memo: MEMO_PROGRAM_ID
|
|
65
|
+
};
|
|
66
|
+
function createMemoInstruction(text, signers = []) {
|
|
67
|
+
return new TransactionInstruction({
|
|
68
|
+
programId: MEMO_PROGRAM_ID,
|
|
69
|
+
keys: signers.map((pubkey) => ({ pubkey, isSigner: true, isWritable: false })),
|
|
70
|
+
data: Buffer.from(text, "utf8")
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
function createTransferTokenInstruction(args) {
|
|
74
|
+
const { source, destination, owner, amount, multiSigners = [] } = args;
|
|
75
|
+
const data = Buffer.alloc(9);
|
|
76
|
+
data.writeUInt8(3, 0);
|
|
77
|
+
data.writeBigUInt64LE(amount, 1);
|
|
78
|
+
const keys = [
|
|
79
|
+
{ pubkey: source, isSigner: false, isWritable: true },
|
|
80
|
+
{ pubkey: destination, isSigner: false, isWritable: true },
|
|
81
|
+
{
|
|
82
|
+
pubkey: owner,
|
|
83
|
+
isSigner: multiSigners.length === 0,
|
|
84
|
+
isWritable: false
|
|
85
|
+
},
|
|
86
|
+
...multiSigners.map((pubkey) => ({ pubkey, isSigner: true, isWritable: false }))
|
|
87
|
+
];
|
|
88
|
+
return new TransactionInstruction({ programId: TOKEN_PROGRAM_ID, keys, data });
|
|
89
|
+
}
|
|
90
|
+
function getAssociatedTokenAddress(mint, owner, tokenProgramId = TOKEN_PROGRAM_ID) {
|
|
91
|
+
const [address] = PublicKey.findProgramAddressSync(
|
|
92
|
+
[owner.toBuffer(), tokenProgramId.toBuffer(), mint.toBuffer()],
|
|
93
|
+
ASSOCIATED_TOKEN_PROGRAM_ID
|
|
94
|
+
);
|
|
95
|
+
return address;
|
|
96
|
+
}
|
|
97
|
+
function createAssociatedTokenAccountInstruction(args) {
|
|
98
|
+
const { payer, owner, mint, tokenProgramId = TOKEN_PROGRAM_ID } = args;
|
|
99
|
+
const ata = getAssociatedTokenAddress(mint, owner, tokenProgramId);
|
|
100
|
+
const keys = [
|
|
101
|
+
{ pubkey: payer, isSigner: true, isWritable: true },
|
|
102
|
+
{ pubkey: ata, isSigner: false, isWritable: true },
|
|
103
|
+
{ pubkey: owner, isSigner: false, isWritable: false },
|
|
104
|
+
{ pubkey: mint, isSigner: false, isWritable: false },
|
|
105
|
+
{ pubkey: SYSTEM_PROGRAM_ID, isSigner: false, isWritable: false },
|
|
106
|
+
{ pubkey: tokenProgramId, isSigner: false, isWritable: false }
|
|
107
|
+
];
|
|
108
|
+
return new TransactionInstruction({
|
|
109
|
+
programId: ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
110
|
+
keys,
|
|
111
|
+
data: Buffer.alloc(0)
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
function createInvokeInstruction(programId, keys, data) {
|
|
115
|
+
return new TransactionInstruction({
|
|
116
|
+
programId,
|
|
117
|
+
keys,
|
|
118
|
+
data: Buffer.from(data)
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// src/wallet.ts
|
|
123
|
+
function resolveProvider(provider) {
|
|
124
|
+
if (provider) return provider;
|
|
125
|
+
if (typeof window === "undefined") {
|
|
126
|
+
throw new Error(
|
|
127
|
+
"getSvmWallet: no browser `window` available. Run in a browser, or pass `provider` explicitly."
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
const injected = window.solana;
|
|
131
|
+
if (!injected) {
|
|
132
|
+
throw new Error(
|
|
133
|
+
"getSvmWallet: no injected Solana provider found (window.solana). Install Phantom, or pass `provider`."
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
return injected;
|
|
137
|
+
}
|
|
138
|
+
async function getSvmWallet(opts = {}) {
|
|
139
|
+
const provider = resolveProvider(opts.provider);
|
|
140
|
+
const { publicKey } = await provider.connect(
|
|
141
|
+
opts.onlyIfTrusted ? { onlyIfTrusted: true } : void 0
|
|
142
|
+
);
|
|
143
|
+
if (!publicKey) {
|
|
144
|
+
throw new Error("getSvmWallet: wallet connected but returned no public key");
|
|
145
|
+
}
|
|
146
|
+
const signTransaction = (tx) => provider.signTransaction(tx);
|
|
147
|
+
const signAllTransactions = async (txs) => {
|
|
148
|
+
if (provider.signAllTransactions) return provider.signAllTransactions(txs);
|
|
149
|
+
const out = [];
|
|
150
|
+
for (const tx of txs) out.push(await provider.signTransaction(tx));
|
|
151
|
+
return out;
|
|
152
|
+
};
|
|
153
|
+
return { publicKey, signTransaction, signAllTransactions, provider };
|
|
154
|
+
}
|
|
155
|
+
function detectSvmProvider() {
|
|
156
|
+
if (typeof window === "undefined") return void 0;
|
|
157
|
+
return window.solana;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// src/errors.ts
|
|
161
|
+
function extractLogs(err) {
|
|
162
|
+
const e = err;
|
|
163
|
+
const logs = e?.logs ?? e?.transactionLogs;
|
|
164
|
+
if (Array.isArray(logs)) return logs;
|
|
165
|
+
return void 0;
|
|
166
|
+
}
|
|
167
|
+
function extractMessage(err) {
|
|
168
|
+
const e = err;
|
|
169
|
+
if (typeof e?.transactionMessage === "string" && e.transactionMessage) {
|
|
170
|
+
return e.transactionMessage;
|
|
171
|
+
}
|
|
172
|
+
if (typeof e?.message === "string" && e.message) return e.message;
|
|
173
|
+
return String(err);
|
|
174
|
+
}
|
|
175
|
+
function parseFromLogs(logs) {
|
|
176
|
+
if (!logs) return {};
|
|
177
|
+
let instructionIndex;
|
|
178
|
+
let customErrorCode;
|
|
179
|
+
for (const line of logs) {
|
|
180
|
+
const instr = /instruction\s+#?(\d+)/i.exec(line);
|
|
181
|
+
if (instr) instructionIndex = Number(instr[1]);
|
|
182
|
+
const custom = /custom program error:\s*(0x[0-9a-fA-F]+|\d+)/i.exec(line);
|
|
183
|
+
if (custom) {
|
|
184
|
+
customErrorCode = custom[1].startsWith("0x") ? parseInt(custom[1], 16) : Number(custom[1]);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return { instructionIndex, customErrorCode };
|
|
188
|
+
}
|
|
189
|
+
function decodeSvmError(error) {
|
|
190
|
+
const logs = extractLogs(error);
|
|
191
|
+
const base = extractMessage(error);
|
|
192
|
+
const { instructionIndex, customErrorCode } = parseFromLogs(logs);
|
|
193
|
+
const name = error?.name;
|
|
194
|
+
const isSendError = name === "SendTransactionError" || /simulation failed|transaction simulation/i.test(base);
|
|
195
|
+
const parts = [base];
|
|
196
|
+
if (instructionIndex !== void 0) {
|
|
197
|
+
parts.push(`(failing instruction #${instructionIndex})`);
|
|
198
|
+
}
|
|
199
|
+
if (customErrorCode !== void 0) {
|
|
200
|
+
parts.push(`(custom program error ${customErrorCode})`);
|
|
201
|
+
}
|
|
202
|
+
let kind;
|
|
203
|
+
if (customErrorCode !== void 0 || instructionIndex !== void 0) {
|
|
204
|
+
kind = "instruction_error";
|
|
205
|
+
} else if (isSendError) {
|
|
206
|
+
kind = "simulation";
|
|
207
|
+
} else if (error instanceof Error) {
|
|
208
|
+
kind = "send";
|
|
209
|
+
} else {
|
|
210
|
+
kind = "unknown";
|
|
211
|
+
}
|
|
212
|
+
return {
|
|
213
|
+
message: parts.join(" "),
|
|
214
|
+
kind,
|
|
215
|
+
instructionIndex,
|
|
216
|
+
customErrorCode,
|
|
217
|
+
logs
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// src/subscribe.ts
|
|
222
|
+
function onLogs(connection, filter, callback, commitment) {
|
|
223
|
+
const id = connection.onLogs(filter, callback, commitment);
|
|
224
|
+
return {
|
|
225
|
+
id,
|
|
226
|
+
off: () => connection.removeOnLogsListener(id)
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
function onAccountChange(connection, pubkey, callback, commitment) {
|
|
230
|
+
const id = connection.onAccountChange(pubkey, callback, commitment);
|
|
231
|
+
return {
|
|
232
|
+
id,
|
|
233
|
+
off: () => connection.removeAccountChangeListener(id)
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
function onSlotChange(connection, callback) {
|
|
237
|
+
const id = connection.onSlotChange(callback);
|
|
238
|
+
return {
|
|
239
|
+
id,
|
|
240
|
+
off: () => connection.removeSlotChangeListener(id)
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
function withComputeBudget(tx, options) {
|
|
244
|
+
const { units, microLamports } = options;
|
|
245
|
+
const prepend = [];
|
|
246
|
+
if (microLamports !== void 0) {
|
|
247
|
+
prepend.unshift(
|
|
248
|
+
ComputeBudgetProgram.setComputeUnitPrice({ microLamports })
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
if (units !== void 0) {
|
|
252
|
+
prepend.unshift(ComputeBudgetProgram.setComputeUnitLimit({ units }));
|
|
253
|
+
}
|
|
254
|
+
tx.instructions.unshift(...prepend);
|
|
255
|
+
return tx;
|
|
256
|
+
}
|
|
257
|
+
async function estimatePriorityFee(connection, accounts = []) {
|
|
258
|
+
const fees2 = await connection.getRecentPrioritizationFees(
|
|
259
|
+
accounts.length > 0 ? { lockedWritableAccounts: [...accounts] } : void 0
|
|
260
|
+
);
|
|
261
|
+
if (!fees2 || fees2.length === 0) return 0;
|
|
262
|
+
return fees2.reduce((max, f) => Math.max(max, f.prioritizationFee), 0);
|
|
263
|
+
}
|
|
264
|
+
var fees = {
|
|
265
|
+
withComputeBudget,
|
|
266
|
+
estimatePriorityFee
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
// src/index.ts
|
|
270
|
+
var VERSION = "0.3.0";
|
|
271
|
+
|
|
272
|
+
export { ASSOCIATED_TOKEN_PROGRAM_ID, DEFAULT_SVM_RPC_URL, MEMO_PROGRAM_ID, PROGRAM_IDS, SYSTEM_PROGRAM_ID, TOKEN_PROGRAM_ID, VERSION, createAssociatedTokenAccountInstruction, createInvokeInstruction, createMemoInstruction, createSvmClient, createTransferTokenInstruction, decodeSvmError, detectSvmProvider, estimatePriorityFee, fees, getAssociatedTokenAddress, getSvmWallet, onAccountChange, onLogs, onSlotChange, svmAddress, svmKeypairFromSecretKey, withComputeBudget };
|
|
273
|
+
//# sourceMappingURL=index.js.map
|
|
274
|
+
//# sourceMappingURL=index.js.map
|