Skip to content

ClassAt

Retrieves the contract class definition at a specific contract address.

Method Signature

func (provider *Provider) ClassAt(
	ctx context.Context,
	blockID BlockID,
	contractAddress *felt.Felt,
) (ClassOutput, error)

Parameters

  • ctx - Context for request cancellation and timeout
  • blockID - The block identifier (see BlockID helper functions)
  • contractAddress - The address of the contract

Returns

  • ClassOutput - The contract class definition (includes sierra_program, entry_points_by_type, and abi)
  • error - 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 block
  • WithBlockTag("pending") - Pending block
  • WithBlockNumber(n) - Specific block number
  • WithBlockHash(hash) - Specific block hash

Usage Example

package main
 
import (
	"context"
	"encoding/json"
	"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 at contract address
	class, err := client.ClassAt(ctx, rpc.WithBlockTag("latest"), contractAddress)
	if err != nil {
		log.Fatal("Failed to get class at:", err)
	}
 
	// Print result (first 500 characters)
	classJSON, _ := json.MarshalIndent(class, "", "  ")
	fmt.Printf("Class at contract (first 500 chars):\n%s\n", string(classJSON)[:500])
}

Expected Output

Class at contract (first 500 chars):
{
  "sierra_program": [
    "0x1",
    "0x7",
    "0x0",
    "0x2",
    "0xa",
    "0x1",
    "0x6a4",
    "0x15c",
    "0xc0",
    "0x52616e6765436865636b",
    "0x800000000000000100000000000000000000000000000000",
    "0x66656c74323532",
    "0x800000000000000700000000000000000000000000000000",
    "0x537472756374",
    "0x800000000000000700000000000000000000000000000002",
    "0x0",
    "0x3ae3c0242bd1c83caced6e5a82afedd0a39d6a01aa4f144085f91115f9678ee",
    "0x1",
    "0x436f6e7374",
    "0x

Note: The full output contains the complete sierra program, entry points, and ABI. Only the first 500 characters are shown here for brevity.

Error Handling

class, err := client.ClassAt(ctx, rpc.WithBlockTag("latest"), contractAddress)
if err != nil {
	// Handle errors like contract not found, invalid block ID, etc.
	return err
}

Related Methods

  • Class - Get class definition by class hash
  • ClassHashAt - Get class hash at a specific contract address