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

WithdrawSol #76

Open
wants to merge 9 commits into
base: master
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
10 changes: 10 additions & 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ stakedex_sdk_common = { path = "./common" }
stakedex_spl_stake_pool = { path = "./libs/spl_stake_pool" }
stakedex_unstake_it = { path = "./libs/unstake_it" }
stakedex_withdraw_stake_interface = { path = "./interfaces/stakedex_withdraw_stake_interface" }
stakedex_withdraw_sol_interface = { path = "./interfaces/stakedex_withdraw_sol_interface" }
2 changes: 2 additions & 0 deletions common/src/address/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ mod spl_deposit_cap_guard;
mod spl_stake_pool_like;
mod stakedex;
mod unstake_it;
mod wrapped_sol;

pub use lido::*;
pub use marinade::*;
pub use spl_deposit_cap_guard::*;
pub use spl_stake_pool_like::*;
pub use stakedex::*;
pub use unstake_it::*;
pub use wrapped_sol::*;
1 change: 1 addition & 0 deletions common/src/address/stakedex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod stakedex_program {
[
("sol-bridge-out", b"sol_bridge_out"),
("prefunder", b"prefunder"),
("wsol-fee-token-account", b"fee", b"\x06\x9b\x88W\xfe\xab\x81\x84\xfbh\x7fcF\x18\xc05\xda\xc49\xdc\x1a\xeb;U\x98\xa0\xf0\x00\x00\x00\x00\x01")
]
);
}
Expand Down
3 changes: 3 additions & 0 deletions common/src/address/wrapped_sol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod wsol {
sanctum_macros::declare_program_keys!("So11111111111111111111111111111111111111112", []);
}
2 changes: 2 additions & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod errs;
mod fees;
mod init_from_keyed_account;
mod pda;
mod withdraw_sol;
mod withdraw_stake;

pub use address::*;
Expand All @@ -18,4 +19,5 @@ pub use errs::*;
pub use fees::*;
pub use init_from_keyed_account::*;
pub use pda::*;
pub use withdraw_sol::*;
pub use withdraw_stake::*;
53 changes: 53 additions & 0 deletions common/src/withdraw_sol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use anyhow::Result;
use jupiter_amm_interface::Quote;
use rust_decimal::{
prelude::{FromPrimitive, Zero},
Decimal,
};
use solana_program::{instruction::Instruction, pubkey::Pubkey};

use crate::{apply_global_fee, wsol, BaseStakePoolAmm};

#[derive(Copy, Clone, Debug)]
pub struct WithdrawSolQuote {
pub in_amount: u64,

/// After subtracting withdraw fees
pub out_amount: u64,

/// Withdrawal fees, in SOL
pub fee_amount: u64,
}

pub trait WithdrawSol: BaseStakePoolAmm {
/// This should only include the stake pool's fees, not stakedex's global fees
fn get_withdraw_sol_quote(&self, lst: u64) -> Result<WithdrawSolQuote>;

fn virtual_ix(&self) -> Result<Instruction>;

fn accounts_len(&self) -> usize;

fn convert_quote(&self, withdraw_sol_quote: WithdrawSolQuote) -> Quote {
let aft_global_fees = apply_global_fee(withdraw_sol_quote.out_amount);
let total_fees = withdraw_sol_quote.fee_amount + aft_global_fees.fee;
let final_out_amount = aft_global_fees.remainder;
let before_fees = (final_out_amount + total_fees) as f64;
// Decimal::from_f64() returns None if infinite or NaN (before_fees = 0)
let fee_pct =
Decimal::from_f64((total_fees as f64) / before_fees).unwrap_or_else(Decimal::zero);
Quote {
in_amount: withdraw_sol_quote.in_amount,
out_amount: final_out_amount,
fee_amount: total_fees,
fee_pct,
// since stakedex program levies fee on output mint,
// we count all fees in terms of output mint (wsol) to be consistent
fee_mint: wsol::ID,
..Quote::default()
}
}

fn underlying_liquidity(&self) -> Option<&Pubkey> {
None
}
}
57 changes: 57 additions & 0 deletions interfaces/stakedex_interface/idl.json
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,63 @@
"type": "u8",
"value": 7
}
},
{
"name": "WithdrawWrappedSol",
"accounts": [
{
"name": "user",
"isMut": false,
"isSigner": true,
"desc": "The withdraw authority of src_token_from."
},
{
"name": "srcTokenFrom",
"isMut": true,
"isSigner": false,
"desc": "The token account to burn and redeem LSTs from"
},
{
"name": "wsolTo",
"isMut": true,
"isSigner": false,
"desc": "The wSOL token account to receive withdrawn wrapped SOL to"
},
{
"name": "wsolFeeTokenAccount",
"isMut": true,
"isSigner": false,
"desc": "The dest_token_mint token account collecting fees. PDA. Seeds = ['fee', dest_token_mint.pubkey]"
},
{
"name": "srcTokenMint",
"isMut": true,
"isSigner": false,
"desc": "Input LST token mint"
},
{
"name": "wsolMint",
"isMut": false,
"isSigner": false,
"desc": "wSOL token mint"
},
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false,
"desc": "Tokenkeg program. The withdraw SOL accounts slice follows"
}
],
"args": [
{
"name": "amount",
"type": "u64"
}
],
"discriminant": {
"type": "u8",
"value": 8
}
}
],
"types": [
Expand Down
Loading