forked from Kamino-Finance/scope
-
Notifications
You must be signed in to change notification settings - Fork 0
Adrena alp token price #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
the-orex
wants to merge
5
commits into
master
Choose a base branch
from
adrena-ALP-token-price
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| [package] | ||
| name = "adrena-perp-itf" | ||
| version = "0.1.0" | ||
| description = "Interface to interact with Adrena Perpetual Protocol (partial)" | ||
| edition = "2021" | ||
|
|
||
| [lib] | ||
| crate-type = ["cdylib", "lib"] | ||
| name = "adrena_perp_itf" | ||
|
|
||
| [features] | ||
| no-entrypoint = [] | ||
| cpi = ["no-entrypoint"] | ||
| test-bpf = [] | ||
| debug = [] | ||
|
|
||
| [dependencies] | ||
| anchor-lang = "0.28.0" | ||
| anchor-spl = "0.28.0" | ||
| solana-program = "~1.16.18" | ||
| bytemuck = { version = "1.12", default-features = false, features = ["derive"] } | ||
| num-traits = "0.2.15" | ||
| num = "0.4.0" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| pub mod state; | ||
|
|
||
| use anchor_lang::prelude::*; | ||
|
|
||
| declare_id!("13gDzEXCdocbj8iAiqrScGo47NiSuYENGsRqi3SEAwet"); | ||
|
|
||
| pub const PRICE_DECIMALS: u8 = 10; | ||
|
|
||
| #[program] | ||
| pub mod adrena {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| use { | ||
| anchor_lang::prelude::*, | ||
| bytemuck::{Pod, Zeroable}, | ||
| std::fmt::Display, | ||
| }; | ||
|
|
||
| // Storage space must be known in advance, as such all strings are limited to 64 characters. | ||
| #[derive(AnchorSerialize, AnchorDeserialize, Clone, Copy, Zeroable, Pod)] | ||
| #[repr(C)] | ||
| pub struct LimitedString { | ||
| pub value: [u8; 31], | ||
| pub length: u8, | ||
| } | ||
|
|
||
| impl std::fmt::Debug for LimitedString { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| write!(f, "{}", self) | ||
| } | ||
| } | ||
|
|
||
| impl PartialEq for LimitedString { | ||
| fn eq(&self, other: &Self) -> bool { | ||
| self.length == other.length | ||
| && self.value[..self.length as usize] == other.value[..other.length as usize] | ||
| } | ||
| } | ||
|
|
||
| impl From<LimitedString> for String { | ||
| fn from(limited_string: LimitedString) -> Self { | ||
| let mut string = String::new(); | ||
| for byte in limited_string.value.iter() { | ||
| if *byte == 0 { | ||
| break; | ||
| } | ||
| string.push(*byte as char); | ||
| } | ||
| string | ||
| } | ||
| } | ||
|
|
||
| impl LimitedString { | ||
| pub fn to_bytes(&self) -> &[u8] { | ||
| &self.value[..self.length as usize] | ||
| } | ||
| } | ||
|
|
||
| impl Display for LimitedString { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| write!(f, "{}", String::from(*self)) | ||
| } | ||
| } | ||
|
|
||
| impl Default for LimitedString { | ||
| fn default() -> Self { | ||
| Self { | ||
| value: [0; Self::MAX_LENGTH], | ||
| length: 0, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl LimitedString { | ||
| pub const MAX_LENGTH: usize = 31; | ||
|
|
||
| pub fn new<S: AsRef<str>>(input: S) -> Self { | ||
| let input_str = input.as_ref(); | ||
| let length = input_str.len() as u8; | ||
| let mut array = [0; Self::MAX_LENGTH]; | ||
| let bytes = input_str.as_bytes(); | ||
| for (index, byte) in bytes.iter().enumerate() { | ||
| if index >= Self::MAX_LENGTH { | ||
| break; | ||
| } | ||
| array[index] = *byte; | ||
| } | ||
| LimitedString { | ||
| value: array, | ||
| length, | ||
| } | ||
| } | ||
|
|
||
| pub const fn new_const(input: &'static str) -> Self { | ||
| let length = input.len() as u8; | ||
| let bytes = input.as_bytes(); | ||
| let mut array = [0; Self::MAX_LENGTH]; | ||
| let mut i = 0; | ||
|
|
||
| while i < Self::MAX_LENGTH && i < length as usize { | ||
| array[i] = bytes[i]; | ||
| i += 1; | ||
| } | ||
|
|
||
| LimitedString { | ||
| value: array, | ||
| length, | ||
| } | ||
| } | ||
|
|
||
| // Converts the LimitedString into a [u8; 32], zero-padded. Useful in tests. | ||
| pub fn to_fixed_32(&self) -> [u8; 32] { | ||
| let mut buf = [0u8; 32]; | ||
|
|
||
| let len = self.length as usize; | ||
|
|
||
| let copy_len = len.min(31); | ||
|
|
||
| buf[..copy_len].copy_from_slice(&self.value[..copy_len]); | ||
| buf[31] = self.length; // mirror the struct layout: 31 bytes + 1 byte len | ||
| buf | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| pub mod limited_string; | ||
| pub mod pool; | ||
| pub mod u128_split; | ||
|
|
||
| pub use limited_string::*; | ||
| pub use pool::*; | ||
| pub use u128_split::*; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| use anchor_lang::prelude::*; | ||
| use bytemuck::{Pod, Zeroable}; | ||
|
|
||
| use super::{LimitedString, U128Split}; | ||
|
|
||
| pub const MAX_CUSTODIES: usize = 8; | ||
|
|
||
| #[account(zero_copy)] | ||
| #[derive(Default, Debug)] | ||
| #[repr(C)] | ||
| pub struct Pool { | ||
| pub bump: u8, | ||
| pub lp_token_bump: u8, | ||
| pub nb_stable_custody: u8, | ||
| pub initialized: u8, | ||
| pub allow_trade: u8, | ||
| pub allow_swap: u8, | ||
| pub liquidity_state: u8, | ||
| pub registered_custody_count: u8, | ||
| pub name: LimitedString, | ||
| pub custodies: [Pubkey; MAX_CUSTODIES], | ||
| pub fees_debt_usd: u64, | ||
| pub referrers_fee_debt_usd: u64, | ||
| pub cumulative_referrer_fee_usd: u64, | ||
| pub lp_token_price_usd: u64, // <--- The price | ||
| pub whitelisted_swapper: Pubkey, | ||
| pub ratios: [TokenRatios; MAX_CUSTODIES], | ||
| pub last_aum_and_lp_token_price_usd_update: i64, // <--- The price update time | ||
| pub unique_limit_order_id_counter: u64, | ||
| pub aum_usd: U128Split, | ||
| pub inception_time: i64, | ||
| pub aum_soft_cap_usd: u64, | ||
| } | ||
|
|
||
| #[derive( | ||
| Copy, Clone, PartialEq, AnchorSerialize, AnchorDeserialize, Default, Debug, Zeroable, Pod, | ||
| )] | ||
| #[repr(C)] | ||
| pub struct TokenRatios { | ||
| pub target: u16, | ||
| pub min: u16, | ||
| pub max: u16, | ||
| pub _padding: [u8; 2], | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| use { | ||
| anchor_lang::prelude::*, | ||
| bytemuck::{Pod, Zeroable}, | ||
| std::ops::{AddAssign, SubAssign}, | ||
| }; | ||
|
|
||
| // U128Split is a struct that represents a u128 as two u64s for zero_copy needs | ||
| #[derive( | ||
| Copy, Clone, PartialEq, AnchorSerialize, AnchorDeserialize, Default, Debug, Pod, Zeroable, | ||
| )] | ||
| #[repr(C)] | ||
| pub struct U128Split { | ||
| high: u64, | ||
| low: u64, | ||
| } | ||
|
|
||
| impl U128Split { | ||
| // Create a new U128Split from a u128 | ||
| pub fn new(value: u128) -> Self { | ||
| let high = (value >> 64) as u64; | ||
| let low = value as u64; | ||
| U128Split { high, low } | ||
| } | ||
|
|
||
| // Convert the U128Split back into a u128 | ||
| pub fn to_u128(&self) -> u128 { | ||
| ((self.high as u128) << 64) | (self.low as u128) | ||
| } | ||
|
|
||
| // Add a u128 to this U128Split | ||
| pub fn add(&mut self, other: u128) { | ||
| let other_split = U128Split::new(other); | ||
| let (low, carry) = self.low.overflowing_add(other_split.low); | ||
| let high = self.high + other_split.high + (carry as u64); | ||
| self.high = high; | ||
| self.low = low; | ||
| } | ||
|
|
||
| // Method to perform left shift operation | ||
| pub fn left_shift(&mut self, shift: u32) { | ||
| if shift == 0 { | ||
| return; | ||
| } | ||
|
|
||
| let total_bits = 128; | ||
| let u64_bits = 64; | ||
|
|
||
| if shift >= total_bits { | ||
| self.high = 0; | ||
| self.low = 0; | ||
| } else if shift >= u64_bits { | ||
| self.high = self.low << (shift - u64_bits); | ||
| self.low = 0; | ||
| } else { | ||
| self.high = (self.high << shift) | (self.low >> (u64_bits - shift)); | ||
| self.low <<= shift; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl std::ops::Add for U128Split { | ||
| type Output = Self; | ||
|
|
||
| fn add(self, other: Self) -> Self::Output { | ||
| let (low, carry) = self.low.overflowing_add(other.low); | ||
| let high = self.high + other.high + (carry as u64); | ||
| U128Split { high, low } | ||
| } | ||
| } | ||
|
|
||
| impl std::ops::Sub for U128Split { | ||
| type Output = Self; | ||
|
|
||
| fn sub(self, other: Self) -> Self::Output { | ||
| let (low, borrow) = self.low.overflowing_sub(other.low); | ||
| let high = self.high - other.high - (borrow as u64); | ||
| U128Split { high, low } | ||
| } | ||
| } | ||
|
|
||
| impl From<u128> for U128Split { | ||
| fn from(item: u128) -> Self { | ||
| U128Split::new(item) | ||
| } | ||
| } | ||
|
|
||
| impl From<u64> for U128Split { | ||
| fn from(item: u64) -> Self { | ||
| U128Split::new(item as u128) | ||
| } | ||
| } | ||
|
|
||
| impl From<i32> for U128Split { | ||
| fn from(item: i32) -> Self { | ||
| U128Split::new(item as u128) | ||
| } | ||
| } | ||
|
|
||
| impl AddAssign for U128Split { | ||
| fn add_assign(&mut self, other: Self) { | ||
| let (low, carry) = self.low.overflowing_add(other.low); | ||
| let high = self.high + other.high + (carry as u64); | ||
| self.high = high; | ||
| self.low = low; | ||
| } | ||
| } | ||
|
|
||
| impl AddAssign<u64> for U128Split { | ||
| fn add_assign(&mut self, other: u64) { | ||
| let (low, carry) = self.low.overflowing_add(other); | ||
| self.high += carry as u64; // Only add to high if there's a carry | ||
| self.low = low; | ||
| } | ||
| } | ||
|
|
||
| impl AddAssign<u128> for U128Split { | ||
| fn add_assign(&mut self, other: u128) { | ||
| let other_split = U128Split::new(other); | ||
| self.add_assign(other_split); | ||
| } | ||
| } | ||
|
|
||
| impl SubAssign for U128Split { | ||
| fn sub_assign(&mut self, other: Self) { | ||
| let (low, borrow) = self.low.overflowing_sub(other.low); | ||
| let high = self.high - other.high - (borrow as u64); | ||
| self.high = high; | ||
| self.low = low; | ||
| } | ||
| } | ||
|
|
||
| impl SubAssign<u64> for U128Split { | ||
| fn sub_assign(&mut self, other: u64) { | ||
| let (low, borrow) = self.low.overflowing_sub(other); | ||
| self.high -= borrow as u64; // Only subtract from high if there's a borrow | ||
| self.low = low; | ||
| } | ||
| } | ||
|
|
||
| impl SubAssign<u128> for U128Split { | ||
| fn sub_assign(&mut self, other: u128) { | ||
| let other_split = U128Split::new(other); | ||
| self.sub_assign(other_split); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.