BuildAndSendInvokeTxn
Builds, signs, and sends an invoke transaction to execute contract functions.
Method Signature
func (account *Account) BuildAndSendInvokeTxn(
ctx context.Context,
functionCalls []rpc.InvokeFunctionCall,
opts *TxnOptions,
) (rpc.AddInvokeTransactionResponse, error)Parameters
ctx- Context for request cancellation and timeoutfunctionCalls- Slice of function calls to executeopts- Transaction options (fee, multiplier, etc.). Passnilfor defaults
Returns
rpc.AddInvokeTransactionResponse- Transaction response with hasherror- Error if transaction fails
Usage Example
Prerequisites: This example assumes you have a funded and deployed account instance. To learn how to create an account, see NewAccount.
package main
import (
"context"
"fmt"
"log"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/starknet.go/account"
"github.com/NethermindEth/starknet.go/rpc"
)
func main() {
// Assuming 'acc' is your Account instance (funded and deployed)
var acc *account.Account
// acc = ... (already initialized)
// Example: Transfer 1 wei ETH to ourselves
// ETH token contract on Starknet Sepolia
ethContract, _ := new(felt.Felt).SetString("0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7")
// Get account address as recipient
recipient := acc.Address
functionCall := rpc.InvokeFunctionCall{
ContractAddress: ethContract,
FunctionName: "transfer",
CallData: []*felt.Felt{
recipient, // recipient
new(felt.Felt).SetUint64(1), // amount (u256 low)
new(felt.Felt).SetUint64(0), // amount (u256 high)
},
}
// Send transaction with default options
ctx := context.Background()
response, err := acc.BuildAndSendInvokeTxn(ctx, []rpc.InvokeFunctionCall{functionCall}, nil)
if err != nil {
log.Fatalf("Error sending transaction: %v", err)
}
fmt.Printf("✅ Transaction sent!\n")
fmt.Printf("Transaction Hash: %s\n", response.Hash)
}Expected Output
✅ Transaction sent!
Transaction Hash: 0x637e8d80050fd18192c250b3cac4c84ed0b19234cefe5f952d8e73666e50b13Transaction Options
Configure transaction behavior with TxnOptions:
opts := &account.TxnOptions{
Multiplier: 1.5, // 50% safety margin for fees
UseLatest: false, // Use pre_confirmed block for estimation
UseQueryBit: false, // Use normal transaction version
SimulationFlag: rpc.SkipValidate, // Skip validation during simulation
}
response, err := acc.BuildAndSendInvokeTxn(context.Background(), functionCalls, opts)Description
BuildAndSendInvokeTxn handles the complete flow of executing a transaction:
- Fetches current nonce
- Formats calldata based on Cairo version
- Creates transaction with zero fees
- Signs transaction for fee estimation
- Estimates fees
- Updates transaction with estimated fees (applying multiplier)
- Signs transaction again with final fees
- Sends transaction to network
All steps are handled automatically.
Error Handling
response, err := acc.BuildAndSendInvokeTxn(ctx, functionCalls, opts)
if err != nil {
// Handle errors like:
// - Insufficient balance
// - Invalid function calls
// - Network errors
// - Fee estimation failure
return err
}Related Methods
- SignInvokeTransaction - Sign invoke transactions
- SendTransaction - Send pre-built transactions
- WaitForTransactionReceipt - Wait for confirmation
- FmtCalldata - Format calldata for invoke transactions

