2.1.1 • Published 15 days ago

@solana/programs v2.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
15 days ago

npm npm-downloads semantic-release code-style-prettier

@solana/programs

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.

Types

Program

The Program type defines a Solana program.

const myProgram: Program<'1234..5678'> = {
    name: 'myProgramName',
    address: '1234..5678' as Address<'1234..5678'>,
};

ProgramWithErrors

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.

enum MyProgramErrorCode {
    UNINITIALIZED_ACCOUNT = 0,
    INVALID_ACCOUNT_OWNER = 1,
    INVALID_ACCOUNT_DATA = 2,
    SOME_OTHER_ERROR = 3,
}

class MyProgramError extends Error {
    // ...
}

const myProgram: Program<'1234..5678'> & ProgramWithErrors<MyProgramErrorCode, MyProgramError> = {
    name: 'myProgramName',
    address: '1234..5678' as Address<'1234..5678'>,
    getErrorFromCode: (code: MyProgramErrorCode, originalError: Error): MyProgramError => {
        // ...
    },
};

Functions

resolveTransactionError()

This function takes a raw error caused by a transaction failure and attempts to resolve it into a custom program error.

For this to work, the resolveTransactionError function also needs the following parameters:

  • The transaction object that failed to execute. This allows us to identify the failing instruction and correctly identify the program that caused the error.
  • 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.

Note that, if the error cannot be resolved into a custom program error, the original error is returned as-is.

// Store your programs.
const programs = [createSplSystemProgram(), createSplComputeBudgetProgram(), createSplAddressLookupTableProgram()];

try {
    // Send and confirm your transaction.
} catch (error) {
    throw resolveTransactionError(error, transaction, programs);
}
2.1.1

1 month ago

2.1.0

4 months ago

2.0.0-rc.4

8 months ago

2.0.0

7 months ago

2.0.0-rc.2

8 months ago

2.0.0-rc.3

8 months ago

2.0.0-preview.4

12 months ago

2.0.0-rc.0

11 months ago

2.0.0-rc.1

10 months ago

2.0.0-preview.3

1 year ago

2.0.0-preview.2

1 year ago

2.0.0-preview.1

1 year ago