// ===== dot/mod.rs ===== pub mod program; // ===== dot/program.rs ===== #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] use crate::{id, seahorse_util::*}; use anchor_lang::{prelude::*, solana_program}; use anchor_spl::token::{self, Mint, Token, TokenAccount}; use std::{cell::RefCell, rc::Rc}; #[account] #[derive(Debug)] pub struct Another { pub data: u8, } impl<'info, 'entrypoint> Another { pub fn load( account: &'entrypoint mut Box>, programs_map: &'entrypoint ProgramsMap<'info>, ) -> Mutable> { let data = account.data; Mutable::new(LoadedAnother { __account__: account, __programs__: programs_map, data, }) } pub fn store(loaded: Mutable) { let mut loaded = loaded.borrow_mut(); let data = loaded.data; loaded.__account__.data = data; } } #[derive(Debug)] pub struct LoadedAnother<'info, 'entrypoint> { pub __account__: &'entrypoint mut Box>, pub __programs__: &'entrypoint ProgramsMap<'info>, pub data: u8, } #[account] #[derive(Debug)] pub struct User { pub data: u8, } impl<'info, 'entrypoint> User { pub fn load( account: &'entrypoint mut Box>, programs_map: &'entrypoint ProgramsMap<'info>, ) -> Mutable> { let data = account.data; Mutable::new(LoadedUser { __account__: account, __programs__: programs_map, data, }) } pub fn store(loaded: Mutable) { let mut loaded = loaded.borrow_mut(); let data = loaded.data; loaded.__account__.data = data; } } #[derive(Debug)] pub struct LoadedUser<'info, 'entrypoint> { pub __account__: &'entrypoint mut Box>, pub __programs__: &'entrypoint ProgramsMap<'info>, pub data: u8, } pub fn ix_handler<'info>( mut payer: SeahorseSigner<'info, '_>, mut user: Mutable>, mut another: Empty>>, ) -> () { let mut a = another.account.clone(); solana_program::msg!("{:?}", user.borrow().__account__.key()); solana_program::msg!("{}", user.borrow().data); } // ===== lib.rs ===== #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] pub mod dot; use anchor_lang::prelude::*; use anchor_spl::{ associated_token::{self, AssociatedToken}, token::{self, Mint, Token, TokenAccount}, }; use dot::program::*; use std::{cell::RefCell, rc::Rc}; declare_id!("4SEMJzX6o2YQNws7yrsfUdjJCR4B5Z3GyR2Pj7UgzDy2"); pub mod seahorse_util { use super::*; use std::{ collections::HashMap, fmt::Debug, ops::{Deref, Index, IndexMut}, }; pub struct Mutable(Rc>); impl Mutable { pub fn new(obj: T) -> Self { Self(Rc::new(RefCell::new(obj))) } } impl Clone for Mutable { fn clone(&self) -> Self { Self(self.0.clone()) } } impl Deref for Mutable { type Target = Rc>; fn deref(&self) -> &Self::Target { &self.0 } } impl Debug for Mutable { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self.0) } } impl Default for Mutable { fn default() -> Self { Self::new(T::default()) } } pub trait IndexWrapped { type Output; fn index_wrapped(&self, index: i128) -> &Self::Output; } pub trait IndexWrappedMut: IndexWrapped { fn index_wrapped_mut(&mut self, index: i128) -> &mut ::Output; } impl IndexWrapped for Vec { type Output = T; fn index_wrapped(&self, mut index: i128) -> &Self::Output { if index < 0 { index += self.len() as i128; } let index: usize = index.try_into().unwrap(); self.index(index) } } impl IndexWrappedMut for Vec { fn index_wrapped_mut(&mut self, mut index: i128) -> &mut ::Output { if index < 0 { index += self.len() as i128; } let index: usize = index.try_into().unwrap(); self.index_mut(index) } } impl IndexWrapped for [T; N] { type Output = T; fn index_wrapped(&self, mut index: i128) -> &Self::Output { if index < 0 { index += N as i128; } let index: usize = index.try_into().unwrap(); self.index(index) } } impl IndexWrappedMut for [T; N] { fn index_wrapped_mut(&mut self, mut index: i128) -> &mut ::Output { if index < 0 { index += N as i128; } let index: usize = index.try_into().unwrap(); self.index_mut(index) } } #[derive(Clone)] pub struct Empty { pub account: T, pub bump: Option, } #[derive(Clone, Debug)] pub struct ProgramsMap<'info>(pub HashMap<&'static str, AccountInfo<'info>>); impl<'info> ProgramsMap<'info> { pub fn get(&self, name: &'static str) -> AccountInfo<'info> { self.0.get(name).unwrap().clone() } } #[derive(Clone, Debug)] pub struct WithPrograms<'info, 'entrypoint, A> { pub account: &'entrypoint A, pub programs: &'entrypoint ProgramsMap<'info>, } impl<'info, 'entrypoint, A> Deref for WithPrograms<'info, 'entrypoint, A> { type Target = A; fn deref(&self) -> &Self::Target { &self.account } } pub type SeahorseAccount<'info, 'entrypoint, A> = WithPrograms<'info, 'entrypoint, Box>>; pub type SeahorseSigner<'info, 'entrypoint> = WithPrograms<'info, 'entrypoint, Signer<'info>>; #[derive(Clone, Debug)] pub struct CpiAccount<'info> { #[doc = "CHECK: CpiAccounts temporarily store AccountInfos."] pub account_info: AccountInfo<'info>, pub is_writable: bool, pub is_signer: bool, pub seeds: Option>>, } #[macro_export] macro_rules! seahorse_const { ($ name : ident , $ value : expr) => { macro_rules! $name { () => { $value }; } pub(crate) use $name; }; } pub trait Loadable { type Loaded; fn load(stored: Self) -> Self::Loaded; fn store(loaded: Self::Loaded) -> Self; } macro_rules! Loaded { ($ name : ty) => { <$name as Loadable>::Loaded }; } pub(crate) use Loaded; #[macro_export] macro_rules! assign { ($ lval : expr , $ rval : expr) => {{ let temp = $rval; $lval = temp; }}; } #[macro_export] macro_rules! index_assign { ($ lval : expr , $ idx : expr , $ rval : expr) => { let temp_rval = $rval; let temp_idx = $idx; $lval[temp_idx] = temp_rval; }; } pub(crate) use assign; pub(crate) use index_assign; pub(crate) use seahorse_const; } #[program] mod account_key { use super::*; use seahorse_util::*; use std::collections::HashMap; #[derive(Accounts)] pub struct Ix<'info> { #[account(mut)] pub payer: Signer<'info>, #[account(mut)] pub user: Box>, # [account (init , space = std :: mem :: size_of :: < dot :: program :: Another > () + 8 , payer = payer , seeds = [user . key () . as_ref ()] , bump)] pub another: Box>, pub rent: Sysvar<'info, Rent>, pub system_program: Program<'info, System>, } pub fn ix(ctx: Context) -> Result<()> { let mut programs = HashMap::new(); programs.insert( "system_program", ctx.accounts.system_program.to_account_info(), ); let programs_map = ProgramsMap(programs); let payer = SeahorseSigner { account: &ctx.accounts.payer, programs: &programs_map, }; let user = dot::program::User::load(&mut ctx.accounts.user, &programs_map); let another = Empty { account: dot::program::Another::load(&mut ctx.accounts.another, &programs_map), bump: ctx.bumps.get("another").map(|bump| *bump), }; ix_handler(payer.clone(), user.clone(), another.clone()); dot::program::User::store(user); dot::program::Another::store(another.account); return Ok(()); } }