Created
December 5, 2021 21:29
-
-
Save milk-duds-pro/625997c4141e03df16e657aef02e3048 to your computer and use it in GitHub Desktop.
an example showing that 1 account can have multiple different data structures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use anchor_lang::prelude::*; | |
declare_id!("8yBiqm4eZjDQACD5JnZrvoKB6nyE9xDh3yvMiPnWtTpL"); | |
#[program] | |
pub mod multi_state_account_example { | |
use super::*; | |
pub fn create_many_faced_account_first_state(ctx: Context<CreateManyFacedAccount>, _account_bump:u8, first_hi1_to_add:u64, first_hi2_to_add:u32) -> ProgramResult { | |
let new_state = State::FirstState { | |
first_hi1: first_hi1_to_add, | |
first_hi2: first_hi2_to_add | |
}; | |
ctx.accounts.many_faced_pda.current = Some(new_state); | |
msg!("created many faced account"); | |
match &ctx.accounts.many_faced_pda.current { | |
Some(State::FirstState { first_hi1, first_hi2 }) => { | |
msg!("first hi1: {}", first_hi1); | |
msg!("first hi2: {}", first_hi2); | |
} | |
_ => return Err(ErrorCode::TestError.into()) | |
} | |
Ok(()) | |
} | |
pub fn update_many_faced_account_second_state(ctx: Context<UpdateManyFacedAccount>, _account_bump:u8, second_hi1_to_add:u32, second_hi2_to_add:u64) -> ProgramResult { | |
let new_state = State::SecondState { | |
second_hi1: second_hi1_to_add, | |
second_hi2: second_hi2_to_add | |
}; | |
ctx.accounts.many_faced_pda.current = Some(new_state); | |
msg!("update many faced account"); | |
match &ctx.accounts.many_faced_pda.current { | |
Some(State::SecondState { second_hi1, second_hi2 }) => { | |
msg!("second hi1: {}", second_hi1); | |
msg!("second hi2: {}", second_hi2); | |
} | |
_ => return Err(ErrorCode::TestError.into()) | |
} | |
Ok(()) | |
} | |
pub fn check_many_faced_account(ctx: Context<UpdateManyFacedAccount>, _account_bump:u8) -> ProgramResult { | |
msg!("checking many faced account"); | |
match &ctx.accounts.many_faced_pda.current { | |
Some(State::FirstState { first_hi1, first_hi2 }) => { | |
msg!("it's a variant of FirstState"); | |
msg!("first hi1: {}", first_hi1); | |
msg!("first hi2: {}", first_hi2); | |
} | |
Some(State::SecondState { second_hi1, second_hi2 }) => { | |
msg!("it's a variant of SecondState"); | |
msg!("second hi1: {}", second_hi1); | |
msg!("second hi2: {}", second_hi2); | |
} | |
_ => return Err(ErrorCode::TestError.into()) | |
} | |
Ok(()) | |
} | |
} | |
#[error] | |
pub enum ErrorCode { | |
TestError, | |
} | |
#[derive(Accounts)] | |
#[instruction(_account_bump:u8)] | |
pub struct CreateManyFacedAccount<'info> { | |
#[account( | |
init, | |
payer = signer, | |
space = 8 + 8 + 50, | |
seeds = [b"many_faces".as_ref()], | |
bump = _account_bump | |
)] | |
many_faced_pda: Account<'info, ManyFacedAccount>, | |
signer: Signer<'info>, | |
system_program: Program<'info, System>, | |
} | |
#[derive(Accounts)] | |
#[instruction(_account_bump:u8)] | |
pub struct UpdateManyFacedAccount<'info> { | |
#[account( | |
mut, | |
seeds = [b"many_faces".as_ref()], | |
bump = _account_bump | |
)] | |
many_faced_pda: Account<'info, ManyFacedAccount>, | |
signer: Signer<'info>, | |
system_program: Program<'info, System>, | |
} | |
#[account] | |
#[derive(Default)] | |
struct ManyFacedAccount { | |
current: Option<State> | |
} | |
#[derive(AnchorSerialize, AnchorDeserialize, Clone)] | |
enum State { | |
FirstState { first_hi1:u64, first_hi2:u32 }, | |
SecondState { second_hi1:u32, second_hi2:u64 } | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment