Skip to content

Add missing get_favorites instruction to Favorites Anchor example #396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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>,
}