Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 7 commits into from
May 29, 2024

Conversation

lorisleiva
Copy link
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.

Copy link

vercel bot commented May 24, 2024

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

A member of the Team first needs to authorize it.

Copy link
Collaborator

@acheroncrypto acheroncrypto left a comment

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?

ts/packages/anchor-errors/package.json Outdated Show resolved Hide resolved
ts/packages/anchor-errors/src/index.ts Outdated Show resolved Hide resolved
@lorisleiva
Copy link
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.

Copy link
Collaborator

@acheroncrypto acheroncrypto left a comment

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
Contributor Author

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

@lorisleiva
Copy link
Contributor Author

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

Copy link
Collaborator

@acheroncrypto acheroncrypto left a comment

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 coral-xyz:master May 29, 2024
48 of 49 checks passed
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