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
3,233 changes: 1,793 additions & 1,440 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
[workspace]
resolver = "2"
members = [
"programs/*"
]
members = ["programs/*"]
exclude = [
"rust-sdk",
"ts-sdk",
"docs",
"examples"
]

[workspace.dependencies]
anchor-lang = "0.31"
anchor-spl = "0.31"

[patch.crates-io]
anchor-lang = { git = "https://github.com/coral-xyz/anchor.git", rev = "4cdb0ebe8f613f7dbbd5c4b00571139b2517898f" }
anchor-spl = { git = "https://github.com/coral-xyz/anchor.git", rev = "4cdb0ebe8f613f7dbbd5c4b00571139b2517898f" }
33 changes: 15 additions & 18 deletions programs/whirlpool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,21 @@ default = []
idl-build = ["anchor-lang/idl-build", "anchor-spl/idl-build"]

[dependencies]
anchor-lang = { version = "=0.29.0" }
anchor-spl = { version = "=0.29.0", features = ["metadata", "memo"] }
spl-token = { version = "=4.0.1", features = ["no-entrypoint"] }
spl-transfer-hook-interface = { version = "=0.5.0" }
spl-token-metadata-interface = { version = "=0.2.0" }
solana-program = { version = "=1.17.22" }
uint = { version = "=0.9.5", default-features = false }
arrayref = { version = "=0.3.8" }
borsh09 = { package = "borsh", version = "=0.9.3" }
anchor-lang = { workspace = true }
anchor-spl = { workspace = true, features = ["metadata", "memo"] }
spl-token = { version = ">=6", features = ["no-entrypoint"] }
spl-transfer-hook-interface = { version = ">=0.8" }
spl-token-metadata-interface = { version = ">=0.5" }
solana-program = "2"
thiserror = "1.0"
uint = { version = "0.9.5", default-features = false }
borsh = "0.10"
arrayref = "0.3.8"
solana-security-txt = { version = "=1.1.1" }
bytemuck = { version = "=1.16.3", features = ["derive", "min_const_generics"] }
num_enum = { version = "=0.7.2" }
bytemuck = { version = "1.16.3", features = ["derive", "min_const_generics"] }
num_enum = { version = "0.7" }

[dev-dependencies]
proptest = "=1.5.0"
serde = "=1.0.206"
serde_json = "=1.0.124"

[dev-dependencies.serde_with]
version = "=2.3.3"
features = ["json"]
proptest = "1.5"
serde = "1.0"
serde_json = "1.0"
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ pub fn handler(
let tick_array = DynamicTickArrayLoader::load_mut(&mut data[8..]);
tick_array.initialize(&ctx.accounts.whirlpool, start_tick_index)
} else if idempotent
&& (data[0..8] == DynamicTickArray::DISCRIMINATOR
|| data[0..8] == FixedTickArray::DISCRIMINATOR)
&& (data[0..8] == *DynamicTickArray::DISCRIMINATOR
|| data[0..8] == *FixedTickArray::DISCRIMINATOR)
{
Ok(())
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,23 @@ pub fn handler(ctx: Context<InitializePositionBundleWithMetadata>) -> Result<()>

let bump = ctx.bumps.position_bundle;

mint_position_bundle_token_with_metadata_and_remove_authority(
&ctx.accounts.funder,
&ctx.accounts.position_bundle,
position_bundle_mint,
&ctx.accounts.position_bundle_token_account,
&ctx.accounts.position_bundle_metadata,
&ctx.accounts.metadata_update_auth,
&ctx.accounts.metadata_program,
&ctx.accounts.token_program,
&ctx.accounts.system_program,
&ctx.accounts.rent,
&[
b"position_bundle".as_ref(),
position_bundle_mint.key().as_ref(),
&[bump],
],
)
// mint_position_bundle_token_with_metadata_and_remove_authority(
// &ctx.accounts.funder,
// &ctx.accounts.position_bundle,
// position_bundle_mint,
// &ctx.accounts.position_bundle_token_account,
// &ctx.accounts.position_bundle_metadata,
// &ctx.accounts.metadata_update_auth,
// &ctx.accounts.metadata_program,
// &ctx.accounts.token_program,
// &ctx.accounts.system_program,
// &ctx.accounts.rent,
// &[
// b"position_bundle".as_ref(),
// position_bundle_mint.key().as_ref(),
// &[bump],
// ],
// )

Ok(())
}
8 changes: 4 additions & 4 deletions programs/whirlpool/src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ pub mod set_reward_authority;
pub mod set_reward_authority_by_super_authority;
pub mod set_reward_emissions;
pub mod set_reward_emissions_super_authority;
pub mod swap;
// pub mod swap;
// pub mod two_hop_swap;
pub mod transfer_locked_position;
pub mod two_hop_swap;
pub mod update_fees_and_rewards;

pub use close_bundled_position::*;
Expand Down Expand Up @@ -73,9 +73,9 @@ pub use set_reward_authority::*;
pub use set_reward_authority_by_super_authority::*;
pub use set_reward_emissions::*;
pub use set_reward_emissions_super_authority::*;
pub use swap::*;
// pub use swap::*;
// pub use two_hop_swap::*;
pub use transfer_locked_position::*;
pub use two_hop_swap::*;
pub use update_fees_and_rewards::*;
pub mod v2;
pub use v2::*;
Expand Down
25 changes: 13 additions & 12 deletions programs/whirlpool/src/instructions/open_position_with_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,17 @@ pub fn handler(
tick_upper_index,
)?;

mint_position_token_with_metadata_and_remove_authority(
whirlpool,
position_mint,
&ctx.accounts.position_token_account,
&ctx.accounts.position_metadata_account,
&ctx.accounts.metadata_update_auth,
&ctx.accounts.funder,
&ctx.accounts.metadata_program,
&ctx.accounts.token_program,
&ctx.accounts.system_program,
&ctx.accounts.rent,
)
// mint_position_token_with_metadata_and_remove_authority(
// whirlpool,
// position_mint,
// &ctx.accounts.position_token_account,
// &ctx.accounts.position_metadata_account,
// &ctx.accounts.metadata_update_auth,
// &ctx.accounts.funder,
// &ctx.accounts.metadata_program,
// &ctx.accounts.token_program,
// &ctx.accounts.system_program,
// &ctx.accounts.rent,
// )
Ok(())
}
2 changes: 1 addition & 1 deletion programs/whirlpool/src/instructions/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub fn handler(
amount_specified_is_input,
a_to_b,
timestamp,
&adaptive_fee_info,
adaptive_fee_info.clone(),
)?;

if amount_specified_is_input {
Expand Down
78 changes: 39 additions & 39 deletions programs/whirlpool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,23 +316,23 @@ pub mod whirlpool {
/// - `TickArrayIndexOutofBounds` - The swap loop attempted to access an invalid array index during tick crossing.
/// - `LiquidityOverflow` - Liquidity value overflowed 128bits during tick crossing.
/// - `InvalidTickSpacing` - The swap pool was initialized with tick-spacing of 0.
pub fn swap(
ctx: Context<Swap>,
amount: u64,
other_amount_threshold: u64,
sqrt_price_limit: u128,
amount_specified_is_input: bool,
a_to_b: bool,
) -> Result<()> {
instructions::swap::handler(
ctx,
amount,
other_amount_threshold,
sqrt_price_limit,
amount_specified_is_input,
a_to_b,
)
}
// pub fn swap(
// ctx: Context<Swap>,
// amount: u64,
// other_amount_threshold: u64,
// sqrt_price_limit: u128,
// amount_specified_is_input: bool,
// a_to_b: bool,
// ) -> Result<()> {
// instructions::swap::handler(
// ctx,
// amount,
// other_amount_threshold,
// sqrt_price_limit,
// amount_specified_is_input,
// a_to_b,
// )
// }

/// Close a position in a Whirlpool. Burns the position token in the owner's wallet.
///
Expand Down Expand Up @@ -508,28 +508,28 @@ pub mod whirlpool {
/// - `InvalidTickSpacing` - The swap pool was initialized with tick-spacing of 0.
/// - `InvalidIntermediaryMint` - Error if the intermediary mint between hop one and two do not equal.
/// - `DuplicateTwoHopPool` - Error if whirlpool one & two are the same pool.
#[allow(clippy::too_many_arguments)]
pub fn two_hop_swap(
ctx: Context<TwoHopSwap>,
amount: u64,
other_amount_threshold: u64,
amount_specified_is_input: bool,
a_to_b_one: bool,
a_to_b_two: bool,
sqrt_price_limit_one: u128,
sqrt_price_limit_two: u128,
) -> Result<()> {
instructions::two_hop_swap::handler(
ctx,
amount,
other_amount_threshold,
amount_specified_is_input,
a_to_b_one,
a_to_b_two,
sqrt_price_limit_one,
sqrt_price_limit_two,
)
}
// #[allow(clippy::too_many_arguments)]
// pub fn two_hop_swap(
// ctx: Context<TwoHopSwap>,
// amount: u64,
// other_amount_threshold: u64,
// amount_specified_is_input: bool,
// a_to_b_one: bool,
// a_to_b_two: bool,
// sqrt_price_limit_one: u128,
// sqrt_price_limit_two: u128,
// ) -> Result<()> {
// instructions::two_hop_swap::handler(
// ctx,
// amount,
// other_amount_threshold,
// amount_specified_is_input,
// a_to_b_one,
// a_to_b_two,
// sqrt_price_limit_one,
// sqrt_price_limit_two,
// )
// }

/// Initializes a PositionBundle account that bundles several positions.
/// A unique token will be minted to represent the position bundle in the users wallet.
Expand Down
48 changes: 6 additions & 42 deletions programs/whirlpool/src/manager/swap_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,28 +160,8 @@ pub fn swap(
.map_or_else(|_| (None, false), |tick| (Some(tick), tick.initialized));

if next_tick_initialized {
let (fee_growth_global_a, fee_growth_global_b) = if a_to_b {
(curr_fee_growth_global_input, whirlpool.fee_growth_global_b)
} else {
(whirlpool.fee_growth_global_a, curr_fee_growth_global_input)
};

let (update, next_liquidity) = calculate_update(
&next_tick.unwrap(),
a_to_b,
curr_liquidity,
fee_growth_global_a,
fee_growth_global_b,
&next_reward_infos,
)?;

curr_liquidity = next_liquidity;
swap_tick_sequence.update_tick(
next_array_index,
next_tick_index,
tick_spacing,
&update,
)?;
curr_liquidity =
calculate_next_liquidity(&next_tick.unwrap(), a_to_b, curr_liquidity)?;
}

let tick_offset = swap_tick_sequence.get_tick_offset(
Expand Down Expand Up @@ -248,12 +228,6 @@ pub fn swap(
(amount_calculated, amount - amount_remaining)
};

fee_rate_manager.update_major_swap_timestamp(
timestamp,
whirlpool.sqrt_price,
curr_sqrt_price,
)?;

Ok(Box::new(PostSwapUpdate {
amount_a,
amount_b,
Expand All @@ -268,7 +242,7 @@ pub fn swap(
}))
}

fn calculate_fees(
pub fn calculate_fees(
fee_amount: u64,
protocol_fee_rate: u16,
curr_liquidity: u128,
Expand Down Expand Up @@ -297,14 +271,7 @@ fn calculate_protocol_fee(global_fee: u64, protocol_fee_rate: u16) -> u64 {
.unwrap()
}

fn calculate_update(
tick: &Tick,
a_to_b: bool,
liquidity: u128,
fee_growth_global_a: u128,
fee_growth_global_b: u128,
reward_infos: &[WhirlpoolRewardInfo; NUM_REWARDS],
) -> Result<(TickUpdate, u128)> {
pub fn calculate_next_liquidity(tick: &Tick, a_to_b: bool, liquidity: u128) -> Result<u128> {
// Use updated fee_growth for crossing tick
// Use -liquidity_net if going left, +liquidity_net going right
let signed_liquidity_net = if a_to_b {
Expand All @@ -313,16 +280,13 @@ fn calculate_update(
tick.liquidity_net
};

let update =
next_tick_cross_update(tick, fee_growth_global_a, fee_growth_global_b, reward_infos)?;

// Update the global liquidity to reflect the new current tick
let next_liquidity = add_liquidity_delta(liquidity, signed_liquidity_net)?;

Ok((update, next_liquidity))
Ok(next_liquidity)
}

fn get_next_sqrt_prices(
pub fn get_next_sqrt_prices(
next_tick_index: i32,
sqrt_price_limit: u128,
a_to_b: bool,
Expand Down
19 changes: 8 additions & 11 deletions programs/whirlpool/src/math/bn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
/// U256 reference:
/// https://crates.parity.io/sp_core/struct.U256.html
///
use borsh09::{BorshDeserialize, BorshSerialize};
use borsh::{BorshDeserialize, BorshSerialize};
use std::borrow::BorrowMut;
use std::convert::TryInto;
use std::io::{Error, ErrorKind, Write};
use std::io::{Error, ErrorKind, Read, Write};
use std::mem::size_of;
use uint::construct_uint;

Expand All @@ -44,15 +44,12 @@ macro_rules! impl_borsh_deserialize_for_bn {
($type: ident) => {
impl BorshDeserialize for $type {
#[inline]
fn deserialize(buf: &mut &[u8]) -> std::io::Result<Self> {
if buf.len() < size_of::<$type>() {
return Err(Error::new(
ErrorKind::InvalidInput,
"Unexpected length of input",
));
}
let res = $type::from_le_bytes(buf[..size_of::<$type>()].try_into().unwrap());
*buf = &buf[size_of::<$type>()..];
fn deserialize_reader<R: Read>(reader: &mut R) -> std::io::Result<Self> {
let mut buf = [0u8; size_of::<$type>()];
reader.read_exact(&mut buf).map_err(|_| {
Error::new(ErrorKind::InvalidInput, "Unexpected length of input")
})?;
let res = $type::from_le_bytes(buf.try_into().unwrap());
Ok(res)
}
}
Expand Down
Loading
Loading