ChainID
Returns the chain ID for transaction replay protection.
Method Signature
func (provider *Provider) ChainID(ctx context.Context) (string, error)Parameters
ctx- Context for request cancellation and timeout
Returns
string- The chain ID (e.g., "SN_MAIN", "SN_SEPOLIA")error- Error if the request fails
Usage Example
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/NethermindEth/starknet.go/rpc"
)
func main() {
// Get RPC URL from environment variable
rpcURL := os.Getenv("STARKNET_RPC_URL")
if rpcURL == "" {
log.Fatal("STARKNET_RPC_URL not set in environment")
}
ctx := context.Background()
client, err := rpc.NewProvider(ctx, rpcURL)
if err != nil {
log.Fatal("Failed to create client:", err)
}
// Get chain ID
chainID, err := client.ChainID(ctx)
if err != nil {
log.Fatal("Failed to get chain ID:", err)
}
fmt.Printf("Chain ID: %s\n", chainID)
}Expected Output
Chain ID: SN_SEPOLIAError Handling
chainID, err := client.ChainID(ctx)
if err != nil {
// Handle error
return err
}Related Methods
- SpecVersion - Get RPC specification version
- Syncing - Get node sync status

