@solana/programs 6.3.1 → 6.3.2-canary-20260313112147

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solana/programs",
3
- "version": "6.3.1",
3
+ "version": "6.3.2-canary-20260313112147",
4
4
  "description": "Helpers for defining programs and resolving program errors",
5
5
  "homepage": "https://www.solanakit.com/api#solanaprograms",
6
6
  "exports": {
@@ -33,7 +33,8 @@
33
33
  "types": "./dist/types/index.d.ts",
34
34
  "type": "commonjs",
35
35
  "files": [
36
- "./dist/"
36
+ "./dist/",
37
+ "./src/"
37
38
  ],
38
39
  "sideEffects": false,
39
40
  "keywords": [
@@ -55,8 +56,8 @@
55
56
  "maintained node versions"
56
57
  ],
57
58
  "dependencies": {
58
- "@solana/addresses": "6.3.1",
59
- "@solana/errors": "6.3.1"
59
+ "@solana/addresses": "6.3.2-canary-20260313112147",
60
+ "@solana/errors": "6.3.2-canary-20260313112147"
60
61
  },
61
62
  "peerDependencies": {
62
63
  "typescript": "^5.0.0"
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ /**
2
+ * This package contains helpers for identifying custom program errors. It can be used standalone,
3
+ * but it is also exported as part of Kit
4
+ * [`@solana/kit`](https://github.com/anza-xyz/kit/tree/main/packages/kit).
5
+ *
6
+ * @packageDocumentation
7
+ */
8
+ export * from './program-error';
@@ -0,0 +1,46 @@
1
+ import type { Address } from '@solana/addresses';
2
+ import { isSolanaError, SOLANA_ERROR__INSTRUCTION_ERROR__CUSTOM, SolanaError } from '@solana/errors';
3
+
4
+ /**
5
+ * Identifies whether an error -- typically caused by a transaction failure -- is a custom program
6
+ * error from the provided program address.
7
+ *
8
+ * @param transactionMessage The transaction message that failed to execute. Since the RPC response
9
+ * only provides the index of the failed instruction, the transaction message is required to
10
+ * determine its program address
11
+ * @param programAddress The address of the program from which the error is expected to have
12
+ * originated
13
+ * @param code The expected error code of the custom program error. When provided, the function will
14
+ * check that the custom program error code matches the given value.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * try {
19
+ * // Send and confirm your transaction.
20
+ * } catch (error) {
21
+ * if (isProgramError(error, transactionMessage, myProgramAddress, 42)) {
22
+ * // Handle custom program error 42 from this program.
23
+ * } else if (isProgramError(error, transactionMessage, myProgramAddress)) {
24
+ * // Handle all other custom program errors from this program.
25
+ * } else {
26
+ * throw error;
27
+ * }
28
+ * }
29
+ * ```
30
+ */
31
+ export function isProgramError<TProgramErrorCode extends number>(
32
+ error: unknown,
33
+ transactionMessage: { instructions: Record<number, { programAddress: Address }> },
34
+ programAddress: Address,
35
+ code?: TProgramErrorCode,
36
+ ): error is Readonly<{ context: Readonly<{ code: TProgramErrorCode }> }> &
37
+ SolanaError<typeof SOLANA_ERROR__INSTRUCTION_ERROR__CUSTOM> {
38
+ if (!isSolanaError(error, SOLANA_ERROR__INSTRUCTION_ERROR__CUSTOM)) {
39
+ return false;
40
+ }
41
+ const instructionProgramAddress = transactionMessage.instructions[error.context.index]?.programAddress;
42
+ if (!instructionProgramAddress || instructionProgramAddress !== programAddress) {
43
+ return false;
44
+ }
45
+ return typeof code === 'undefined' || error.context.code === code;
46
+ }