1

I'm very starting with Solana so sorry for the noob questions.

I'm trying to extract data via Solana API as follows:

 use solana_client::rpc_client::RpcClient;


 let client = RpcClient::new("https://api.devnet.solana.com".to_string());
 let block = client.get_block_with_config(latest_slot, config)?;

As a result, I got some structure named UIConfirmedBlock with transactions field, which is Option<LongTransactionType>. I runned this several time both on devnet and testnet, and this field always is none, while according to https://explorer.solana.com/?cluster=devnet there are some transactions on the network.

I also looked on block_height on explorer I mentioned above and in this UIConfirmedBlock structure I receive, and they are similar, which mean I'm looking in actual Solana blocks. Thus I have few questions:

  1. Why I don't see any transactions in this UIConfirmedBlock? Was it designed to see no transactions here or it is a problem in my code?
  2. If it is designed such that there is no transactions here how can I get list of transactions from the block?
2
  • Under "Transactions are always none" I mean I runned it a lot (~1000) of times, checked that it returns different blocks, and for all of them transactions are None
    – mcjohn974
    Commented Aug 22 at 9:25
  • Thanks you all for you replies! I fixed it, it was problem in my RpcBlockConfig
    – mcjohn974
    Commented Aug 22 at 16:13

2 Answers 2

2

If you want to fetch devnet transactions, you need to change your API to point to that cluster https://api.devnet.solana.com

As the name suggests, the EncodedTransactionWithStatusMeta is the encoded transaction which you can paste into the solana explorer inspector tab to see what's happening in it.

https://explorer.solana.com/tx/inspector?cluster=devnet

Note that this tab accepts base 64 encoded string so you'll need to change your encoding to this format.

fn main() {
    let client = RpcClient::new("https://api.devnet.solana.com".to_string());

    let config = RpcBlockConfig {
        encoding: Some(UiTransactionEncoding::Base64),
        transaction_details: None,
        rewards: Some(true),
        commitment: None,
        max_supported_transaction_version: Some(0),
    };
    let block = client.get_block_with_config(320611583, config).unwrap();

    println!("{block:?}");
}
3

The transactions field in UIConfirmedBlock being of type Option means that this field can either contain some data (Some(LongTransactionType)) or be empty (None). If you're consistently getting None, it suggests that no transaction data is being returned for the blocks you're querying.

Possible Reasons for None in transactions Empty Blocks: Solana produces blocks even if there are no transactions. So, it's possible you're querying blocks that genuinely have no transactions.

RPC Node Configuration: Some Solana RPC nodes might be configured to return minimal data for performance reasons, particularly on devnet or testnet. This could include omitting transaction data.

Incorrect Slot: If the latest_slot you're querying doesn't actually correspond to the most recent block with transactions, you won't see any transactions. Make sure latest_slot is pointing to a slot with confirmed transactions.

Steps to Retrieve Transactions

  1. Check Block Time: Confirm the block time you're querying matches a time when transactions were likely to occur (use the explorer for reference).

  2. Use get_confirmed_block: Instead of using get_block_with_config, try using get_confirmed_block (if available) to fetch older blocks with transactions. This method might give you better results on devnet/testnet.

  3. Adjust RpcBlockConfig: Ensure your RpcBlockConfig is set to include full transaction data, not just summaries. You can configure it like this:

    use solana_client::rpc_config::RpcBlockConfig;

    let config = RpcBlockConfig {
        encoding: Some(solana_client::rpc_config::UiTransactionEncoding::Json), // or base64 if you prefer
        ..Default::default()
    };
    
    let block = client.get_block_with_config(latest_slot, config)?;
    
  4. Fetch Recent Transactions: Alternatively, you can use get_confirmed_signature_for_address2 to fetch recent transaction signatures for an account and then use get_confirmed_transaction to retrieve the transaction details.

Here’s how you might adjust your code to ensure you get transaction data:

use solana_client::rpc_client::RpcClient;
use solana_client::rpc_config::{RpcBlockConfig, UiTransactionEncoding};

let client = RpcClient::new("https://api.devnet.solana.com".to_string());
let config = RpcBlockConfig {
    encoding: Some(UiTransactionEncoding::Json),
    ..Default::default()
};
let block = client.get_block_with_config(latest_slot, config)?;

if let Some(transactions) = block.transactions {
    // Process transactions
} else {
    println!("No transactions found in this block.");
}

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.