feat: major refactor with calculation utilities and instruction optimization
- Add comprehensive calculation utilities for all protocols (bonk, pumpfun, pumpswap, raydium) - Refactor instruction modules to reduce code complexity and improve maintainability - Optimize trading parameters structure and enhance common utilities - Separate calculation logic from trading modules for better code organization - Update dependencies and module exports
This commit is contained in:
@@ -11,118 +11,6 @@ pub async fn find_pool(rpc: &SolanaRpcClient, mint: &Pubkey) -> Result<Pubkey, a
|
||||
Ok(pool_address)
|
||||
}
|
||||
|
||||
pub async fn get_token_amount(
|
||||
quote_mint_is_wsol: bool,
|
||||
pool_base_token_reserves: u64,
|
||||
pool_quote_token_reserves: u64,
|
||||
sol_amount: u64,
|
||||
lp_fee_basis_points: u64,
|
||||
protocol_fee_basis_points: u64,
|
||||
coin_creator_fee_basis_points: u64,
|
||||
) -> Result<u64, anyhow::Error> {
|
||||
let product = pool_base_token_reserves as u128 * pool_quote_token_reserves as u128;
|
||||
if quote_mint_is_wsol {
|
||||
// base_amount_out
|
||||
let mut sol_amount = sol_amount as u128;
|
||||
sol_amount = sol_amount
|
||||
.checked_mul(10000)
|
||||
.unwrap()
|
||||
.checked_div(
|
||||
(10000
|
||||
+ lp_fee_basis_points
|
||||
+ protocol_fee_basis_points
|
||||
+ coin_creator_fee_basis_points) as u128,
|
||||
)
|
||||
.unwrap()
|
||||
.checked_sub(1)
|
||||
.unwrap();
|
||||
let new_quote_amount = pool_quote_token_reserves as u128 + sol_amount as u128;
|
||||
let new_base_amount = product / new_quote_amount;
|
||||
let token_amount = pool_base_token_reserves as u128 - new_base_amount;
|
||||
return Ok(token_amount as u64);
|
||||
} else {
|
||||
// min_quote_amount_out
|
||||
let new_base_amount = pool_base_token_reserves as u128 + sol_amount as u128;
|
||||
let new_quote_amount = product / new_base_amount;
|
||||
let token_amount = pool_quote_token_reserves as u128 - new_quote_amount;
|
||||
let lp_fee = token_amount
|
||||
.checked_mul(lp_fee_basis_points as u128)
|
||||
.unwrap()
|
||||
.checked_div(10000)
|
||||
.unwrap();
|
||||
let protocol_fee = token_amount
|
||||
.checked_mul(protocol_fee_basis_points as u128)
|
||||
.unwrap()
|
||||
.checked_div(10000)
|
||||
.unwrap();
|
||||
let coin_creator_fee = token_amount
|
||||
.checked_mul(coin_creator_fee_basis_points as u128)
|
||||
.unwrap()
|
||||
.checked_div(10000)
|
||||
.unwrap();
|
||||
let token_amount = token_amount.checked_sub(lp_fee).unwrap();
|
||||
let token_amount = token_amount.checked_sub(protocol_fee).unwrap();
|
||||
let token_amount = token_amount.checked_sub(coin_creator_fee).unwrap();
|
||||
return Ok(token_amount as u64);
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_wsol_amount(
|
||||
quote_mint_is_wsol: bool,
|
||||
pool_base_token_reserves: u64,
|
||||
pool_quote_token_reserves: u64,
|
||||
token_amount: u64,
|
||||
lp_fee_basis_points: u64,
|
||||
protocol_fee_basis_points: u64,
|
||||
coin_creator_fee_basis_points: u64,
|
||||
) -> Result<u64, anyhow::Error> {
|
||||
let product = pool_base_token_reserves as u128 * pool_quote_token_reserves as u128;
|
||||
if !quote_mint_is_wsol {
|
||||
// base_amount_out
|
||||
let mut token_amount = token_amount as u128;
|
||||
token_amount = token_amount
|
||||
.checked_mul(10000)
|
||||
.unwrap()
|
||||
.checked_div(
|
||||
(10000
|
||||
+ lp_fee_basis_points
|
||||
+ protocol_fee_basis_points
|
||||
+ coin_creator_fee_basis_points) as u128,
|
||||
)
|
||||
.unwrap()
|
||||
.checked_sub(1)
|
||||
.unwrap();
|
||||
let new_quote_amount = pool_quote_token_reserves as u128 + token_amount as u128;
|
||||
let new_base_amount = product / new_quote_amount;
|
||||
let wsol_amount = pool_base_token_reserves as u128 - new_base_amount;
|
||||
Ok(wsol_amount as u64)
|
||||
} else {
|
||||
// min_quote_amount_out
|
||||
let new_base_amount = pool_base_token_reserves as u128 + token_amount as u128;
|
||||
let new_quote_amount = product / new_base_amount;
|
||||
let token_amount = pool_quote_token_reserves as u128 - new_quote_amount;
|
||||
let lp_fee = token_amount
|
||||
.checked_mul(lp_fee_basis_points as u128)
|
||||
.unwrap()
|
||||
.checked_div(10000)
|
||||
.unwrap();
|
||||
let protocol_fee = token_amount
|
||||
.checked_mul(protocol_fee_basis_points as u128)
|
||||
.unwrap()
|
||||
.checked_div(10000)
|
||||
.unwrap();
|
||||
let coin_creator_fee = token_amount
|
||||
.checked_mul(coin_creator_fee_basis_points as u128)
|
||||
.unwrap()
|
||||
.checked_div(10000)
|
||||
.unwrap();
|
||||
let wsol_amount = token_amount.checked_sub(lp_fee).unwrap();
|
||||
let wsol_amount = wsol_amount.checked_sub(protocol_fee).unwrap();
|
||||
let wsol_amount = wsol_amount.checked_sub(coin_creator_fee).unwrap();
|
||||
Ok(wsol_amount as u64)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn coin_creator_vault_authority(coin_creator: Pubkey) -> Pubkey {
|
||||
let (pump_pool_authority, _) = Pubkey::find_program_address(
|
||||
&[b"creator_vault", &coin_creator.to_bytes()],
|
||||
@@ -276,37 +164,3 @@ pub async fn get_token_balances(
|
||||
|
||||
Ok((base_amount, quote_amount))
|
||||
}
|
||||
|
||||
pub async fn calculate_buy_amount(
|
||||
pool: &Pool,
|
||||
rpc: &SolanaRpcClient,
|
||||
sol_amount: u64,
|
||||
) -> Result<u64, anyhow::Error> {
|
||||
let (base_amount, quote_amount) = get_token_balances(pool, rpc).await?;
|
||||
|
||||
// 使用常数乘积公式 (x * y = k) 计算
|
||||
let product = base_amount as u128 * quote_amount as u128;
|
||||
let new_quote_amount = quote_amount as u128 + sol_amount as u128;
|
||||
let new_base_amount = product / new_quote_amount;
|
||||
|
||||
let token_amount = base_amount as u128 - new_base_amount;
|
||||
|
||||
Ok(token_amount as u64)
|
||||
}
|
||||
|
||||
pub async fn calculate_sell_amount(
|
||||
pool: &Pool,
|
||||
rpc: &SolanaRpcClient,
|
||||
token_amount: u64,
|
||||
) -> Result<u64, anyhow::Error> {
|
||||
let (base_amount, quote_amount) = get_token_balances(pool, rpc).await?;
|
||||
|
||||
// 使用常数乘积公式 (x * y = k) 计算
|
||||
let product = base_amount as u128 * quote_amount as u128;
|
||||
let new_base_amount = base_amount as u128 + token_amount as u128;
|
||||
let new_quote_amount = product / new_base_amount;
|
||||
|
||||
let sol_amount = quote_amount as u128 - new_quote_amount;
|
||||
|
||||
Ok(sol_amount as u64)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user