BlockTransactionCount
Gets the number of transactions in a specific block.
Method Signature
func (provider *Provider) BlockTransactionCount(ctx context.Context, blockID BlockID) (uint64, error)Parameters
ctx- Context for request cancellation and timeoutblockID- Block identifier (number, hash, or tag)
Returns
uint64- Number of transactions in the blockerror- Error if the request fails
BlockID Parameter
This method requires a BlockID parameter. The RPC package provides helper functions to create BlockID:
// Using block number
blockID := rpc.WithBlockNumber(123456)
// Using block hash
hash, _ := new(felt.Felt).SetString("0x1234...")
blockID := rpc.WithBlockHash(hash)
// Using block tag
blockID := rpc.WithBlockTag("latest")Usage Example
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/NethermindEth/starknet.go/rpc"
)
func main() {
// Create RPC client
// Get RPC URL from environment variable
rpcURL := os.Getenv("STARKNET_RPC_URL")
if rpcURL == "" {
log.Fatal("STARKNET_RPC_URL not set in environment")
}
ctx := context.Background()
client, err := rpc.NewProvider(ctx, rpcURL)
if err != nil {
log.Fatal("Failed to create client:", err)
}
// Get transaction count for latest block
blockID := rpc.WithBlockTag("latest")
txCount, err := client.BlockTransactionCount(ctx, blockID)
if err != nil {
log.Fatal("Failed to get transaction count:", err)
}
fmt.Printf("Latest block contains %d transactions\n", txCount)
}Expected Output
Latest block contains 11 transactionsAdvanced Examples
For more comprehensive examples including transaction volume analysis and block monitoring, see Block Analysis Examples.
Error Handling
txCount, err := client.BlockTransactionCount(ctx, blockID)
if err != nil {
log.Printf("Error getting transaction count: %v", err)
return
}
if txCount == 0 {
fmt.Printf("Block is empty (no transactions)\n")
} else {
fmt.Printf("Block contains %d transactions\n", txCount)
}
