Skip to content

Syncing

Gets the synchronization status of the node. This method returns whether the node is currently syncing with the network and provides progress information including the starting block, current block, and highest block known to the node.

Method Signature

func (provider *Provider) Syncing(ctx context.Context) (SyncStatus, error)

Source: chain.go

Parameters

  • ctx (context.Context): Context for request cancellation and timeout

Returns

  • SyncStatus: Synchronization status containing sync progress information
  • error: Error if the request fails

Type Definitions

SyncStatus

type SyncStatus struct {
    // A boolean indicating whether the node is syncing. If false, all other fields are empty.
    IsSyncing bool
 
    // All these fields are only present if IsSyncing is true.
    StartingBlockHash *felt.Felt `json:"starting_block_hash,omitempty"`
    StartingBlockNum  uint64     `json:"starting_block_num,omitempty"`
    CurrentBlockHash  *felt.Felt `json:"current_block_hash,omitempty"`
    CurrentBlockNum   uint64     `json:"current_block_num,omitempty"`
    HighestBlockHash  *felt.Felt `json:"highest_block_hash,omitempty"`
    HighestBlockNum   uint64     `json:"highest_block_num,omitempty"`
}

Source: types.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)
    }
 
    ctx := context.Background()
 
    // Get synchronization status
    syncStatus, err := provider.Syncing(ctx)
    if err != nil {
        log.Fatal(err)
    }
 
    // Check if node is syncing
    if syncStatus.IsSyncing {
        fmt.Printf("Node is syncing:\n")
        fmt.Printf("  Starting Block: %d (Hash: %s)\n",
            syncStatus.StartingBlockNum, syncStatus.StartingBlockHash)
        fmt.Printf("  Current Block: %d (Hash: %s)\n",
            syncStatus.CurrentBlockNum, syncStatus.CurrentBlockHash)
        fmt.Printf("  Highest Block: %d (Hash: %s)\n",
            syncStatus.HighestBlockNum, syncStatus.HighestBlockHash)
 
        // Calculate sync progress
        if syncStatus.HighestBlockNum > syncStatus.StartingBlockNum {
            progress := float64(syncStatus.CurrentBlockNum-syncStatus.StartingBlockNum) /
                       float64(syncStatus.HighestBlockNum-syncStatus.StartingBlockNum) * 100
            fmt.Printf("  Progress: %.2f%%\n", progress)
        }
    } else {
        fmt.Println("Node is fully synchronized")
    }
}

Error Handling

syncStatus, err := provider.Syncing(ctx)
if err != nil {
    log.Printf("Failed to get sync status: %v", err)
    return
}
 
if syncStatus.IsSyncing {
    log.Printf("Warning: Node is still syncing (at block %d/%d)",
        syncStatus.CurrentBlockNum, syncStatus.HighestBlockNum)
} else {
    log.Println("Node is ready - fully synchronized")
}

Common Use Cases

  • Checkin if your node is fully synchronized before serving requests.
  • Monitoring synchronization progress and estimate completion time.
  • Waiting for node to finish syncing before starting your application.