#让SDK中打印 submit_timing 的数据( SwqosType和 提交耗时)返回给buy 和 sell 方法。
主要解决问题: 当 wait_tx_confirmed 参数为 false 时,SUBMIT_TIMEOUT_SECS 等待改为2秒,要么全部提交完成返回,要么不等待提交未返回的交易,方便快速创建交易订单。 #Let the SDK print submit_timing data (SwqosType and submission time) and return it to the buy and sell methods. Main problems solved: When the wait_tx_confirmed parameter is false, the SUBMIT_TIMEOUT_SECS wait is changed to 2 seconds. Either all submissions are completed and returned, or the unreturned transactions are not waited for submission, which facilitates the rapid creation of transaction orders.
This commit is contained in:
+7
-7
@@ -740,7 +740,7 @@ impl TradingClient {
|
|||||||
pub async fn buy(
|
pub async fn buy(
|
||||||
&self,
|
&self,
|
||||||
params: TradeBuyParams,
|
params: TradeBuyParams,
|
||||||
) -> Result<(bool, Vec<Signature>, Option<TradeError>), anyhow::Error> {
|
) -> Result<(bool, Vec<Signature>, Option<TradeError>, Vec<(crate::swqos::SwqosType, i64)>), anyhow::Error> {
|
||||||
if params.recent_blockhash.is_none() && params.durable_nonce.is_none() {
|
if params.recent_blockhash.is_none() && params.durable_nonce.is_none() {
|
||||||
return Err(anyhow::anyhow!(
|
return Err(anyhow::anyhow!(
|
||||||
"Must provide either recent_blockhash or durable_nonce for buy (required for transaction validity)"
|
"Must provide either recent_blockhash or durable_nonce for buy (required for transaction validity)"
|
||||||
@@ -814,8 +814,8 @@ impl TradingClient {
|
|||||||
|
|
||||||
let swap_result = executor.swap(buy_params).await;
|
let swap_result = executor.swap(buy_params).await;
|
||||||
let result =
|
let result =
|
||||||
swap_result.map(|(success, sigs, err)| (success, sigs, err.map(TradeError::from)));
|
swap_result.map(|(success, sigs, err, timings)| (success, sigs, err.map(TradeError::from), timings));
|
||||||
return result;
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Execute a sell order for a specified token
|
/// Execute a sell order for a specified token
|
||||||
@@ -847,7 +847,7 @@ impl TradingClient {
|
|||||||
pub async fn sell(
|
pub async fn sell(
|
||||||
&self,
|
&self,
|
||||||
params: TradeSellParams,
|
params: TradeSellParams,
|
||||||
) -> Result<(bool, Vec<Signature>, Option<TradeError>), anyhow::Error> {
|
) -> Result<(bool, Vec<Signature>, Option<TradeError>, Vec<(crate::swqos::SwqosType, i64)>), anyhow::Error> {
|
||||||
#[cfg(feature = "perf-trace")]
|
#[cfg(feature = "perf-trace")]
|
||||||
if sdk_log::sdk_log_enabled() && params.slippage_basis_points.is_none() {
|
if sdk_log::sdk_log_enabled() && params.slippage_basis_points.is_none() {
|
||||||
debug!(
|
debug!(
|
||||||
@@ -921,8 +921,8 @@ impl TradingClient {
|
|||||||
|
|
||||||
let swap_result = executor.swap(sell_params).await;
|
let swap_result = executor.swap(sell_params).await;
|
||||||
let result =
|
let result =
|
||||||
swap_result.map(|(success, sigs, err)| (success, sigs, err.map(TradeError::from)));
|
swap_result.map(|(success, sigs, err, timings)| (success, sigs, err.map(TradeError::from), timings));
|
||||||
return result;
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Execute a sell order for a percentage of the specified token amount
|
/// Execute a sell order for a percentage of the specified token amount
|
||||||
@@ -956,7 +956,7 @@ impl TradingClient {
|
|||||||
mut params: TradeSellParams,
|
mut params: TradeSellParams,
|
||||||
amount_token: u64,
|
amount_token: u64,
|
||||||
percent: u64,
|
percent: u64,
|
||||||
) -> Result<(bool, Vec<Signature>, Option<TradeError>), anyhow::Error> {
|
) -> Result<(bool, Vec<Signature>, Option<TradeError>, Vec<(crate::swqos::SwqosType, i64)>), 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"));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -562,7 +562,7 @@ pub async fn execute_parallel(
|
|||||||
// All jobs enqueued (no spawn on hot path)
|
// All jobs enqueued (no spawn on hot path)
|
||||||
|
|
||||||
if !wait_transaction_confirmed {
|
if !wait_transaction_confirmed {
|
||||||
const SUBMIT_TIMEOUT_SECS: u64 = 30;
|
const SUBMIT_TIMEOUT_SECS: u64 = 2;//无需确认的交易,一般2秒合适了 一般2秒内发送全都返回 没返回的也不等了,没返回的就是太慢的swqos
|
||||||
let ret = collector.wait_for_all_submitted(SUBMIT_TIMEOUT_SECS).await.unwrap_or((
|
let ret = collector.wait_for_all_submitted(SUBMIT_TIMEOUT_SECS).await.unwrap_or((
|
||||||
false,
|
false,
|
||||||
vec![],
|
vec![],
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ use crate::{
|
|||||||
trading::MiddlewareManager,
|
trading::MiddlewareManager,
|
||||||
};
|
};
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
|
use crate::swqos::{ SwqosType};
|
||||||
|
|
||||||
/// Global syscall bypass manager (reserved for future time/IO optimizations).
|
/// Global syscall bypass manager (reserved for future time/IO optimizations).
|
||||||
/// 全局系统调用绕过管理器(预留,后续可接入时间/IO 等优化)。
|
/// 全局系统调用绕过管理器(预留,后续可接入时间/IO 等优化)。
|
||||||
@@ -55,7 +56,7 @@ impl TradeExecutor for GenericTradeExecutor {
|
|||||||
async fn swap(
|
async fn swap(
|
||||||
&self,
|
&self,
|
||||||
params: SwapParams,
|
params: SwapParams,
|
||||||
) -> Result<(bool, Vec<Signature>, Option<anyhow::Error>)> {
|
) -> Result<(bool, Vec<Signature>, Option<anyhow::Error>, Vec<(SwqosType, i64)>)> {
|
||||||
// Sample total start only when logging or simulate. 仅在有日志或 simulate 时取起点。
|
// Sample total start only when logging or simulate. 仅在有日志或 simulate 时取起点。
|
||||||
let total_start = (params.log_enabled || params.simulate).then(Instant::now);
|
let total_start = (params.log_enabled || params.simulate).then(Instant::now);
|
||||||
let timing_start_us: Option<i64> = if params.log_enabled {
|
let timing_start_us: Option<i64> = if params.log_enabled {
|
||||||
@@ -215,7 +216,9 @@ impl TradeExecutor for GenericTradeExecutor {
|
|||||||
} else {
|
} else {
|
||||||
(ok, signatures, err)
|
(ok, signatures, err)
|
||||||
};
|
};
|
||||||
Ok(confirm_result)
|
|
||||||
|
//就是把confirm_result 拆开 再加上 submit_timings
|
||||||
|
Ok((confirm_result.0, confirm_result.1, confirm_result.2, submit_timings))
|
||||||
} else {
|
} else {
|
||||||
// Not waiting for confirmation: confirmed is not measured (-); total is per-channel submit time only.
|
// Not waiting for confirmation: confirmed is not measured (-); total is per-channel submit time only.
|
||||||
if log_enabled {
|
if log_enabled {
|
||||||
@@ -229,7 +232,11 @@ impl TradeExecutor for GenericTradeExecutor {
|
|||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Ok((ok, signatures, err))
|
|
||||||
|
|
||||||
|
|
||||||
|
Ok((ok, signatures, err, submit_timings))
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
result
|
result
|
||||||
@@ -254,7 +261,7 @@ async fn simulate_transaction(
|
|||||||
is_buy: bool,
|
is_buy: bool,
|
||||||
with_tip: bool,
|
with_tip: bool,
|
||||||
gas_fee_strategy: GasFeeStrategy,
|
gas_fee_strategy: GasFeeStrategy,
|
||||||
) -> Result<(bool, Vec<Signature>, Option<anyhow::Error>)> {
|
) -> Result<(bool, Vec<Signature>, Option<anyhow::Error>, Vec<(SwqosType, i64)>)> {
|
||||||
use crate::trading::common::build_transaction;
|
use crate::trading::common::build_transaction;
|
||||||
use solana_client::rpc_config::RpcSimulateTransactionConfig;
|
use solana_client::rpc_config::RpcSimulateTransactionConfig;
|
||||||
use solana_commitment_config::CommitmentLevel;
|
use solana_commitment_config::CommitmentLevel;
|
||||||
@@ -330,7 +337,7 @@ async fn simulate_transaction(
|
|||||||
trace!(target: "sol_trade_sdk", "Compute Units Consumed: {}", units_consumed);
|
trace!(target: "sol_trade_sdk", "Compute Units Consumed: {}", units_consumed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Ok((false, vec![signature], Some(anyhow::anyhow!("{:?}", err))));
|
return Ok((false, vec![signature], Some(anyhow::anyhow!("{:?}", err)), Vec::new()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simulation succeeded
|
// Simulation succeeded
|
||||||
@@ -345,7 +352,7 @@ async fn simulate_transaction(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok((true, vec![signature], None))
|
Ok((true, vec![signature], None, Vec::new()))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use crate::trading::SwapParams;
|
use crate::trading::SwapParams;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use solana_sdk::{instruction::Instruction, signature::Signature};
|
use solana_sdk::{instruction::Instruction, signature::Signature};
|
||||||
|
use crate::swqos::{SwqosType};
|
||||||
/// 交易执行器trait - 定义了所有交易协议都需要实现的核心方法
|
/// 交易执行器trait - 定义了所有交易协议都需要实现的核心方法
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
pub trait TradeExecutor: Send + Sync {
|
pub trait TradeExecutor: Send + Sync {
|
||||||
@@ -9,10 +9,7 @@ pub trait TradeExecutor: Send + Sync {
|
|||||||
/// - bool: 是否至少有一个交易成功
|
/// - bool: 是否至少有一个交易成功
|
||||||
/// - Vec<Signature>: 所有提交的交易签名(按SWQOS顺序)
|
/// - Vec<Signature>: 所有提交的交易签名(按SWQOS顺序)
|
||||||
/// - Option<anyhow::Error>: 最后一个错误(如果全部失败)
|
/// - Option<anyhow::Error>: 最后一个错误(如果全部失败)
|
||||||
async fn swap(
|
async fn swap(&self, params: SwapParams) -> Result<(bool, Vec<Signature>, Option<anyhow::Error>, Vec<(SwqosType, i64)>)>;
|
||||||
&self,
|
|
||||||
params: SwapParams,
|
|
||||||
) -> Result<(bool, Vec<Signature>, Option<anyhow::Error>)>;
|
|
||||||
/// 获取协议名称
|
/// 获取协议名称
|
||||||
fn protocol_name(&self) -> &'static str;
|
fn protocol_name(&self) -> &'static str;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user