Skip to main content

Call a Smart Contract on the Destination Chain

info

This is a beta feature of Meson. If you wish to use it, please reach out to the Meson team for further assistance.

Meson Protocol now provides the capability to initiate a cross-chain transaction that directly interacts with a smart contract. Through this feature, assets transferred cross-chain can immediately trigger the internal logic of a specified smart contract. This adds value by enabling cross-chain execution for your Web3 application. Please note, however, that this feature is available only through the SDK integration.

To properly accept cross-chain assets from Meson, your application's contract needs to implement the depositWithBeneficiary method. Here's a sample code snippet:

function depositWithBeneficiary(address token, uint256 amount, address beneficiary, uint64 data)
payable external returns (bool);

This function receives four parameters:

  • token: The address of the token transferred cross-chain
  • amount: The amount of the token transferred
  • beneficiary: Will be the address that initiated the cross-chain transaction
  • data: Extra data passed in by the transaction

You can modify this function to suit your application's requirements. A sample contract is giving below:

contract ForwardTokenContract {
function depositWithBeneficiary(
address token,
uint256 amount,
address beneficiary,
uint64 data
) payable external override returns (bool) {
if (token == address(0)) {
// ETH
// No need to take ETH since it is transferred in msg.value
} else {
// Stablecoins
// Required. Take cross-chain'ed token to dapp's contract
IERC20Minimal(token).transferFrom(msg.sender, address(this), amount);
}

// The dapp can do it's own logic with depositing tokens
// e.g. exchange for other tokens, mint NFTs, etc.
// Could use data to determine specific operations.


// Send benefits to the user. Here as an example we just transfer
// deposited tokens to the user.
if (token == address(0)) {
// ETH
(bool success, ) = beneficiary.call{value: amount}("");
require(success, "Transfer failed");
} else {
// Stablecoins
IERC20Minimal(token).transfer(beneficiary, amount);
}

return true;
}
}

Once you have deployed your smart contract with the required method, reach out to the Meson team to add your contract address to the whitelist of accepted recipient smart contracts. Afterwards, you can use this address as the recipient in the @mesonfi/to SDK.

Please continue to monitor our documentation for updates regarding this feature. As we work to bring this functionality out of beta, we appreciate your patience and encourage your feedback.