description |
---|
Recommended approaches for encoding and decoding messages |
Abacus interchain messages are encoded as raw byte arrays. In order to send and receive messages, developers should implement functions for encoding and decoding interchain messages.
In this section, we describe two recommended approaches and their tradeoffs.
In most cases, we expect that developers are sending interchain messages in order to call a function on a contract on remote chain. For that, it is necessary to encode and decode raw bytes to an equivalent of a function call with arguments.
Abacus developers can encode and decode data according to the Application Binary Interface (ABI). Developers can use built-in functions like abi.encodeWithSelector
to easily encode function calls as raw byte arrays.
bytes memory _msg = abi.encodeWithSelector(
this._setRouter.selector,
_destination,
_recipient
);
_dispatchToRemoteRouter(_destination, _msg);
Developers can leverage Solidity's built-in ABI decoder to translate encoded messages back into function calls.
function handle(
uint32 _origin,
bytes32 _sender,
bytes memory _msg
)
external
onlyInbox
{
address(this).call(_msg);
}
This approach has the advantage of being very simple to implement, but may not easily extend to non-EVM chains that Abacus may support in the future, as those chains may not offer native support ABI encoding and decoding.
The native Abacus contracts have been built using the TypedMemView
library, which allows developers to build a custom, typed, non EVM-specific message format. TypedMemView
offers developers a solution for chain-agnostic message encoding and decoding, at the expense of greater complexity of implementation.