Skip to main content

wp_solana_test_core/
execute.rs

1//! Transaction execution helpers for [`TestContext`].
2//!
3//! These are async because they go through the mock RPC client, which is
4//! itself async.
5
6use anyhow::Result;
7use solana_client::rpc_config::RpcTransactionConfig;
8use solana_sdk::{
9    instruction::Instruction,
10    signature::{Keypair, Signature, Signer},
11    transaction::Transaction,
12};
13use solana_transaction_status::{option_serializer::OptionSerializer, UiTransactionEncoding};
14
15use crate::context::TestContext;
16
17/// Build, sign, and send a transaction through the mock RPC client.
18///
19/// The payer from `ctx` is always included as the first signer.
20pub async fn execute(
21    ctx: &TestContext,
22    ixs: &[Instruction],
23    additional_signers: &[&Keypair],
24) -> Result<Signature> {
25    let blockhash = ctx.rpc.get_latest_blockhash().await?;
26
27    let mut signers: Vec<&Keypair> = vec![ctx.payer.as_ref()];
28    signers.extend(additional_signers);
29
30    let tx =
31        Transaction::new_signed_with_payer(ixs, Some(&ctx.payer.pubkey()), &signers, blockhash);
32
33    let sig = ctx.rpc.send_and_confirm_transaction(&tx).await?;
34    Ok(sig)
35}
36
37/// Execute a transaction and return the signature together with program logs.
38pub async fn execute_with_logs(
39    ctx: &TestContext,
40    ixs: &[Instruction],
41    additional_signers: &[&Keypair],
42) -> Result<(Signature, Vec<String>)> {
43    let sig = execute(ctx, ixs, additional_signers).await?;
44
45    let config = RpcTransactionConfig {
46        encoding: Some(UiTransactionEncoding::Json),
47        commitment: None,
48        max_supported_transaction_version: Some(0),
49    };
50
51    let tx_detail = ctx.rpc.get_transaction_with_config(&sig, config).await?;
52
53    let logs = tx_detail
54        .transaction
55        .meta
56        .and_then(|m| match m.log_messages {
57            OptionSerializer::Some(v) => Some(v),
58            _ => None,
59        })
60        .unwrap_or_default();
61
62    Ok((sig, logs))
63}