Skip to content

Commit

Permalink
refactor(experimental): remove program types and resolveTransactionEr…
Browse files Browse the repository at this point in the history
…ror helper (#2491)

Kinobi no longer makes use of program types such as `Program` and `ProgramWithErrors` as errors are now exported as simple numeric constants which can then be compared with the custom error code provided by the RPC.

As such, the `resolveTransactionError` helper which requires an array of `ProgramWithErrors` instances is also no longer needed. Instead, one can use the `isProgramError` helper and provide the relevant generated constants as illustrated in the previous PR.

This PR removes everything from the `@solana/programs` package aside from the `isProgramError` helper.
  • Loading branch information
lorisleiva committed Apr 16, 2024
1 parent 1672346 commit 2040f96
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 285 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-berries-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solana/programs": patch
---

Remove program types and `resolveTransactionError` helper
62 changes: 1 addition & 61 deletions packages/programs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,7 @@

# @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`](https://github.com/solana-labs/solana-web3.js/tree/master/packages/library).

## Types

### `Program`

The `Program` type defines a Solana program.

```ts
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.

```ts
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 => {
// ...
},
};
```
This package contains helpers for identifying custom 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).

## Functions

Expand All @@ -78,25 +40,3 @@ try {
}
}
```

### `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.

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

try {
// Send and confirm your transaction.
} catch (error) {
throw resolveTransactionError(error, transaction, programs);
}
```
7 changes: 4 additions & 3 deletions packages/programs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@
"supports bigint and not dead",
"maintained node versions"
],
"devDependencies": {
"dependencies": {
"@solana/addresses": "workspace:*",
"@solana/errors": "workspace:*",
"@solana/errors": "workspace:*"
},
"devDependencies": {
"@solana/functional": "workspace:*",
"@solana/transactions": "workspace:*",
"@solana/transaction-messages": "workspace:*"
},
"bundlewatch": {
Expand Down
127 changes: 0 additions & 127 deletions packages/programs/src/__tests__/resolve-transaction-error-test.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/programs/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from './program';
export * from './program-error';
export * from './resolve-transaction-error';
2 changes: 1 addition & 1 deletion packages/programs/src/program-error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address } from '@solana/addresses';
import type { Address } from '@solana/addresses';
import { isSolanaError, SOLANA_ERROR__INSTRUCTION_ERROR__CUSTOM, SolanaError } from '@solana/errors';

export function isProgramError<TProgramErrorCode extends number>(
Expand Down
34 changes: 0 additions & 34 deletions packages/programs/src/program.ts

This file was deleted.

53 changes: 0 additions & 53 deletions packages/programs/src/resolve-transaction-error.ts

This file was deleted.

6 changes: 2 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2040f96

Please sign in to comment.