Skip to content

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.

@vercel
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?

@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 solana-foundation:master May 29, 2024
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