get_transaction_by_hash
Retrieve detailed transaction information by specifying its hash using the Cryo MCP Server. Query Ethereum blockchain data for precise insights into transaction details.
Instructions
Get detailed information about a transaction by its hash
Args:
tx_hash: The transaction hash to look up
Returns:
Detailed information about the transaction
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| tx_hash | Yes |
Input Schema (JSON Schema)
{
"properties": {
"tx_hash": {
"title": "Tx Hash",
"type": "string"
}
},
"required": [
"tx_hash"
],
"title": "get_transaction_by_hashArguments",
"type": "object"
}
Implementation Reference
- cryo_mcp/server.py:479-572 (handler)The main handler function for the 'get_transaction_by_hash' tool. It uses Ethereum JSON-RPC calls (eth_getTransactionByHash and eth_getTransactionReceipt) to fetch and combine transaction details including block info, gas usage, status, and EIP-1559 fields if applicable.@mcp.tool() def get_transaction_by_hash( tx_hash: str ) -> Dict[str, Any]: """ Get detailed information about a transaction by its hash Args: tx_hash: The transaction hash to look up Returns: Detailed information about the transaction """ # Ensure we have the RPC URL rpc_url = os.environ.get("ETH_RPC_URL", DEFAULT_RPC_URL) # Use RPC directly to get the transaction payload = { "jsonrpc": "2.0", "method": "eth_getTransactionByHash", "params": [tx_hash], "id": 1 } try: response = requests.post(rpc_url, json=payload) response_data = response.json() if 'result' in response_data and response_data['result']: tx_data = response_data['result'] # Get the receipt as well for additional information (gas used, status) receipt_payload = { "jsonrpc": "2.0", "method": "eth_getTransactionReceipt", "params": [tx_hash], "id": 2 } receipt_response = requests.post(rpc_url, json=receipt_payload) receipt_data = receipt_response.json() if 'result' in receipt_data and receipt_data['result']: receipt = receipt_data['result'] # Combine transaction and receipt data result = { "transaction_hash": tx_hash, "block_number": int(tx_data.get("blockNumber", "0x0"), 16), "block_hash": tx_data.get("blockHash"), "from_address": tx_data.get("from"), "to_address": tx_data.get("to"), "value": tx_data.get("value"), "value_decimal": int(tx_data.get("value", "0x0"), 16), "gas_limit": int(tx_data.get("gas", "0x0"), 16), "gas_price": int(tx_data.get("gasPrice", "0x0"), 16), "nonce": int(tx_data.get("nonce", "0x0"), 16), "input": tx_data.get("input"), "transaction_index": int(tx_data.get("transactionIndex", "0x0"), 16), "gas_used": int(receipt.get("gasUsed", "0x0"), 16), "status": int(receipt.get("status", "0x0"), 16), "logs_count": len(receipt.get("logs", [])), "contract_address": receipt.get("contractAddress") } # Handle EIP-1559 transactions if "maxFeePerGas" in tx_data: result["max_fee_per_gas"] = int(tx_data.get("maxFeePerGas", "0x0"), 16) result["max_priority_fee_per_gas"] = int(tx_data.get("maxPriorityFeePerGas", "0x0"), 16) result["transaction_type"] = int(tx_data.get("type", "0x0"), 16) return result else: # Return just the transaction data if receipt is not available return { "transaction_hash": tx_hash, "block_number": int(tx_data.get("blockNumber", "0x0"), 16), "block_hash": tx_data.get("blockHash"), "from_address": tx_data.get("from"), "to_address": tx_data.get("to"), "value": tx_data.get("value"), "value_decimal": int(tx_data.get("value", "0x0"), 16), "gas_limit": int(tx_data.get("gas", "0x0"), 16), "gas_price": int(tx_data.get("gasPrice", "0x0"), 16), "nonce": int(tx_data.get("nonce", "0x0"), 16), "input": tx_data.get("input"), "transaction_index": int(tx_data.get("transactionIndex", "0x0"), 16), "error": "Failed to retrieve transaction receipt" } else: return {"error": f"Transaction not found: {tx_hash}"} except Exception as e: return {"error": f"Exception when fetching transaction: {e}"}