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
- Initialize the Starknet client
- Create an account instance
- Read the compiled contract file
- Declare the contract class
- 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