2

Does anyone have experience using HashMaps in an Anchor project?
I have the following implementation for a struct:

#[account]
#[derive(Debug)]
pub struct Company {
    pub id: u64, // 8 bytes
    pub authority: Pubkey, // 32 bytes
    pub name: String, // 24 bytes
    pub worker_container: HashMap<u64, bool>, // 8 * 10 = 80 bytes
    pub product_container: HashMap<u64, bool>, // (8 + 1) * 50 = 450 bytes
    pub bump: u8, // 1 byte
}

I can build and deploy the struct with no errors
However, while testing on JS side I got the following error:

IdlError: Type not found: {"name":"workerContainer","type":{"defined":"HashMap<u64,bool>"}}

I don't think it's due to some serialization problems.
According to https://github.com/near/borsh-js#type-mappings Borsch can serialize HashMaps so I wonder what the error could be.

(note: I'm aware that I could use a Vec, however I want to be able to check whether a key value exists quickly so I chose to use HashMaps instead)

1 Answer 1

1

Unfortunately, unlike Borsh; Anchor does not implement a JS type for hashmaps. See https://www.anchor-lang.com/docs/javascript-anchor-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.