|
| 1 | +#![allow(unused_imports)] |
| 2 | +#![allow(unused_variables)] |
| 3 | +#![allow(unused_mut)] |
| 4 | + |
| 5 | +pub mod dot; |
| 6 | + |
| 7 | +use anchor_lang::prelude::*; |
| 8 | +use anchor_spl::{ |
| 9 | + associated_token::{self, AssociatedToken}, |
| 10 | + token::{self, Mint, Token, TokenAccount}, |
| 11 | +}; |
| 12 | + |
| 13 | +use dot::program::*; |
| 14 | +use std::{cell::RefCell, rc::Rc}; |
| 15 | + |
| 16 | +declare_id!("9USP8f9ooxUxWTyqrQSDfyiXE1FP7Wfsg34NfAbdK1ur"); |
| 17 | + |
| 18 | +pub mod seahorse_util { |
| 19 | + use super::*; |
| 20 | + |
| 21 | + #[cfg(feature = "pyth-sdk-solana")] |
| 22 | + pub use pyth_sdk_solana::{load_price_feed_from_account_info, PriceFeed}; |
| 23 | + use std::{collections::HashMap, fmt::Debug, ops::Deref}; |
| 24 | + |
| 25 | + pub struct Mutable<T>(Rc<RefCell<T>>); |
| 26 | + |
| 27 | + impl<T> Mutable<T> { |
| 28 | + pub fn new(obj: T) -> Self { |
| 29 | + Self(Rc::new(RefCell::new(obj))) |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + impl<T> Clone for Mutable<T> { |
| 34 | + fn clone(&self) -> Self { |
| 35 | + Self(self.0.clone()) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + impl<T> Deref for Mutable<T> { |
| 40 | + type Target = Rc<RefCell<T>>; |
| 41 | + |
| 42 | + fn deref(&self) -> &Self::Target { |
| 43 | + &self.0 |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + impl<T: Debug> Debug for Mutable<T> { |
| 48 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 49 | + write!(f, "{:?}", self.0) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + impl<T: Default> Default for Mutable<T> { |
| 54 | + fn default() -> Self { |
| 55 | + Self::new(T::default()) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + impl<T: Clone> Mutable<Vec<T>> { |
| 60 | + pub fn wrapped_index(&self, mut index: i128) -> usize { |
| 61 | + if index >= 0 { |
| 62 | + return index.try_into().unwrap(); |
| 63 | + } |
| 64 | + |
| 65 | + index += self.borrow().len() as i128; |
| 66 | + |
| 67 | + return index.try_into().unwrap(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + impl<T: Clone, const N: usize> Mutable<[T; N]> { |
| 72 | + pub fn wrapped_index(&self, mut index: i128) -> usize { |
| 73 | + if index >= 0 { |
| 74 | + return index.try_into().unwrap(); |
| 75 | + } |
| 76 | + |
| 77 | + index += self.borrow().len() as i128; |
| 78 | + |
| 79 | + return index.try_into().unwrap(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + #[derive(Clone)] |
| 84 | + pub struct Empty<T: Clone> { |
| 85 | + pub account: T, |
| 86 | + pub bump: Option<u8>, |
| 87 | + } |
| 88 | + |
| 89 | + #[derive(Clone, Debug)] |
| 90 | + pub struct ProgramsMap<'info>(pub HashMap<&'static str, AccountInfo<'info>>); |
| 91 | + |
| 92 | + impl<'info> ProgramsMap<'info> { |
| 93 | + pub fn get(&self, name: &'static str) -> AccountInfo<'info> { |
| 94 | + self.0.get(name).unwrap().clone() |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + #[derive(Clone, Debug)] |
| 99 | + pub struct WithPrograms<'info, 'entrypoint, A> { |
| 100 | + pub account: &'entrypoint A, |
| 101 | + pub programs: &'entrypoint ProgramsMap<'info>, |
| 102 | + } |
| 103 | + |
| 104 | + impl<'info, 'entrypoint, A> Deref for WithPrograms<'info, 'entrypoint, A> { |
| 105 | + type Target = A; |
| 106 | + |
| 107 | + fn deref(&self) -> &Self::Target { |
| 108 | + &self.account |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + pub type SeahorseAccount<'info, 'entrypoint, A> = |
| 113 | + WithPrograms<'info, 'entrypoint, Box<Account<'info, A>>>; |
| 114 | + |
| 115 | + pub type SeahorseSigner<'info, 'entrypoint> = WithPrograms<'info, 'entrypoint, Signer<'info>>; |
| 116 | + |
| 117 | + #[derive(Clone, Debug)] |
| 118 | + pub struct CpiAccount<'info> { |
| 119 | + #[doc = "CHECK: CpiAccounts temporarily store AccountInfos."] |
| 120 | + pub account_info: AccountInfo<'info>, |
| 121 | + pub is_writable: bool, |
| 122 | + pub is_signer: bool, |
| 123 | + pub seeds: Option<Vec<Vec<u8>>>, |
| 124 | + } |
| 125 | + |
| 126 | + #[macro_export] |
| 127 | + macro_rules! seahorse_const { |
| 128 | + ($ name : ident , $ value : expr) => { |
| 129 | + macro_rules! $name { |
| 130 | + () => { |
| 131 | + $value |
| 132 | + }; |
| 133 | + } |
| 134 | + |
| 135 | + pub(crate) use $name; |
| 136 | + }; |
| 137 | + } |
| 138 | + |
| 139 | + #[macro_export] |
| 140 | + macro_rules! assign { |
| 141 | + ($ lval : expr , $ rval : expr) => {{ |
| 142 | + let temp = $rval; |
| 143 | + |
| 144 | + $lval = temp; |
| 145 | + }}; |
| 146 | + } |
| 147 | + |
| 148 | + #[macro_export] |
| 149 | + macro_rules! index_assign { |
| 150 | + ($ lval : expr , $ idx : expr , $ rval : expr) => { |
| 151 | + let temp_rval = $rval; |
| 152 | + let temp_idx = $idx; |
| 153 | + |
| 154 | + $lval[temp_idx] = temp_rval; |
| 155 | + }; |
| 156 | + } |
| 157 | + |
| 158 | + pub(crate) use assign; |
| 159 | + |
| 160 | + pub(crate) use index_assign; |
| 161 | + |
| 162 | + pub(crate) use seahorse_const; |
| 163 | +} |
| 164 | + |
| 165 | +#[program] |
| 166 | +mod seahorse { |
| 167 | + use super::*; |
| 168 | + use seahorse_util::*; |
| 169 | + use std::collections::HashMap; |
| 170 | + |
| 171 | + #[derive(Accounts)] |
| 172 | + pub struct GetPythPrice<'info> { |
| 173 | + #[account()] |
| 174 | + #[doc = "CHECK: This account is unchecked."] |
| 175 | + pub pyth_price_account: UncheckedAccount<'info>, |
| 176 | + #[account(mut)] |
| 177 | + pub signer: Signer<'info>, |
| 178 | + } |
| 179 | + |
| 180 | + pub fn get_pyth_price(ctx: Context<GetPythPrice>) -> Result<()> { |
| 181 | + let mut programs = HashMap::new(); |
| 182 | + let programs_map = ProgramsMap(programs); |
| 183 | + let pyth_price_account = &ctx.accounts.pyth_price_account.clone(); |
| 184 | + let signer = SeahorseSigner { |
| 185 | + account: &ctx.accounts.signer, |
| 186 | + programs: &programs_map, |
| 187 | + }; |
| 188 | + |
| 189 | + get_pyth_price_handler(pyth_price_account.clone(), signer.clone()); |
| 190 | + |
| 191 | + return Ok(()); |
| 192 | + } |
| 193 | +} |
0 commit comments