ClassAt
Retrieves the contract class definition at a specific contract address and block. This is useful when you know the contract address but not the class hash, and you want to inspect the contract's code, ABI, and entry points. The method automatically resolves the class hash for the given address and returns the complete class definition.
Method Signature
func (provider *Provider) ClassAt(
ctx context.Context,
blockID BlockID,
contractAddress *felt.Felt,
) (ClassOutput, error)Source: contract.go
Parameters
ctx(context.Context): Context for request cancellation and timeoutblockID(BlockID): Block identifier (number, hash, or tag)contractAddress(*felt.Felt): The address of the deployed contract
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 address on Sepolia
contractAddress, err := utils.HexToFelt("0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7")
if err != nil {
log.Fatal(err)
}
// Get class definition at the contract address
blockID := rpc.WithBlockTag("latest")
class, err := provider.ClassAt(ctx, blockID, contractAddress)
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 at contract address:\n%s\n", classStr)
}Error Handling
class, err := provider.ClassAt(ctx, blockID, contractAddress)
if err != nil {
if errors.Is(err, rpc.ErrContractNotFound) {
log.Printf("Contract not found at address: %s", contractAddress)
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
- Inspecting deployed contracts to understand their functionality and available methods.
- Retrieving the ABI from a deployed contract address for interaction.
- Verifyin that a deployed contract matches expected implementation.

