Skip to content

Commit

Permalink
compile examples
Browse files Browse the repository at this point in the history
  • Loading branch information
z80dev committed Nov 28, 2024
1 parent 06ccfd4 commit a240e4c
Show file tree
Hide file tree
Showing 8 changed files with 0 additions and 3,231 deletions.
454 changes: 0 additions & 454 deletions tests/compiled-examples/calculator.rs

Large diffs are not rendered by default.

279 changes: 0 additions & 279 deletions tests/compiled-examples/constants.rs
Original file line number Diff line number Diff line change
@@ -1,279 +0,0 @@
// ===== 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<T>(Rc<RefCell<T>>);

impl<T> Mutable<T> {
pub fn new(obj: T) -> Self {
Self(Rc::new(RefCell::new(obj)))
}
}

impl<T> Clone for Mutable<T> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}

impl<T> Deref for Mutable<T> {
type Target = Rc<RefCell<T>>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<T: Debug> Debug for Mutable<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.0)
}
}

impl<T: Default> Default for Mutable<T> {
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 <Self as IndexWrapped>::Output;
}

impl<T> IndexWrapped for Vec<T> {
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<T> IndexWrappedMut for Vec<T> {
fn index_wrapped_mut(&mut self, mut index: i128) -> &mut <Self as IndexWrapped>::Output {
if index < 0 {
index += self.len() as i128;
}

let index: usize = index.try_into().unwrap();

self.index_mut(index)
}
}

impl<T, const N: usize> 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<T, const N: usize> IndexWrappedMut for [T; N] {
fn index_wrapped_mut(&mut self, mut index: i128) -> &mut <Self as IndexWrapped>::Output {
if index < 0 {
index += N as i128;
}

let index: usize = index.try_into().unwrap();

self.index_mut(index)
}
}

#[derive(Clone)]
pub struct Empty<T: Clone> {
pub account: T,
pub bump: Option<u8>,
}

#[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<Account<'info, A>>>;

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<Vec<Vec<u8>>>,
}

#[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<UseConstants>) -> 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(());
}
}

Loading

0 comments on commit a240e4c

Please sign in to comment.