Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsource147 committed May 10, 2024
1 parent bba9b2f commit c08e388
Show file tree
Hide file tree
Showing 77 changed files with 12,148 additions and 2,477 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions programs/lb_clmm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ num-integer = "0.1.45"
mpl-token-metadata = "3.0.1"
solana-program = "1.16.0"
num_enum = "0.7.1"
static_assertions = "1.1.0"

[dev-dependencies]
proptest = "1.2.0"
Expand Down
228 changes: 220 additions & 8 deletions programs/lb_clmm/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anchor_lang::prelude::*;
use anchor_lang::solana_program::{pubkey, pubkey::Pubkey};

// TODO: Macro to compute the constants which changes based on the bit system used ?
// Smallest step between bin is 0.01%, 1 bps
Expand All @@ -10,9 +9,17 @@ pub const BASIS_POINT_MAX: i32 = 10000;
#[constant]
pub const MAX_BIN_PER_ARRAY: usize = 70;

/// Default number of bin per position contains.
#[constant]
pub const DEFAULT_BIN_PER_POSITION: usize = 70;

/// Max resize length allowed
#[constant]
pub const MAX_RESIZE_LENGTH: usize = 70;

/// Maximum number of bin per position contains.
#[constant]
pub const MAX_BIN_PER_POSITION: usize = 70;
pub const POSITION_MAX_LENGTH: usize = 1400;

/// Minimum bin ID supported. Computed based on 1 bps.
#[constant]
Expand Down Expand Up @@ -65,10 +72,215 @@ pub const MAX_FEE_UPDATE_WINDOW: i64 = 0;
#[constant]
pub const MAX_REWARD_BIN_SPLIT: usize = 15;

#[cfg(feature = "localnet")]
pub static ALPHA_ACCESS_COLLECTION_MINTS: [Pubkey; 1] =
[pubkey!("J1S9H3QjnRtBbbuD4HjPV6RpRhwuk4zKbxsnCHuTgh9w")];
#[cfg(test)]
pub mod tests {
use super::*;
use crate::math::price_math;
use crate::state::parameters::StaticParameters;
pub const PRESET_BIN_STEP: [u16; 12] = [1, 2, 4, 5, 8, 10, 15, 20, 25, 50, 60, 100];

/// Preset / supported static parameters. These default values are references from Trader Joe by querying trader joe factory.
/// https://snowtrace.io/address/0x8e42f2F4101563bF679975178e880FD87d3eFd4e
pub const fn get_preset(bin_step: u16) -> Option<StaticParameters> {
let params = match bin_step {
// TODO: enable protocol share back later
1 => Some(StaticParameters {
base_factor: 20000,
filter_period: 10,
decay_period: 120,
reduction_factor: 5000,
variable_fee_control: 2000000,
// protocol_share: 500,
protocol_share: 0,
max_volatility_accumulator: 100000,
max_bin_id: 436704,
min_bin_id: -436704,
_padding: [0u8; 6],
}),
2 => Some(StaticParameters {
base_factor: 15000,
filter_period: 10,
decay_period: 120,
reduction_factor: 5000,
variable_fee_control: 500000,
// protocol_share: 1000,
protocol_share: 0,
max_bin_id: 218363,
min_bin_id: -218363,
max_volatility_accumulator: 250000,
_padding: [0u8; 6],
}),
4 => Some(StaticParameters {
base_factor: 50000,
filter_period: 30,
decay_period: 600,
reduction_factor: 5000,
variable_fee_control: 120000,
// protocol_share: 2500,
protocol_share: 0,
max_bin_id: 109192,
min_bin_id: -109192,
max_volatility_accumulator: 300000,
_padding: [0u8; 6],
}),
5 => Some(StaticParameters {
base_factor: 8000,
filter_period: 30,
decay_period: 600,
reduction_factor: 5000,
variable_fee_control: 120000,
// protocol_share: 2500,
protocol_share: 0,
max_bin_id: 87358,
min_bin_id: -87358,
max_volatility_accumulator: 300000,
_padding: [0u8; 6],
}),
// this preset is included to match with orca pools
8 => Some(StaticParameters {
base_factor: 6250,
filter_period: 30,
decay_period: 600,
reduction_factor: 5000,
variable_fee_control: 120000,
// protocol_share: 2500,
protocol_share: 0,
max_bin_id: 54190,
min_bin_id: -54190,
max_volatility_accumulator: 300000,
_padding: [0u8; 6],
}),
10 => Some(StaticParameters {
base_factor: 10000,
filter_period: 30,
decay_period: 600,
reduction_factor: 5000,
variable_fee_control: 40000,
// protocol_share: 1000,
protocol_share: 0,
max_bin_id: 43690,
min_bin_id: -43690,
max_volatility_accumulator: 350000,
_padding: [0u8; 6],
}),
15 => Some(StaticParameters {
base_factor: 10000,
filter_period: 30,
decay_period: 600,
reduction_factor: 5000,
variable_fee_control: 30000,
// protocol_share: 1000,
protocol_share: 0,
max_bin_id: 29134,
min_bin_id: -29134,
max_volatility_accumulator: 350000,
_padding: [0u8; 6],
}),
20 => Some(StaticParameters {
base_factor: 10000,
filter_period: 30,
decay_period: 600,
reduction_factor: 5000,
variable_fee_control: 20000,
// protocol_share: 2000,
protocol_share: 0,
max_bin_id: 21855,
min_bin_id: -21855,
max_volatility_accumulator: 350000,
_padding: [0u8; 6],
}),
25 => Some(StaticParameters {
base_factor: 10000,
filter_period: 30,
decay_period: 600,
reduction_factor: 5000,
variable_fee_control: 15000,
// protocol_share: 2000,
protocol_share: 0,
max_bin_id: 17481,
min_bin_id: -17481,
max_volatility_accumulator: 350000,
_padding: [0u8; 6],
}),
50 => Some(StaticParameters {
base_factor: 8000,
filter_period: 120,
decay_period: 1200,
reduction_factor: 5000,
variable_fee_control: 10000,
// protocol_share: 2500,
protocol_share: 0,
max_bin_id: 8754,
min_bin_id: -8754,
max_volatility_accumulator: 250000,
_padding: [0u8; 6],
}),
60 => Some(StaticParameters {
base_factor: 5000,
filter_period: 120,
decay_period: 1200,
reduction_factor: 5000,
variable_fee_control: 10000,
max_volatility_accumulator: 250000,
min_bin_id: -7299,
max_bin_id: 7299,
// protocol_share: 2500,
protocol_share: 0,
_padding: [0u8; 6],
}),
100 => Some(StaticParameters {
base_factor: 8000,
filter_period: 300,
decay_period: 1200,
reduction_factor: 5000,
variable_fee_control: 7500,
// protocol_share: 2500,
protocol_share: 0,
max_bin_id: 4386,
min_bin_id: -4386,
max_volatility_accumulator: 150000,
_padding: [0u8; 6],
}),
_ => None,
};

// Is it possible to move the checking to compile time ?
if let Some(params) = &params {
// Make sure the params stay within the bound. But it result in ugly runtime panic ...
// This couldn't prevent the team deploy with invalid parameters that causes the program overflow unexpectedly. But, at least it prevent user from creating such pools ...
// Increasing the bound will increase the bytes needed for fee calculation.
assert!(params.max_volatility_accumulator <= U24_MAX);
assert!(params.variable_fee_control <= U24_MAX);
assert!(params.protocol_share <= MAX_PROTOCOL_SHARE);
}

params
}

#[test]
fn test_get_preset() {
for bin_step in PRESET_BIN_STEP {
assert!(get_preset(bin_step).is_some());
}
}

#[test]
fn test_preset_min_max_bin_id() {
for bin_step in PRESET_BIN_STEP {
let param = get_preset(bin_step);
assert!(param.is_some());

if let Some(param) = param {
let max_price = price_math::get_price_from_id(param.max_bin_id, bin_step);
let min_price = price_math::get_price_from_id(param.min_bin_id, bin_step);

assert!(max_price.is_ok());
assert!(min_price.is_ok());

#[cfg(not(feature = "localnet"))]
pub static ALPHA_ACCESS_COLLECTION_MINTS: [Pubkey; 1] =
[pubkey!("5rwhXUgAAdbVEaFQzAwgrcWwoCqYGzR1Mo2KwUYfbRuS")];
// Bin is not swap-able when the price is u128::MAX, and 1
assert!(max_price.unwrap() == 170141183460469231731687303715884105727);
assert!(min_price.unwrap() == 2);
}
}
}
}
30 changes: 30 additions & 0 deletions programs/lb_clmm/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,27 @@ pub enum LBError {
#[msg("Must withdraw ineligible reward")]
MustWithdrawnIneligibleReward,

#[msg("Unauthorized address")]
UnauthorizedAddress,

#[msg("Cannot update because operators are the same")]
OperatorsAreTheSame,

#[msg("Withdraw to wrong token account")]
WithdrawToWrongTokenAccount,

#[msg("Wrong rent receiver")]
WrongRentReceiver,

#[msg("Already activated")]
AlreadyPassActivationSlot,

#[msg("Last slot cannot be smaller than activate slot")]
LastSlotCannotBeSmallerThanActivateSlot,

#[msg("Swapped amount is exceeded max swapped amount")]
ExceedMaxSwappedAmount,

#[msg("Invalid strategy parameters")]
InvalidStrategyParameters,

Expand All @@ -155,4 +176,13 @@ pub enum LBError {

#[msg("Invalid lock release slot")]
InvalidLockReleaseSlot,

#[msg("Bin range is not empty")]
BinRangeIsNotEmpty,

#[msg("Invalid side")]
InvalidSide,

#[msg("Invalid resize length")]
InvalidResizeLength,
}
38 changes: 38 additions & 0 deletions programs/lb_clmm/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,34 @@ pub struct PositionCreate {
pub owner: Pubkey,
}

#[event]
pub struct IncreasePositionLength {
// Liquidity pool pair
pub lb_pair: Pubkey,
// Address of the position
pub position: Pubkey,
// Owner of the position
pub owner: Pubkey,
// Length to add
pub length_to_add: u16,
// side
pub side: u8,
}

#[event]
pub struct DecreasePositionLength {
// Liquidity pool pair
pub lb_pair: Pubkey,
// Address of the position
pub position: Pubkey,
// Owner of the position
pub owner: Pubkey,
// Length to remove
pub length_to_remove: u16,
// side
pub side: u8,
}

#[event]
pub struct FeeParameterUpdate {
// Liquidity pool pair
Expand Down Expand Up @@ -230,3 +258,13 @@ pub struct UpdatePositionLockReleaseSlot {
// Sender public key
pub sender: Pubkey,
}

#[event]
pub struct GoToABin {
// Pool pair
pub lb_pair: Pubkey,
// from bin id
pub from_bin_id: i32,
// to bin id
pub to_bin_id: i32,
}
Loading

0 comments on commit c08e388

Please sign in to comment.