Skip to content
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

fix: remove unwrap #6

Open
wants to merge 6 commits into
base: jupiter
Choose a base branch
from
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
274 changes: 196 additions & 78 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ itertools = ">=0.1"
jupiter-amm-interface = "~0.3.2"
lazy_static = "^1.0"
# set git dependencies to branch instead of locking to rev so that consumers can upgrade easily
lido = { git = "https://github.com/jup-ag/solido", branch = "jupiter", features = ["no-entrypoint"] } # rev = "ec25a9b"
marinade_finance_interface = { git = "https://github.com/igneous-labs/marinade_finance_interface", branch = "master" } # rev = "4d1895b"
lido = { git = "https://github.com/jup-ag/solido", rev = "2c85ddf7b50d8162d2b81d79d7fcbfd5e05dc967", features = ["no-entrypoint"] }
marinade_finance_interface = { git = "https://github.com/jup-ag/marinade_finance_interface", rev = "5747b5350c5505fc2ea597c3f8ae1f8cf71c363d" } # rev = "4d1895b"
num-derive = ">=0.1"
num-traits = ">=0.1"
rand = "0.8.5"
Expand All @@ -31,7 +31,8 @@ serde = "^1"
serde_json = "^1"
spl-associated-token-account = { version = ">=1", features = ["no-entrypoint"] }
spl-math = { version = "0.1.0", features = ["no-entrypoint"]}
spl-stake-pool = { version = "^1", features = ["no-entrypoint"] }
spl-stake-pool = { git = "https://github.com/solana-labs/solana-program-library.git", rev = "a3996814cb44eab2834f72113b742c875ac7b1b9", features = ["no-entrypoint"] }
#spl-stake-pool = { version = "^1", features = ["no-entrypoint"] }
spl-token = ">=3.0"
thiserror = "^1.0"
tokio = "^1.0"
Expand Down
108 changes: 20 additions & 88 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 Expand Up @@ -1385,7 +1317,7 @@ impl From<DepositStakeKeys> for [AccountMeta; DEPOSIT_STAKE_IX_ACCOUNTS_LEN] {
[
AccountMeta {
pubkey: keys.user,
is_signer: true,
is_signer: false,
is_writable: true,
},
AccountMeta {
Expand Down Expand Up @@ -1668,7 +1600,7 @@ impl From<PrefundWithdrawStakeKeys> for [AccountMeta; PREFUND_WITHDRAW_STAKE_IX_
[
AccountMeta {
pubkey: keys.user,
is_signer: true,
is_signer: false,
is_writable: true,
},
AccountMeta {
Expand Down Expand Up @@ -2098,7 +2030,7 @@ impl From<PrefundSwapViaStakeKeys> for [AccountMeta; PREFUND_SWAP_VIA_STAKE_IX_A
[
AccountMeta {
pubkey: keys.user,
is_signer: true,
is_signer: false, // Jupiter needs to disable signers since shared accounts are PDAs
is_writable: true,
},
AccountMeta {
Expand Down
6 changes: 1 addition & 5 deletions jup_interface/src/pool_pair/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,7 @@ pub fn manual_concat_get_account_metas<W: WithdrawStake + ?Sized, D: DepositStak
Ok(prefund_withdraw_prefix
.into_iter()
.chain(withdraw_from.virtual_ix(&withdraw_quote)?.accounts)
.chain(std::iter::once(AccountMeta {
pubkey: jup_v6_program_id::ID,
is_signer: false,
is_writable: false,
}))
.chain(std::iter::once(swap_params.placeholder_account_meta()))
.chain(deposit_prefix)
.chain(
deposit_to
Expand Down
2 changes: 1 addition & 1 deletion jup_interface/src/pool_pair/two_way.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ where
account_metas.extend(other_account_metas);
account_metas.push(swap_params.placeholder_account_meta());
Ok(SwapAndAccountMetas {
swap: Swap::StakeDexSwapViaStake { bridge_stake_seed },
swap: Swap::StakeDexPrefundWithdrawStakeAndDepositStake { bridge_stake_seed },
account_metas,
})
}
Expand Down
26 changes: 14 additions & 12 deletions libs/marinade/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use anyhow::{anyhow, Result};
use borsh::BorshDeserialize;
use consts::VALIDATOR_RECORD_BYTE_LENGTH;
use marinade_finance_interface::{
Fee, FeeCents, LiqPool, List, StakeSystem, State, ValidatorRecord, ValidatorSystem,
Fee, LiqPool, List, StakeSystem, State, ValidatorRecord, ValidatorSystem,
};
use solana_program::{borsh0_10::try_from_slice_unchecked, pubkey::Pubkey};

Expand All @@ -28,11 +28,13 @@ impl Default for MarinadeStakedex {
account: Pubkey::default(),
item_size: 0,
count: 0,
reserved1: Pubkey::default(),
reserved2: 0,
new_account: Pubkey::default(),
copied_count: 0,
// reserved1: Pubkey::default(),
// reserved2: 0,
};
let zero_fee = Fee { basis_points: 0 };
let zero_fee_cents = FeeCents { bp_cents: 0 };
// let zero_fee_cents = FeeCents { bp_cents: 0 };
Self {
state: State {
msol_mint: Pubkey::default(),
Expand Down Expand Up @@ -84,14 +86,14 @@ impl Default for MarinadeStakedex {
min_withdraw: 0,
staking_sol_cap: 0,
emergency_cooling_down: 0,
pause_authority: Pubkey::default(),
paused: false,
delayed_unstake_fee: zero_fee_cents.clone(),
withdraw_stake_account_fee: zero_fee_cents,
withdraw_stake_account_enabled: false,
last_stake_move_epoch: 0,
stake_moved: 0,
max_stake_moved_per_epoch: zero_fee,
// pause_authority: Pubkey::default(),
// paused: false,
// delayed_unstake_fee: zero_fee_cents.clone(),
// withdraw_stake_account_fee: zero_fee_cents,
// withdraw_stake_account_enabled: false,
// last_stake_move_epoch: 0,
// stake_moved: 0,
// max_stake_moved_per_epoch: zero_fee,
},
validator_records: Vec::new(),
}
Expand Down
10 changes: 8 additions & 2 deletions libs/spl_stake_pool/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{Context, Result};
use solana_program::{borsh0_10::try_from_slice_unchecked, pubkey::Pubkey, stake_history::Epoch};
use spl_stake_pool::{
error::StakePoolError,
Expand All @@ -8,6 +8,7 @@ use spl_stake_pool::{
use stakedex_sdk_common::{WithdrawStakeQuote, STAKE_ACCOUNT_RENT_EXEMPT_LAMPORTS};

mod stakedex_traits;
pub use spl_stake_pool::ID;
pub use stakedex_traits::*;

/// A SPL stake pool with possibly custom program ID
Expand Down Expand Up @@ -47,7 +48,12 @@ impl SplStakePoolStakedex {
validator_index: usize,
withdraw_amount: u64,
) -> Result<WithdrawStakeQuote, StakePoolError> {
let validator_list_entry = self.validator_list.validators.get(validator_index).unwrap();
let validator_list_entry = self
.validator_list
.validators
.get(validator_index)
.context("Validator index out of bounds")
.map_err(|_err| StakePoolError::InvalidState)?;
// only handle withdrawal from active stake accounts for simplicity.
// Likely other stake pools can't accept non active stake anyway
if validator_list_entry.status != StakeStatus::Active.into() {
Expand Down
1 change: 1 addition & 0 deletions stakedex_sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ stakedex_unstake_it = { workspace = true }

[dev-dependencies]
solana-client = { workspace = true }
lazy_static = { workspace = true }
26 changes: 19 additions & 7 deletions stakedex_sdk/tests/test_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,25 @@ fn fetch_accounts(accounts_pubkeys: &[Pubkey]) -> HashMap<Pubkey, Account> {
}

#[test]
fn test_swap_via_stake_unknown_token() {
fn test_quote_swap_via_stake_jitosol_bsol() {
STAKEDEX
.quote_swap_via_stake(&QuoteParams {
amount: 1_000_000_000,
input_mint: jitosol::ID,
output_mint: bsol::ID,
swap_mode: jupiter_amm_interface::SwapMode::ExactIn,
})
.unwrap();
}

#[test]
fn test_quote_swap_via_stake_unknown_token() {
let unknown_token = Pubkey::new_unique();
let res = STAKEDEX.quote_swap_via_stake(&QuoteParams {
amount: 1_000_000_000,
input_mint: unknown_token,
output_mint: bsol::ID,
swap_mode: SwapMode::default(),
swap_mode: jupiter_amm_interface::SwapMode::ExactIn,
});
assert!(res.is_err());
}
Expand Down Expand Up @@ -307,7 +319,7 @@ fn test_jsol_drain_vsa_edge_case() {
.validator_list
.validators
.iter()
.max_by_key(|v| v.active_stake_lamports)
.max_by_key(|v| u64::from(v.active_stake_lamports))
.unwrap();
let max_withdraw_lamports = largest_active_stake_vsi.active_stake_lamports;
let parts_after_fees = (STAKEDEX.jpool.stake_pool.stake_withdrawal_fee.denominator
Expand All @@ -325,17 +337,17 @@ fn test_jsol_drain_vsa_edge_case() {
.unwrap();
let max_possible_quote = STAKEDEX
.quote_swap_via_stake(&QuoteParams {
amount: max_withdraw_jsol,
input_mint: jsol::ID,
amount: 100_000_000_000,
input_mint: stsol::ID,
output_mint: msol::ID,
swap_mode: SwapMode::default(),
swap_mode: jupiter_amm_interface::SwapMode::ExactIn,
})
.unwrap();
let should_fail = STAKEDEX.quote_swap_via_stake(&QuoteParams {
amount: max_withdraw_jsol + 1,
input_mint: jsol::ID,
output_mint: msol::ID,
swap_mode: SwapMode::default(),
swap_mode: SwapMode::ExactIn,
});
assert!(should_fail.is_err());

Expand Down