AddDeclareTransaction
Submits a declare transaction to the network to register a new contract class.
Method Signature
func (provider *Provider) AddDeclareTransaction(
ctx context.Context,
declareTransaction *BroadcastDeclareTxnV3,
) (AddDeclareTransactionResponse, error)Parameters
ctx- Context for request cancellation and timeoutdeclareTransaction- The declare transaction to submit
Returns
AddDeclareTransactionResponse- Response containing transaction hash and class hasherror- Error if the submission fails
Usage Example
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/NethermindEth/juno/core/felt"
)
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)
}
accountAddr, _ := new(felt.Felt).SetString("0x0517c64b48079568a30f02967ade11d08f2dfdc45d2c0124650f013d07613802")
compiledClassHash, _ := new(felt.Felt).SetString("0x01234567890abcdef01234567890abcdef01234567890abcdef01234567890ab")
// Create a V3 declare transaction
declareTransaction := &rpc.BroadcastDeclareTxnV3{
Type: rpc.TransactionType("DECLARE"),
Version: rpc.TransactionVersion("0x3"),
SenderAddress: accountAddr,
CompiledClassHash: compiledClassHash,
Signature: []*felt.Felt{
new(felt.Felt).SetString("0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"),
new(felt.Felt).SetString("0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890"),
},
Nonce: new(felt.Felt).SetUint64(1),
ContractClass: &rpc.ContractClass{
// Sierra contract class definition
// This would include the full contract class JSON
},
ResourceBounds: &rpc.ResourceBoundsMapping{
L1Gas: rpc.ResourceBounds{
MaxAmount: rpc.U64("0x0"),
MaxPricePerUnit: rpc.U128("0x33a937098d80"),
},
L2Gas: rpc.ResourceBounds{
MaxAmount: rpc.U64("0x15c2a0"),
MaxPricePerUnit: rpc.U128("0x10c388d00"),
},
L1DataGas: rpc.ResourceBounds{
MaxAmount: rpc.U64("0xc0"),
MaxPricePerUnit: rpc.U128("0xdb31"),
},
},
Tip: rpc.U64("0x0"),
PayMasterData: []*felt.Felt{},
AccountDeploymentData: []*felt.Felt{},
NonceDataMode: rpc.DAModeL1,
FeeMode: rpc.DAModeL1,
}
// Submit the declare transaction
result, err := client.AddDeclareTransaction(ctx, declareTransaction)
if err != nil {
log.Fatal("Failed to add declare transaction:", err)
}
// Print the result
resultJSON, _ := json.MarshalIndent(result, "", " ")
fmt.Printf("Class declared:\n%s\n", resultJSON)
}Expected Output
{
"transaction_hash": "0x05a12345678901234567890123456789012345678901234567890123456789ab",
"class_hash": "0x01234567890abcdef01234567890abcdef01234567890abcdef01234567890ab"
}Error Handling
Common errors include invalid class definitions, class already declared, and insufficient balance for fees. Always verify the contract class before submission.
result, err := client.AddDeclareTransaction(ctx, declareTransaction)
if err != nil {
log.Printf("Failed to declare class: %v", err)
return
}
// Safe to use result.TransactionHash and result.ClassHashRelated Methods
- Class - Retrieve a declared contract class
- EstimateFee - Estimate fees for the declare transaction
- TransactionReceipt - Check declaration status
- SimulateTransactions - Test declaration before submitting

