Untitled
unknown
plain_text
a year ago
2.0 kB
4
Indexable
use anchor_lang::prelude::*; declare_id!("4AZiGinuagXiWgPyDjYGCufzP7qDJEoqQn4cwUH8k7Vb"); #[program] pub mod profile_manager { use super::*; pub fn create_profile( ctx: Context<CreateProfile>, first_name: String, last_name: String, age: u8, genre: String, childrens: Option<Vec<Children>>, ) -> Result<()> { let new_profile = &mut ctx.accounts.new_profile; new_profile.authority = ctx.accounts.signer.key(); new_profile.first_name = first_name; new_profile.last_name = last_name; new_profile.age = age; new_profile.genre = genre; new_profile.childrens = childrens; msg!( "Profile of {} {} created with {} childrens", new_profile.first_name, new_profile.last_name, new_profile.childrens.as_ref().map_or(0, |c| c.len()) ); Ok(()) } } #[derive(Accounts)] pub struct CreateProfile<'info> { #[account(init, payer = signer, space = 8 + Profile::LEN)] pub new_profile: Account<'info, Profile>, #[account(mut)] pub signer: Signer<'info>, pub system_program: Program<'info, System>, } #[account] pub struct Profile { pub authority: Pubkey, pub first_name: String, pub last_name: String, pub age: u8, pub genre: String, pub childrens: Option<Vec<Children>>, } impl Profile { const LEN: usize = 32 // authority + 4 + 50 // first_name (4 bytes for length prefix + max 50 bytes for string) + 4 + 50 // last_name (4 bytes for length prefix + max 50 bytes for string) + 1 // age + 4 + 50 // genre (4 bytes for length prefix + max 50 bytes for string) + 4 + (4 + 50 + 1) * 50; // childrens (4 bytes for length prefix + up to 50 children each 55 bytes) } #[derive(AnchorSerialize, AnchorDeserialize, Clone)] pub struct Children { pub age: u8, }
Editor is loading...
Leave a Comment