Skip to main content

Custom Contract Development

For more complex dApps requiring custom logic, Mailbox contracts can be integrated directly into custom contracts:

Example: Using IMailbox Interface

import { IMailbox } from "@ssv/src/core/interfaces/IMailbox.sol";

contract CustomCrossChainDApp {
IMailbox public immutable mailbox;

constructor(address _mailbox) {
mailbox = IMailbox(_mailbox);
}

function sendMessage(uint256 destinationChain, address targetContract,
uint256 sessionId, string memory messageType, bytes memory data) external {
mailbox.write(destinationChain, targetContract, sessionId, messageType, data);
}

function processMessage(uint256 sourceChain, address sourceContract,
uint256 sessionId, string memory messageType) external {
bytes memory m = mailbox.read(sourceChain, sourceContract, sessionId, messageType);

if (m.length == 0) {
revert EmptyMessage();
}

// Decode the message data
(address sender, address receiver, uint256 amount) = abi.decode(m, (address, address, uint256));

// Process the message
// ... custom logic here ...

// Send acknowledgment
bytes memory ack = abi.encode("OK");
mailbox.write(sourceChain, sourceContract, sessionId, "ACK", ack);
}
}

Reading and Writing Messages

The IMailbox interface provides two core functions for cross-chain communication:

  • write(uint256 chainId, address contract, uint256 sessionId, string messageType, bytes data) - Send message to another chain
  • read(uint256 chainId, address contract, uint256 sessionId, string messageType) - Read message from another chain

This approach provides full control over cross-chain logic while maintaining atomicity guarantees.