Skip to content

BlockWithReceipts

Gets a block with both full transaction details and their corresponding receipts. This is the most comprehensive block query method, providing complete information about transactions and their execution results including gas usage, events emitted, execution status, and fees paid. Each transaction is paired with its receipt, making this ideal for analyzing transaction outcomes.

Method Signature

func (provider *Provider) BlockWithReceipts(
    ctx context.Context,
    blockID BlockID,
) (interface{}, 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

  • interface{}: The retrieved block (either *BlockWithReceipts or *PreConfirmedBlockWithReceipts)
  • 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

BlockWithReceipts

type BlockWithReceipts struct {
    BlockHeader
    Status BlockStatus `json:"status"`
    BlockBodyWithReceipts
}

Source: types_block.go

Usage Example

package main
 
import (
    "context"
    "encoding/json"
    "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 latest block with receipts
    blockID := rpc.WithBlockTag("latest")
    block, err := provider.BlockWithReceipts(context.Background(), blockID)
    if err != nil {
        log.Fatal(err)
    }
 
    // Pretty print the result
    blockJSON, _ := json.MarshalIndent(block, "", "  ")
    fmt.Printf("Block with receipts:\n%s\n", blockJSON)
}

Error Handling

block, err := provider.BlockWithReceipts(ctx, blockID)
if err != nil {
    switch {
    case errors.Is(err, rpc.ErrBlockNotFound):
        log.Printf("Block not found")
    default:
        log.Printf("Failed to get block: %v", err)
    }
    return
}
 
fmt.Printf("Successfully retrieved block with transactions and receipts\n")

Common Use Cases

  • Analyzing complete transaction execution including gas costs and events. See example.
  • Analyzing gas usage patterns and costs across all transactions in a block.
  • Verifying transaction execution status and results for auditing purposes.