// ===== 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}; seahorse_const! { MAX , 7 } seahorse_const! { MESSAGE , "Hello constants" . to_string () } seahorse_const! { MIN , 2 } seahorse_const! { RANGE , (MAX ! () - MIN ! ()) } pub fn use_constants_handler<'info>(mut signer: SeahorseSigner<'info, '_>) -> () { solana_program::msg!("{}", MESSAGE!()); for mut i in MIN!()..MAX!() { solana_program::msg!("{} {}", "Step:".to_string(), i); } solana_program::msg!("{} {}", "Range:".to_string(), RANGE!()); } // ===== 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!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"); 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 constants { use super::*; use seahorse_util::*; use std::collections::HashMap; #[derive(Accounts)] pub struct UseConstants<'info> { #[account(mut)] pub signer: Signer<'info>, } pub fn use_constants(ctx: Context) -> Result<()> { let mut programs = HashMap::new(); let programs_map = ProgramsMap(programs); let signer = SeahorseSigner { account: &ctx.accounts.signer, programs: &programs_map, }; use_constants_handler(signer.clone()); return Ok(()); } }