<h4>Introduction -></h4>
<p>Web3 as a whole brings together the idea of storing (non-)critical information in a non-mutable public registry. This means that you can be sure that the information you store can not be erased or altered or that it is not a hostage to a particular infrastructure or company.</p>
<p>Now this idea brings a lot of things to be considered when building something on Web3. Different people have different needs, some people want to build games, some want to build financial applications and some want to build limited edition collections like NFTs and give proper ownership to certain individuals. All of these problems require different types of blockchain architecture, which different chains solve and sharing information between them at this point is a bit non-straightforward.</p>
<p>There exists a bunch of solutions that can help information between blockchains and one of them is Chainlinks CCIP (i.e. Cross Chain Interoperability Protocol). It utilises Chainlinks already present and battle-tested DON (Decentralised Oracle Network) architecture to make this information sharing more robust and secure.</p>
<p>Using Chainlinks CCIP, You can:</p>
<ol>
<li><p>send tokens from one blockchain to another</p>
</li>
<li><p>send arbitrary messages</p>
</li>
<li><p>send messages and tokens altogether.</p>
</li>
</ol>
<p>The only thing that you need to see is whether your source and destination blockchains both have CCIP support enabled or not which you can quickly check here:</p>
<p><a href="https://docs.chain.link/ccip/supported-networks">https://docs.chain.link/ccip/supported-networks</a></p>
<h4>Architecture -></h4>
<p>This is how the architecture of a message being transferred using CCIP looks like</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693416065129/9cf36b62-ebba-43d1-873f-39cf1001d153.png" alt="Architecture diagram of how CCIP works according to me"></p>
<p>This is the simplest representation that I could come up with at the point of writing this blog on how I think the CCIP protocol works, It's much more complicated than this, but overall it should be somewhat like this.</p>
<p>Now that we have some basic understanding of how these things work we can start with utilising CCIP to send and receive messages across chains.</p>
<h4>Smart Contracts -></h4>
<p>Let's start with seeing how Sender and Receiver Contracts are built.</p>
<p>%[<a href="https://gist.github.com/gat786/081d3400fe745aa5705d84f1871f16e8%5D">https://gist.github.com/gat786/081d3400fe745aa5705d84f1871f16e8]</a> </p>
<p>The above is a sample <code>Sender.sol</code> to demonstrate cross-chain transfers we will deploy it on Sepolia testnet of Ethereum. You can use this <a href="https://remix.ethereum.org/#url=https://docs.chain.link/samples/CCIP/Sender.sol">link</a> to open this contract in Remix directly and do the deployment using it.</p>
<p>The below example can be used to write a <code>Receiver.sol</code> smart contract.</p>
<p>%[<a href="https://gist.github.com/gat786/176eb16e0e9af44f5327f78cbf618cb1%5D">https://gist.github.com/gat786/176eb16e0e9af44f5327f78cbf618cb1]</a> </p>
<p>We will deploy the Sender Contract on Ethereum Sepolia and the Receiver Contract on Polygon Mumbai.</p>
<p>Both of these contracts are copied from <a href="https://docs.chain.link/ccip/getting-started">Chainlink's Official CCIP documentation</a> and are only for representation purposes and should be used with caution anywhere other than just demonstrating CCIP and its use case.</p>
<h4>Coding Demo -></h4>
<p>While you can deploy and test these smart contracts at your own will and choice of platform I went ahead and made it easy for you to test these on your local machine by creating a hardhat project, which deploys both contracts, sends messages from one and reads messages from another one.</p>
<p>It is present here at <a href="https://github.com/gat786/ccip">https://github.com/gat786/ccip</a>.</p>
<p>The scripts and contracts are present in the <code>scripts</code> and <code>contracts</code> directories respectively in the above-mentioned repository.</p>
<p>To begin using the repository, clone it on your machine and start by installing the packages that are required to run it by running the following command</p>
<pre><code class="language-bash">npm install
</code></pre>
<p>Once you have packages installed you can copy the <code>.env_example</code> file and create a <code>.env</code> by putting actual values of variables in their place.</p>
<h5>Creating the Env file</h5>
<pre><code class="language-bash"># SENDER AND RECEIVER URLS are RPC Endpoints for
# sender and receiver chains
SENDER_URL=
RECEIVER_URL=
# Private key is the private key of account which you own
# and you wish to use to deploy the contracts and interact with them
# you need to have native tokens and LINK tokens on both chains
# at this address
PRIVATE_KEY=
# Sender Router Contract is Chainlinks Router Contract Address on
# Sender chain
SENDER_ROUTER_CONTRACT=
# Sender Link Contract is LINK Contract Address on
# Sender chain
SENDER_LINK_CONTRACT=
# Receiver Router Contract is Chainlinks Router Contract Address on
# Receiver chain
RECEIVER_ROUTER_CONTRACT=
# Each chain that is supported by Chainlinks CCIP has its own selector
# you need your destination chain address here
DESTINATION_CHAIN_SELECTOR=
# You can set these after deployment of Actual contracts
# you dont need to worry about it in beginning.
# Address of Sender contract you just deployed
SENDER_CONTRACT_ADDRESS=
# Address of Receiver contract you just deployed.
RECEIVER_CONTRACT_ADDRESS=
</code></pre>
<p>You can see that I have not mentioned Sepolia or Mumbai anywhere in the codebase and have made it so that you can pick and enter any chain information in the codebase and the demo would just work. So any point forward I want you to remember that whenever I say <code>Sender</code> chain I mean Sepolia and <code>Receiver</code> I mean Polygon Mumbai chain.</p>
<p>Make sure that the Private key that you are entering in the env is not shared with anyone and that you are not uploading to Github as part of your repository as it will be a major breach and will result in loss of funds.</p>
<h5>Steps to follow</h5>
<p>All of the necessary steps that you need to do are listed below in a gist</p>
<ol>
<li><p>Deploy Sender Contract</p>
</li>
<li><p>Deploy Receiver Contract</p>
</li>
<li><p>Add LINK to both the contracts on their respective Chains</p>
</li>
<li><p>Send a message from the sender chain</p>
</li>
<li><p>Check the status on the CCIP explorer</p>
</li>
<li><p>Read the message on the receiver chain</p>
</li>
</ol>
<h5>Deploying Sender</h5>
<p>Once you have the env variables set you can run the command</p>
<pre><code class="language-bash">npx hardhat run scripts/deploy-sender.ts --network sender
</code></pre>
<p>and it will deploy the sender contract displaying a message like this</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693507807084/06b35a59-0a8c-4f5a-9b38-f579d982725d.png" alt=""></p>
<h5>Deploying Receiver</h5>
<pre><code class="language-bash">npx hardhat run scripts/deploy-receiver.ts --network receiver
</code></pre>
<p>and it will deploy the receiver contract displaying a message like this</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693507855945/2dbe9245-9947-479a-bb2d-e007c9e76ca3.png" alt=""></p>
<p>Once you have both sender and receiver contracts deployed you can add their addresses in the .env file and carry on with the next steps.</p>
<h5>Sending a message</h5>
<p>Let's start with sending our first message. While I already have a message written in the <code>scripts/send-message.ts</code> file you can edit it and change it to something else if you want to.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693508171090/c590bc03-99ea-4d90-9a4e-d563029f2928.png" alt=""></p>
<p>Once you have a message that you want to send you can run the following command to initiate a message send.</p>
<pre><code class="language-bash">npx hardhat run scripts/send-message.ts --network sender
</code></pre>
<p>You will receive an output that includes the transaction hash which you can use to view its status on the CCIP Explorer and once it is completed we can move ahead and read the message from the receiver smart contract.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693508785766/69aee414-1e1a-49e3-a4bc-54ce3beafa3f.png" alt=""></p>
<h5>Tracking a message status</h5>
<p>You can view the CCIP transaction status at <a href="https://ccip.chain.link">https://ccip.chain.link</a></p>
<p>The output of command that sends a message should also give you a link through which you can directly view the transaction on CCIP Explorer.</p>
<h5>Receiving a message</h5>
<p>Once you see that it is finalised we can move further and read the message on the destination chain using this command -></p>
<pre><code class="language-bash">npx hardhat run scripts/receive-message.ts --network receiver
</code></pre>
<p>The output for it should look something like this</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693509095373/883d5535-45d3-4538-9e30-4fcd41186696.png" alt=""></p>
<h4>Conclusions</h4>
<p>It took me about 20 minutes to send a message from Sepolia to Polygon let me know if you use it to send messages across any other chain on how much it took for you.</p>
<p>While doing all these steps make sure to correctly fill in the environment files or else these steps will not work and you will be left scratching your head</p>
<p>While this blog explains what CCIP is and how you can utilise it to send messages across chains I have a video coming soon that will describe the process in more detail and going through the codebase soon, so do watch out for it.</p>
<p>Thank you for reading it completely, see you soon!</p>