Skip to content

Commit

Permalink
Merge pull request #48 from helius-labs/feat/docs-w-jito-smart-transa…
Browse files Browse the repository at this point in the history
…ctions

feat(docs): Add Jito Smart Transactions and Helper Methods
  • Loading branch information
0xIchigo authored Jun 19, 2024
2 parents 02b351a + c49e444 commit 5a18ded
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ Our SDK is designed to provide a seamless developer experience when building on
- [`poll_transaction_confirmation`](https://github.com/helius-labs/helius-rust-sdk/blob/a79a751e1a064125010bdb359068a366d635d005/src/optimized_transaction.rs#L77-L112) - Polls a transaction to check whether it has been confirmed in 5 second intervals with a 15 second timeout
- [`send_smart_transaction`](https://github.com/helius-labs/helius-rust-sdk/blob/a79a751e1a064125010bdb359068a366d635d005/src/optimized_transaction.rs#L114-L332) - Builds and sends an optimized transaction, and handles its confirmation status

### Jito Smart Transactions and Helper Methods
- [`add_tip_instruction`](https://github.com/helius-labs/helius-rust-sdk/blob/02b351a5ee3fe16a36078b40f92dc72d0ad077ed/src/jito.rs#L66-L83) - Adds a tip instruction to the instructions provided
- [`create_smart_transaction_with_tip`](https://github.com/helius-labs/helius-rust-sdk/blob/02b351a5ee3fe16a36078b40f92dc72d0ad077ed/src/jito.rs#L85-L124) - Creates a smart transaction with a Jito tip
- [`get_bundle_statuses`](https://github.com/helius-labs/helius-rust-sdk/blob/02b351a5ee3fe16a36078b40f92dc72d0ad077ed/src/jito.rs#L169-L202) - Get the status of Jito bundles
- [`send_jito_bundle`](https://github.com/helius-labs/helius-rust-sdk/blob/02b351a5ee3fe16a36078b40f92dc72d0ad077ed/src/jito.rs#L126-L167) - Sends a bundle of transactions to the Jito Block Engine
- [`send_smart_transaction_with_tip`](https://github.com/helius-labs/helius-rust-sdk/blob/02b351a5ee3fe16a36078b40f92dc72d0ad077ed/src/jito.rs#L204-L269) - Sends a smart transaction as a Jito bundle with a tip

### Helper Methods
- [`get_priority_fee_estimate`](https://docs.helius.dev/solana-rpc-nodes/alpha-priority-fee-api) - Gets an estimate of the priority fees required for a transaction to be processed more quickly
- [`deserialize_str_to_number`](https://github.com/helius-labs/helius-rust-sdk/blob/dev/src/utils/deserialize_str_to_number.rs) - Deserializes a `String` to a `Number`
Expand Down
70 changes: 70 additions & 0 deletions examples/send_smart_transaction_with_tip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use helius::types::*;
use helius::Helius;
use solana_client::rpc_config::RpcSendTransactionConfig;
use solana_sdk::{
instruction::Instruction, native_token::LAMPORTS_PER_SOL, pubkey::Pubkey, signature::Keypair, signer::Signer,
system_instruction::transfer,
};
use std::str::FromStr;
use std::time::Duration;
use tokio::time::sleep;

#[tokio::main]
async fn main() {
let api_key: &str = "YOUR_API_KEY";
let cluster: Cluster = Cluster::MainnetBeta;
let helius: Helius = Helius::new(api_key, cluster).unwrap();

// Replace with your actual keypair
let from_keypair: Keypair = Keypair::new();
let from_pubkey: Pubkey = from_keypair.pubkey();

// Replace with the recipient's public key
let to_pubkey: Pubkey = Pubkey::from_str("RecipientPublicKeyHere").unwrap();

// Create a simple instruction (transfer 0.01 SOL from from_pubkey to to_pubkey)
let transfer_amount: u64 = 100_000; // 0.01 SOL in lamports
let instructions: Vec<Instruction> = vec![transfer(&from_pubkey, &to_pubkey, transfer_amount)];

let create_config: CreateSmartTransactionConfig = CreateSmartTransactionConfig {
instructions,
signers: vec![&from_keypair],
lookup_tables: None,
fee_payer: None,
};

let config: SmartTransactionConfig = SmartTransactionConfig {
create_config,
send_options: RpcSendTransactionConfig {
skip_preflight: true,
preflight_commitment: None,
encoding: None,
max_retries: None,
min_context_slot: None,
},
};

// Send the optimized transaction with a 10k lamport tip using the New York region's API URL
match helius
.send_smart_transaction_with_tip(config, Some(10000), Some("NY"))
.await
{
Ok(bundle_id) => {
println!("Transaction sent successfully: {}", bundle_id);
sleep(Duration::from_secs(5)).await;

// Get final balances
let balance_from = helius.connection().get_balance(&from_pubkey).unwrap_or(0);
println!(
"From Wallet Balance: {} SOL",
balance_from as f64 / LAMPORTS_PER_SOL as f64
);

let balance_to = helius.connection().get_balance(&to_pubkey).unwrap_or(0);
println!("To Wallet Balance: {} SOL", balance_to as f64 / LAMPORTS_PER_SOL as f64);
}
Err(e) => {
eprintln!("Failed to send transaction: {:?}", e);
}
}
}

0 comments on commit 5a18ded

Please sign in to comment.