Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions basics/favorites/anchor/programs/favorites/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ pub mod favorites {
}

// We can also add a get_favorites instruction handler to return the user's favorite number and color
pub fn get_favorites(context: Context<GetFavorites>) -> Result<()> {
let favorites = &context.accounts.favorites;
msg!("User {}'s favorites: number = {}, color = {}, hobbies = {:?}",
context.accounts.user.key(),
favorites.number,
favorites.color,
favorites.hobbies
);
Ok(())
}
}

// What we will put inside the Favorites PDA
Expand Down Expand Up @@ -59,3 +69,15 @@ pub struct SetFavorites<'info> {

pub system_program: Program<'info, System>,
}

// When people call the get_favorites instruction, they will need to provide the account to read
#[derive(Accounts)]
pub struct GetFavorites<'info> {
pub user: Signer<'info>,

#[account(
seeds = [b"favorites", user.key().as_ref()],
bump,
)]
pub favorites: Account<'info, Favorites>,
}