logoAcademy

Encoding of multiple Values

Learn how to encode multiple function parameters

In this section, we will learn how to pack multiple values into a single message.

We can use abi.encode() to encode multiple values into a single byte array:

bytes message = abi.encode(
      someString,
      someNumber,
      someAddress
  );

Here we can see how the function abi.encode() is used to turn multiple values (someString, someNumber & someAddress) of various types (string, uint & address) into a single value of the type bytes called message. This bytearray can then be sent to the destination chain using the Teleporter.

function sendMessage(address destinationAddress) external returns (uint256 messageID) {
  string someString = "test";
  uint someNumber = 43;
  address someAddress = address(0);
 
  bytes message = abi.encode( 
      someString,
      someNumber,
      someAddress
  );
 
  return teleporterMessenger.sendCrossChainMessage(
      TeleporterMessageInput({
        destinationChainID: destinationChainID,
        destinationAddress: destinationAddress,
        feeInfo: TeleporterFeeInfo({
          feeTokenAddress: feeContractAddress,
          amount: adjustedFeeAmount
        }),
        requiredGasLimit: requiredGasLimit,
        allowedRelayerAddresses: new address[](0),
        message: message 
      })
  );
}

The receiving contract can then decode the byte array back into its original values:

function receiveTeleporterMessage(
  bytes32 originChainID,
  address originSenderAddress,
  bytes calldata message
) external {
  // Only the Interchain Messaging receiver can deliver a message.
  if (msg.sender != address(teleporterMessenger)) {
    revert Unauthorized();
  }
 
  // Decoding the function parameters
  (
    string someString,
    uint256 someNumber,
    address someAddress
  ) = abi.decode(message, (string, uint256, address));
  
  // Calling the internal function
  _someFunction(someString, someNumber, someAddress) 
  
}
 
function _someFunction(string someString, uint256 someNumber, address someAddress) private {
  // Do something
}

Here we are using abi.decode() to unpack the three values (someString, someNumber & someAddress) from the parameter message of the type bytes. As you can see, we need to provide the message as well as the types of values encoded in the message. It is important to note the types must be the same order as parameters to abi.decode().

(
  string memory someString,
  uint someNumber,
  address someAddress
) = abi.decode(message, (string, uint, address));  
Loading...

On this page

No Headings