1

My off-chain program is sending transactions to Solana, and I want to inspect the logs of the transaction inside this program.

// Send transaction
let transaction_signature = client.send_transaction(&my_transaction)?;
// Get logs
let my_logs = client.i_want_this_get_logs_method(&transaction_signature)?;
// Futher log-related logic...

2 Answers 2

2

This answer helped me to find get_transaction() method of solana_client::rpc_client::RpcClient, so to obtain logs of the transaction one needs to do the following:

let signature = rpc_client.send_and_confirm_transaction(&tx)?;
let transaction_with_meta = rpc_client.get_transaction(
    &signature,
    UiTransactionEncoding::Json,
)?;
// transaction itself can be extracted this way:
let transaction = transaction_with_meta.transaction;

This struct contains 3 fields: meta, transaction and version.

  • transaction is what was actually sent to the network (reсent blockhash, instructions, accounts, signatures and so on)
  • meta contains different meta-information (see this), logs are among it.
// Type annotation here is just for verbosity 
let logs:Vec<String> = transaction
    .meta
    .log_messages
    .expect("There are no logs!");
1

To get the log messages for a transaction, you can use the getTransaction endpoint from JSON RPC, and then pull out the meta.logMessages.

Not all RPC endpoints provide log messages, however, so you may not receive them in the response. In that case, you'll need to find a different node / talk to your provider / use a different provider.

More information about getTransaction at https://docs.solana.com/developing/clients/jsonrpc-api#getTransaction

1
  • I was looking for Rust API, but your answer helped me to find the relevant method in solana-client docs. I'll post my findings in separate answer.
    – totikom
    Commented Dec 6, 2022 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.