1

I'm sending a message from an Ethereum contract to a Starknet one via the Starknet core contract's sendMessageToL2 function. This function is payable and I'm meant to send ETH to cover the cost of processing the message at the other end. What I don't know is how to estimate the amount of ETH required, which could vary from one call to another, as the processing required on the receiving Starknet contract will vary.

I have searched the docs, but didn't find anything on this. Until recently everything worked with no fee being sent, but that has been tightened up now.

1 Answers1

0

You can use Starknet's cli to call a function called estimate_message_fee. See it here. It will tell you how much you should attach to your message.

Under the hood, it is called Starknet's feeder gateway API with the same name "estimate_message_fee". You can also call this one directly. The call should be similar to estimate_fee of which you can find an example here

Happy building!

Henri
  • 86
  • 3
  • OK thanks, I think you are suggesting I do this before deploying my L1 contract so I have a fixed fee that my L1 contract sends every time it calls sendMessageToL2? If so, then doesn't that require that the processing performed by my Starknet contract in the L1Handler has a fairly predictable and unchanging gas cost? I don't see how to handle the case that the processing cost is only known at the time of sending the message. Is there any way to estimate the fee at the time of sending the message to L2 so that a different fee can be sent each time? – Alex Sumner Mar 27 '23 at 17:00
  • I meant that you should do this check before sending each individual transaction, actually. Your contract can have a function `triggerMessage() payable` that passes the necessary fee when called I think. – Henri Mar 28 '23 at 08:54
  • Thanks again! My scenario is as follows. I have an L1 contract with a function doSomething(...) that end users will call. doSomething(...) will call sendMessageToL2 with a payload that varies according to what the end user passed to doSomething(). doSomething could be payable or not, if not then my L1 contract covers the cost. My end user knows nothing about the Starknet CLI, they just know how to call doSomething on my L1 contract. Is there a way for the code in doSomething to estimate the required fee and send it, from the balance of the L1 contract, in the sendMessageToL2 call? – Alex Sumner Mar 28 '23 at 10:02
  • ...or is it necessary to call estimate_message_fee separately, before starting the transaction for doSomething()? – Alex Sumner Mar 28 '23 at 10:12
  • Hello, It is necessary indeed to call `estimate_message_fee` separately, off chain, before calling `doSomething`. Another option would be for your to create a system similar to how the messaging bridge works for L2 -> L1 communication. 1. Pass a message hash to L2, 2. Have this message be registered in a contract on L2, 3. Have automated watchers trigger an action, based on the message – Henri Mar 30 '23 at 10:35