OP Stack
Deposit Flow

Deposit Flow

This guide explains the deposit flow process for L2 deposit transactions, triggered by transactions or events on L1. In Optimism terminology, "deposit transaction" refers to any L2 transaction that is triggered by a transaction or event on L1.

The process is somewhat similar to the way most networking stacks work (opens in a new tab). Information is encapsulated in lower layer packets on the sending side and then retrieved and used by those layers on the receiving side while going up the stack to the receiving application.

Deposit Flow Diagram.

L1 Processing

  1. An L1 entity, either a smart contract or an externally owned account (EOA), sends a deposit transaction to L1CrossDomainMessenger (opens in a new tab), using sendMessage (opens in a new tab). This function accepts three parameters:

    • _target, target address on L2.
    • _message, the L2 transaction's calldata, formatted as per the ABI (opens in a new tab) of the target account.
    • _minGasLimit, the minimum gas limit allowed for the transaction on L2. Note that this is a minimum and the actual amount provided on L2 may be higher (but never lower) than the specified gas limit. Note that the actual amount provided on L2 will be higher, because the portal contract on L2 needs to do some processing before submitting the call to _target.
  2. The L1 cross domain messenger calls its own _send function (opens in a new tab). It uses these parameters:

  3. _sendMessage (opens in a new tab) calls the portal's depositTransaction function (opens in a new tab).

    Note that other contracts can also call depositTransaction (opens in a new tab) directly. However, doing so bypasses certain safeguards, so in most cases it's a bad idea.

  4. The depositTransaction function (opens in a new tab) runs a few sanity checks, and then emits a TransactionDeposited (opens in a new tab) event.

L2 Processing

  1. The op-node component looks for TransactionDeposited events on L1 (opens in a new tab). If it sees any such events, it parses (opens in a new tab) them.

  2. Next, op-node converts (opens in a new tab) those TransactionDeposited events into deposit transactions (opens in a new tab).

  3. In most cases user deposit transactions call the relayMessage (opens in a new tab) function of L2CrossDomainMessenger (opens in a new tab).

  4. relayMessage runs a few sanity checks and then, if everything is good, calls the real target contract with the relayed calldata (opens in a new tab).

Denial of service (DoS) prevention

As with all other L1 transactions, the L1 costs of a deposit are borne by the transaction's originator. However, the L2 processing of the transaction is performed by the Optimism nodes. If there were no cost attached, an attacker could be able to submit a transaction that had high costs of run on L2, and that way perform a denial of service attack.

To avoid this DoS vector, depositTransaction (opens in a new tab), and the functions that call it, require a gas limit parameter. This gas limit is encoded into the []TransactionDeposited event (opens in a new tab), and used as the gas limit for the user deposit transaction on L2.

This L2 gas is paid for by burning L1 gas here (opens in a new tab).

Replaying messages

Deposits transactions can fail due to several reasons:

  • Not enough gas provided.
  • The state on L2 does not allow the transaction to be successful.

It is possible to replay a failed deposit, possibly with more gas,

Replays in action

To see how replays work, you can use this contract on OP Sepolia (opens in a new tab).

  1. Call stopChanges, using this Foundry command:

    PRIV_KEY=<your private key here>
    export ETH_RPC_URL=https://sepolia.optimism.io
    cast send --private-key $PRIV_KEY $GREETER "stopChanges()"
  2. Verify that getStatus() returns false, meaning changes are not allowed, and see the value of greet() using Foundry. Note that Foundry returns false as zero.

    cast call $GREETER "greet()" | cast --to-ascii ; cast call $GREETER "getStatus()"
  3. Get the calldata. You can use this Foundry command:

    cast calldata "setGreeting(string)" "testing"

    Or just use this value:

  4. Send a greeting change as a deposit. Use these commands:

    CALLDATA=`cast calldata "setGreeting(string)" "testing"`
    cast send --rpc-url $L1_RPC --private-key $PRIV_KEY $L1XDM_ADDRESS $FUNC $GREETER $CALLDATA 10000000

    The transaction will be successful on L1, but then emit a fail event on L2.

  5. The next step is to find the hash of the failed relay. The easiest way to do this is to look in the internal transactions of the destination contract (opens in a new tab), and select the latest one that appears as a failure. It should be a call to L2CrossDomainMessenger at address 0x420...007. This is the call you need to replay.

    If the latest internal transaction is a success, it probably means your transaction hasn't relayed yet. Wait until it is, that may take a few minutes.

  6. Get the transaction information using Foundry.

    TX_HASH=<transaction hash from Etherscan>
    REPLAY_DATA=`cast tx $TX_HASH input`
  7. Call startChanges() to allow changes using this Foundry command:

    cast send --private-key $PRIV_KEY $GREETER "startChanges()"

    Don't do this prematurely

    If you call startChanges() too early, it will happen when the message is relayed to L2, and then the initial deposit will be successful and there will be no need to replay it.

  8. Verify that getStatus() returns true, meaning changes are not allowed, and see the value of greet(). Foundry returns true as one.

    cast call $GREETER "greet()" | cast --to-ascii ; cast call $GREETER "getStatus()"
  9. Now send the replay transaction.

    cast send --private-key $PRIV_KEY --gas-limit 10000000 $L2XDM_ADDRESS $REPLAY_DATA 

    Why do we need to specify the gas limit?

    The gas estimation mechanism tries to find the minimum gas limit at which the transaction would be successful. However, L2CrossDomainMessenger does not revert when a replay fails due to low gas limit, it just emits a failure message. The gas estimation mechanism considers that a success.

    To get a gas estimate, you can use this command:

    cast estimate --from 0x0000000000000000000000000000000000000001 $L2XDM_ADDRESS $REPLAY_DATA

    That address is a special case in which the contract does revert.

  10. Verify the greeting has changed:

    cast call $GREETER "greet()" | cast --to-ascii ; cast call $GREETER "getStatus()"


To debug deposit transactions you can ask the L2 cross domain messenger for the state of the transaction.

  1. Look on Etherscan to see the FailedRelayedMessage event. Set MSG_HASH to that value.

  2. To check if the message is listed as failed, run this:

    cast call $L2XDM_ADDRESS "failedMessages(bytes32)" $MSG_HASH

    To check if it is listed as successful, run this:

    cast call $L2XDM_ADDRESS "successfulMessages(bytes32)" $MSG_HASH