starknet_estimateMessageFee - Estimate L1-to-L2 Message Fee
Estimate the L2 fee for processing a message sent from Ethereum L1 to Starknet L2. Essential for cross-layer communication and bridge integrations.
Estimates the L2 fee required to process a message sent from Ethereum (L1) to Starknet (L2). This method simulates the l1_handler transaction that would be triggered by the L1 message and returns the estimated gas and fee breakdown.
Overview
Starknet's L1-to-L2 messaging allows Ethereum smart contracts to send messages to Starknet contracts. When a message is sent from L1, the Starknet sequencer creates an l1_handler transaction to execute the corresponding function on the L2 contract. This method estimates the resources and fees required for that L2 execution.
This is important for bridge operators, cross-layer DeFi protocols, and any application that sends messages from Ethereum to Starknet, because the fee must be paid on L1 when the message is sent.
Important: The sample payload below is illustrative.
starknet_estimateMessageFeeonly succeeds when the message matches a reall1_handlerflow on the target contract. Reusing the literal values here on mainnet returnsEXPECTED_FROM_BRIDGE_ONLY, so replace them with a bridge-generated message from your integration before relying on the success example.
How L1-to-L2 Messaging Works
- An Ethereum contract calls the Starknet Core contract's
sendMessageToL2function - The message includes the target L2 contract, entry point selector, and payload
- The Starknet sequencer picks up the message and creates an
l1_handlertransaction - The L2 contract's handler function executes with the provided payload
- The fee for L2 execution must be estimated and paid upfront on L1
Illustrative Payload Error Example
{
"jsonrpc": "2.0",
"id": 1,
"error": {
"code": 40,
"message": "Contract error",
"data": {
"revert_error": {
"error": {
"error": "0x45585045435445445f46524f4d5f4252494447455f4f4e4c59 ('EXPECTED_FROM_BRIDGE_ONLY')"
}
}
}
}
}When you replace the illustrative payload with a real bridge-handler message, the method returns the standard fee object described in the table above. Keep the selector and payload aligned with the target contract's l1_handler ABI or Starknet rejects the estimate before execution.
Errors
| Code | Message | Description |
|---|---|---|
| 40 | Contract error | The L2 contract execution failed during simulation |
| 24 | Block not found | The specified block for estimation context does not exist |
Examples
Use Cases
- Bridge Operations - Estimate the L2 processing fee before sending tokens from Ethereum to Starknet
- Cross-Layer DeFi - Calculate costs for L1-initiated actions on L2 protocols
- Fee Budgeting - Pre-calculate message fees with safety margins to avoid underpayment
- User Interfaces - Display estimated bridging costs to users before they confirm L1 transactions
- Protocol Development - Test and optimize
l1_handlerfunctions for gas efficiency
Related Methods
- starknet_estimateFee - Estimate fee for L2 transactions
- starknet_getMessagesStatus - Check L1-to-L2 message processing status
- starknet_getTransactionReceipt - Get receipt for the resulting l1_handler transaction
- starknet_call - Read-only contract call for testing handler logic
starknet_estimateFee - Estimate Transaction...
Estimate fees for Starknet transactions before execution.
starknet_getBlockTransactionCount - Get Block Transaction Count
Get the number of transactions in a Starknet block by block ID. Useful for block analysis, throughput monitoring, and indexing pipelines.