Back to Tutorials
Beginner10 min

Querying Transactions

Search and retrieve transaction information

Querying Transactions

In this tutorial, you'll learn how to query and retrieve transaction information from the Beatoz blockchain.

Query by Transaction Hash

The most common way to get transaction details:

const { Web3 } = require('@beatoz/web3');

const web3 = new Web3('https://rpc-testnet0.beatoz.io');

async function getTransaction(hash) {
  const tx = await web3.beatoz.tx(hash);
  return tx;
}

// Usage
const txHash = '0x...transaction_hash';
const tx = await getTransaction(txHash);
console.log('Transaction:', tx);
Testnet

Transaction Object Properties

PropertyDescription
hashTransaction hash
heightBlock height
txEncoded transaction data
tx_resultExecution result

Decoding Transaction Data

async function getTransactionDetails(hash) {
  const tx = await web3.beatoz.tx(hash);
  
  return {
    hash: tx.hash,
    blockHeight: tx.height,
    success: tx.tx_result.code === 0,
    gasUsed: tx.tx_result.gas_used,
    gasWanted: tx.tx_result.gas_wanted,
  };
}

const details = await getTransactionDetails('0x...');
console.log('Details:', details);

Searching Transactions

Use txSearch to find transactions with specific criteria:

async function searchTransactions(query, page = 1, perPage = 10) {
  const result = await web3.beatoz.txSearch(query, false, page, perPage);
  
  return result;
}

// Search by sender
const results = await searchTransactions("tx.from='0x...'");
console.log(`Found ${results.total_count} transactions`);

Query Syntax

The query syntax follows Tendermint's event query format.

Common Query Examples

Find Transactions by Address

// Transactions sent FROM an address
const fromQuery = "tx.from='0x...address'";

// Transactions sent TO an address
const toQuery = "tx.to='0x...address'";

// Both directions
async function getAddressTransactions(address) {
  const [sent, received] = await Promise.all([
    searchTransactions(`tx.from='${address}'`),
    searchTransactions(`tx.to='${address}'`),
  ]);
  
  return {
    sent: sent.txs || [],
    received: received.txs || [],
  };
}

Find Transactions in a Block

async function getBlockTransactions(height) {
  const result = await searchTransactions(`tx.height=${height}`);
  return result.txs || [];
}

const txs = await getBlockTransactions(12345);
console.log(`Block 12345 has ${txs.length} transactions`);

Getting Block Information

async function getBlock(height) {
  const block = await web3.beatoz.block(height);
  return block;
}

async function getLatestBlock() {
  const status = await web3.beatoz.status();
  const latestHeight = status.sync_info.latest_block_height;
  return getBlock(latestHeight);
}

const latest = await getLatestBlock();
console.log('Latest Block:', latest.block.header.height);

Waiting for Transaction Confirmation

async function waitForConfirmation(hash, maxAttempts = 30, intervalMs = 2000) {
  for (let i = 0; i < maxAttempts; i++) {
    try {
      const tx = await web3.beatoz.tx(hash);
      
      if (tx.tx_result.code === 0) {
        return { success: true, tx };
      } else {
        return { success: false, error: tx.tx_result.log };
      }
    } catch (error) {
      // Transaction not yet indexed
      console.log(`Attempt ${i + 1}/${maxAttempts}: Waiting...`);
      await new Promise(resolve => setTimeout(resolve, intervalMs));
    }
  }
  
  throw new Error('Transaction confirmation timeout');
}

// Usage
const result = await waitForConfirmation('0x...');
if (result.success) {
  console.log('Transaction confirmed at block:', result.tx.height);
}

Performance Tip

For production applications, consider using WebSocket connections for real-time transaction updates instead of polling.

Network Status

Check the overall network status:

async function getNetworkInfo() {
  const [status, netInfo] = await Promise.all([
    web3.beatoz.status(),
    web3.beatoz.netInfo(),
  ]);
  
  return {
    network: status.node_info.network,
    latestBlock: status.sync_info.latest_block_height,
    latestBlockTime: status.sync_info.latest_block_time,
    catching_up: status.sync_info.catching_up,
    peers: netInfo.n_peers,
  };
}

const info = await getNetworkInfo();
console.log('Network Info:', info);

Error Handling

async function safeGetTransaction(hash) {
  try {
    const tx = await web3.beatoz.tx(hash);
    return { found: true, tx };
  } catch (error) {
    if (error.message.includes('not found')) {
      return { found: false, error: 'Transaction not found' };
    }
    throw error;
  }
}

Transaction queries may fail if:

  • The hash is invalid
  • The transaction hasn't been indexed yet
  • The node is still syncing

Complete Example

const { Web3 } = require('@beatoz/web3');

async function main() {
  const web3 = new Web3('https://rpc-testnet0.beatoz.io');
  
  // Get network status
  const status = await web3.beatoz.status();
  console.log('Network:', status.node_info.network);
  console.log('Latest Block:', status.sync_info.latest_block_height);
  
  // Query a specific transaction
  const txHash = '0x...';
  try {
    const tx = await web3.beatoz.tx(txHash);
    console.log('Transaction found at block:', tx.height);
  } catch (error) {
    console.log('Transaction not found');
  }
}

main().catch(console.error);

Next Steps

Congratulations! You've completed the beginner tutorials! 🎉

Continue your learning journey with intermediate topics:

Beatoz Developer Portal