EVM IBC hooks
Overview
EVM hooks, implemented as IBC middleware, play a critical role in facilitating cross-chain contract calls that involve token transfers. This capability is particularly crucial for cross-chain swaps, providing a robust mechanism for decentralized trading across different blockchain networks. The key to this functionality is the memo
field in the ICS20 and ICS721 transfer packets, as introduced in IBC v3.4.0.
EVM Contract Execution Format
Before we dive into the IBC metadata format, let's take a look at the hook data format and address which fields we need to be setting. The EVM MsgCall
is defined here and other types are defined here.
So we detail where we want to get each of these fields from:
Sender: We cannot trust the sender of an IBC packet, the counter-party chain has full ability to lie about it. We cannot risk this sender being confused for a particular user or module address on Initia. So we replace the sender with an account to represent the sender prefixed by the channel and a evm module prefix. This is done by setting the sender to
Bech32(Hash(Hash("ibc-evm-hook-intermediary") + channelID/sender))
, where the channelId is the channel id on the local chain.ContractAddr: This field should be directly obtained from the ICS-20 packet metadata
Input: This field should be directly obtained from the ICS-20 packet metadata.
So our constructed evm call message that we execute will look like:
ICS20 packet structure
So given the details above, we propogate the implied ICS20 packet data structure. ICS20 is JSON native, so we use JSON for the memo format.
An ICS20 packet is formatted correctly for evmhooks iff the following all hold:
memo
is not blankmemo
is valid JSONmemo
has at least one key, with value"evm"
memo["evm"]["message"]
has exactly five entries,"contract_addr"
and"input"
receiver
== "" ||receiver
== "module_address::module_name::function_name"
We consider an ICS20 packet as directed towards evmhooks iff all of the following hold:
memo
is not blankmemo
is valid JSONmemo
has at least one key, with name"evm"
If an ICS20 packet is not directed towards evmhooks, evmhooks doesn't do anything. If an ICS20 packet is directed towards evmhooks, and is formatted incorrectly, then evmhooks returns an error.
Execution flow
Pre evm hooks:
Ensure the incoming IBC packet is cryptogaphically valid
Ensure the incoming IBC packet is not timed out.
In evm hooks, pre packet execution:
Ensure the packet is correctly formatted (as defined above)
Edit the receiver to be the hardcoded IBC module account
In evm hooks, post packet execution:
Construct evm message as defined before
Execute evm message
if evm message has error, return ErrAck
otherwise continue through middleware
Async Callback
A contract that sends an IBC transfer, may need to listen for the ACK from that packet. To allow contracts to listen on the ack of specific packets, we provide Ack callbacks. The contract, which wants to receive ack callback, have to implement two functions.
ibc_ack
ibc_timeout
Also when a contract make IBC transfer request, it should provide async callback data through memo field.
memo['evm']['async_callback']['id']
: the async callback id is assigned from the contract. so later it will be passed as argument ofibc_ack
andibc_timeout
.memo['evm']['async_callback']['contract_addr']
: The address of module which defines the callback function.
Conclusion
The EVM hook represents an advancement in interoperability, enabling seamless cross-chain functionalities and enhancing the utility of token transfers. By leveraging the EVM hook, developers can initiate complex contract interactions across different chains, fostering new use cases and applications. This development not only simplifies the technical barriers previously associated with cross-chain communications but also paves the way for more integrated and efficient blockchain ecosystems. As this technology matures, it will undoubtedly unlock further potential for decentralized applications, contributing to the broader adoption and operational effectiveness of blockchain technologies.
Last updated