feat: 为 buy 和 sell 方法添加 Signature 返回值
This commit is contained in:
+28
-21
@@ -5,6 +5,7 @@ pub mod protos;
|
|||||||
pub mod swqos;
|
pub mod swqos;
|
||||||
pub mod trading;
|
pub mod trading;
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
|
// use solana_program::example_mocks::solana_signature::Signature;
|
||||||
use solana_sdk::signer::Signer;
|
use solana_sdk::signer::Signer;
|
||||||
pub use solana_streamer_sdk;
|
pub use solana_streamer_sdk;
|
||||||
|
|
||||||
@@ -22,10 +23,10 @@ use crate::trading::MiddlewareManager;
|
|||||||
use crate::trading::SellParams;
|
use crate::trading::SellParams;
|
||||||
use crate::trading::TradeFactory;
|
use crate::trading::TradeFactory;
|
||||||
use common::{PriorityFee, SolanaRpcClient, TradeConfig};
|
use common::{PriorityFee, SolanaRpcClient, TradeConfig};
|
||||||
|
use parking_lot::Mutex;
|
||||||
use rustls::crypto::{ring::default_provider, CryptoProvider};
|
use rustls::crypto::{ring::default_provider, CryptoProvider};
|
||||||
use solana_sdk::hash::Hash;
|
use solana_sdk::hash::Hash;
|
||||||
use solana_sdk::{pubkey::Pubkey, signature::Keypair};
|
use solana_sdk::{pubkey::Pubkey, signature::Keypair, signature::Signature};
|
||||||
use parking_lot::Mutex;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use swqos::SwqosClient;
|
use swqos::SwqosClient;
|
||||||
|
|
||||||
@@ -157,7 +158,7 @@ impl SolanaTrade {
|
|||||||
extension_params: Box<dyn ProtocolParams>,
|
extension_params: Box<dyn ProtocolParams>,
|
||||||
lookup_table_key: Option<Pubkey>,
|
lookup_table_key: Option<Pubkey>,
|
||||||
wait_transaction_confirmed: bool,
|
wait_transaction_confirmed: bool,
|
||||||
) -> Result<(), anyhow::Error> {
|
) -> Result<Signature, anyhow::Error> {
|
||||||
if slippage_basis_points.is_none() {
|
if slippage_basis_points.is_none() {
|
||||||
println!(
|
println!(
|
||||||
"slippage_basis_points is none, use default slippage basis points: {}",
|
"slippage_basis_points is none, use default slippage basis points: {}",
|
||||||
@@ -205,9 +206,10 @@ impl SolanaTrade {
|
|||||||
return Err(anyhow::anyhow!("Invalid protocol params for Trade"));
|
return Err(anyhow::anyhow!("Invalid protocol params for Trade"));
|
||||||
}
|
}
|
||||||
|
|
||||||
executor
|
let sig = executor
|
||||||
.buy_with_tip(buy_params, self.swqos_clients.clone(), self.middleware_manager.clone())
|
.buy_with_tip(buy_params, self.swqos_clients.clone(), self.middleware_manager.clone())
|
||||||
.await
|
.await;
|
||||||
|
sig
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Execute a sell order for a specified token
|
/// Execute a sell order for a specified token
|
||||||
@@ -249,7 +251,7 @@ impl SolanaTrade {
|
|||||||
extension_params: Box<dyn ProtocolParams>,
|
extension_params: Box<dyn ProtocolParams>,
|
||||||
lookup_table_key: Option<Pubkey>,
|
lookup_table_key: Option<Pubkey>,
|
||||||
wait_transaction_confirmed: bool,
|
wait_transaction_confirmed: bool,
|
||||||
) -> Result<(), anyhow::Error> {
|
) -> Result<Signature, anyhow::Error> {
|
||||||
if slippage_basis_points.is_none() {
|
if slippage_basis_points.is_none() {
|
||||||
println!(
|
println!(
|
||||||
"slippage_basis_points is none, use default slippage basis points: {}",
|
"slippage_basis_points is none, use default slippage basis points: {}",
|
||||||
@@ -301,7 +303,10 @@ impl SolanaTrade {
|
|||||||
if !with_tip { self.rpc_client.clone() } else { self.swqos_clients.clone() };
|
if !with_tip { self.rpc_client.clone() } else { self.swqos_clients.clone() };
|
||||||
|
|
||||||
// Execute sell based on tip preference
|
// Execute sell based on tip preference
|
||||||
executor.sell_with_tip(sell_params, _swqos_clients, self.middleware_manager.clone()).await
|
let sig = executor
|
||||||
|
.sell_with_tip(sell_params, _swqos_clients, self.middleware_manager.clone())
|
||||||
|
.await;
|
||||||
|
sig
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Execute a sell order for a percentage of the specified token amount
|
/// Execute a sell order for a percentage of the specified token amount
|
||||||
@@ -349,23 +354,25 @@ impl SolanaTrade {
|
|||||||
extension_params: Box<dyn ProtocolParams>,
|
extension_params: Box<dyn ProtocolParams>,
|
||||||
lookup_table_key: Option<Pubkey>,
|
lookup_table_key: Option<Pubkey>,
|
||||||
wait_transaction_confirmed: bool,
|
wait_transaction_confirmed: bool,
|
||||||
) -> Result<(), anyhow::Error> {
|
) -> Result<Signature, anyhow::Error> {
|
||||||
if percent == 0 || percent > 100 {
|
if percent == 0 || percent > 100 {
|
||||||
return Err(anyhow::anyhow!("Percentage must be between 1 and 100"));
|
return Err(anyhow::anyhow!("Percentage must be between 1 and 100"));
|
||||||
}
|
}
|
||||||
let amount = amount_token * percent / 100;
|
let amount = amount_token * percent / 100;
|
||||||
self.sell(
|
let sig = self
|
||||||
dex_type,
|
.sell(
|
||||||
mint,
|
dex_type,
|
||||||
amount,
|
mint,
|
||||||
slippage_basis_points,
|
amount,
|
||||||
recent_blockhash,
|
slippage_basis_points,
|
||||||
custom_priority_fee,
|
recent_blockhash,
|
||||||
with_tip,
|
custom_priority_fee,
|
||||||
extension_params,
|
with_tip,
|
||||||
lookup_table_key,
|
extension_params,
|
||||||
wait_transaction_confirmed,
|
lookup_table_key,
|
||||||
)
|
wait_transaction_confirmed,
|
||||||
.await
|
)
|
||||||
|
.await;
|
||||||
|
sig
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
use solana_sdk::signature::Signature;
|
||||||
use std::{sync::Arc, time::Instant};
|
use std::{sync::Arc, time::Instant};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
@@ -32,7 +33,7 @@ impl TradeExecutor for GenericTradeExecutor {
|
|||||||
params: BuyParams,
|
params: BuyParams,
|
||||||
swqos_clients: Vec<Arc<SwqosClient>>,
|
swqos_clients: Vec<Arc<SwqosClient>>,
|
||||||
middleware_manager: Option<Arc<MiddlewareManager>>,
|
middleware_manager: Option<Arc<MiddlewareManager>>,
|
||||||
) -> Result<()> {
|
) -> Result<Signature> {
|
||||||
let mut data_size_limit = params.data_size_limit;
|
let mut data_size_limit = params.data_size_limit;
|
||||||
if data_size_limit == 0 {
|
if data_size_limit == 0 {
|
||||||
data_size_limit = MAX_LOADED_ACCOUNTS_DATA_SIZE_LIMIT;
|
data_size_limit = MAX_LOADED_ACCOUNTS_DATA_SIZE_LIMIT;
|
||||||
@@ -55,7 +56,7 @@ impl TradeExecutor for GenericTradeExecutor {
|
|||||||
println!("Building buy transaction instructions time cost: {:?}", start.elapsed());
|
println!("Building buy transaction instructions time cost: {:?}", start.elapsed());
|
||||||
|
|
||||||
// Execute transactions in parallel
|
// Execute transactions in parallel
|
||||||
parallel_execute_with_tips(
|
let sig = parallel_execute_with_tips(
|
||||||
swqos_clients,
|
swqos_clients,
|
||||||
params.payer,
|
params.payer,
|
||||||
final_instructions,
|
final_instructions,
|
||||||
@@ -71,7 +72,7 @@ impl TradeExecutor for GenericTradeExecutor {
|
|||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(sig)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn sell_with_tip(
|
async fn sell_with_tip(
|
||||||
@@ -79,7 +80,7 @@ impl TradeExecutor for GenericTradeExecutor {
|
|||||||
params: SellParams,
|
params: SellParams,
|
||||||
swqos_clients: Vec<Arc<SwqosClient>>,
|
swqos_clients: Vec<Arc<SwqosClient>>,
|
||||||
middleware_manager: Option<Arc<MiddlewareManager>>,
|
middleware_manager: Option<Arc<MiddlewareManager>>,
|
||||||
) -> Result<()> {
|
) -> Result<Signature> {
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
|
|
||||||
// Build instructions directly from params to avoid unnecessary cloning
|
// Build instructions directly from params to avoid unnecessary cloning
|
||||||
@@ -97,7 +98,7 @@ impl TradeExecutor for GenericTradeExecutor {
|
|||||||
println!("Building sell transaction instructions time cost: {:?}", start.elapsed());
|
println!("Building sell transaction instructions time cost: {:?}", start.elapsed());
|
||||||
|
|
||||||
// Execute transactions in parallel
|
// Execute transactions in parallel
|
||||||
parallel_execute_with_tips(
|
let sig = parallel_execute_with_tips(
|
||||||
swqos_clients,
|
swqos_clients,
|
||||||
params.payer,
|
params.payer,
|
||||||
final_instructions,
|
final_instructions,
|
||||||
@@ -113,7 +114,7 @@ impl TradeExecutor for GenericTradeExecutor {
|
|||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(sig)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn protocol_name(&self) -> &'static str {
|
fn protocol_name(&self) -> &'static str {
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use solana_hash::Hash;
|
use solana_hash::Hash;
|
||||||
use solana_sdk::{instruction::Instruction, pubkey::Pubkey, signature::Keypair};
|
// use solana_program::example_mocks::solana_signature::Signature;
|
||||||
|
use solana_sdk::{
|
||||||
|
instruction::Instruction, pubkey::Pubkey, signature::Keypair, signature::Signature,
|
||||||
|
};
|
||||||
use std::{str::FromStr, sync::Arc, time::Instant};
|
use std::{str::FromStr, sync::Arc, time::Instant};
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
use tokio::task::JoinHandle;
|
use tokio::task::JoinHandle;
|
||||||
@@ -25,9 +28,9 @@ pub async fn parallel_execute_with_tips(
|
|||||||
is_buy: bool,
|
is_buy: bool,
|
||||||
wait_transaction_confirmed: bool,
|
wait_transaction_confirmed: bool,
|
||||||
with_tip: bool,
|
with_tip: bool,
|
||||||
) -> Result<()> {
|
) -> Result<Signature> {
|
||||||
let cores = core_affinity::get_core_ids().unwrap();
|
let cores = core_affinity::get_core_ids().unwrap();
|
||||||
let mut handles: Vec<JoinHandle<Result<()>>> = Vec::with_capacity(swqos_clients.len());
|
let mut handles: Vec<JoinHandle<Result<Signature>>> = Vec::with_capacity(swqos_clients.len());
|
||||||
if is_buy
|
if is_buy
|
||||||
&& (swqos_clients.len() > priority_fee.buy_tip_fees.len()
|
&& (swqos_clients.len() > priority_fee.buy_tip_fees.len()
|
||||||
|| priority_fee.buy_tip_fees.is_empty())
|
|| priority_fee.buy_tip_fees.is_empty())
|
||||||
@@ -103,7 +106,11 @@ pub async fn parallel_execute_with_tips(
|
|||||||
start.elapsed()
|
start.elapsed()
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok::<(), anyhow::Error>(())
|
transaction
|
||||||
|
.signatures
|
||||||
|
.first()
|
||||||
|
.ok_or_else(|| anyhow!("Transaction has no signatures"))
|
||||||
|
.cloned()
|
||||||
});
|
});
|
||||||
|
|
||||||
handles.push(handle);
|
handles.push(handle);
|
||||||
@@ -125,13 +132,20 @@ pub async fn parallel_execute_with_tips(
|
|||||||
let mut errors = Vec::new();
|
let mut errors = Vec::new();
|
||||||
|
|
||||||
if !wait_transaction_confirmed {
|
if !wait_transaction_confirmed {
|
||||||
return Ok(());
|
if let Some(result) = rx.recv().await {
|
||||||
|
match result {
|
||||||
|
Ok(Ok(sig)) => return Ok(sig),
|
||||||
|
Ok(Err(e)) => errors.push(format!("Task error: {}", e)),
|
||||||
|
Err(e) => errors.push(format!("Join error: {}", e)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Err(anyhow!("No transaction signature available"));
|
||||||
}
|
}
|
||||||
|
|
||||||
while let Some(result) = rx.recv().await {
|
while let Some(result) = rx.recv().await {
|
||||||
match result {
|
match result {
|
||||||
Ok(Ok(_)) => {
|
Ok(Ok(sig)) => {
|
||||||
return Ok(());
|
return Ok(sig);
|
||||||
}
|
}
|
||||||
Ok(Err(e)) => errors.push(format!("Task error: {}", e)),
|
Ok(Err(e)) => errors.push(format!("Task error: {}", e)),
|
||||||
Err(e) => errors.push(format!("Join error: {}", e)),
|
Err(e) => errors.push(format!("Join error: {}", e)),
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use std::sync::Arc;
|
|||||||
|
|
||||||
use crate::{swqos::SwqosClient, trading::MiddlewareManager};
|
use crate::{swqos::SwqosClient, trading::MiddlewareManager};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use solana_sdk::instruction::Instruction;
|
use solana_sdk::{instruction::Instruction, signature::Signature};
|
||||||
|
|
||||||
use super::params::{BuyParams, SellParams};
|
use super::params::{BuyParams, SellParams};
|
||||||
|
|
||||||
@@ -15,14 +15,14 @@ pub trait TradeExecutor: Send + Sync {
|
|||||||
params: BuyParams,
|
params: BuyParams,
|
||||||
swqos_clients: Vec<Arc<SwqosClient>>,
|
swqos_clients: Vec<Arc<SwqosClient>>,
|
||||||
middleware_manager: Option<Arc<MiddlewareManager>>,
|
middleware_manager: Option<Arc<MiddlewareManager>>,
|
||||||
) -> Result<()>;
|
) -> Result<Signature>;
|
||||||
/// 使用MEV服务执行卖出交易
|
/// 使用MEV服务执行卖出交易
|
||||||
async fn sell_with_tip(
|
async fn sell_with_tip(
|
||||||
&self,
|
&self,
|
||||||
params: SellParams,
|
params: SellParams,
|
||||||
swqos_clients: Vec<Arc<SwqosClient>>,
|
swqos_clients: Vec<Arc<SwqosClient>>,
|
||||||
middleware_manager: Option<Arc<MiddlewareManager>>,
|
middleware_manager: Option<Arc<MiddlewareManager>>,
|
||||||
) -> Result<()>;
|
) -> Result<Signature>;
|
||||||
/// 获取协议名称
|
/// 获取协议名称
|
||||||
fn protocol_name(&self) -> &'static str;
|
fn protocol_name(&self) -> &'static str;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user