Skip to content

BlockWithTxs

Gets a block with all its transactions included as full transaction objects. Unlike BlockWithTxHashes which only returns transaction hashes, this method returns complete transaction details including calldata, signatures, resource bounds, and all transaction-specific fields. The method can return either a confirmed block (Block) or a pre-confirmed block (PreConfirmedBlock) depending on the block status. This can return ErrBlockNotFound if the specified block doesn't exist.

Method Signature

func (provider *Provider) BlockWithTxs(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 *Block or *PreConfirmedBlock)
  • 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

Block

type Block struct {
    BlockHeader
    Status BlockStatus `json:"status"`
    Transactions []BlockTransaction `json:"transactions"`
}

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

Error Handling

block, err := provider.BlockWithTxs(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\n")

Common Use Cases

  • Analyzing all transactions in a block with full details. See example.
  • Displaying complete block information including all transaction data. See example.
  • Fetching historical blocks with all transaction details for analysis or archival purposes.
  • Verifying transaction inclusion and details within a specific block.