Skip to main content
1 of 2

Support of Anchor IDL for enums

Currently I am writing smart contract code in anchor. When i declare a pub struct in smart contract I can access it in type script easily using IdlType example:

#[derive(AnchorSerialize, AnchorDeserialize, Clone, Copy)]
#[repr(C)]
/// Stores meta information about the `Account` on chain
pub struct UserData {
    // user pk
    pub user_pk : Pubkey,
    // user balance in lamports for the game / user
    pub user_balance : u64,
}

can be accessed in typescript code as follows

type UD = anchor.IdlTypes<Program>["UserData"];
let userData : UD = account.userData;
let userBalance = data.userBalance;

But i am not able to do same things with enums instead of structs.

// rust code
enum UserType {
 client,
 employee
}

// ts code
type UT = anchor.IdlTypes<Program>["UserType"];
let userType : UT = account.userType;
assert(userType == UT.client);

Could you help me to find out how to use enums.