I'm following this tutorial (https://solanacookbook.com/guides/account-maps.html#single-map-account) to create an account that store a mapping. But when I run anchor test, the compiler returns
"IdlError: Type not found: {"name":"map","type":{"defined":"BTreeMap<u128,MarketItem>"}}"
This is my code:
pub mod nft {
use super::*;
pub fn init_state(ctx: Context<InitState>) -> Result<()> {
let authority_account = &ctx.accounts.authority_account;
let system_program = &ctx.accounts.system_program;
let state_account = &ctx.accounts.state_account;
let (state_pda, state_bump) =
Pubkey::find_program_address(&[b"state".as_ref()], system_program.key);
if state_pda != *state_account.key
|| !state_account.is_writable
|| !state_account.data_is_empty()
{
return Err(error::Error::InvalidStateAccount.into());
}
let rent = Rent::get()?;
let rent_lamports = rent.minimum_balance(State::LEN);
let create_state_ix = &solana_program::system_instruction::create_account(
authority_account.key,
state_account.key,
rent_lamports,
State::LEN.try_into().unwrap(),
system_program.key,
);
// msg!("Creating stateAccount account");
solana_program::program::invoke_signed(
create_state_ix,
&[
authority_account.clone(),
state_account.clone(),
system_program.to_account_info(),
],
&[&[b"state".as_ref(), &[state_bump]]],
)?;
// msg!("Deserializing MapAccount account");
let mut program_state =
solana_program::borsh::try_from_slice_unchecked::<State>(&state_account.data.borrow())
.unwrap();
let empty_map: BTreeMap<u128, MarketItem> = BTreeMap::new();
program_state.map = empty_map;
// msg!("Serializing MapAccount account");
program_state.serialize(&mut &mut state_account.data.borrow_mut()[..])?;
Ok(())
}
}
#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
pub struct MarketItem {
item_id: u128,
nft_contract: Pubkey, // program id,
token_id: Pubkey, // ATA
seller: Pubkey,
owner: Option<Pubkey>,
price: u128,
file_name: String,
description: String,
cash_back: u8,
sold: bool,
gacha: bool,
bump: u8,
}
#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
pub struct State {
pub map: BTreeMap<u128, MarketItem>,
pub item_ids: u128,
pub item_sold: u128,
pub owner: Pubkey,
pub seed: u64,
}
impl State {
const LEN: usize = 1 + (4 + (10 * 64));
}
How can I fix this? Thanks