Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
287 changes: 24 additions & 263 deletions Cargo.lock

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions programs/adrena-perp-itf/Cargo.toml
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"
10 changes: 10 additions & 0 deletions programs/adrena-perp-itf/src/lib.rs
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 {}
111 changes: 111 additions & 0 deletions programs/adrena-perp-itf/src/state/limited_string.rs
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
}
}
7 changes: 7 additions & 0 deletions programs/adrena-perp-itf/src/state/mod.rs
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::*;
44 changes: 44 additions & 0 deletions programs/adrena-perp-itf/src/state/pool.rs
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],
}
145 changes: 145 additions & 0 deletions programs/adrena-perp-itf/src/state/u128_split.rs
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);
}
}
10 changes: 8 additions & 2 deletions programs/scope/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ bytemuck = { version = "1.4.0", features = ["min_const_generics", "derive"] }
num_enum = "0.7.0"
cfg-if = "1.0.0"
serde = { version = "1.0.136", optional = true }
strum = { git = "https://github.com/Kamino-Finance/strum", features = ["derive"], branch = "checked_arithmetics" }
strum = { git = "https://github.com/Kamino-Finance/strum", features = [
"derive",
], branch = "checked_arithmetics" }
pyth-sdk-solana = "0.10.1"
switchboard-program = "0.2.0"
arrayref = "0.3.6"
Expand All @@ -56,7 +58,10 @@ whirlpool = { git = "https://github.com/Kamino-Finance/whirlpools", branch = "an
"no-entrypoint",
"cpi",
] }
raydium-amm-v3 = { git = "https://github.com/raydium-io/raydium-clmm", features = ["no-entrypoint", "cpi"] }
raydium-amm-v3 = { git = "https://github.com/raydium-io/raydium-clmm", features = [
"no-entrypoint",
"cpi",
] }
jup-perp-itf = { path = "../jup-perp-itf", features = ["cpi"] }
lb-clmm-itf = { path = "../lb-clmm-itf", features = ["no-entrypoint"] }
sbod-itf = { path = "../sbod-itf" }
Expand All @@ -74,3 +79,4 @@ tracing = { version = "0.1.10", optional = true }
chainlink-streams-report = { git = "https://github.com/smartcontractkit/data-streams-sdk.git", package = "data-streams-report" }
num-bigint = "0.4"

adrena-perp-itf = { path = "../adrena-perp-itf", features = ["cpi"] }
Loading