Welcome to the CCIP-router repository. This project demonstrates the implementation of Chainlink's Cross-Chain Interoperability Protocol (CCIP) for executing multi-hop transactions across various testnets. The primary focus is on MedX multihop functionality and its evolution into a more advanced approach with CCIPCrossChainHop.
The MedX multihop functionality allows cross-chain transactions starting with a first hop. Below is an example of a transaction on the CCIP network:
- Transaction: MedX Multihop First Hop
The medX contract was the initial implementation designed for cross-chain transactions requiring up to one intermediate hop between the source and destination chains. This contract efficiently handles scenarios where a message or asset needs to pass through an intermediate blockchain before reaching its final destination.
-
Simple Multi-Hop Handling: The contract is built to handle a single intermediate hop, making it suitable for scenarios where the transaction only requires one intermediary blockchain before reaching the final destination.
-
Gas Limit Management: The contract allows for specifying gas limits for both the initial hop and the intermediate hop, ensuring sufficient resources are allocated for each transaction.
-
Minimal Complexity: By focusing on a single hop, the contract is optimized for simplicity and ease of use in cases where complex multi-hop transactions are not required.
For a transaction that needs to move from Chain A to Chain C via an intermediate Chain B:
- medX handles this efficiently with a single intermediate hop (A -> B -> C).
As the need for more complex cross-chain transactions grew, the medX approach was extended and evolved into the CCIPCrossChainHop contract. This new contract supports transactions that require multiple hops across an arbitrary number of blockchains, making it far more versatile and capable of handling complex scenarios where a message or asset must traverse multiple blockchains before reaching its final destination.
-
Dynamic Multi-Hop Capability: The contract can handle multiple hops, making it suitable for transactions that need to traverse several blockchains before reaching their destination.
-
Scalability: The design allows for scaling to support an arbitrary number of chains, making it a robust solution for more complex cross-chain operations.
-
Extended Gas Management: Similar to medX, this contract manages gas limits for each hop, but it extends this capability to handle multiple hops, ensuring that each step of the transaction is adequately funded.
-
Versatile Use Cases: The contract is designed to be adaptable to various scenarios, from simple one-hop transactions to complex multi-hop transactions involving multiple intermediary blockchains.
For a transaction that needs to move from Chain A to Chain D via two intermediate chains B and C (due to lanes doesnt exists between A<-->D):
- CCIPCrossChainHop effectively handles the full path (A -> B -> C -> D) and can be extended to even more hops if needed (A -> B -> C -> D -> … -> N).
To manage and optimize cross-chain transactions, a graph was created where each node represents a testnet chain and each edge represents a possible lane for CCIP transactions between these chains.
Dijkstra's algorithm was utilized to find the shortest paths between different testnet chains. This allows the contracts to efficiently determine the optimal route for cross-chain messages, ensuring that transactions are completed in the shortest time possible while minimizing gas costs. The helper script processes this graph to generate the necessary input parameters for executing multi-hop transactions.
The prepareData
function is an integral part of this project, responsible for dynamically generating the input parameters required for multi-hop transactions. Here's how it works:
-
From and To Addresses: These parameters define the sender and receiver addresses. The receiver address is updated at each hop to ensure the message is correctly routed.
-
Chain Selectors: The function maps the chain selectors for each blockchain in the path, allowing the contract to identify which blockchain to forward the message to.
-
Gas Limits: Gas limits are provided for each hop, ensuring that each transaction has sufficient gas to complete on each blockchain.
-
Amount: The amount of the transaction, usually in USDC or another stablecoin, is specified for each hop.
In cases where multiple paths exist between two chains, the helper function generates data for each path, allowing the contract to choose the most optimal path. Each path's message data and hop details are logged for reference.
Here are the contract addresses for different testnets involved in the medx singlehop functionality:
- Contract Address: 0x98e7375398DE78FcFA685204D219A1571B888535
- Contract Address: 0xc1ca35997dd2c981c7ade0c73bd8628079fd0a4e
- Contract Address: 0x309222b7833D3D0A59A8eBf9C64A5790bf43E2aA
- Contract Address: 0x7A409A3A36112bd6906a113d9612D7f7e1abd6d4
- Contract Address: 0xF8539a832FFc12e0adc615c5759cD675Daa49Ca5
The following are the CCIP Multichain Hop Router contract addresses deployed across different testnets:
- Router Address: 0x273C282A9f1B45416CB9967611d431C116286ef9
- Router Address: 0x96EE5fb7bc57C1f03D560Fcb1b8574ddC8bf5F37
- Router Address: 0xF99b791257ab50be7F235BC825E7d4B83942cf38
- Router Address: 0x40Fee4c8A3a66Dba113b881Dca0E4B2828b86BB7
- Router Address: 0x309222b7833D3D0A59A8eBf9C64A5790bf43E2aA
Here are some examples of multi-hop transactions executed across various testnets:
-
Base ➡ Optimism ➡ Amoy
- Transaction: CCIP Multi-Hop
-
Base ➡ Optimism ➡ Sepolia ➡ Amoy
- Transaction Optimism --> Sepolia : CCIP Multi-Hop
- Transaction Sepolia --> Amoy : CCIP Multi-Hop
This repository contains:
- Smart Contracts: Contracts implementing the CCIP functionality for multi-hop transactions.
- Helper Scripts: Scripts for generating input parameters and testing various transaction paths.
- Documentation: Detailed explanations of the contract functionality, use cases, and examples.
Feel free to open issues or submit pull requests if you find any bugs or want to contribute new features or enhancements.
This project is licensed under the MIT License.