Skip to content

Commit

Permalink
Merge pull request #86 from grandima/timeout-configurable
Browse files Browse the repository at this point in the history
feat: configurable timeout
  • Loading branch information
0xIchigo authored Oct 30, 2024
2 parents 1cd26cb + e66b0dc commit a659ba6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
1 change: 1 addition & 0 deletions examples/send_smart_transaction_with_tip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ async fn main() {
max_retries: None,
min_context_slot: None,
},
timeout: Timeout::default(),
};

// Send the optimized transaction with a 10k lamport tip using the New York region's API URL
Expand Down
17 changes: 7 additions & 10 deletions src/optimized_transaction.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::error::{HeliusError, Result};
use crate::types::{
CreateSmartTransactionConfig, CreateSmartTransactionSeedConfig, GetPriorityFeeEstimateOptions,
GetPriorityFeeEstimateRequest, GetPriorityFeeEstimateResponse, SmartTransaction, SmartTransactionConfig,
GetPriorityFeeEstimateRequest, GetPriorityFeeEstimateResponse, SmartTransaction, SmartTransactionConfig, Timeout,
};
use crate::Helius;

Expand Down Expand Up @@ -337,13 +337,7 @@ impl Helius {
let (transaction, last_valid_block_height) = self.create_smart_transaction(&config.create_config).await?;

// Common logic for sending transactions
let send_transaction_config: RpcSendTransactionConfig = RpcSendTransactionConfig {
skip_preflight: config.send_options.skip_preflight,
preflight_commitment: config.send_options.preflight_commitment,
encoding: config.send_options.encoding,
max_retries: config.send_options.max_retries,
min_context_slot: config.send_options.min_context_slot,
};
let send_transaction_config = config.send_options.clone();

let send_result = |transaction: &Transaction| {
self.connection()
Expand All @@ -354,8 +348,8 @@ impl Helius {
.send_transaction_with_config(transaction, send_transaction_config)
};

// Retry logic with a timeout of 60 seconds
let timeout: Duration = Duration::from_secs(60);
// Retry logic with a timeout
let timeout: Duration = config.timeout.into();
let start_time: Instant = Instant::now();

while Instant::now().duration_since(start_time) < timeout
Expand Down Expand Up @@ -400,6 +394,7 @@ impl Helius {
/// - `fee_payer_seed`: Optional seed bytes for generating the fee payer keypair.
/// - `lookup_tables`: Optional address lookup tables for the transaction.
/// * `send_options` - Optional `RpcSendTransactionConfig` for sending the transaction.
/// * `timeout` - Optional `Timeout` wait time for polling transaction confirmation.
///
/// # Returns
///
Expand All @@ -417,6 +412,7 @@ impl Helius {
&self,
create_config: CreateSmartTransactionSeedConfig,
send_options: Option<RpcSendTransactionConfig>,
timeout: Option<Timeout>,
) -> Result<Signature> {
if create_config.signer_seeds.is_empty() {
return Err(HeliusError::InvalidInput(
Expand Down Expand Up @@ -452,6 +448,7 @@ impl Helius {
let smart_transaction_config: SmartTransactionConfig<'_> = SmartTransactionConfig {
create_config: create_smart_transaction_config,
send_options: send_options.unwrap_or_default(),
timeout: timeout.unwrap_or_default(),
};

self.send_smart_transaction(smart_transaction_config).await
Expand Down
23 changes: 21 additions & 2 deletions src/types/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use super::{
TransactionStatus, TransactionType, UiTransactionEncoding, WebhookType,
};
use crate::types::{DisplayOptions, GetAssetOptions};
// use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::time::Duration;

use solana_client::rpc_config::RpcSendTransactionConfig;
use solana_sdk::{address_lookup_table::AddressLookupTableAccount, instruction::Instruction, signature::Signer};
Expand Down Expand Up @@ -967,17 +967,36 @@ impl<'a> CreateSmartTransactionConfig<'a> {
}
}
}
pub struct Timeout {
pub duration: Duration,
}

impl Default for Timeout {
fn default() -> Self {
Self {
duration: Duration::from_secs(60),
}
}
}

impl Into<Duration> for Timeout {
fn into(self) -> Duration {
self.duration
}
}

pub struct SmartTransactionConfig<'a> {
pub create_config: CreateSmartTransactionConfig<'a>,
pub send_options: RpcSendTransactionConfig,
pub timeout: Timeout,
}

impl<'a> SmartTransactionConfig<'a> {
pub fn new(instructions: Vec<Instruction>, signers: Vec<&'a dyn Signer>) -> Self {
pub fn new(instructions: Vec<Instruction>, signers: Vec<&'a dyn Signer>, timeout: Timeout) -> Self {
Self {
create_config: CreateSmartTransactionConfig::new(instructions, signers),
send_options: RpcSendTransactionConfig::default(),
timeout,
}
}
}
Expand Down

0 comments on commit a659ba6

Please sign in to comment.