Skip to content

Class

Retrieves the contract class definition associated with the given class hash.

Method Signature

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

Parameters

  • ctx - Context for request cancellation and timeout
  • blockID - The block identifier (see BlockID helper functions)
  • classHash - The hash of the class to retrieve

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 class hash on Sepolia
	classHash, err := utils.HexToFelt("0x046ded64ae2dead6448e247234bab192a9c483644395b66f2155f2614e5804b0")
	if err != nil {
		log.Fatal("Failed to parse class hash:", err)
	}
 
	// Get class information
	class, err := client.Class(ctx, rpc.WithBlockTag("latest"), classHash)
	if err != nil {
		log.Fatal("Failed to get class:", err)
	}
 
	// Print result (first 500 characters)
	classJSON, _ := json.MarshalIndent(class, "", "  ")
	fmt.Printf("Class info (first 500 chars):\n%s\n", string(classJSON)[:500])
}

Expected Output

Class info (first 500 chars):
{
  "sierra_program": [
    "0x1",
    "0x4",
    "0x0",
    "0x2",
    "0x4",
    "0x1",
    "0x367",
    "0x99",
    "0x59",
    "0x52616e6765436865636b",
    "0x800000000000000100000000000000000000000000000000",
    "0x75313238",
    "0x800000000000000700000000000000000000000000000000",
    "0x537472756374",
    "0x800000000000000f00000000000000000000000000000001",
    "0x0",
    "0x2ee1e2b1b89f8c495f200e4956278a4d47395fe262f27b52e5865c9524c08c3",
    "0x456e756d",
    "0x80000000000000070000

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.Class(ctx, rpc.WithBlockTag("latest"), classHash)
if err != nil {
	// Handle errors like class not found, invalid block ID, etc.
	return err
}

Related Methods

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