ClassHashAt
Retrieves the class hash at a specific contract address.
Method Signature
func (provider *Provider) ClassHashAt(
ctx context.Context,
blockID BlockID,
contractAddress *felt.Felt,
) (*felt.Felt, error)Parameters
ctx- Context for request cancellation and timeoutblockID- The block identifier (see BlockID helper functions)contractAddress- The address of the contract
Returns
*felt.Felt- The class hasherror- Error if the request fails
BlockID Parameter
The blockID parameter specifies which block state to query. See BlockID helper functions for available options:
WithBlockTag("latest")- Latest blockWithBlockTag("pending")- Pending blockWithBlockNumber(n)- Specific block numberWithBlockHash(hash)- Specific block hash
Usage Example
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/NethermindEth/starknet.go/utils"
)
func main() {
// Create RPC client
// 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)
}
// ETH contract address on Sepolia
contractAddress, err := utils.HexToFelt("0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7")
if err != nil {
log.Fatal("Failed to parse contract address:", err)
}
// Get class hash at contract address
classHash, err := client.ClassHashAt(ctx, rpc.WithBlockTag("latest"), contractAddress)
if err != nil {
log.Fatal("Failed to get class hash at:", err)
}
fmt.Printf("Class hash at contract: %s\n", classHash)
}Expected Output
Class hash at contract: 0x76791ef97c042f81fbf352ad95f39a22554ee8d7927b2ce3c681f3418b5206aError Handling
classHash, err := client.ClassHashAt(ctx, rpc.WithBlockTag("latest"), contractAddress)
if err != nil {
// Handle errors like contract not found, invalid block ID, etc.
return err
}
