Unified Interface for Cross-Chain Functionality

Modular Contracts v0.0.4 - Unified Interface for Cross-Chain Functionality

Unified Interface for Cross-Chain Functionality

More and more protocols and chains have been releasing functionality around cross-chain messaging and token transfers. With Modular Contracts being released in beta, we saw this as an opportunity to partner up and create modules around these functionalities.

However, as we explored deeper into each protocol's cross-chain mechanisms, we noticed a significant amount of overlap across all functionalities, including:

  • A function to handle sending cross-chain payloads and tokens
  • A function to handle receiving cross-chain payloads and tokens
  • Setter and getter functions for their version of a router

With that in mind, we decided to create CrossChain.sol, an abstract contract that provides a unified interface to implement cross-chain functionality across any protocol.

Breakdown of CrossChain.sol

Inside the contract CrossChain.sol, there are three main functions:

sendCrossChainTransaction

function sendCrossChainTransaction(
	uint64 _destinationChain,
	address _callAddress,
	bytes calldata _payload,
	bytes calldata _extraArgs
) external payable virtual;

As the name suggests, this is the primary function responsible for sending cross-chain transactions from the protocol.

onCrossChainTransactionSent

function onCrossChainTransactionSent(
	uint64 _destinationChain,
	address _callAddress,
	bytes calldata _payload,
	bytes calldata _extraArgs
) internal virtual;

This callback function is used within the context of the sendCrossChainTransaction function.

onCrossChainTransactionReceived

function onCrossChainTransactionReceived(
	uint64 _sourceChain,
	address _sourceAddress,
	bytes memory _payload,
	bytes memory _extraArgs
) internal virtual;

This callback function is used within the context of the receive function for the specified cross-chain protocol.

With these two callback functions, the user’s application logic can be separated from the cross-chain functionality logic, making it easier to differentiate between the two.


And there you have it—an abstract contract to create a unified interface for cross-chain functionality.

If you want to find out more, check out the code here:
https://github.com/thirdweb-dev/modular-contracts