kevinheavey / anchor-bankrun

An Anchor wrapper for solana-bankrun
MIT License
20 stars 5 forks source link

Possible to return `AnchorProvider`-like errors? #1

Closed metaproph3t closed 1 year ago

metaproph3t commented 1 year ago

When I call a foo.methods.bar().rpc() in a regular Anchor test and I expect an error, I get back an error that looks like this:

{"errorLogs":["Program log: AnchorError thrown in programs/autocrat/src/lib.rs:70. Error Code: InvalidMetaDAOSigner. Error Number: 6003. Error Message: A signer pubkey should have been set to the Meta-DAO account pubkey but it wasn't."],"logs":["Program 11111111111111111111111111111111 invoke [1]","Program 11111111111111111111111111111111 success","Program Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS invoke [1]","Program log: Instruction: InitializeProposal","Program log: AnchorError thrown in programs/autocrat/src/lib.rs:70. Error Code: InvalidMetaDAOSigner. Error Number: 6003. Error Message: A signer pubkey should have been set to the Meta-DAO account pubkey but it wasn't.","Program Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS consumed 10113 of 400000 compute units","Program Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS failed: custom program error: 0x1773"],"error":{"errorCode":{"code":"InvalidMetaDAOSigner","number":6003},"errorMessage":"A signer pubkey should have been set to the Meta-DAO account pubkey but it wasn't","origin":{"file":"programs/autocrat/src/lib.rs","line":70}},"_programErrorStack":{"stack":["Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"]}}

In anchor-bankrun, I get one that looks like this:

{"code":6003,"msg":"A signer pubkey should have been set to the Meta-DAO account pubkey but it wasn't"}

Is it technically possible to have the latter look more like the former? Specifically, I assert against the human-readable code (InvalidMetaDAOSigner in this example), and was wondering if this is possible to include

kevinheavey commented 1 year ago

Yes I think this can be done. May need to make some changes to solana-bankrun, like adding a method that processes a transaction and returns a result-like object instead of raising an error. Currently I think solana-bankrun is swallowing up the log messages when the transaction fails

metaproph3t commented 1 year ago

Took a dive into the code; wasn't able to identify the place that needed fixing. Here's the workaround I'm using at the moment, in case useful to anyone else:

export const expectError = (
  program: Program,
  expectedError: string,
  message: string
): [() => void, (e: any) => void] => {
  return [
    () => assert.fail(message),
    (e) => {
      assert(e.code != undefined, "problem retrieving program error code");
      for (let idlError of program.idl.errors) {
        if (idlError.code == e.code) {
          assert.equal(idlError.name, expectedError);
          return;
        }
      }
      assert.fail("error doesn't match idl");
    },
  ];
};

const callbacks = expectError(
        vaultProgram,
        "InsufficientUnderlyingTokens",
        "mint suceeded despite user not having enough underlying tokens"
      );
      await mintConditionalTokens(
        vaultProgram,
        amount + 10,
        bob,
        bobDepositSlip,
        vault,
        vaultUnderlyingTokenAccount,
        bobUnderlyingTokenAccount,
        conditionalTokenMint,
        bobConditionalTokenAccount
      ).then(callbacks[0], callbacks[1]);