coral-xyz / anchor

⚓ Solana Sealevel Framework
https://anchor-lang.com
Apache License 2.0
3.36k stars 1.25k forks source link

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

Closed lorisleiva closed 1 month ago

lorisleiva commented 1 month ago

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[bot] commented 1 month ago

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

A member of the Team first needs to authorize it.

lorisleiva commented 1 month ago

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.

lorisleiva commented 1 month ago

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

lorisleiva commented 1 month ago

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