Skip to content

BlockTransactionCount

Gets the number of transactions in a specific block without fetching the full transaction data. This is a lightweight method ideal for quickly checking block activity or monitoring transaction volume over time. The method returns a simple count, making it much more efficient than fetching full block data when you only need to know how many transactions are present.

Method Signature

func (provider *Provider) BlockTransactionCount(
    ctx context.Context,
    blockID BlockID,
) (uint64, error)

Source: block.go

Parameters

  • ctx (context.Context): Context for request cancellation and timeout
  • blockID (BlockID): Block identifier (number, hash, or tag like "latest" or "pending")

Returns

  • uint64: The number of transactions in the block
  • error: Error if the request fails

Type Definitions

BlockID

The blockID parameter specifies which block to retrieve. You can identify a block in three ways:

type BlockID struct {
    Number *uint64    `json:"block_number,omitempty"`
    Hash   *felt.Felt `json:"block_hash,omitempty"`
    Tag    BlockTag   `json:"tag,omitempty"`
}

Source: types_block.go

There are helper functions for creating BlockID. So Instead of manually creating the BlockID struct, use these convenience functions.

  • By Block Number - Get a specific block by its height

    blockID := rpc.WithBlockNumber(123456)

    Source: block.go

  • By Block Hash - Get a specific block by its hash

    hash, _ := new(felt.Felt).SetString("0x1234...")
    blockID := rpc.WithBlockHash(hash)

    Source: block.go

  • By Block Tag - Get a dynamic block reference

    blockID := rpc.WithBlockTag("latest")      // Latest accepted block
    blockID := rpc.WithBlockTag("pending")     // Block currently being built

    Source: block.go

Usage Example

package main
 
import (
    "context"
    "fmt"
    "log"
    "os"
 
    "github.com/NethermindEth/starknet.go/rpc"
    "github.com/joho/godotenv"
)
 
func main() {
    // Load environment variables from .env file
    err := godotenv.Load()
    if err != nil {
        log.Fatal("Error loading .env file")
    }
 
    // Get RPC URL from environment variable
    rpcURL := os.Getenv("STARKNET_RPC_URL")
    if rpcURL == "" {
        log.Fatal("STARKNET_RPC_URL not found in .env file")
    }
 
    // Initialize provider
    provider, err := rpc.NewProvider(context.Background(), rpcURL)
    if err != nil {
        log.Fatal(err)
    }
 
    // Get transaction count for latest block
    blockID := rpc.WithBlockTag("latest")
    txCount, err := provider.BlockTransactionCount(context.Background(), blockID)
    if err != nil {
        log.Fatal(err)
    }
 
    fmt.Printf("Latest block contains %d transactions\n", txCount)
}

Error Handling

txCount, err := provider.BlockTransactionCount(ctx, blockID)
if err != nil {
    switch {
    case errors.Is(err, rpc.ErrBlockNotFound):
        log.Printf("Block not found")
    default:
        log.Printf("Failed to get 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)
}

Common Use Cases

  • Tracking network activity by monitoring transaction counts over time. See example.
  • Quickly identifying busy blocks without fetching full transaction data. See example.
  • Collecting data for analyzing network usage patterns and trends.
  • Identifying blocks with no transactions for network analysis.