Skip to main content

@morph-l2/sdk / Exports / CrossChainMessenger

Class: CrossChainMessenger

Table of contents

Constructors

Properties

Accessors

Methods

Constructors

constructor

new CrossChainMessenger(opts)

Creates a new CrossChainProvider instance.

Parameters

NameTypeDescription
optsObjectOptions for the provider.
opts.backendURL?stringbackend for withdraw proof gen.
opts.bridges?BridgeAdapterDataOptional bridge address list.
opts.contracts?DeepPartial<OEContractsLike>Optional contract address overrides.
opts.depositConfirmationBlocks?NumberLikeOptional number of blocks before a deposit is confirmed.
opts.l1BlockTimeSeconds?NumberLikeOptional estimated block time in seconds for the L1 chain.
opts.l1ChainIdNumberLikeChain ID for the L1 chain.
opts.l1SignerOrProviderSignerOrProviderLikeSigner or Provider for the L1 chain, or a JSON-RPC url.
opts.l2ChainIdNumberLikeChain ID for the L2 chain.
opts.l2SignerOrProviderSignerOrProviderLikeSigner or Provider for the L2 chain, or a JSON-RPC url.

Defined in

src/cross-chain-messenger.ts:130

Properties

backendURL

backendURL: string

Backend url for withdrawal prove server

Defined in

src/cross-chain-messenger.ts:73


bridges

bridges: BridgeAdapters

List of custom bridges for the given network.

Defined in

src/cross-chain-messenger.ts:103


contracts

contracts: OEContracts

Contract objects attached to their respective providers and addresses.

Defined in

src/cross-chain-messenger.ts:98


depositConfirmationBlocks

depositConfirmationBlocks: number

Number of blocks before a deposit is considered confirmed.

Defined in

src/cross-chain-messenger.ts:108


estimateGas

estimateGas: Object

Object that holds the functions that estimates the gas required for a given transaction. Follows the pattern used by ethers.js.

Type declaration

NameType
approveERC20(l1Token: AddressLike, l2Token: AddressLike, amount: NumberLike, opts?: { overrides?: CallOverrides }) => Promise<BigNumber>
depositERC20(l1Token: AddressLike, l2Token: AddressLike, amount: NumberLike, opts?: { l2GasLimit?: NumberLike ; overrides?: CallOverrides ; recipient?: AddressLike }) => Promise<BigNumber>
depositETH(amount: NumberLike, opts?: { l2GasLimit?: NumberLike ; overrides?: CallOverrides ; recipient?: AddressLike }) => Promise<BigNumber>
finalizeMessage(message: MessageLike, opts?: { overrides?: CallOverrides }) => Promise<BigNumber>
proveMessage(message: MessageLike, opts?: { overrides?: CallOverrides }) => Promise<BigNumber>
resendMessage(message: MessageLike, messageGasLimit: NumberLike, opts?: { overrides?: CallOverrides }) => Promise<BigNumber>
sendMessage(message: CrossChainMessageRequest, opts?: { l2GasLimit?: NumberLike ; overrides?: CallOverrides }) => Promise<BigNumber>
withdrawERC20(l1Token: AddressLike, l2Token: AddressLike, amount: NumberLike, opts?: { overrides?: CallOverrides ; recipient?: AddressLike }) => Promise<BigNumber>
withdrawETH(amount: NumberLike, opts?: { overrides?: CallOverrides ; recipient?: AddressLike }) => Promise<BigNumber>

Defined in

src/cross-chain-messenger.ts:1606


l1BlockTimeSeconds

l1BlockTimeSeconds: number

Estimated average L1 block time in seconds.

Defined in

src/cross-chain-messenger.ts:113


l1ChainId

l1ChainId: number

Chain ID for the L1 network.

Defined in

src/cross-chain-messenger.ts:88


l1SignerOrProvider

l1SignerOrProvider: Provider | Signer

Provider connected to the L1 chain.

Defined in

src/cross-chain-messenger.ts:78


l2ChainId

l2ChainId: number

Chain ID for the L2 network.

Defined in

src/cross-chain-messenger.ts:93


l2SignerOrProvider

l2SignerOrProvider: Provider | Signer

Provider connected to the L2 chain.

Defined in

src/cross-chain-messenger.ts:83


populateTransaction

populateTransaction: Object

Object that holds the functions that generate transactions to be signed by the user. Follows the pattern used by ethers.js.

Type declaration

NameType
approveERC20(l1Token: AddressLike, l2Token: AddressLike, amount: NumberLike, opts?: { overrides?: Overrides }) => Promise<TransactionRequest>
depositERC20(l1Token: AddressLike, l2Token: AddressLike, amount: NumberLike, opts?: { l2GasLimit?: NumberLike ; overrides?: Overrides ; recipient?: AddressLike }) => Promise<TransactionRequest>
depositETH(amount: NumberLike, opts?: { l2GasLimit?: NumberLike ; overrides?: PayableOverrides ; recipient?: AddressLike }) => Promise<TransactionRequest>
finalizeMessage(message: MessageLike, opts?: { overrides?: PayableOverrides }) => Promise<TransactionRequest>
proveMessage(message: MessageLike, opts?: { overrides?: PayableOverrides }) => Promise<TransactionRequest>
resendMessage(message: MessageLike, messageGasLimit: NumberLike, opts?: { overrides?: Overrides }) => Promise<TransactionRequest>
sendMessage(message: CrossChainMessageRequest, opts?: { l2GasLimit?: NumberLike ; overrides?: Overrides }) => Promise<TransactionRequest>
withdrawERC20(l1Token: AddressLike, l2Token: AddressLike, amount: NumberLike, opts?: { overrides?: Overrides ; recipient?: AddressLike }) => Promise<TransactionRequest>
withdrawETH(amount: NumberLike, opts?: { overrides?: Overrides ; recipient?: AddressLike }) => Promise<TransactionRequest>

Defined in

src/cross-chain-messenger.ts:1337

Accessors

l1Provider

get l1Provider(): Provider

Provider connected to the L1 chain.

Returns

Provider

Defined in

src/cross-chain-messenger.ts:183


l1Signer

get l1Signer(): Signer

Signer connected to the L1 chain.

Returns

Signer

Defined in

src/cross-chain-messenger.ts:205


l2Provider

get l2Provider(): Provider

Provider connected to the L2 chain.

Returns

Provider

Defined in

src/cross-chain-messenger.ts:194


l2Signer

get l2Signer(): Signer

Signer connected to the L2 chain.

Returns

Signer

Defined in

src/cross-chain-messenger.ts:216

Methods

approval

approval(l1Token, l2Token, opts?): Promise<BigNumber>

Queries the account's approval amount for a given L1 token.

Parameters

NameTypeDescription
l1TokenAddressLikeThe L1 token address.
l2TokenAddressLikeThe L2 token address.
opts?ObjectAdditional options.
opts.signer?SignerOptional signer to get the approval for.

Returns

Promise<BigNumber>

Amount of tokens approved for deposits from the account.

Defined in

src/cross-chain-messenger.ts:1226


approveERC20

approveERC20(l1Token, l2Token, amount, opts?): Promise<TransactionResponse>

Approves a deposit into the L2 chain.

Parameters

NameTypeDescription
l1TokenAddressLikeThe L1 token address.
l2TokenAddressLikeThe L2 token address.
amountNumberLikeAmount of the token to approve.
opts?ObjectAdditional options.
opts.overrides?OverridesOptional transaction overrides.
opts.signer?SignerOptional signer to use to send the transaction.

Returns

Promise<TransactionResponse>

Transaction response for the approval transaction.

Defined in

src/cross-chain-messenger.ts:1248


depositERC20

depositERC20(l1Token, l2Token, amount, opts?): Promise<TransactionResponse>

Deposits some ERC20 tokens into the L2 chain.

Parameters

NameTypeDescription
l1TokenAddressLikeAddress of the L1 token.
l2TokenAddressLikeAddress of the L2 token.
amountNumberLikeAmount to deposit.
opts?ObjectAdditional options.
opts.l2GasLimit?NumberLikeOptional gas limit to use for the transaction on L2.
opts.overrides?OverridesOptional transaction overrides.
opts.recipient?AddressLikeOptional address to receive the funds on L2. Defaults to sender.
opts.signer?SignerOptional signer to use to send the transaction.

Returns

Promise<TransactionResponse>

Transaction response for the deposit transaction.

Defined in

src/cross-chain-messenger.ts:1280


depositETH

depositETH(amount, opts?): Promise<TransactionResponse>

Deposits some ETH into the L2 chain.

Parameters

NameTypeDescription
amountNumberLikeAmount of ETH to deposit (in wei).
opts?ObjectAdditional options.
opts.l2GasLimit?NumberLikeOptional gas limit to use for the transaction on L2.
opts.overrides?OverridesOptional transaction overrides.
opts.recipient?AddressLikeOptional address to receive the funds on L2. Defaults to sender.
opts.signer?SignerOptional signer to use to send the transaction.

Returns

Promise<TransactionResponse>

Transaction response for the deposit transaction.

Defined in

src/cross-chain-messenger.ts:1180


estimateL2MessageGasLimit

estimateL2MessageGasLimit(message, opts?): Promise<BigNumber>

Estimates the amount of gas required to fully execute a given message on L2. Only applies to L1 => L2 messages. You would supply this gas limit when sending the message to L2.

Parameters

NameTypeDescription
messageMessageRequestLikeMessage get a gas estimate for.
opts?ObjectOptions object.
opts.bufferPercent?numberPercentage of gas to add to the estimate. Defaults to 20.
opts.from?stringAddress to use as the sender.

Returns

Promise<BigNumber>

Estimates L2 gas limit.

Defined in

src/cross-chain-messenger.ts:875


estimateMessageWaitTimeSeconds

estimateMessageWaitTimeSeconds(message): Promise<number>

Returns the estimated amount of time before the message can be executed. When this is a message being sent to L1, this will return the estimated time until the message will complete its challenge period. When this is a message being sent to L2, this will return the estimated amount of time until the message will be picked up and executed on L2.

Parameters

NameTypeDescription
messageMessageLikeMessage to estimate the time remaining for.

Returns

Promise<number>

Estimated amount of time remaining (in seconds) before the message can be executed.

Defined in

src/cross-chain-messenger.ts:917


finalizeMessage

finalizeMessage(message, opts?): Promise<TransactionResponse>

Finalizes a cross chain message that was sent from L2 to L1. Only applicable for L2 to L1 messages. Will throw an error if the message has not completed its challenge period yet.

Parameters

NameTypeDescription
messageMessageLikeMessage to finalize.
opts?ObjectAdditional options.
opts.overrides?PayableOverridesOptional transaction overrides.
opts.signer?SignerOptional signer to use to send the transaction.

Returns

Promise<TransactionResponse>

Transaction response for the finalization transaction.

Defined in

src/cross-chain-messenger.ts:1157


getBackendTreeSyncIndex

getBackendTreeSyncIndex(): Promise<number>

Returns

Promise<number>

Defined in

src/cross-chain-messenger.ts:1057


getBridgeForTokenPair

getBridgeForTokenPair(l1Token, l2Token): Promise<IBridgeAdapter>

Parameters

NameTypeDescription
l1TokenAddressLikeL1 token address.
l2TokenAddressLikeL2 token address.

Returns

Promise<IBridgeAdapter>

The appropriate bridge adapter for the given token pair.

Defined in

src/cross-chain-messenger.ts:455


getChallengePeriodSeconds

getChallengePeriodSeconds(): Promise<number>

Queries the current challenge period in seconds from the StateCommitmentChain.

Returns

Promise<number>

Current challenge period in seconds.

Defined in

src/cross-chain-messenger.ts:980


getDepositsByAddress

getDepositsByAddress(address, opts?): Promise<TokenBridgeMessage[]>

Gets all deposits for a given address.

Parameters

NameTypeDescription
addressAddressLikeAddress to search for messages from.
optsObjectOptions object.
opts.fromBlock?BlockTagBlock to start searching for messages from. If not provided, will start from the first block (block #0).
opts.toBlock?BlockTagBlock to stop searching for messages at. If not provided, will stop at the latest known block ("latest").

Returns

Promise<TokenBridgeMessage[]>

All deposit token bridge messages sent by the given address.

Defined in

src/cross-chain-messenger.ts:488


getMessageReceipt

getMessageReceipt(message): Promise<MessageReceipt>

Finds the receipt of the transaction that executed a particular cross chain message.

Parameters

NameTypeDescription
messageMessageLikeMessage to find the receipt of.

Returns

Promise<MessageReceipt>

CrossChainMessage receipt including receipt of the transaction that relayed the given message.

Defined in

src/cross-chain-messenger.ts:683


getMessageStatus

getMessageStatus(message): Promise<MessageStatus>

Retrieves the status of a particular message as an enum.

Parameters

NameTypeDescription
messageMessageLikeCross chain message to check the status of.

Returns

Promise<MessageStatus>

Status of the message.

Defined in

src/cross-chain-messenger.ts:617


getMessagesByTransaction

getMessagesByTransaction(transaction, opts?): Promise<CrossChainMessage[]

Retrieves all cross chain messages sent within a given transaction.

Parameters

NameTypeDescription
transactionTransactionLikeTransaction hash or receipt to find messages from.
optsObjectOptions object.
opts.direction?MessageDirectionDirection to search for messages in. If not provided, will attempt to automatically search both directions under the assumption that a transaction hash will only exist on one chain. If the hash exists on both chains, will throw an error.

Returns

Promise<CrossChainMessage[]>

All cross chain messages sent within the transaction.

Defined in

src/cross-chain-messenger.ts:234


getProvenWithdrawal

getProvenWithdrawal(withdrawalHash): Promise<ProvenWithdrawal>

Queries the MorphPortal contract's provenWithdrawals mapping for a ProvenWithdrawal that matches the passed withdrawalHash

Parameters

NameType
withdrawalHashstring

Returns

Promise<ProvenWithdrawal>

A ProvenWithdrawal object

Bedrock

Note: This function is bedrock-specific.

Defined in

src/cross-chain-messenger.ts:995


getWithdrawMessageProof

getWithdrawMessageProof(message): Promise<WithdrawMessageProof>

Generates the bedrock proof required to finalize an L2 to L1 message.

Parameters

NameTypeDescription
messageMessageLikeMessage to generate a proof for.

Returns

Promise<WithdrawMessageProof>

Proof that can be used to finalize the message.

Defined in

src/cross-chain-messenger.ts:1007


getWithdrawalsByAddress

getWithdrawalsByAddress(address, opts?): Promise<TokenBridgeMessage[]>

Gets all withdrawals for a given address.

Parameters

NameTypeDescription
addressAddressLikeAddress to search for messages from.
optsObjectOptions object.
opts.fromBlock?BlockTagBlock to start searching for messages from. If not provided, will start from the first block (block #0).
opts.toBlock?BlockTagBlock to stop searching for messages at. If not provided, will stop at the latest known block ("latest").

Returns

Promise<TokenBridgeMessage[]>

All withdrawal token bridge messages sent by the given address.

Defined in

src/cross-chain-messenger.ts:522


proveMessage

proveMessage(message, opts?): Promise<TransactionResponse>

Proves a cross chain message that was sent from L2 to L1. Only applicable for L2 to L1 messages.

Parameters

NameTypeDescription
messageMessageLikeMessage to finalize.
opts?ObjectAdditional options.
opts.overrides?OverridesOptional transaction overrides.
opts.signer?SignerOptional signer to use to send the transaction.

Returns

Promise<TransactionResponse>

Transaction response for the finalization transaction.

Defined in

src/cross-chain-messenger.ts:1136


resendMessage

resendMessage(message, messageGasLimit, opts?): Promise<TransactionResponse>

Resends a given cross chain message with a different gas limit. Only applies to L1 to L2 messages. If provided an L2 to L1 message, this function will throw an error.

Parameters

NameTypeDescription
messageMessageLikeCross chain message to resend.
messageGasLimitNumberLikeNew gas limit to use for the message.
opts?ObjectAdditional options.
opts.overrides?OverridesOptional transaction overrides.
opts.signer?SignerOptional signer to use to send the transaction.

Returns

Promise<TransactionResponse>

Transaction response for the message resending transaction.

Defined in

src/cross-chain-messenger.ts:1109


sendMessage

sendMessage(message, opts?): Promise<TransactionResponse>

Sends a given cross chain message. Where the message is sent depends on the direction attached to the message itself.

Parameters

NameTypeDescription
messageCrossChainMessageRequestCross chain message to send.
opts?ObjectAdditional options.
opts.l2GasLimit?NumberLikeOptional gas limit to use for the transaction on L2.
opts.overrides?OverridesOptional transaction overrides.
opts.signer?SignerOptional signer to use to send the transaction.

Returns

Promise<TransactionResponse>

Transaction response for the message sending transaction.

Defined in

src/cross-chain-messenger.ts:1082


toBedrockCrossChainMessage

toBedrockCrossChainMessage(message): Promise<CrossChainMessage>

Transforms a legacy message into its corresponding Bedrock representation.

Parameters

NameTypeDescription
messageMessageLikeLegacy message to transform.

Returns

Promise<CrossChainMessage>

Bedrock representation of the message.

Defined in

src/cross-chain-messenger.ts:324


toCrossChainMessage

toCrossChainMessage(message): Promise<CrossChainMessage>

Resolves a MessageLike into a CrossChainMessage object. Unlike other coercion functions, this function is stateful and requires making additional requests. For now I'm going to keep this function here, but we could consider putting a similar function inside of utils/coercion.ts if people want to use this without having to create an entire CrossChainProvider object.

Parameters

NameTypeDescription
messageMessageLikeMessageLike to resolve into a CrossChainMessage.

Returns

Promise<CrossChainMessage>

Message coerced into a CrossChainMessage.

Defined in

src/cross-chain-messenger.ts:555


toLowLevelMessage

toLowLevelMessage(message): Promise<CoreCrossChainMessage>

Transforms a CrossChainMessenger message into its low-level representation inside the L2ToL1MessagePasser contract on L2.

Parameters

NameTypeDescription
messageMessageLikeMessage to transform.

Returns

Promise<CoreCrossChainMessage>

Transformed message.

Defined in

src/cross-chain-messenger.ts:370


waitForMessageReceipt

waitForMessageReceipt(message, opts?): Promise<MessageReceipt>

Waits for a message to be executed and returns the receipt of the transaction that executed the given message.

Parameters

NameTypeDescription
messageMessageLikeMessage to wait for.
optsObjectOptions to pass to the waiting function.
opts.confirmations?numberNumber of transaction confirmations to wait for before returning.
opts.pollIntervalMs?numberNumber of milliseconds to wait between polling for the receipt.
opts.timeoutMs?numberMilliseconds to wait before timing out.

Returns

Promise<MessageReceipt>

CrossChainMessage receipt including receipt of the transaction that relayed the given message.

Defined in

src/cross-chain-messenger.ts:760


waitForMessageStatus

waitForMessageStatus(message, status, opts?): Promise<void>

Waits until the status of a given message changes to the expected status. Note that if the status of the given message changes to a status that implies the expected status, this will still return. If the status of the message changes to a status that exclues the expected status, this will throw an error.

Parameters

NameTypeDescription
messageMessageLikeMessage to wait for.
statusMessageStatusExpected status of the message.
optsObjectOptions to pass to the waiting function.
opts.pollIntervalMs?numberNumber of milliseconds to wait when polling.
opts.timeoutMs?numberMilliseconds to wait before timing out.

Returns

Promise<void>

Defined in

src/cross-chain-messenger.ts:798


withdrawERC20

withdrawERC20(l1Token, l2Token, amount, opts?): Promise<TransactionResponse>

Withdraws some ERC20 tokens back to the L1 chain.

Parameters

NameTypeDescription
l1TokenAddressLikeAddress of the L1 token.
l2TokenAddressLikeAddress of the L2 token.
amountNumberLikeAmount to withdraw.
opts?ObjectAdditional options.
opts.overrides?OverridesOptional transaction overrides.
opts.recipient?AddressLikeOptional address to receive the funds on L1. Defaults to sender.
opts.signer?SignerOptional signer to use to send the transaction.

Returns

Promise<TransactionResponse>

Transaction response for the withdraw transaction.

Defined in

src/cross-chain-messenger.ts:1313


withdrawETH

withdrawETH(amount, opts?): Promise<TransactionResponse>

Withdraws some ETH back to the L1 chain.

Parameters

NameTypeDescription
amountNumberLikeAmount of ETH to withdraw.
opts?ObjectAdditional options.
opts.overrides?OverridesOptional transaction overrides.
opts.recipient?AddressLikeOptional address to receive the funds on L1. Defaults to sender.
opts.signer?SignerOptional signer to use to send the transaction.

Returns

Promise<TransactionResponse>

Transaction response for the withdraw transaction.

Defined in

src/cross-chain-messenger.ts:1204