EstimateMessageFee
Estimates the L2 fee of a message sent from Ethereum L1 to Starknet L2.
Method Signature
func (provider *Provider) EstimateMessageFee(
ctx context.Context,
msg MsgFromL1,
blockID BlockID,
) (MessageFeeEstimation, error)Parameters
ctx- Context for request cancellation and timeoutmsg- The L1->L2 message containing:FromAddress- The Ethereum L1 address (string)ToAddress- The Starknet L2 contract addressSelector- The L1 handler entry point selectorPayload- The message payload
blockID- The block identifier (see BlockID helper functions)
Returns
MessageFeeEstimation- Fee estimation for the L1->L2 messageerror- 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)
}
fromAddress := "0x8453fc6cd1bcfe8d4dfc069c400b433054d47bdc"
toAddress, _ := utils.HexToFelt("0x04c5772d1914fe6ce891b64eb35bf3522aeae1315647314aac58b01137607f3f")
selector, _ := utils.HexToFelt("0x1b64b1b3b690b43b9b514fb81377518f4039cd3e4f4914d8a6bdf01d679fb19")
payload, _ := utils.HexArrToFelt([]string{
"0x455448",
"0x2f14d277fc49e0e2d2967d019aea8d6bd9cb3998",
"0x02000e6213e24b84012b1f4b1cbd2d7a723fb06950aeab37bedb6f098c7e051a",
"0x01a055690d9db80000",
"0x00",
})
l1Handler := rpc.MsgFromL1{
FromAddress: fromAddress,
ToAddress: toAddress,
Selector: selector,
Payload: payload,
}
blockNumber := uint64(523066)
result, err := client.EstimateMessageFee(ctx, l1Handler, rpc.WithBlockNumber(blockNumber))
if err != nil {
log.Fatal(err)
}
resultJSON, _ := json.MarshalIndent(result, "", " ")
fmt.Printf("Estimate Message Fee: %s\n", resultJSON)
}Expected Output
Estimate Message Fee: {
"l1_gas_consumed": "0x4ed1",
"l1_gas_price": "0x7e15d2b5",
"l2_gas_consumed": "0x0",
"l2_gas_price": "0x0",
"l1_data_gas_consumed": "0x80",
"l1_data_gas_price": "0x1",
"overall_fee": "0x26d196042c45",
"unit": "WEI"
}Note: This method is specifically for L1->L2 messaging. The FromAddress is an Ethereum address (string), and the ToAddress must be a deployed Starknet contract that has an L1 handler.
Error Handling
result, err := client.EstimateMessageFee(ctx, l1Handler, rpc.WithBlockNumber(blockNumber))
if err != nil {
// Handle errors like contract not found, invalid message, etc.
return err
}Related Methods
- EstimateFee - Estimate L2 transaction fees
- GetMessagesStatus - Get status of L1->L2 messages

