Class
Retrieves the contract class definition associated with the given class hash at a specific block. This returns the complete class definition including the Sierra program, entry points, and ABI, which is essential for understanding contract functionality and interacting with contracts.
Method Signature
func (provider *Provider) Class(
ctx context.Context,
blockID BlockID,
classHash *felt.Felt,
) (ClassOutput, error)Source: contract.go
Parameters
ctx(context.Context): Context for request cancellation and timeoutblockID(BlockID): Block identifier (number, hash, or tag)classHash(*felt.Felt): The hash of the contract class to retrieve
Returns
ClassOutput: The contract class definition (interface type that can be a DeprecatedContractClass or ContractClass)error: Error if the request fails
Type Definitions
BlockID
The blockID parameter specifies which block state to query. You can identify a block in three ways:
type BlockID struct {
Number *uint64 `json:"block_number,omitempty"`
Hash *felt.Felt `json:"block_hash,omitempty"`
Tag BlockTag `json:"tag,omitempty"`
}Source: types_block.go
There are helper functions for creating BlockID. So Instead of manually creating the BlockID struct, use these convenience functions.
-
By Block Number - Get a specific block by its height
blockID := rpc.WithBlockNumber(123456)Source: block.go
-
By Block Hash - Get a specific block by its hash
hash, _ := new(felt.Felt).SetString("0x1234...") blockID := rpc.WithBlockHash(hash)Source: block.go
-
By Block Tag - Get a dynamic block reference
blockID := rpc.WithBlockTag("latest") // Latest accepted block blockID := rpc.WithBlockTag("pending") // Block currently being builtSource: block.go
ClassOutput
type ClassOutput interface{}
var (
_ ClassOutput = (*contracts.DeprecatedContractClass)(nil)
_ ClassOutput = (*contracts.ContractClass)(nil)
)The ClassOutput can be either a DeprecatedContractClass (Cairo 0) or ContractClass (Cairo 1+).
Source: types_contract.go
Usage Example
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/NethermindEth/starknet.go/utils"
"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()
// ETH contract class hash on Sepolia
classHash, err := utils.HexToFelt("0x046ded64ae2dead6448e247234bab192a9c483644395b66f2155f2614e5804b0")
if err != nil {
log.Fatal(err)
}
// Get class definition
blockID := rpc.WithBlockTag("latest")
class, err := provider.Class(ctx, blockID, classHash)
if err != nil {
log.Fatal(err)
}
// Print class information (first 500 characters)
classJSON, _ := json.MarshalIndent(class, "", " ")
classStr := string(classJSON)
if len(classStr) > 500 {
classStr = classStr[:500] + "..."
}
fmt.Printf("Class definition:\n%s\n", classStr)
}Error Handling
class, err := provider.Class(ctx, blockID, classHash)
if err != nil {
if errors.Is(err, rpc.ErrClassHashNotFound) {
log.Printf("Class hash not found: %s", classHash)
return
}
if errors.Is(err, rpc.ErrBlockNotFound) {
log.Printf("Block not found")
return
}
log.Printf("Error retrieving class: %v", err)
return
}
fmt.Printf("Successfully retrieved class definition\n")Common Use Cases
- Contract Analysis - Retrieve and analyze contract class definitions for security auditing or verification. See example.
- ABI Extraction - Extract the ABI from a class hash to interact with contracts. See example.
- Contract Verification - Verify that deployed contracts match expected class definitions. See example.
Related Methods
- ClassAt - Get class definition at a specific contract address
- ClassHashAt - Get class hash at a specific contract address
- StorageAt - Get storage value at a contract address

