Purpose
This skill enables Claude to help users explore blockchain data including blocks, transactions, and network information.
Quick Start
Get Latest Block
import { createPublicClient, http } from 'viem'
import { bsc } from 'viem/chains'
const client = createPublicClient({
chain: bsc,
transport: http()
})
const block = await client.getBlock()
console.log('Block Number:', block.number) console.log('Block Hash:', block.hash) console.log('Timestamp:', new Date(Number(block.timestamp) * 1000)) console.log('Transactions:', block.transactions.length) console.log('Gas Used:', block.gasUsed) console.log('Gas Limit:', block.gasLimit)
### Get Block by Number
```typescript
const block = await client.getBlock({
blockNumber: 12345678n
})
console.log('Block:', block)
Get Block by Hash
const block = await client.getBlock({
blockHash: '0x...'
})
console.log('Block:', block)
Get Transaction Details
const txHash = '0x...'
const tx = await client.getTransaction({
hash: txHash
})
console.log('From:', tx.from)
console.log('To:', tx.to)
console.log('Value:', formatEther(tx.value), 'BNB')
console.log('Gas Price:', formatGwei(tx.gasPrice), 'Gwei')
console.log('Nonce:', tx.nonce)
console.log('Block:', tx.blockNumber)
console.log('Input Data:', tx.input)
Get Transaction Receipt
const receipt = await client.getTransactionReceipt({
hash: txHash
})
console.log('Status:', receipt.status === 'success' ? 'Success' : 'Failed')
console.log('Block Number:', receipt.blockNumber)
console.log('Gas Used:', receipt.gasUsed)
console.log('Effective Gas Price:', receipt.effectiveGasPrice)
console.log('Logs:', receipt.logs.length)
MCP Tools
| Tool | Description |
|---|---|
get_block_by_hash |
Get block by its hash |
get_block_by_number |
Get block by its number |
get_latest_block |
Get the most recent block |
get_transaction |
Get transaction details by hash |
estimate_gas |
Estimate gas for a transaction |
get_chain_info |
Get chain information |
get_supported_networks |
List all supported networks |
Common Operations
Estimate Gas Cost
import { createPublicClient, http, parseEther, formatEther } from 'viem'
import { bsc } from 'viem/chains'
const client = createPublicClient({
chain: bsc,
transport: http()
})
// Estimate gas for a simple transfer
const gasEstimate = await client.estimateGas({
account: '0xSender...',
to: '0xRecipient...',
value: parseEther('1')
})
// Get current gas price
const gasPrice = await client.getGasPrice()
// Calculate total cost
const totalCost = gasEstimate * gasPrice
console.log('Gas Estimate:', gasEstimate)
console.log('Gas Price:', formatGwei(gasPrice), 'Gwei')
console.log('Total Cost:', formatEther(totalCost), 'BNB')
Get Chain Information
const chainId = await client.getChainId()
const blockNumber = await client.getBlockNumber()
const gasPrice = await client.getGasPrice()
console.log('Chain ID:', chainId)
console.log('Current Block:', blockNumber)
console.log('Gas Price:', formatGwei(gasPrice), 'Gwei')
Watch for New Blocks
const unwatch = client.watchBlocks({
onBlock: (block) => {
console.log('New block:', block.number)
console.log('Transactions:', block.transactions.length)
}
})
// Stop watching after some time
setTimeout(() => unwatch(), 60000)
Get Transaction Count (Nonce)
const nonce = await client.getTransactionCount({
address: '0x...'
})
console.log('Transaction count:', nonce)
Check Transaction Status
async function checkTxStatus(client, txHash) {
try {
const receipt = await client.getTransactionReceipt({ hash: txHash })
if (receipt) {
return {
confirmed: true,
success: receipt.status === 'success',
blockNumber: receipt.blockNumber,
gasUsed: receipt.gasUsed
}
}
} catch (error) {
// Transaction not found or pending
}
// Check if transaction exists but not confirmed
try {
const tx = await client.getTransaction({ hash: txHash })
return {
confirmed: false,
pending: true,
from: tx.from,
to: tx.to
}
} catch {
return { confirmed: false, pending: false, notFound: true }
}
}
Decode Transaction Logs
import { decodeEventLog, parseAbi } from 'viem'
const abi = parseAbi([
'event Transfer(address indexed from, address indexed to, uint256 value)'
])
const receipt = await client.getTransactionReceipt({ hash: txHash })
for (const log of receipt.logs) {
try {
const decoded = decodeEventLog({
abi,
data: log.data,
topics: log.topics
})
console.log('Event:', decoded.eventName)
console.log('Args:', decoded.args)
} catch {
// Not a Transfer event
}
}
Supported Networks
| Network | Chain ID | Explorer |
|---|---|---|
| Ethereum | 1 | etherscan.io |
| BNB Chain | 56 | bscscan.com |
| Polygon | 137 | polygonscan.com |
| Arbitrum | 42161 | arbiscan.io |
| Optimism | 10 | optimistic.etherscan.io |
| Base | 8453 | basescan.org |
Block Explorer Links
Generate Explorer URLs
function getExplorerUrl(chainId: number, type: 'tx' | 'address' | 'block', value: string): string {
const explorers = {
1: 'https://etherscan.io',
56: 'https://bscscan.com',
137: 'https://polygonscan.com',
42161: 'https://arbiscan.io',
10: 'https://optimistic.etherscan.io',
8453: 'https://basescan.org'
}
const base = explorers[chainId] || 'https://etherscan.io'
return `${base}/${type}/${value}`
}
// Usage
console.log(getExplorerUrl(56, 'tx', txHash))
console.log(getExplorerUrl(56, 'address', walletAddress))
console.log(getExplorerUrl(56, 'block', blockNumber))
Tips
- Block finality - Wait for multiple confirmations for important transactions
- Gas estimation - Add 10-20% buffer to gas estimates
- RPC rate limits - Use your own RPC endpoint for heavy queries
- Archive nodes - Required for historical data queries