1

Not sure if this is really an anchor or solana related question, since I am able to compile, just not to run tests.

I can run code and test against code that has an account that has following enum:

#[derive(Debug, AnchorSerialize, AnchorDeserialize, Clone, Copy, PartialEq, Eq)]
pub enum HandResult {
    Win,
    Lose,
    Draw,
}

But it fails to do anything the moment enums have anything in, like:

#[derive(Debug, AnchorSerialize, AnchorDeserialize, Clone, Copy, PartialEq, Eq)]
pub enum HandResult {
    Win,
    Lose(u64),
    Draw,
}

It then fails for me silently with without doing anything:

  0 passing (0ms)

✨  Done in 1.11s.
2
  • If you can provide more code, it would help. Are these unit tests or do they run against the blockchain? Make sure you're not running tests with code where the enum has data associated with it's variants, while using an account that was initialized with the initial version of the enum, that doesn't have data associated with it.
    – Serban
    Oct 2, 2023 at 10:06
  • This is for a unit tests running against a local validator. I can't initialise anything as it doesn't even attempt to go to the first test (in the original file first test was initialising other account without issues, now it skips it altogether). Here were just minimum examples that would cause it to crash, i've written quite a chunky implementation in rust before realising I can't actually test it at all. Oct 2, 2023 at 16:15

2 Answers 2

2

So try/catching

Loading of the IDL in my unit test:

let program;
  try {
    program = anchor.workspace.ProgramA as Program<ProgramA>;
  } catch (err) {
    console.log("err: ", err);
  }

Gives me:

err:  Error: Tuple enum variants not yet implemented.

So this will work:

#[derive(Debug, AnchorSerialize, AnchorDeserialize, Clone, Copy, PartialEq, Eq)]
pub enum HandResult {
    Win,
    Lose{val: u64},
    Draw,
}
2

The following types of enums are called tuple enums:

#[derive(Debug, AnchorSerialize, AnchorDeserialize, Clone, Copy, PartialEq, Eq)]
pub enum HandResult {
    Win,
    Lose(u64),
    Draw,
}

Earlier it wasn't possible to use such enums in Anchor so you get the following error if you try to call methods involving such enums:

err:  Error: Tuple enum variants not yet implemented.

But now Anchor supports tuple enums. Make sure you are using the recent version of Anchor client. If not then update your anchor npm package. Also, make sure you are using @coral-xyz/anchor and not @project-serum/anchor.

Once you are running the recent version of anchor package, you can use the tuple enum as argument in following way:

const tx = await program.methods.initialize(
    {lose: [new BN(8)]}
)
.accounts({myAccount: myAccount.publicKey})
.signers([myAccount])
.rpc()

The content of the tuple which in your case was u64 will go inside an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.