Skip to content

Commit

Permalink
Jupiter changes for jupiter-core compat
Browse files Browse the repository at this point in the history
  • Loading branch information
Arrowana committed Jan 22, 2024
1 parent fab358a commit 6314bd6
Show file tree
Hide file tree
Showing 36 changed files with 1,592 additions and 578 deletions.
1,616 changes: 1,238 additions & 378 deletions Cargo.lock

Large diffs are not rendered by default.

19 changes: 7 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
[workspace]
resolver = "2"

members = [
"stakedex_sdk",
"common",
"libs/*",
"interfaces/*"
]
members = ["stakedex_sdk", "common", "libs/*", "interfaces/*"]

[workspace.dependencies]
anyhow = "^1.0"
bincode = "^1.0"
borsh = "^0.9"
jupiter-amm-interface = "~0.3.2"
lazy_static = "^1.0"
num-derive = ">=0.1"
num-traits = ">=0.1"
rust_decimal = "^1.0"
sanctum-macros = "^1.2"
serde = "^1"
solana-client = "^1.9"
solana-program = "^1.9"
solana-sdk = "^1.9"
borsh = "^0.10"
jupiter-amm-interface = "~0.3.0"
lazy_static = "^1.0"
solana-client = "~1.16"
solana-program = "~1.16"
solana-sdk = "~1.16"
spl-token = "^3.0"
thiserror = "^1.0"

Expand Down
2 changes: 2 additions & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ spl-token = { workspace = true }
stakedex_deposit_sol_interface = { workspace = true }
stakedex_interface = { workspace = true }
thiserror = { workspace = true }

rand = "0.8.5"
2 changes: 2 additions & 0 deletions common/src/base_amm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use jupiter_amm_interface::AccountMap;
use solana_program::pubkey::Pubkey;

pub trait BaseStakePoolAmm {
fn program_id(&self) -> Pubkey;

fn stake_pool_label(&self) -> &'static str;

/// For ID purposes
Expand Down
2 changes: 1 addition & 1 deletion common/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ pub const SWAP_VIA_STAKE_SRC_TOKEN_MINT_ACCOUNT_INDEX: usize = 5;

pub const SWAP_VIA_STAKE_DST_TOKEN_MINT_ACCOUNT_INDEX: usize = 6;

pub const TEMPORARY_JUP_AMM_LABEL: &str = "unstake.it";
pub const TEMPORARY_JUP_AMM_LABEL: &str = "Sanctum";
13 changes: 13 additions & 0 deletions common/src/deposit_sol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ pub trait DepositSol: BaseStakePoolAmm {
..Quote::default()
}
}

fn accounts_len(&self) -> usize;
}

// newtype pattern in order to impl external trait on internal generic
Expand Down Expand Up @@ -157,4 +159,15 @@ where
fn unidirectional(&self) -> bool {
true
}

fn get_accounts_len(&self) -> usize {
1 + STAKE_WRAPPED_SOL_IX_ACCOUNTS_LEN + self.0.accounts_len()
}

fn program_dependencies(&self) -> Vec<(Pubkey, String)> {
vec![(
self.0.program_id(),
self.0.stake_pool_label().to_lowercase(),
)]
}
}
6 changes: 6 additions & 0 deletions common/src/deposit_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,10 @@ pub trait DepositStake: BaseStakePoolAmm {
..Quote::default()
}
}

fn underlying_liquidity(&self) -> Option<&Pubkey> {
None
}

fn accounts_len(&self) -> usize;
}
110 changes: 106 additions & 4 deletions common/src/pool_pair.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

use anyhow::{anyhow, Result};
use jupiter_amm_interface::{
AccountMap, Amm, KeyedAccount, Quote, QuoteParams, SwapAndAccountMetas, SwapParams,
Expand Down Expand Up @@ -126,14 +128,49 @@ pub fn get_account_metas<W: WithdrawStake + ?Sized, D: DepositStake + ?Sized>(
Ok(metas)
}

fn prepare_underlying_liquidities(
underlying_liquidities: &[Option<&Pubkey>],
) -> Option<HashSet<Pubkey>> {
let uls = HashSet::from_iter(
underlying_liquidities
.into_iter()
.filter_map(|ul| ul.cloned()),
);
if uls.len() > 0 {
Some(uls)
} else {
None
}
}

#[derive(Clone)]
pub struct OneWayPoolPair<
W: WithdrawStake + Clone + Send + Sync + 'static,
D: DepositStake + Clone + Send + Sync + 'static,
> {
pub withdraw: W,
pub deposit: D,
pub clock: Clock,
clock: Clock,
underlying_liquidities: Option<HashSet<Pubkey>>,
}

impl<W, D> OneWayPoolPair<W, D>
where
W: WithdrawStake + Clone + Send + Sync,
D: DepositStake + Clone + Send + Sync,
{
pub fn new(withdraw: W, deposit: D) -> Self {
let underlying_liquidities = prepare_underlying_liquidities(&[
withdraw.underlying_liquidity(),
deposit.underlying_liquidity(),
]);
Self {
withdraw,
deposit,
clock: Clock::default(),
underlying_liquidities,
}
}
}

impl<W, D> Amm for OneWayPoolPair<W, D>
Expand Down Expand Up @@ -200,7 +237,7 @@ where
}

fn get_swap_and_account_metas(&self, swap_params: &SwapParams) -> Result<SwapAndAccountMetas> {
let bridge_stake_seed = (self.clock.slot % u64::from(u32::MAX)).try_into().unwrap();
let bridge_stake_seed = rand::random();
let mut account_metas = vec![STAKEDEX_ACCOUNT_META.clone()];
account_metas.extend(get_account_metas(
swap_params,
Expand All @@ -226,6 +263,27 @@ where
fn unidirectional(&self) -> bool {
true
}

fn get_accounts_len(&self) -> usize {
1 + self.withdraw.accounts_len() + self.deposit.accounts_len()
}

fn underlying_liquidities(&self) -> Option<HashSet<Pubkey>> {
self.underlying_liquidities.clone()
}

fn program_dependencies(&self) -> Vec<(Pubkey, String)> {
vec![
(
self.withdraw.program_id(),
self.withdraw.stake_pool_label().to_lowercase(),
),
(
self.deposit.program_id(),
self.deposit.stake_pool_label().to_lowercase(),
),
]
}
}

#[derive(Clone)]
Expand All @@ -235,7 +293,29 @@ pub struct TwoWayPoolPair<
> {
pub p1: P1,
pub p2: P2,
pub clock: Clock,
clock: Clock,
underlying_liquidities: Option<HashSet<Pubkey>>,
}

impl<P1, P2> TwoWayPoolPair<P1, P2>
where
P1: DepositStake + WithdrawStake + Clone + Send + Sync,
P2: DepositStake + WithdrawStake + Clone + Send + Sync,
{
pub fn new(p1: P1, p2: P2) -> Self {
let underlying_liquidities = prepare_underlying_liquidities(&[
DepositStake::underlying_liquidity(&p1),
<dyn WithdrawStake>::underlying_liquidity(&p1),
DepositStake::underlying_liquidity(&p2),
<dyn WithdrawStake>::underlying_liquidity(&p2),
]);
Self {
p1,
p2,
clock: Clock::default(),
underlying_liquidities,
}
}
}

impl<P1, P2> Amm for TwoWayPoolPair<P1, P2>
Expand Down Expand Up @@ -299,7 +379,7 @@ where
}

fn get_swap_and_account_metas(&self, swap_params: &SwapParams) -> Result<SwapAndAccountMetas> {
let bridge_stake_seed = (self.clock.slot % u64::from(u32::MAX)).try_into().unwrap();
let bridge_stake_seed = rand::random();
let mut account_metas = vec![STAKEDEX_ACCOUNT_META.clone()];
let other_account_metas = if swap_params.source_mint == self.p1.staked_sol_mint()
&& swap_params.destination_mint == self.p2.staked_sol_mint()
Expand Down Expand Up @@ -331,4 +411,26 @@ where
fn program_id(&self) -> Pubkey {
stakedex_interface::ID
}

fn get_accounts_len(&self) -> usize {
// Pick a single direction
1 + <dyn WithdrawStake>::accounts_len(&self.p1) + DepositStake::accounts_len(&self.p2) + 1
}

fn underlying_liquidities(&self) -> Option<HashSet<Pubkey>> {
self.underlying_liquidities.clone()
}

fn program_dependencies(&self) -> Vec<(Pubkey, String)> {
vec![
(
self.p1.program_id(),
self.p1.stake_pool_label().to_lowercase(),
),
(
self.p2.program_id(),
self.p2.stake_pool_label().to_lowercase(),
),
]
}
}
6 changes: 6 additions & 0 deletions common/src/withdraw_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ pub trait WithdrawStakeBase {
fn can_accept_stake_withdrawals(&self) -> bool;

fn virtual_ix(&self, quote: &WithdrawStakeQuote) -> Result<Instruction>;

fn underlying_liquidity(&self) -> Option<&Pubkey> {
None
}

fn accounts_len(&self) -> usize;
}

pub trait WithdrawStake: BaseStakePoolAmm + WithdrawStakeBase {
Expand Down
102 changes: 17 additions & 85 deletions interfaces/stakedex_interface/src/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,56 +158,16 @@ impl From<StakeWrappedSolAccounts<'_, '_>> for StakeWrappedSolKeys {
impl From<StakeWrappedSolKeys> for [AccountMeta; STAKE_WRAPPED_SOL_IX_ACCOUNTS_LEN] {
fn from(keys: StakeWrappedSolKeys) -> Self {
[
AccountMeta {
pubkey: keys.user,
is_signer: true,
is_writable: false,
},
AccountMeta {
pubkey: keys.wsol_from,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: keys.dest_token_to,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: keys.wsol_bridge_in,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: keys.sol_bridge_out,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: keys.dest_token_fee_token_account,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: keys.dest_token_mint,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: keys.wsol_mint,
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: keys.token_program,
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: keys.system_program,
is_signer: false,
is_writable: false,
},
AccountMeta::new_readonly(keys.user, false),
AccountMeta::new(keys.wsol_from, false),
AccountMeta::new(keys.dest_token_to, false),
AccountMeta::new(keys.wsol_bridge_in, false),
AccountMeta::new(keys.sol_bridge_out, false),
AccountMeta::new(keys.dest_token_fee_token_account, false),
AccountMeta::new(keys.dest_token_mint, false),
AccountMeta::new_readonly(keys.wsol_mint, false),
AccountMeta::new_readonly(keys.token_program, false),
AccountMeta::new_readonly(keys.system_program, false),
]
}
}
Expand Down Expand Up @@ -464,41 +424,13 @@ impl From<SwapViaStakeAccounts<'_, '_>> for SwapViaStakeKeys {
impl From<SwapViaStakeKeys> for [AccountMeta; SWAP_VIA_STAKE_IX_ACCOUNTS_LEN] {
fn from(keys: SwapViaStakeKeys) -> Self {
[
AccountMeta {
pubkey: keys.user,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: keys.src_token_from,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: keys.dest_token_to,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: keys.bridge_stake,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: keys.dest_token_fee_token_account,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: keys.src_token_mint,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: keys.dest_token_mint,
is_signer: false,
is_writable: true,
},
AccountMeta::new(keys.user, false),
AccountMeta::new(keys.src_token_from, false),
AccountMeta::new(keys.dest_token_to, false),
AccountMeta::new(keys.bridge_stake, false),
AccountMeta::new(keys.dest_token_fee_token_account, false),
AccountMeta::new(keys.src_token_mint, false),
AccountMeta::new(keys.dest_token_mint, false),
]
}
}
Expand Down
6 changes: 4 additions & 2 deletions libs/lido/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ edition = "2021"
anyhow = { workspace = true }
bincode = { workspace = true }
borsh = { workspace = true }
lido = { git = "https://github.com/igneous-labs/solido", branch = "mod/2.0.0-loose-deps", features = ["no-entrypoint"] }
lido = { git = "https://github.com/jup-ag/solido.git", rev = "2c85ddf7b50d8162d2b81d79d7fcbfd5e05dc967", features = [
"no-entrypoint",
] }
solana-program = { workspace = true }
spl-token = { workspace = true }
stakedex_deposit_sol_interface = { workspace = true }
stakedex_withdraw_stake_interface = { workspace = true }
stakedex_sdk_common = { workspace = true }

[dev-dependencies]
solana-client = "^1.9"
solana-client = { workspace = true }
Loading

0 comments on commit 6314bd6

Please sign in to comment.