From c68e6ab0312f7e6b87a57a3eb7be8de5eb08c370 Mon Sep 17 00:00:00 2001 From: ilija Date: Mon, 3 Feb 2025 20:22:08 +0100 Subject: [PATCH] Add Test Struct PDA --- .../contract-reader-interface/src/lib.rs | 106 +++++++++++++++++- 1 file changed, 105 insertions(+), 1 deletion(-) diff --git a/contracts/programs/contract-reader-interface/src/lib.rs b/contracts/programs/contract-reader-interface/src/lib.rs index 752712c21..9ceb25b24 100644 --- a/contracts/programs/contract-reader-interface/src/lib.rs +++ b/contracts/programs/contract-reader-interface/src/lib.rs @@ -36,8 +36,32 @@ pub mod contract_reader_interface { pub fn store_val(ctx: Context, value: u64) -> Result<()> { let val = &mut ctx.accounts.value; val.u64_value = value; + Ok(()) } + + pub fn store_test_struct( + ctx: Context, + 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)] @@ -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::() + 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>, } @@ -121,4 +165,64 @@ pub struct DataAccount { #[account] pub struct Value { pub u64_value: u64 -} \ No newline at end of file +} + +#[account] +pub struct TestStruct { + pub idx: u64, + pub bump: u8, + + pub field: Option, + pub oracle_id: [u8; 32], + pub oracle_ids: [[u8; 32]; 32], + pub account_struct: AccountStruct, + pub accounts: Vec>, + 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, + pub oracle_id: [u8; 32], + pub oracle_ids: [[u8; 32]; 32], + pub account_struct: AccountStruct, + pub accounts: Vec>, + 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, + 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, +}