@solana/programs 2.0.0-experimental.109615d

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/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2023 Solana Labs, Inc
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,79 @@
1
+ [![npm][npm-image]][npm-url]
2
+ [![npm-downloads][npm-downloads-image]][npm-url]
3
+ [![semantic-release][semantic-release-image]][semantic-release-url]
4
+ <br />
5
+ [![code-style-prettier][code-style-prettier-image]][code-style-prettier-url]
6
+
7
+ [code-style-prettier-image]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square
8
+ [code-style-prettier-url]: https://github.com/prettier/prettier
9
+ [npm-downloads-image]: https://img.shields.io/npm/dm/@solana/programs/experimental.svg?style=flat
10
+ [npm-image]: https://img.shields.io/npm/v/@solana/programs/experimental.svg?style=flat
11
+ [npm-url]: https://www.npmjs.com/package/@solana/programs/v/experimental
12
+ [semantic-release-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
13
+ [semantic-release-url]: https://github.com/semantic-release/semantic-release
14
+
15
+ # @solana/programs
16
+
17
+ This package contains types for defining programs and helpers for resolving program errors. It can be used standalone, but it is also exported as part of the Solana JavaScript SDK [`@solana/web3.js@experimental`](https://github.com/solana-labs/solana-web3.js/tree/master/packages/library).
18
+
19
+ ## Types
20
+
21
+ ### `Program`
22
+
23
+ The `Program` type defines a Solana program.
24
+
25
+ ```ts
26
+ const myProgram: Program<'1234..5678'> = {
27
+ name: 'myProgramName',
28
+ address: '1234..5678' as Address<'1234..5678'>,
29
+ };
30
+ ```
31
+
32
+ ### `ProgramWithErrors`
33
+
34
+ The `ProgramWithErrors` type helps extend the `Program` type by defining a `getErrorFromCode` function that can be used to resolve a custom program error from a transaction error code.
35
+
36
+ ```ts
37
+ enum MyProgramErrorCode {
38
+ UNINITIALIZED_ACCOUNT = 0,
39
+ INVALID_ACCOUNT_OWNER = 1,
40
+ INVALID_ACCOUNT_DATA = 2,
41
+ SOME_OTHER_ERROR = 3,
42
+ }
43
+
44
+ class MyProgramError extends Error {
45
+ // ...
46
+ }
47
+
48
+ const myProgram: Program<'1234..5678'> & ProgramWithErrors<MyProgramErrorCode, MyProgramError> = {
49
+ name: 'myProgramName',
50
+ address: '1234..5678' as Address<'1234..5678'>,
51
+ getErrorFromCode: (code: MyProgramErrorCode, originalError: Error): MyProgramError => {
52
+ // ...
53
+ },
54
+ };
55
+ ```
56
+
57
+ ## Functions
58
+
59
+ ### `resolveTransactionError()`
60
+
61
+ This function takes a raw error caused by a transaction failure and attempts to resolve it into a custom program error.
62
+
63
+ For this to work, the `resolveTransactionError` function also needs the following parameters:
64
+
65
+ - The `transaction` object that failed to execute. This allows us to identify the failing instruction and correctly identify the program that caused the error.
66
+ - An array of all `programs` that can be used to resolve the error. If the program that caused the error is not present in the array, the function won't be able to return a custom program error.
67
+
68
+ Note that, if the error cannot be resolved into a custom program error, the original error is returned as-is.
69
+
70
+ ```ts
71
+ // Store your programs.
72
+ const programs = [createSplSystemProgram(), createSplComputeBudgetProgram(), createSplAddressLookupTableProgram()];
73
+
74
+ try {
75
+ // Send and confirm your transaction.
76
+ } catch (error) {
77
+ throw resolveTransactionError(error, transaction, programs);
78
+ }
79
+ ```
@@ -0,0 +1,29 @@
1
+ 'use strict';
2
+
3
+ // src/resolve-transaction-error.ts
4
+ function resolveTransactionError(error, transaction, programs) {
5
+ const fullLogs = error.message + "\n" + (error.logs ?? []).join("\n");
6
+ const instructionRegex = /Error processing Instruction (\d+)/;
7
+ const instructionIndexString = fullLogs.match(instructionRegex)?.[1] ?? null;
8
+ const instructionIndex = instructionIndexString ? parseInt(instructionIndexString, 10) : null;
9
+ if (instructionIndex === null)
10
+ return error;
11
+ const errorCodeRegex = /Custom program error: (0x[a-f0-9]+)/i;
12
+ const errorCodeString = fullLogs.match(errorCodeRegex)?.[1] ?? null;
13
+ const errorCode = errorCodeString ? parseInt(errorCodeString, 16) : null;
14
+ if (errorCode === null)
15
+ return error;
16
+ const programAddress = transaction.instructions[instructionIndex]?.programAddress ?? null;
17
+ if (!programAddress)
18
+ return error;
19
+ const program = programs.find(
20
+ (program2) => program2.address === programAddress && typeof program2.getErrorFromCode !== "undefined"
21
+ ) ?? null;
22
+ if (!program)
23
+ return error;
24
+ return program.getErrorFromCode(errorCode, error);
25
+ }
26
+
27
+ exports.resolveTransactionError = resolveTransactionError;
28
+ //# sourceMappingURL=out.js.map
29
+ //# sourceMappingURL=index.browser.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/resolve-transaction-error.ts"],"names":["program"],"mappings":";AAkBO,SAAS,wBACZ,OACA,aACA,UACK;AAEL,QAAM,WAAW,MAAM,UAAU,QAAQ,MAAM,QAAQ,CAAC,GAAG,KAAK,IAAI;AAGpE,QAAM,mBAAmB;AACzB,QAAM,yBAAyB,SAAS,MAAM,gBAAgB,IAAI,CAAC,KAAK;AACxE,QAAM,mBAAmB,yBAAyB,SAAS,wBAAwB,EAAE,IAAI;AACzF,MAAI,qBAAqB;AAAM,WAAO;AAGtC,QAAM,iBAAiB;AACvB,QAAM,kBAAkB,SAAS,MAAM,cAAc,IAAI,CAAC,KAAK;AAC/D,QAAM,YAAY,kBAAkB,SAAS,iBAAiB,EAAE,IAAI;AACpE,MAAI,cAAc;AAAM,WAAO;AAG/B,QAAM,iBAAiC,YAAY,aAAa,gBAAgB,GAAG,kBAAkB;AACrG,MAAI,CAAC;AAAgB,WAAO;AAG5B,QAAM,UACF,SAAS;AAAA,IACL,CAACA,aACGA,SAAQ,YAAY,kBAAkB,OAAOA,SAAQ,qBAAqB;AAAA,EAClF,KAAK;AACT,MAAI,CAAC;AAAS,WAAO;AAGrB,SAAO,QAAQ,iBAAiB,WAAW,KAAK;AACpD","sourcesContent":["import type { Address } from '@solana/addresses';\nimport type { Transaction } from '@solana/transactions';\n\nimport { Program, ProgramWithErrors } from './program';\n\n/**\n * Resolves a custom program error from a transaction error\n * with logs using the provided list of programs.\n * The original error is returned if the error cannot be\n * resolved from the given programs.\n *\n * @param error The raw error to resolve containing the program logs.\n * @param transaction The transaction that caused the error.\n * @param programs The list of programs to go through when resolving the transaction error.\n * They should ideally contain all programs the transaction is sending instructions to.\n * @returns The resolved program error, or the original transaction error\n * if the error cannot be resolved using the provided programs.\n */\nexport function resolveTransactionError(\n error: Error & Readonly<{ logs?: readonly string[] }>,\n transaction: Transaction,\n programs: Program[],\n): Error {\n // Compute the full logs from which to parse the instruction index and error code.\n const fullLogs = error.message + '\\n' + (error.logs ?? []).join('\\n');\n\n // Parse the instruction number, or return the original error.\n const instructionRegex = /Error processing Instruction (\\d+)/;\n const instructionIndexString = fullLogs.match(instructionRegex)?.[1] ?? null;\n const instructionIndex = instructionIndexString ? parseInt(instructionIndexString, 10) : null;\n if (instructionIndex === null) return error;\n\n // Parse the error code, or return the original error.\n const errorCodeRegex = /Custom program error: (0x[a-f0-9]+)/i;\n const errorCodeString = fullLogs.match(errorCodeRegex)?.[1] ?? null;\n const errorCode = errorCodeString ? parseInt(errorCodeString, 16) : null;\n if (errorCode === null) return error;\n\n // Get the program address from the instruction in the transaction, or return the original error.\n const programAddress: Address | null = transaction.instructions[instructionIndex]?.programAddress ?? null;\n if (!programAddress) return error;\n\n // Find a matching program with error handling, or return the original error.\n const program: ProgramWithErrors | null =\n programs.find(\n (program): program is Program & ProgramWithErrors =>\n program.address === programAddress && typeof program.getErrorFromCode !== 'undefined',\n ) ?? null;\n if (!program) return error;\n\n // Resolve the error from the identified program.\n return program.getErrorFromCode(errorCode, error);\n}\n"]}
@@ -0,0 +1,27 @@
1
+ // src/resolve-transaction-error.ts
2
+ function resolveTransactionError(error, transaction, programs) {
3
+ const fullLogs = error.message + "\n" + (error.logs ?? []).join("\n");
4
+ const instructionRegex = /Error processing Instruction (\d+)/;
5
+ const instructionIndexString = fullLogs.match(instructionRegex)?.[1] ?? null;
6
+ const instructionIndex = instructionIndexString ? parseInt(instructionIndexString, 10) : null;
7
+ if (instructionIndex === null)
8
+ return error;
9
+ const errorCodeRegex = /Custom program error: (0x[a-f0-9]+)/i;
10
+ const errorCodeString = fullLogs.match(errorCodeRegex)?.[1] ?? null;
11
+ const errorCode = errorCodeString ? parseInt(errorCodeString, 16) : null;
12
+ if (errorCode === null)
13
+ return error;
14
+ const programAddress = transaction.instructions[instructionIndex]?.programAddress ?? null;
15
+ if (!programAddress)
16
+ return error;
17
+ const program = programs.find(
18
+ (program2) => program2.address === programAddress && typeof program2.getErrorFromCode !== "undefined"
19
+ ) ?? null;
20
+ if (!program)
21
+ return error;
22
+ return program.getErrorFromCode(errorCode, error);
23
+ }
24
+
25
+ export { resolveTransactionError };
26
+ //# sourceMappingURL=out.js.map
27
+ //# sourceMappingURL=index.browser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/resolve-transaction-error.ts"],"names":["program"],"mappings":";AAkBO,SAAS,wBACZ,OACA,aACA,UACK;AAEL,QAAM,WAAW,MAAM,UAAU,QAAQ,MAAM,QAAQ,CAAC,GAAG,KAAK,IAAI;AAGpE,QAAM,mBAAmB;AACzB,QAAM,yBAAyB,SAAS,MAAM,gBAAgB,IAAI,CAAC,KAAK;AACxE,QAAM,mBAAmB,yBAAyB,SAAS,wBAAwB,EAAE,IAAI;AACzF,MAAI,qBAAqB;AAAM,WAAO;AAGtC,QAAM,iBAAiB;AACvB,QAAM,kBAAkB,SAAS,MAAM,cAAc,IAAI,CAAC,KAAK;AAC/D,QAAM,YAAY,kBAAkB,SAAS,iBAAiB,EAAE,IAAI;AACpE,MAAI,cAAc;AAAM,WAAO;AAG/B,QAAM,iBAAiC,YAAY,aAAa,gBAAgB,GAAG,kBAAkB;AACrG,MAAI,CAAC;AAAgB,WAAO;AAG5B,QAAM,UACF,SAAS;AAAA,IACL,CAACA,aACGA,SAAQ,YAAY,kBAAkB,OAAOA,SAAQ,qBAAqB;AAAA,EAClF,KAAK;AACT,MAAI,CAAC;AAAS,WAAO;AAGrB,SAAO,QAAQ,iBAAiB,WAAW,KAAK;AACpD","sourcesContent":["import type { Address } from '@solana/addresses';\nimport type { Transaction } from '@solana/transactions';\n\nimport { Program, ProgramWithErrors } from './program';\n\n/**\n * Resolves a custom program error from a transaction error\n * with logs using the provided list of programs.\n * The original error is returned if the error cannot be\n * resolved from the given programs.\n *\n * @param error The raw error to resolve containing the program logs.\n * @param transaction The transaction that caused the error.\n * @param programs The list of programs to go through when resolving the transaction error.\n * They should ideally contain all programs the transaction is sending instructions to.\n * @returns The resolved program error, or the original transaction error\n * if the error cannot be resolved using the provided programs.\n */\nexport function resolveTransactionError(\n error: Error & Readonly<{ logs?: readonly string[] }>,\n transaction: Transaction,\n programs: Program[],\n): Error {\n // Compute the full logs from which to parse the instruction index and error code.\n const fullLogs = error.message + '\\n' + (error.logs ?? []).join('\\n');\n\n // Parse the instruction number, or return the original error.\n const instructionRegex = /Error processing Instruction (\\d+)/;\n const instructionIndexString = fullLogs.match(instructionRegex)?.[1] ?? null;\n const instructionIndex = instructionIndexString ? parseInt(instructionIndexString, 10) : null;\n if (instructionIndex === null) return error;\n\n // Parse the error code, or return the original error.\n const errorCodeRegex = /Custom program error: (0x[a-f0-9]+)/i;\n const errorCodeString = fullLogs.match(errorCodeRegex)?.[1] ?? null;\n const errorCode = errorCodeString ? parseInt(errorCodeString, 16) : null;\n if (errorCode === null) return error;\n\n // Get the program address from the instruction in the transaction, or return the original error.\n const programAddress: Address | null = transaction.instructions[instructionIndex]?.programAddress ?? null;\n if (!programAddress) return error;\n\n // Find a matching program with error handling, or return the original error.\n const program: ProgramWithErrors | null =\n programs.find(\n (program): program is Program & ProgramWithErrors =>\n program.address === programAddress && typeof program.getErrorFromCode !== 'undefined',\n ) ?? null;\n if (!program) return error;\n\n // Resolve the error from the identified program.\n return program.getErrorFromCode(errorCode, error);\n}\n"]}
@@ -0,0 +1,27 @@
1
+ // src/resolve-transaction-error.ts
2
+ function resolveTransactionError(error, transaction, programs) {
3
+ const fullLogs = error.message + "\n" + (error.logs ?? []).join("\n");
4
+ const instructionRegex = /Error processing Instruction (\d+)/;
5
+ const instructionIndexString = fullLogs.match(instructionRegex)?.[1] ?? null;
6
+ const instructionIndex = instructionIndexString ? parseInt(instructionIndexString, 10) : null;
7
+ if (instructionIndex === null)
8
+ return error;
9
+ const errorCodeRegex = /Custom program error: (0x[a-f0-9]+)/i;
10
+ const errorCodeString = fullLogs.match(errorCodeRegex)?.[1] ?? null;
11
+ const errorCode = errorCodeString ? parseInt(errorCodeString, 16) : null;
12
+ if (errorCode === null)
13
+ return error;
14
+ const programAddress = transaction.instructions[instructionIndex]?.programAddress ?? null;
15
+ if (!programAddress)
16
+ return error;
17
+ const program = programs.find(
18
+ (program2) => program2.address === programAddress && typeof program2.getErrorFromCode !== "undefined"
19
+ ) ?? null;
20
+ if (!program)
21
+ return error;
22
+ return program.getErrorFromCode(errorCode, error);
23
+ }
24
+
25
+ export { resolveTransactionError };
26
+ //# sourceMappingURL=out.js.map
27
+ //# sourceMappingURL=index.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/resolve-transaction-error.ts"],"names":["program"],"mappings":";AAkBO,SAAS,wBACZ,OACA,aACA,UACK;AAEL,QAAM,WAAW,MAAM,UAAU,QAAQ,MAAM,QAAQ,CAAC,GAAG,KAAK,IAAI;AAGpE,QAAM,mBAAmB;AACzB,QAAM,yBAAyB,SAAS,MAAM,gBAAgB,IAAI,CAAC,KAAK;AACxE,QAAM,mBAAmB,yBAAyB,SAAS,wBAAwB,EAAE,IAAI;AACzF,MAAI,qBAAqB;AAAM,WAAO;AAGtC,QAAM,iBAAiB;AACvB,QAAM,kBAAkB,SAAS,MAAM,cAAc,IAAI,CAAC,KAAK;AAC/D,QAAM,YAAY,kBAAkB,SAAS,iBAAiB,EAAE,IAAI;AACpE,MAAI,cAAc;AAAM,WAAO;AAG/B,QAAM,iBAAiC,YAAY,aAAa,gBAAgB,GAAG,kBAAkB;AACrG,MAAI,CAAC;AAAgB,WAAO;AAG5B,QAAM,UACF,SAAS;AAAA,IACL,CAACA,aACGA,SAAQ,YAAY,kBAAkB,OAAOA,SAAQ,qBAAqB;AAAA,EAClF,KAAK;AACT,MAAI,CAAC;AAAS,WAAO;AAGrB,SAAO,QAAQ,iBAAiB,WAAW,KAAK;AACpD","sourcesContent":["import type { Address } from '@solana/addresses';\nimport type { Transaction } from '@solana/transactions';\n\nimport { Program, ProgramWithErrors } from './program';\n\n/**\n * Resolves a custom program error from a transaction error\n * with logs using the provided list of programs.\n * The original error is returned if the error cannot be\n * resolved from the given programs.\n *\n * @param error The raw error to resolve containing the program logs.\n * @param transaction The transaction that caused the error.\n * @param programs The list of programs to go through when resolving the transaction error.\n * They should ideally contain all programs the transaction is sending instructions to.\n * @returns The resolved program error, or the original transaction error\n * if the error cannot be resolved using the provided programs.\n */\nexport function resolveTransactionError(\n error: Error & Readonly<{ logs?: readonly string[] }>,\n transaction: Transaction,\n programs: Program[],\n): Error {\n // Compute the full logs from which to parse the instruction index and error code.\n const fullLogs = error.message + '\\n' + (error.logs ?? []).join('\\n');\n\n // Parse the instruction number, or return the original error.\n const instructionRegex = /Error processing Instruction (\\d+)/;\n const instructionIndexString = fullLogs.match(instructionRegex)?.[1] ?? null;\n const instructionIndex = instructionIndexString ? parseInt(instructionIndexString, 10) : null;\n if (instructionIndex === null) return error;\n\n // Parse the error code, or return the original error.\n const errorCodeRegex = /Custom program error: (0x[a-f0-9]+)/i;\n const errorCodeString = fullLogs.match(errorCodeRegex)?.[1] ?? null;\n const errorCode = errorCodeString ? parseInt(errorCodeString, 16) : null;\n if (errorCode === null) return error;\n\n // Get the program address from the instruction in the transaction, or return the original error.\n const programAddress: Address | null = transaction.instructions[instructionIndex]?.programAddress ?? null;\n if (!programAddress) return error;\n\n // Find a matching program with error handling, or return the original error.\n const program: ProgramWithErrors | null =\n programs.find(\n (program): program is Program & ProgramWithErrors =>\n program.address === programAddress && typeof program.getErrorFromCode !== 'undefined',\n ) ?? null;\n if (!program) return error;\n\n // Resolve the error from the identified program.\n return program.getErrorFromCode(errorCode, error);\n}\n"]}
@@ -0,0 +1,29 @@
1
+ 'use strict';
2
+
3
+ // src/resolve-transaction-error.ts
4
+ function resolveTransactionError(error, transaction, programs) {
5
+ const fullLogs = error.message + "\n" + (error.logs ?? []).join("\n");
6
+ const instructionRegex = /Error processing Instruction (\d+)/;
7
+ const instructionIndexString = fullLogs.match(instructionRegex)?.[1] ?? null;
8
+ const instructionIndex = instructionIndexString ? parseInt(instructionIndexString, 10) : null;
9
+ if (instructionIndex === null)
10
+ return error;
11
+ const errorCodeRegex = /Custom program error: (0x[a-f0-9]+)/i;
12
+ const errorCodeString = fullLogs.match(errorCodeRegex)?.[1] ?? null;
13
+ const errorCode = errorCodeString ? parseInt(errorCodeString, 16) : null;
14
+ if (errorCode === null)
15
+ return error;
16
+ const programAddress = transaction.instructions[instructionIndex]?.programAddress ?? null;
17
+ if (!programAddress)
18
+ return error;
19
+ const program = programs.find(
20
+ (program2) => program2.address === programAddress && typeof program2.getErrorFromCode !== "undefined"
21
+ ) ?? null;
22
+ if (!program)
23
+ return error;
24
+ return program.getErrorFromCode(errorCode, error);
25
+ }
26
+
27
+ exports.resolveTransactionError = resolveTransactionError;
28
+ //# sourceMappingURL=out.js.map
29
+ //# sourceMappingURL=index.node.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/resolve-transaction-error.ts"],"names":["program"],"mappings":";AAkBO,SAAS,wBACZ,OACA,aACA,UACK;AAEL,QAAM,WAAW,MAAM,UAAU,QAAQ,MAAM,QAAQ,CAAC,GAAG,KAAK,IAAI;AAGpE,QAAM,mBAAmB;AACzB,QAAM,yBAAyB,SAAS,MAAM,gBAAgB,IAAI,CAAC,KAAK;AACxE,QAAM,mBAAmB,yBAAyB,SAAS,wBAAwB,EAAE,IAAI;AACzF,MAAI,qBAAqB;AAAM,WAAO;AAGtC,QAAM,iBAAiB;AACvB,QAAM,kBAAkB,SAAS,MAAM,cAAc,IAAI,CAAC,KAAK;AAC/D,QAAM,YAAY,kBAAkB,SAAS,iBAAiB,EAAE,IAAI;AACpE,MAAI,cAAc;AAAM,WAAO;AAG/B,QAAM,iBAAiC,YAAY,aAAa,gBAAgB,GAAG,kBAAkB;AACrG,MAAI,CAAC;AAAgB,WAAO;AAG5B,QAAM,UACF,SAAS;AAAA,IACL,CAACA,aACGA,SAAQ,YAAY,kBAAkB,OAAOA,SAAQ,qBAAqB;AAAA,EAClF,KAAK;AACT,MAAI,CAAC;AAAS,WAAO;AAGrB,SAAO,QAAQ,iBAAiB,WAAW,KAAK;AACpD","sourcesContent":["import type { Address } from '@solana/addresses';\nimport type { Transaction } from '@solana/transactions';\n\nimport { Program, ProgramWithErrors } from './program';\n\n/**\n * Resolves a custom program error from a transaction error\n * with logs using the provided list of programs.\n * The original error is returned if the error cannot be\n * resolved from the given programs.\n *\n * @param error The raw error to resolve containing the program logs.\n * @param transaction The transaction that caused the error.\n * @param programs The list of programs to go through when resolving the transaction error.\n * They should ideally contain all programs the transaction is sending instructions to.\n * @returns The resolved program error, or the original transaction error\n * if the error cannot be resolved using the provided programs.\n */\nexport function resolveTransactionError(\n error: Error & Readonly<{ logs?: readonly string[] }>,\n transaction: Transaction,\n programs: Program[],\n): Error {\n // Compute the full logs from which to parse the instruction index and error code.\n const fullLogs = error.message + '\\n' + (error.logs ?? []).join('\\n');\n\n // Parse the instruction number, or return the original error.\n const instructionRegex = /Error processing Instruction (\\d+)/;\n const instructionIndexString = fullLogs.match(instructionRegex)?.[1] ?? null;\n const instructionIndex = instructionIndexString ? parseInt(instructionIndexString, 10) : null;\n if (instructionIndex === null) return error;\n\n // Parse the error code, or return the original error.\n const errorCodeRegex = /Custom program error: (0x[a-f0-9]+)/i;\n const errorCodeString = fullLogs.match(errorCodeRegex)?.[1] ?? null;\n const errorCode = errorCodeString ? parseInt(errorCodeString, 16) : null;\n if (errorCode === null) return error;\n\n // Get the program address from the instruction in the transaction, or return the original error.\n const programAddress: Address | null = transaction.instructions[instructionIndex]?.programAddress ?? null;\n if (!programAddress) return error;\n\n // Find a matching program with error handling, or return the original error.\n const program: ProgramWithErrors | null =\n programs.find(\n (program): program is Program & ProgramWithErrors =>\n program.address === programAddress && typeof program.getErrorFromCode !== 'undefined',\n ) ?? null;\n if (!program) return error;\n\n // Resolve the error from the identified program.\n return program.getErrorFromCode(errorCode, error);\n}\n"]}
@@ -0,0 +1,27 @@
1
+ // src/resolve-transaction-error.ts
2
+ function resolveTransactionError(error, transaction, programs) {
3
+ const fullLogs = error.message + "\n" + (error.logs ?? []).join("\n");
4
+ const instructionRegex = /Error processing Instruction (\d+)/;
5
+ const instructionIndexString = fullLogs.match(instructionRegex)?.[1] ?? null;
6
+ const instructionIndex = instructionIndexString ? parseInt(instructionIndexString, 10) : null;
7
+ if (instructionIndex === null)
8
+ return error;
9
+ const errorCodeRegex = /Custom program error: (0x[a-f0-9]+)/i;
10
+ const errorCodeString = fullLogs.match(errorCodeRegex)?.[1] ?? null;
11
+ const errorCode = errorCodeString ? parseInt(errorCodeString, 16) : null;
12
+ if (errorCode === null)
13
+ return error;
14
+ const programAddress = transaction.instructions[instructionIndex]?.programAddress ?? null;
15
+ if (!programAddress)
16
+ return error;
17
+ const program = programs.find(
18
+ (program2) => program2.address === programAddress && typeof program2.getErrorFromCode !== "undefined"
19
+ ) ?? null;
20
+ if (!program)
21
+ return error;
22
+ return program.getErrorFromCode(errorCode, error);
23
+ }
24
+
25
+ export { resolveTransactionError };
26
+ //# sourceMappingURL=out.js.map
27
+ //# sourceMappingURL=index.node.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/resolve-transaction-error.ts"],"names":["program"],"mappings":";AAkBO,SAAS,wBACZ,OACA,aACA,UACK;AAEL,QAAM,WAAW,MAAM,UAAU,QAAQ,MAAM,QAAQ,CAAC,GAAG,KAAK,IAAI;AAGpE,QAAM,mBAAmB;AACzB,QAAM,yBAAyB,SAAS,MAAM,gBAAgB,IAAI,CAAC,KAAK;AACxE,QAAM,mBAAmB,yBAAyB,SAAS,wBAAwB,EAAE,IAAI;AACzF,MAAI,qBAAqB;AAAM,WAAO;AAGtC,QAAM,iBAAiB;AACvB,QAAM,kBAAkB,SAAS,MAAM,cAAc,IAAI,CAAC,KAAK;AAC/D,QAAM,YAAY,kBAAkB,SAAS,iBAAiB,EAAE,IAAI;AACpE,MAAI,cAAc;AAAM,WAAO;AAG/B,QAAM,iBAAiC,YAAY,aAAa,gBAAgB,GAAG,kBAAkB;AACrG,MAAI,CAAC;AAAgB,WAAO;AAG5B,QAAM,UACF,SAAS;AAAA,IACL,CAACA,aACGA,SAAQ,YAAY,kBAAkB,OAAOA,SAAQ,qBAAqB;AAAA,EAClF,KAAK;AACT,MAAI,CAAC;AAAS,WAAO;AAGrB,SAAO,QAAQ,iBAAiB,WAAW,KAAK;AACpD","sourcesContent":["import type { Address } from '@solana/addresses';\nimport type { Transaction } from '@solana/transactions';\n\nimport { Program, ProgramWithErrors } from './program';\n\n/**\n * Resolves a custom program error from a transaction error\n * with logs using the provided list of programs.\n * The original error is returned if the error cannot be\n * resolved from the given programs.\n *\n * @param error The raw error to resolve containing the program logs.\n * @param transaction The transaction that caused the error.\n * @param programs The list of programs to go through when resolving the transaction error.\n * They should ideally contain all programs the transaction is sending instructions to.\n * @returns The resolved program error, or the original transaction error\n * if the error cannot be resolved using the provided programs.\n */\nexport function resolveTransactionError(\n error: Error & Readonly<{ logs?: readonly string[] }>,\n transaction: Transaction,\n programs: Program[],\n): Error {\n // Compute the full logs from which to parse the instruction index and error code.\n const fullLogs = error.message + '\\n' + (error.logs ?? []).join('\\n');\n\n // Parse the instruction number, or return the original error.\n const instructionRegex = /Error processing Instruction (\\d+)/;\n const instructionIndexString = fullLogs.match(instructionRegex)?.[1] ?? null;\n const instructionIndex = instructionIndexString ? parseInt(instructionIndexString, 10) : null;\n if (instructionIndex === null) return error;\n\n // Parse the error code, or return the original error.\n const errorCodeRegex = /Custom program error: (0x[a-f0-9]+)/i;\n const errorCodeString = fullLogs.match(errorCodeRegex)?.[1] ?? null;\n const errorCode = errorCodeString ? parseInt(errorCodeString, 16) : null;\n if (errorCode === null) return error;\n\n // Get the program address from the instruction in the transaction, or return the original error.\n const programAddress: Address | null = transaction.instructions[instructionIndex]?.programAddress ?? null;\n if (!programAddress) return error;\n\n // Find a matching program with error handling, or return the original error.\n const program: ProgramWithErrors | null =\n programs.find(\n (program): program is Program & ProgramWithErrors =>\n program.address === programAddress && typeof program.getErrorFromCode !== 'undefined',\n ) ?? null;\n if (!program) return error;\n\n // Resolve the error from the identified program.\n return program.getErrorFromCode(errorCode, error);\n}\n"]}
@@ -0,0 +1,3 @@
1
+ export * from './program.js';
2
+ export * from './resolve-transaction-error.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,6BAA6B,CAAC"}
@@ -0,0 +1,31 @@
1
+ import type { Address } from '@solana/addresses';
2
+ /**
3
+ * Defines a Solana program.
4
+ */
5
+ export type Program<TAddress extends string = string> = {
6
+ /**
7
+ * A unique name for the Program.
8
+ *
9
+ * To avoid conflict with other organizations, it is recommended
10
+ * to prefix the program name with a namespace that is unique to
11
+ * your organization. For instance, programs belonging to the
12
+ * Solana Program Library are prefixed with `spl` like so:
13
+ * `splMemo` or `splToken`.
14
+ */
15
+ name: string;
16
+ /**
17
+ * The base58 address of the program.
18
+ */
19
+ address: Address<TAddress>;
20
+ /**
21
+ * Retrieves a program-specific error from a given error code.
22
+ */
23
+ getErrorFromCode?: (code: number, cause?: Error) => Error;
24
+ };
25
+ /**
26
+ * Defines a Solana program that can return custom errors from a provided error code.
27
+ */
28
+ export type ProgramWithErrors<TErrorCode extends number = number, TError extends Error = Error> = {
29
+ getErrorFromCode: (code: TErrorCode, cause?: Error) => TError;
30
+ };
31
+ //# sourceMappingURL=program.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"program.d.ts","sourceRoot":"","sources":["../../src/program.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAEjD;;GAEG;AACH,MAAM,MAAM,OAAO,CAAC,QAAQ,SAAS,MAAM,GAAG,MAAM,IAAI;IACpD;;;;;;;;OAQG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;IAE3B;;OAEG;IACH,gBAAgB,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,KAAK,KAAK,KAAK,CAAC;CAC7D,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,iBAAiB,CAAC,UAAU,SAAS,MAAM,GAAG,MAAM,EAAE,MAAM,SAAS,KAAK,GAAG,KAAK,IAAI;IAC9F,gBAAgB,EAAE,CAAC,IAAI,EAAE,UAAU,EAAE,KAAK,CAAC,EAAE,KAAK,KAAK,MAAM,CAAC;CACjE,CAAC"}
@@ -0,0 +1,19 @@
1
+ import type { Transaction } from '@solana/transactions';
2
+ import { Program } from './program.js';
3
+ /**
4
+ * Resolves a custom program error from a transaction error
5
+ * with logs using the provided list of programs.
6
+ * The original error is returned if the error cannot be
7
+ * resolved from the given programs.
8
+ *
9
+ * @param error The raw error to resolve containing the program logs.
10
+ * @param transaction The transaction that caused the error.
11
+ * @param programs The list of programs to go through when resolving the transaction error.
12
+ * They should ideally contain all programs the transaction is sending instructions to.
13
+ * @returns The resolved program error, or the original transaction error
14
+ * if the error cannot be resolved using the provided programs.
15
+ */
16
+ export declare function resolveTransactionError(error: Error & Readonly<{
17
+ logs?: readonly string[];
18
+ }>, transaction: Transaction, programs: Program[]): Error;
19
+ //# sourceMappingURL=resolve-transaction-error.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolve-transaction-error.d.ts","sourceRoot":"","sources":["../../src/resolve-transaction-error.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAExD,OAAO,EAAE,OAAO,EAAqB,MAAM,WAAW,CAAC;AAEvD;;;;;;;;;;;;GAYG;AACH,wBAAgB,uBAAuB,CACnC,KAAK,EAAE,KAAK,GAAG,QAAQ,CAAC;IAAE,IAAI,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;CAAE,CAAC,EACrD,WAAW,EAAE,WAAW,EACxB,QAAQ,EAAE,OAAO,EAAE,GACpB,KAAK,CA8BP"}
package/package.json ADDED
@@ -0,0 +1,94 @@
1
+ {
2
+ "name": "@solana/programs",
3
+ "version": "2.0.0-experimental.109615d",
4
+ "description": "Helpers for defining programs and resolving program errors",
5
+ "exports": {
6
+ "browser": {
7
+ "import": "./dist/index.browser.js",
8
+ "require": "./dist/index.browser.cjs"
9
+ },
10
+ "node": {
11
+ "import": "./dist/index.node.js",
12
+ "require": "./dist/index.node.cjs"
13
+ },
14
+ "react-native": "./dist/index.native.js",
15
+ "types": "./dist/types/index.d.ts"
16
+ },
17
+ "browser": {
18
+ "./dist/index.node.cjs": "./dist/index.browser.cjs",
19
+ "./dist/index.node.js": "./dist/index.browser.js"
20
+ },
21
+ "main": "./dist/index.node.cjs",
22
+ "module": "./dist/index.node.js",
23
+ "react-native": "./dist/index.native.js",
24
+ "types": "./dist/types/index.d.ts",
25
+ "type": "module",
26
+ "files": [
27
+ "./dist/"
28
+ ],
29
+ "sideEffects": false,
30
+ "keywords": [
31
+ "blockchain",
32
+ "solana",
33
+ "web3"
34
+ ],
35
+ "author": "Solana Labs Maintainers <maintainers@solanalabs.com>",
36
+ "license": "MIT",
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "https://github.com/solana-labs/solana-web3.js"
40
+ },
41
+ "bugs": {
42
+ "url": "http://github.com/solana-labs/solana-web3.js/issues"
43
+ },
44
+ "browserslist": [
45
+ "supports bigint and not dead",
46
+ "maintained node versions"
47
+ ],
48
+ "devDependencies": {
49
+ "@solana/eslint-config-solana": "^1.0.2",
50
+ "@swc/jest": "^0.2.29",
51
+ "@types/jest": "^29.5.11",
52
+ "@typescript-eslint/eslint-plugin": "^6.13.2",
53
+ "@typescript-eslint/parser": "^6.3.0",
54
+ "agadoo": "^3.0.0",
55
+ "eslint": "^8.45.0",
56
+ "eslint-plugin-sort-keys-fix": "^1.1.2",
57
+ "jest": "^29.7.0",
58
+ "jest-runner-eslint": "^2.1.2",
59
+ "jest-runner-prettier": "^1.0.0",
60
+ "prettier": "^3.1",
61
+ "tsup": "^8.0.1",
62
+ "typescript": "^5.2.2",
63
+ "version-from-git": "^1.1.1",
64
+ "@solana/addresses": "2.0.0-experimental.109615d",
65
+ "@solana/functional": "2.0.0-experimental.109615d",
66
+ "@solana/transactions": "2.0.0-experimental.109615d",
67
+ "build-scripts": "0.0.0",
68
+ "test-config": "0.0.0",
69
+ "tsconfig": "0.0.0"
70
+ },
71
+ "bundlewatch": {
72
+ "defaultCompression": "gzip",
73
+ "files": [
74
+ {
75
+ "path": "./dist/index*.js"
76
+ }
77
+ ]
78
+ },
79
+ "scripts": {
80
+ "compile:js": "tsup --config build-scripts/tsup.config.package.ts",
81
+ "compile:typedefs": "tsc -p ./tsconfig.declarations.json && node node_modules/build-scripts/add-js-extension-to-types.mjs",
82
+ "dev": "jest -c node_modules/test-config/jest-dev.config.ts --rootDir . --watch",
83
+ "publish-packages": "pnpm publish --tag experimental --access public --no-git-checks",
84
+ "style:fix": "pnpm eslint --fix src/* && pnpm prettier -w src/* package.json",
85
+ "test:lint": "jest -c node_modules/test-config/jest-lint.config.ts --rootDir . --silent",
86
+ "test:prettier": "jest -c node_modules/test-config/jest-prettier.config.ts --rootDir . --silent",
87
+ "test:treeshakability:browser": "agadoo dist/index.browser.js",
88
+ "test:treeshakability:native": "agadoo dist/index.native.js",
89
+ "test:treeshakability:node": "agadoo dist/index.node.js",
90
+ "test:typecheck": "tsc --noEmit",
91
+ "test:unit:browser": "jest -c node_modules/test-config/jest-unit.config.browser.ts --rootDir . --silent",
92
+ "test:unit:node": "jest -c node_modules/test-config/jest-unit.config.node.ts --rootDir . --silent"
93
+ }
94
+ }