3

My HashMap is not functioning properly on Solana when using a Rust program. Is there an alternative approach to achieving a similar functionality to a HashMap? Essentially, I want to retrieve data associated with each user who has donated SOL. When I paste an address, I want to find out what they have donated.

2 Answers 2

3

For your application, the "Solana" way would be to create a PDA for each user who has donated when the donation ix runs, e.g:

    #[account(
        init,
        seeds = [b"user-data-acc", user.key().as_ref()],
        bump,
        payer = payer,
        space = SIZE_OF_YOUR_STRUCT
    )]
    pub user_data: Account<'info, YourUserDataAcc>,

Now you can store whatever information you want in that struct, like:

#[account]
#[derive(Default)]
pub struct YourUserDataAcc {
    pub some_key: Pubkey,
    pub another_key: Pubkey,
    pub amt: u64,
}

And now you can always derive and load your struct for some userKey the front end with:

PublicKey.findProgramAddressSync(
    [Buffer.from("user-data-acc", "utf-8"), userKey.toBuffer()],
    programId
  );
1

Hashmaps aren't supported in Solana. Try using a Btree instead.

2

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.