Skip to content

TransactionByHash

Gets the complete details of a transaction by its hash. This method returns full transaction information including type, calldata, signatures, resource bounds, and all transaction-specific fields. This is essential for transaction verification, analysis, and displaying transaction details.

Method Signature

func (provider *Provider) TransactionByHash(
    ctx context.Context,
    hash *felt.Felt,
) (*BlockTransaction, error)

Source: transaction.go

Parameters

  • ctx (context.Context): Context for request cancellation and timeout
  • hash (*felt.Felt): The transaction hash to look up

Returns

  • *BlockTransaction: Complete transaction details
  • error: Error if the request fails

Usage Example

package main
 
import (
    "context"
    "encoding/json"
    "fmt"
    "log"
    "os"
 
    "github.com/NethermindEth/juno/core/felt"
    "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)
    }
 
    // Parse transaction hash
    txHashStr := "0x6dda0d2e2716227b87d912d654e1bc8b96441f043c29834e082413ae1320afa"
    txHash, err := new(felt.Felt).SetString(txHashStr)
    if err != nil {
        log.Fatal(err)
    }
 
    // Get transaction by hash
    transaction, err := provider.TransactionByHash(context.Background(), txHash)
    if err != nil {
        log.Fatal(err)
    }
 
    // Pretty print the result
    txJSON, _ := json.MarshalIndent(transaction, "", "  ")
    fmt.Printf("Transaction details:\n%s\n", txJSON)
}

Error Handling

transaction, err := provider.TransactionByHash(ctx, txHash)
if err != nil {
    switch {
    case errors.Is(err, rpc.ErrHashNotFound):
        log.Printf("Transaction not found")
    default:
        log.Printf("Failed to get transaction: %v", err)
    }
    return
}
 
fmt.Printf("Transaction type: %s\n", transaction.Type())

Common Use Cases

  • Verifying transaction details and status.
  • Analyzing transaction calldata, signatures, and resource usage.
  • Displaying complete transaction information to users.
  • Debuging failed transactions by examining their details.