Skip to content

Declare Transaction Example

This example demonstrates how to declare a new contract class on StarkNet. Declaring a contract is the process of registering a contract class on the network before it can be deployed.

Prerequisites

  • Go 1.18 or higher
  • Starknet.go installed
  • A Starknet node URL
  • Compiled Cairo contract
  • Account with sufficient funds

Code Example

package main
 
import (
    "context"
    "fmt"
    "os"
    "github.com/NethermindEth/starknet.go"
)
 
func main() {
    // Initialize client
    client, err := starknet.NewClient("YOUR_NODE_URL")
    if err != nil {
        panic(err)
    }
 
    // Create account
    account, err := client.NewAccount("YOUR_PRIVATE_KEY")
    if err != nil {
        panic(err)
    }
 
    // Read compiled contract
    contractBytes, err := os.ReadFile("path/to/compiled/contract.json")
    if err != nil {
        panic(err)
    }
 
    // Declare contract
    tx, err := account.Declare(context.Background(), contractBytes)
    if err != nil {
        panic(err)
    }
 
    fmt.Printf("Contract declared with transaction hash: %s\n", tx.Hash)
}

Explanation

  1. Initialize the Starknet client
  2. Create an account instance
  3. Read the compiled contract file
  4. Declare the contract class
  5. The transaction hash is returned upon successful declaration

Best Practices

  • Verify contract compilation
  • Use proper error handling
  • Consider gas estimation
  • Test on testnet first
  • Keep track of declared class hashes
  • Verify declaration success

Common Issues

  • Invalid contract bytecode
  • Insufficient funds
  • Contract size limits
  • Network congestion
  • Compilation errors
  • Class already declared