Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(optimized_transaction): Separate Smart Transaction Creation #39

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions src/optimized_transaction.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::error::{HeliusError, Result};
use crate::types::{
GetPriorityFeeEstimateOptions, GetPriorityFeeEstimateRequest, GetPriorityFeeEstimateResponse,
GetPriorityFeeEstimateOptions, GetPriorityFeeEstimateRequest, GetPriorityFeeEstimateResponse, SmartTransaction,
SmartTransactionConfig,
};
use crate::Helius;
Expand Down Expand Up @@ -110,15 +110,15 @@ impl Helius {
}
}

/// Builds and sends an optimized transaction, and handles its confirmation status
/// Creates an optimized transaction based on the provided configuration
///
/// # Arguments
/// * `config` - The configuration for the smart transaction, which includes the transaction's instructions, and the user's keypair. If provided, it also
/// includes whether preflight checks should be skipped, how many times to retry the transaction, and any address lookup tables to be included in the transaction
/// * `config` - The configuration for the smart transaction, which includes the transaction's instructions, signers, and lookup tables, depending on
/// whether it's a legacy or versioned smart transaction. The transaction's send configuration can also be changed, if provided
///
/// # Returns
/// The transaction signature, if successful
pub async fn send_smart_transaction(&self, config: SmartTransactionConfig<'_>) -> Result<Signature> {
/// An optimized `Transaction` or `VersionedTransaction`
pub async fn create_smart_transaction(&self, config: &SmartTransactionConfig<'_>) -> Result<SmartTransaction> {
if config.signers.is_empty() {
return Err(HeliusError::InvalidInput(
"The fee payer must sign the transaction".to_string(),
Expand Down Expand Up @@ -261,11 +261,27 @@ impl Helius {
signatures,
message: versioned_message,
});

Ok(SmartTransaction::Versioned(versioned_transaction.unwrap()))
} else {
let mut tx: Transaction = Transaction::new_with_payer(&final_instructions, Some(&payer_pubkey));
tx.try_sign(&config.signers, recent_blockhash)?;
legacy_transaction = Some(tx);

Ok(SmartTransaction::Legacy(legacy_transaction.unwrap()))
}
}

/// Builds and sends an optimized transaction, and handles its confirmation status
///
/// # Arguments
/// * `config` - The configuration for the smart transaction, which includes the transaction's instructions, signers, and lookup tables, depending on
/// whether it's a legacy or versioned smart transaction. The transaction's send configuration can also be changed, if provided
///
/// # Returns
/// The transaction signature, if successful
pub async fn send_smart_transaction(&self, config: SmartTransactionConfig<'_>) -> Result<Signature> {
let transaction: SmartTransaction = self.create_smart_transaction(&config).await?;

// Common logic for sending transactions
let send_transaction_config: RpcSendTransactionConfig = RpcSendTransactionConfig {
Expand All @@ -290,10 +306,9 @@ impl Helius {
let start_time: Instant = Instant::now();

while Instant::now().duration_since(start_time) < timeout {
let result = if is_versioned {
send_versioned_result(versioned_transaction.as_ref().unwrap())
} else {
send_result(legacy_transaction.as_ref().unwrap())
let result = match &transaction {
SmartTransaction::Legacy(tx) => send_result(tx),
SmartTransaction::Versioned(tx) => send_versioned_result(tx),
};

match result {
Expand Down
7 changes: 7 additions & 0 deletions src/types/enums.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use serde::{Deserialize, Serialize};
use serde_enum_str::{Deserialize_enum_str, Serialize_enum_str};
use solana_sdk::transaction::{Transaction, VersionedTransaction};

use super::*;

Expand Down Expand Up @@ -732,3 +733,9 @@ pub enum CollectionIdentifier {
#[serde(rename = "verifiedCollectionAddress")]
VerifiedCollectionAddress(Vec<String>),
}

#[derive(Serialize, Deserialize, Debug)]
pub enum SmartTransaction {
Legacy(Transaction),
Versioned(VersionedTransaction),
}
Loading