Skip to content

ts: Extract Anchor error codes into their own package#2983

Merged
acheroncrypto merged 7 commits into
otter-sec:masterfrom
lorisleiva:loris/extract-error-codes
May 29, 2024
Merged

ts: Extract Anchor error codes into their own package#2983
acheroncrypto merged 7 commits into
otter-sec:masterfrom
lorisleiva:loris/extract-error-codes

Conversation

@lorisleiva

Copy link
Copy Markdown
Contributor

This PR extract all Anchor error codes as plain constants into a new granular package named @coral-xyz/anchor-errors.

This enables anyone to access these constants without having to depend on Anchor's dependencies such as web3.js v1.

For instance, it enables web3.js v2 users to assert that a custom program error is an Anchor error using the following code snippet.

import { isProgramError } from "@solana/web3.js"; // v2
import { ANCHOR_ERROR__ACCOUNT_NOT_INITIALIZED } from "@coral-xyz/anchor-errors";

isProgramError(
  error,
  transactionMessage,
  MY_PROGRAM_ADDRESS,
  ANCHOR_ERROR__ACCOUNT_NOT_INITIALIZED
);

Note that I went for multiple constants instead of an enum for reasons explained here.

Also note that I did not remove the existing LangErrorCode object to avoid introducing a breaking change. However, this may be safely dissolved on the next breaking change version.

@vercel

vercel Bot commented May 24, 2024

Copy link
Copy Markdown

@lorisleiva is attempting to deploy a commit to the coral-xyz Team on Vercel.

A member of the Team first needs to authorize it.

@acheroncrypto acheroncrypto left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the effort. Are errors the only code that needs to be separated for Anchor to work nicely with web3.js v2, or are there more?

Comment thread ts/packages/anchor-errors/package.json
Comment thread ts/packages/anchor-errors/src/index.ts Outdated
@lorisleiva

Copy link
Copy Markdown
Contributor Author

Sorry I missed this question:

Are errors the only code that needs to be separated for Anchor to work nicely with web3.js v2, or are there more?

Yes, that is everything we need from Anchor for web3.js v2.

Since we use Kinobi to generate web3.js v2 compatible clients, all the rest is generated using the Anchor IDL of the program.

@acheroncrypto acheroncrypto left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is everything we need from Anchor for web3.js v2.

Awesome. We just need the following:

@lorisleiva

Copy link
Copy Markdown
Contributor Author

All done! Let me know if that's all good. 🙏

@lorisleiva

Copy link
Copy Markdown
Contributor Author

Looks like there was one more workflow to update. We should be good with CI now. 🤞

@acheroncrypto acheroncrypto left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing the ANCHOR_ERROR__ prefix was added to make the naming consistent with the web3js v2 errors, but I think this kind of naming is redundant for this package since all errors have that prefix. (Not a big deal, just wanted to note this)

Thank you!

@acheroncrypto acheroncrypto merged commit cd82627 into otter-sec:master May 29, 2024
Otter-0x4ka5h pushed a commit to Otter-0x4ka5h/anchor that referenced this pull request Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants