2

I was trying pass Enum type as argument in test function, but I am getting undefined error.

Here's the program enum and function:

#[derive(AnchorSerialize, AnchorDeserialize, Copy, Clone, PartialEq, Eq)]
pub enum AppState {
    Finished,
    Active,
}


pub fn set_state(ctx: Context<Init>, state: AppState) -> Result<()> {
       // DO SOMETHING USEFULL
}

Here's test script:

import { App } from "../target/types/app";

type APP_STATE = anchor.IdlTypes<App>["AppState"];
await program.methods
       .setState(APP_STATE.Active)
       .accounts({
           user: user.publicKey,
           state: statePDA,
       })
       .signers([user])
       .rpc();

The error shows that APP_STATE is undefined.

1
  • 1
    Use {finished:{}} or {active:{}}
    – CryptoSar
    Sep 4, 2022 at 22:29

1 Answer 1

5

You can pass it as { active: {} }.

Enums with internal data

pub enum AppState {
    Paused { reason: String }
}

You can pass it as { paused: { reason: "Not enough players" } }

Reference: Anchor Javascript Types

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.