refactor: unify trading parameters and add USD1 token pool support
- Merge BuyParams and SellParams into unified SwapParams structure - Add USD1 token pool support with related constants and configurations - Refactor trade executor by combining buy_with_tip and sell_with_tip into swap method - Update all protocol instruction builders to support new parameter structure - Standardize ATA creation/closing parameter naming conventions - Update example code to use new API interfaces - Optimize trading logic with automatic direction detection based on input token type
This commit is contained in:
@@ -2,13 +2,13 @@ use anyhow::Result;
|
||||
use solana_sdk::signature::Signature;
|
||||
use std::{sync::Arc, time::Instant};
|
||||
|
||||
use crate::trading::core::parallel::{buy_parallel_execute, sell_parallel_execute};
|
||||
|
||||
use super::{
|
||||
params::{BuyParams, SellParams},
|
||||
traits::{InstructionBuilder, TradeExecutor},
|
||||
use crate::trading::core::{
|
||||
parallel::{buy_parallel_execute, sell_parallel_execute},
|
||||
traits::TradeExecutor,
|
||||
};
|
||||
|
||||
use super::{params::SwapParams, traits::InstructionBuilder};
|
||||
|
||||
/// Generic trade executor implementation
|
||||
pub struct GenericTradeExecutor {
|
||||
instruction_builder: Arc<dyn InstructionBuilder>,
|
||||
@@ -26,46 +26,35 @@ impl GenericTradeExecutor {
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl TradeExecutor for GenericTradeExecutor {
|
||||
async fn buy_with_tip(&self, params: BuyParams) -> Result<Signature> {
|
||||
async fn swap(&self, params: SwapParams) -> Result<Signature> {
|
||||
let start = Instant::now();
|
||||
|
||||
// 暂时支持这三种。后续重构扩展builder 支持所有的 swap
|
||||
let is_buy = params.input_mint == crate::constants::SOL_TOKEN_ACCOUNT
|
||||
|| params.input_mint == crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
|| (params.input_mint == crate::constants::USD1_TOKEN_ACCOUNT
|
||||
&& params.output_mint != crate::constants::WSOL_TOKEN_ACCOUNT);
|
||||
// Build instructions directly from params to avoid unnecessary cloning
|
||||
let instructions = self.instruction_builder.build_buy_instructions(¶ms).await?;
|
||||
let instructions = if is_buy {
|
||||
self.instruction_builder.build_buy_instructions(¶ms).await?
|
||||
} else {
|
||||
self.instruction_builder.build_sell_instructions(¶ms).await?
|
||||
};
|
||||
let final_instructions = match ¶ms.middleware_manager {
|
||||
Some(middleware_manager) => middleware_manager
|
||||
.apply_middlewares_process_protocol_instructions(
|
||||
instructions,
|
||||
self.protocol_name.to_string(),
|
||||
true,
|
||||
is_buy,
|
||||
)?,
|
||||
None => instructions,
|
||||
};
|
||||
|
||||
println!("Building buy transaction instructions time cost: {:?}", start.elapsed());
|
||||
|
||||
println!("Building swap transaction instructions time cost: {:?}", start.elapsed());
|
||||
// Execute transactions in parallel
|
||||
buy_parallel_execute(params, final_instructions, self.protocol_name).await
|
||||
}
|
||||
|
||||
async fn sell_with_tip(&self, params: SellParams) -> Result<Signature> {
|
||||
let start = Instant::now();
|
||||
|
||||
// Build instructions directly from params to avoid unnecessary cloning
|
||||
let instructions = self.instruction_builder.build_sell_instructions(¶ms).await?;
|
||||
let final_instructions = match ¶ms.middleware_manager {
|
||||
Some(middleware_manager) => middleware_manager
|
||||
.apply_middlewares_process_protocol_instructions(
|
||||
instructions,
|
||||
self.protocol_name.to_string(),
|
||||
false,
|
||||
)?,
|
||||
None => instructions,
|
||||
};
|
||||
|
||||
println!("Building sell transaction instructions time cost: {:?}", start.elapsed());
|
||||
|
||||
// Execute transactions in parallel
|
||||
sell_parallel_execute(params, final_instructions, self.protocol_name).await
|
||||
if is_buy {
|
||||
buy_parallel_execute(params, final_instructions, self.protocol_name).await
|
||||
} else {
|
||||
sell_parallel_execute(params, final_instructions, self.protocol_name).await
|
||||
}
|
||||
}
|
||||
|
||||
fn protocol_name(&self) -> &'static str {
|
||||
|
||||
Reference in New Issue
Block a user