5

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.

4
  • 1
    Can you share what that last TS code block does? What type do you get as UT and what value do you get as userType?
    – Callum M
    Commented Jul 25, 2022 at 10:30
  • 1
    Is the interchangeable use of data and account variables intentional?
    – sohrab
    Commented Jul 25, 2022 at 10:31
  • @CallumM The type UT is as follows type UT = {} but in case of UD I have type UD = { userPk: anchor.web3.PublicKey; userBalance: anchor.BN; } value of user type is : const userType: TypeDef<{ name: "UserType"; type: { kind: "enum"; variants: [{ name: "client"; }, { name: "employee"; }]; }; } & { name: "UserType"; }, Record<string, never>> Commented Jul 27, 2022 at 13:24
  • @sohrab account is a variable that contains solana account which can be fetched by anchor.IdlAccount<Program> and data is member of that account. Commented Jul 27, 2022 at 13:26

2 Answers 2

3

For example if you have an account defined in Rust code

#[account()]
/// Stores meta information about the `Account` on chain
pub struct User {
    // user data
    pub user_data : UserData,
    // use type
    pub user_type : UserType,
}

You have created an account User on solana cluster then in ts code you can fetch:

type UserType = {client:{}} | {employee:{}};
type User = Omit<anchor.IdlAccounts<ProgramName>["user"], "userType"> & {userType: UserType};
const user : User = await this.program.account.user.fetch(pubkey);
const userType = user.userType;

account is instance 'user' in this case.

2

I found a way to compare enums in typescript.

We have to define custom type: type UserType = {client: {}} | {employee:{}}

then you can create a variable const userType : UserType = account.userType

Then comparision goes as follows :

import loadash

import _ from "lodash";

Assert:

assert(_.isEqual(userType,{client: {}}))

2
  • i am running into a similar problem. please i would like to know what account(account.userType) stands for in your code Commented Oct 11, 2022 at 2:22
  • Hey I have created in form of other answer as I had problems putting it as comments. Commented Oct 12, 2022 at 16:24

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.