1

This error comes in the picture :- the trait bound MerkleTree: BorshSerialize is not satisfied the following other types implement trait BorshSerialize: bool i8 i16 i32 i64 i128 usize u8 and 110 others see issue #48214

Here is the code :-

use {
    borsh::{BorshDeserialize, BorshSerialize},
    merkletreers::{tree::MerkleTree, utils::hash_it},
    solana_program::{entrypoint::ProgramResult, msg, program_error::ProgramError},
};

#[derive(BorshSerialize, BorshDeserialize, Debug)]
pub struct WhitelistArgs {
    pub accounts: Vec<String>,
}

const ADMIN_ACCOUNT: &str = "Cv66dQwcSJDdXNVHybchYqp73d75Y8XWj6pKcjN3ffAy";

#[derive(BorshSerialize, BorshDeserialize)]
pub struct Tree {
    pub merkle_tree: MerkleTree,
}

pub(crate) static mut WHITELIST_TREE: Option<Tree> = None;

pub fn whitelist_account(args: WhitelistArgs, admin_account: String) -> ProgramResult {
    if admin_account != ADMIN_ACCOUNT {
        return Err(ProgramError::IllegalOwner);
    }

    let leaves: Vec<[u8; 32]> = args
        .accounts
        .iter()
        .map(|account_str| {
            let account_bytes = account_str.as_bytes();
            let mut buffer = [0u8; 32];
            hash_it(account_bytes, &mut buffer);
            buffer
        })
        .collect();

    let whitelist_tree = MerkleTree::new(leaves);

    let tree = Tree {
        merkle_tree: whitelist_tree,
    };

    msg!("Accounts have been added to the whitelist");
    msg!("{:?}", tree.merkle_tree.root);

    unsafe {
        WHITELIST_TREE = Some(tree);
    }

    Ok(())
}

1 Answer 1

1

The MerkleTree in your Tree struct also needs to implement the BorshSerialize and BorshDeserialize traits in order to work.

You may need to create a new struct that can convert between the external MerkleTree and your own merkle tree, and then have that new struct derive the BorshSerialize / BorshDeserialize traits.

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.