EstimateFee
Estimates the resources required by transactions when applied on a given state.
Method Signature
func (provider *Provider) EstimateFee(
ctx context.Context,
requests []BroadcastTxn,
simulationFlags []SimulationFlag,
blockID BlockID,
) ([]FeeEstimation, error)Parameters
ctx- Context for request cancellation and timeoutrequests- Sequence of transactions to estimatesimulationFlags- Flags describing transaction execution (e.g.,SKIP_VALIDATE)blockID- The block identifier (see BlockID helper functions)
Returns
[]FeeEstimation- Fee estimation for each transaction (includes gas consumed and prices)error- Error if the request fails
BlockID Parameter
The blockID parameter specifies which block state to use for estimation. See BlockID helper functions for available options:
WithBlockTag("latest")- Latest blockWithBlockTag("pending")- Pending blockWithBlockNumber(n)- Specific block numberWithBlockHash(hash)- Specific block hash
Usage Example
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/NethermindEth/starknet.go/utils"
)
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)
}
// Account address
senderAddress, err := utils.HexToFelt("0x36d67ab362562a97f9fba8a1051cf8e37ff1a1449530fb9f1f0e32ac2da7d06")
if err != nil {
log.Fatal("Failed to parse sender:", err)
}
// Get current nonce
nonce, err := client.Nonce(ctx, rpc.WithBlockTag("latest"), senderAddress)
if err != nil {
log.Fatal("Failed to get nonce:", err)
}
fmt.Printf("Current nonce: %s\n", nonce)
// Build transaction JSON with current nonce
txData := fmt.Sprintf(`{
"type": "INVOKE",
"version": "0x3",
"nonce": "%s",
"sender_address": "0x36d67ab362562a97f9fba8a1051cf8e37ff1a1449530fb9f1f0e32ac2da7d06",
"signature": [
"0x33a831e9428920f71c1df9248d4dbf9101fb5ee2bd100c0ad0d10c94c28dfe3",
"0x3fa865114ae29b2a49469401e11eb0db953a7d854916512c2ed400320405c8a"
],
"calldata": [
"0x1",
"0x669e24364ce0ae7ec2864fb03eedbe60cfbc9d1c74438d10fa4b86552907d54",
"0x2f0b3c5710379609eb5495f1ecd348cb28167711b73609fe565a72734550354",
"0x2",
"0xffffffff",
"0x0"
],
"resource_bounds": {
"l1_data_gas": {
"max_amount": "0x1e0",
"max_price_per_unit": "0x922"
},
"l1_gas": {
"max_amount": "0x0",
"max_price_per_unit": "0xfbfdefe2186"
},
"l2_gas": {
"max_amount": "0x16eea0",
"max_price_per_unit": "0x1830e58f7"
}
},
"tip": "0x0",
"paymaster_data": [],
"account_deployment_data": [],
"nonce_data_availability_mode": "L1",
"fee_data_availability_mode": "L1"
}`, nonce)
var invokeTx rpc.BroadcastInvokeTxnV3
if err := json.Unmarshal([]byte(txData), &invokeTx); err != nil {
log.Fatal(err)
}
result, err := client.EstimateFee(
context.Background(),
[]rpc.BroadcastTxn{invokeTx},
[]rpc.SimulationFlag{rpc.SKIP_VALIDATE},
rpc.WithBlockTag("latest"),
)
if err != nil {
log.Fatal("Failed to estimate fee:", err)
}
resultJSON, _ := json.MarshalIndent(result, "", " ")
fmt.Printf("Fee estimate:\n%s\n", resultJSON)
}Expected Output
Current nonce: 0x57
Fee estimate:
[
{
"l1_gas_consumed": "0x0",
"l1_gas_price": "0x1bdb18dc85e8",
"l2_gas_consumed": "0xf49c0",
"l2_gas_price": "0xb2d05e00",
"l1_data_gas_consumed": "0x140",
"l1_data_gas_price": "0x77a3",
"overall_fee": "0xaadb909aa0bc0",
"unit": "FRI"
}
]Error Handling
result, err := client.EstimateFee(ctx, []rpc.BroadcastTxn{invokeTx}, []rpc.SimulationFlag{rpc.SKIP_VALIDATE}, rpc.WithBlockTag("latest"))
if err != nil {
// Handle errors like invalid transaction, execution errors, etc.
return err
}Related Methods
- Nonce - Get current nonce for fee estimation
- EstimateMessageFee - Estimate L1->L2 message fees
- SimulateTransactions - Simulate transaction execution

