Deposit Flow

# Introduction

In Optimism terminology, "deposit transaction" refers to any L2 transaction that is triggered by a transaction or event on L1. A deposit transaction may or may not have assets (ETH, tokens, etc.) attached to it.

The process is somewhat similar to the way most networking stacks work (opens new window). 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.

Overall process

# L1 Processing

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

    • _target, target address on L2.
    • _message, the L2 transaction's calldata, formatted as per the ABI (opens new window) 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.

    You can see code that implements this call in the tutorial (opens new window).

  2. The L1 cross domain messenger calls its own _send function (opens new window). It uses these parameters:

  3. _sendMessage (opens new window) calls the portal's depositTransaction function (opens new window).

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

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

# L2 Processing

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

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

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

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

# 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 new window), and the functions that call it, require a gas limit parameter. This gas limit is encoded into the []TransactionDeposited event (opens new window), 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 new window).

# 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 Goerli (opens new window).

  1. Call stopChanges, using this Foundry command:

    PRIV_KEY=<your private key here>
    export ETH_RPC_URL=<url to OP Goerli>
    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:

    L1_RPC=<URL to Goerli>
    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 new window), 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 from 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