Skip to main content
Single input multiple output

Single input multiple output smart contract

If you already read the previous tutorials you already know how to use zEVM. A very common use case on zEVM is a smart contract with a single input from one chain, do some magic, and then execute the output to another or multiple chains.

In this tutorial we will show you how to do that, leaving an empty place to write your own code.

// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;

import "@openzeppelin/contracts/access/Ownable.sol";
import "@zetachain/zevm-protocol-contracts/contracts/system/SystemContract.sol";
import "../shared/BytesHelperLib.sol";
import "../shared/SwapHelperLib.sol";

interface ZetaMultiOutputErrors {
error NoAvailableTransfers();

contract ZetaMultiOutput is zContract, Ownable, ZetaMultiOutputErrors {
SystemContract public immutable systemContract;
address[] public destinationTokens;

event DestinationRegistered(address);
event Withdrawal(address, uint256, address);

constructor(address systemContractAddress) {
systemContract = SystemContract(systemContractAddress);

function registerDestinationToken(address destinationToken) external onlyOwner {
emit DestinationRegistered(destinationToken);

function _getTotalTransfers(address zrc20) internal view returns (uint256) {
uint256 total = 0;
for (uint256 i; i < destinationTokens.length; i++) {
if (destinationTokens[i] == zrc20) continue;

return total;

function onCrossChainCall(address zrc20, uint256 amount, bytes calldata message) external virtual override {
if (_getTotalTransfers(zrc20) == 0) revert NoAvailableTransfers();

address receipient = BytesHelperLib.bytesToAddress(message, 0);
uint256 amountToTransfer = amount / _getTotalTransfers(zrc20);
uint256 leftOver = amount - amountToTransfer * _getTotalTransfers(zrc20);

uint256 lastTransferIndex = destinationTokens[destinationTokens.length - 1] == zrc20
? destinationTokens.length - 2
: destinationTokens.length - 1;

for (uint256 i; i < destinationTokens.length; i++) {
address targetZRC20 = destinationTokens[i];
if (targetZRC20 == zrc20) continue;

if (lastTransferIndex == i) {
amountToTransfer += leftOver;

uint256 outputAmount = SwapHelperLib._doSwap(
SwapHelperLib._doWithdrawal(targetZRC20, outputAmount, BytesHelperLib.addressToBytes(receipient));
emit Withdrawal(targetZRC20, outputAmount, receipient);

If you need the helpers we use in this example you can find it in the Helpers Section.

What does this contract do? At the beginning reads an address from message, and then send some tokens to that address in several chains.

This contract shows a fundamental building block for your smart contract. You can complete the contract in the todo section and change the final transfer to your own needs.