Skip to content

Commit

Permalink
Add Test Struct PDA
Browse files Browse the repository at this point in the history
  • Loading branch information
ilija42 authored and amit-momin committed Feb 4, 2025
1 parent 3d60917 commit c68e6ab
Showing 1 changed file with 105 additions and 1 deletion.
106 changes: 105 additions & 1 deletion contracts/programs/contract-reader-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,32 @@ pub mod contract_reader_interface {
pub fn store_val(ctx: Context<StoreVal>, value: u64) -> Result<()> {
let val = &mut ctx.accounts.value;
val.u64_value = value;

Ok(())
}

pub fn store_test_struct(
ctx: Context<StoreTestStruct>,
test_idx: u64,
data: TestStructData,
) -> Result<()> {
let test_struct_account = &mut ctx.accounts.test_struct;

test_struct_account.idx = test_idx;
test_struct_account.bump = ctx.bumps.test_struct;

test_struct_account.field = data.field;
test_struct_account.oracle_id = data.oracle_id;
test_struct_account.oracle_ids = data.oracle_ids;
test_struct_account.account_struct = data.account_struct;
test_struct_account.accounts = data.accounts;
test_struct_account.different_field = data.different_field;
test_struct_account.big_field = data.big_field;
test_struct_account.nested_dynamic_struct = data.nested_dynamic_struct;
test_struct_account.nested_static_struct = data.nested_static_struct;

Ok(())
}
}

#[derive(Accounts)]
Expand Down Expand Up @@ -98,6 +122,26 @@ pub struct StoreVal<'info> {
seeds=[b"val"],
bump)]
pub value: Account<'info, Value>,
}

#[derive(Accounts)]
#[instruction(test_idx: u64)]
pub struct StoreTestStruct<'info> {
#[account(mut)]
pub signer: Signer<'info>,

#[account(
init,
payer = signer,
// Add extra buffer for variable fields
space = 8 + size_of::<TestStruct>() + 400,
seeds = [
b"test-struct",
test_idx.to_le_bytes().as_ref()
],
bump
)]
pub test_struct: Account<'info, TestStruct>,

pub system_program: Program<'info, System>,
}
Expand All @@ -121,4 +165,64 @@ pub struct DataAccount {
#[account]
pub struct Value {
pub u64_value: u64
}
}

#[account]
pub struct TestStruct {
pub idx: u64,
pub bump: u8,

pub field: Option<i32>,
pub oracle_id: [u8; 32],
pub oracle_ids: [[u8; 32]; 32],
pub account_struct: AccountStruct,
pub accounts: Vec<Vec<u8>>,
pub different_field: String,
pub big_field: Option<[u8; 32]>,

pub nested_dynamic_struct: MidLevelDynamicTestStruct,
pub nested_static_struct: MidLevelStaticTestStruct,
}

#[account]
pub struct TestStructData {
pub field: Option<i32>,
pub oracle_id: [u8; 32],
pub oracle_ids: [[u8; 32]; 32],
pub account_struct: AccountStruct,
pub accounts: Vec<Vec<u8>>,
pub different_field: String,
pub big_field: Option<[u8; 32]>,
pub nested_dynamic_struct: MidLevelDynamicTestStruct,
pub nested_static_struct: MidLevelStaticTestStruct,
}

#[account]
pub struct AccountStruct {
pub account: Vec<u8>,
pub account_str: String,
}

#[account]
pub struct MidLevelDynamicTestStruct {
pub fixed_bytes: [u8; 2],
pub inner: InnerDynamicTestStruct,
}

#[account]
pub struct InnerDynamicTestStruct {
pub i: i64,
pub s: String,
}

#[account]
pub struct MidLevelStaticTestStruct {
pub fixed_bytes: [u8; 2],
pub inner: InnerStaticTestStruct,
}

#[account]
pub struct InnerStaticTestStruct {
pub i: i64,
pub a: Vec<u8>,
}

0 comments on commit c68e6ab

Please sign in to comment.