Skip to content

Commit acdd712

Browse files
authored
Add get_favorites instruction to Anchor favorites example
1 parent 4b8594e commit acdd712

File tree

1 file changed

+22
-0
lines changed
  • basics/favorites/anchor/programs/favorites/src

1 file changed

+22
-0
lines changed

basics/favorites/anchor/programs/favorites/src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ pub mod favorites {
2828
}
2929

3030
// We can also add a get_favorites instruction handler to return the user's favorite number and color
31+
pub fn get_favorites(context: Context<GetFavorites>) -> Result<()> {
32+
let favorites = &context.accounts.favorites;
33+
msg!("User {}'s favorites: number = {}, color = {}, hobbies = {:?}",
34+
context.accounts.user.key(),
35+
favorites.number,
36+
favorites.color,
37+
favorites.hobbies
38+
);
39+
Ok(())
40+
}
3141
}
3242

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

6070
pub system_program: Program<'info, System>,
6171
}
72+
73+
// When people call the get_favorites instruction, they will need to provide the account to read
74+
#[derive(Accounts)]
75+
pub struct GetFavorites<'info> {
76+
pub user: Signer<'info>,
77+
78+
#[account(
79+
seeds = [b"favorites", user.key().as_ref()],
80+
bump,
81+
)]
82+
pub favorites: Account<'info, Favorites>,
83+
}

0 commit comments

Comments
 (0)