New Ethereum talks, every Monday. The week's conference uploads by event, in your inbox.

Loading player…

Chainlink 🛠️ How To Transfer Tokens and Send Data Cross-Chain With Chainlink CCIP

ETHGlobalWed, Oct 9, 2024, 12:00 AM

Chainlink Cross-Chain Interoperability Protocol (CCIP) provides developers with a seamless and secure tool to transfer tokens and send arbitrary messages across blockchains. In this tutorial, Chainlink Labs Developer Advocate Richard Gottleber provides a step-by-step tutorial on how to send a testnet token, as well as an arbitrary message, from Ethereum Sepolia to Polygon Mumbai. Chainlink is the industry-standard Web3 services platform. It has enabled trillions of dollars in transaction volume across DeFi, on-chain finance, gaming, NFTs, and other major industries. As the leading decentralized oracle network, Chainlink empowers developers to build feature-rich Web3 applications with seamless access to real-world data and off-chain computation across any blockchain and provides global enterprises with a universal gateway to all blockchains. With a week of open learning, hacking, and workshops on all things stablecoin, as well as $75,000+ USD in prizes, LFGHO is a great way to connect with the community from around the world, and help shape the future of Ethereum and DeFi! ----------------- Follow us: https://x.com/ETHGlobal?s=20 Check us out: https://ethglobal.com/ Email us at: hello@ethglobal.com

Transcript

hi I'm Richard one of the developer Advocates here at chain link labs and today we're going to be taking a look at chain link cross-chain interoperability protocol or ccip in this video you'll learn more about what ccip is what it's capable of and how you can use it I'll also teach you how to use ccip to transfer tokens cross chain as well as how to use ccip to send arbitrary data between smart contracts on different blockchains so what is chain link ccip ccip is a seamless protocol that allows you to create cross-chain interoperability in your smart contracts with ccip you can transfer assets and information across different blockchains ccip lets you leverage the strengths and benefits of different chains and collaborate with developers from different blockchain ecosystems enabling you to provide additional features or products to those ecosystems anded its core ccip supports three main capabilities the first arbitrary messaging wait why is it called arbitrary it just means that you can send any message you want as long as it's encoded in bytes this allows you to call Smart contract functions across chains second is token transfers with these you can transfer tokens between smart contracts or eoas think Wallets on different blockchains and finally programmable token transfers these combine both messages and token transfers into a single transaction think of it like providing a note along with the tokens on what you want to do with them but enough talk about what it is let's dive into the documentation and see how to use chain link ccip all right so here we are in the documentation it's the place of start for any chain link tutorial and anything you really need to know about chain link this is the best place to start we've got a new section here if we scroll down just a little bit you have ccip right here that's right chain link cross chain interoperability protocol is in the documentation we'll head to the introduction and take a look around we can see here an explanation of what ccip is how to get started and what we need so we'll need some eth on spolia somatic on Mumbai and some link tokens on sepolia as well if we head to faucets. chain. link you'll be able to get those now you'll need both polygon Mumbai as well as ethereum onoa and get those tokens from here from here we're going to go into the transfer token example as well and the send arbitrary data EX example we'll start with the transfer tokens so in this example we'll be transferring tokens from a contract on suia into our eoa or wallet on Mumbai so how do we do that what do we need to get started we'll need to check and make sure that we have our funds we have our wallet set up and we understand remix again we also need to acquire some test tokens for ccip we'll be using the b& M tokens which are the burn and mint tokens these are erc20 tokens that are available on the test networks that we have ccip available on when you transfer these tokens between test net blockchains ccip burns the tokens on the source chain and mints them on the destination chain so let's take a look at that first and see how to do that the contract for ccip bnm and L&M both have a drip function so we if we go to the sport networks page here and we scroll down we will see that we are using ethereum test Nets we're using spolia and spolia we're going to use the ccip B&M token so we'll need to go to this address here this will take us to Ether scan from here we'll click on contract and we'll click on write contract so this is when you'll see the drip function we'll need to connect our wallet once it's connected we'll run the drip function It'll ask us for the address so we'll copy our wallet address and paste it in here and we'll click right confirm this transaction and we should see that transaction come through momentarily let's go back to the documentation so how do we get this token into our metamask wallet well if we copy this token address and we click on metamask you may be on the activity tab if you click on tokens you can then import a token here if you paste in the contract address it'll probably populate this information for you if it doesn't it's right here we have the token symbol as well as the decimals We'll add this custom token and you probably have a balance of zero so that's okay we'll get those tokens here in a moment import those tokens and now once they're imported we can see that our token balance is here so we look at our activity the drip should be done and we should have a token balance of the ccip B&M token that's one that we'll be using to transfer all right so we have our tokens added to metamask let's go back and continue with this tutorial so in this tutorial we'll be using this massive contract here and the reason this contract is so big is because it basically does everything for us that we need for both the examples that we'll be looking at from transferring tokens so we scroll down all the way down we should see an open and REM button let's go ahead and click on that and that'll take us to remix and here we can see remix and we have our token transfer contract so this is a big contract that we use to send tokens from one blockchain to another this contract is set up in a generic manner that we can use any of our supported networks we'll do a brief walk through it's a pretty long contract like I mentioned before but it's very well documented so you can see what every line does let's walk through the basic flow we've got our contract we have our event for transfer tokens that we can emit we have a Constructor and again I mentioned that this is set up in a generic manner so we have our router and our link token address those will be chain specific we do have modifiers for whitelisted chains so you will need to whitel list a chain before you can send tokens to it and then we have our first function that we want to look at this is the transfer token paying with link function now one thing to keep in mind is that ccip supports fee payments in link and in alternative assets which currently takes the form of native blockchain gas coins and their erc20 wrapped version an automated onchain conversion mechanism is being worked on where fee payments and alternative assets are converted into link the transfer token function here will take the destination chain the receiver address what the token address is the amount of that token and that's basically all that we need for this when we look at the transfer tokens paying with the native fee we see this s been a very similar manner again I'm being very brief in my explanation here because these are such well documented examples if you need to know what any of these lines are doing they're documented for you and they explain all of that information so let's head back to the documentation and look at how to actually use this contract so we need to open this contract up in remix we've done that we need to compile the contract ra if we head back to remix if you're not familiar this is the compile tab I have mine set to autoc compile if you don't see that check and a green check mark next to it you can either turn it on or click that compile button and it'll do the compiling for you you should have this green check mark here which means you're good to go we need to deploy our contract now to do this we need to make sure first of all that we are on ethereum sepolia this is something that is probably the trickiest part of dealing with ccip is managing what network work you're on when so now before we go too far we need to make sure that we have both POA and Mumbai set up so let's goe and take a look at Mumbai real quick and see that we have our tokens we've set them up here you can see ccip B&M if you don't have that done again it's the same basic process as we did on spolia you can go to the supported networks page here and you can scroll down to polygon Mumbai and you can add the sported tokens remember the way to do that is you go into metamask tokens import tokens you put the contract address symbol and decimals and add that token you'll want to do that before you go any further because that way you can see when those tokens actually get transferred into your wallet all right let's head back to the documentation we've got our metam Mass set up we're not on supp poio let's make sure we are on the right one again like I said that's the hardest part about ccip in my opinion is just keeping track of what network you're on we'll need to open remix and go to deploy and run and from there we'll need to interact with our injected provider for metamask and we'll need to add the router so we can go and copy that now and by clicking on the copy button we'll head to remix we'll go to our deploy tab I'll change the injected provider to metamask that takes our metamask wallet and puts it into remix we'll expand by clicking the drop down here and add our router address here then we'll also need to add the link token address now remember these are specific to aoia so we've got our link token address and we've got our router address we'll go ahead and click transact confirm this transaction and once that's confirmed we should see our deployed contract down here all right so it's been confirmed we've got our deployed contract with all of our functions in it here what's next we've got our deployed contract we need to fund our contract with ccip B&M tokens in this case we'll be doing 02 of the tokens but you can do whatever you like so we've got our contract address here we'll copy that we'll open up metamask we'll go to the send tab we'll put our contract address we'll change this to be the ccip B&M tokens and we'll send 0 two tokens to our contract click next confirm this transaction and we'll wait for this to complete all right our transaction has completed our contract has a balance of tokens now to transfer we'll need to enable our contract to transfer those tokens to polygon Mumbai we'll copy the chain selector address here if you wanted to use a different chain you could also go to the supported networks page and you can see the selector addresses here for example for ethereum main net so we'll copy our chain selector though for sepolia go back to remix we'll need to whitelist the destination chain paste that chain selector in here and we'll click transact and confirm this transaction as well once this transaction is complete we should be able to transfer tokens and pay in Link in order to pay in link we'll need to fund our contract though with some link tokens so in the same basic process that we did with funding it for the ccip B&M tokens we'll get our contract address one more time we'll go to metamask go to send put in our contract address here this time we'll be sending link give it 01 link click next confirm this transaction that should be enough for this transaction we'll wait for this transaction to be confirmed and now our contract has both link tokens and ccip B&M tokens so we can pay for and transfer those tokens we'll open up the transfer token pay in link function and we've got some arguments here so the first thing is that we'll copy this first argument for the designation chain selector we'll go back into remix this is going to be a bit of back and forth between the docs and this page so we want to transfer tokens and pay in link put our destination change selector in here get the receiver which is going to be my wallet address it's going to be the same wallet address on both polygon and sepolia so we're telling it the polygon chain we're telling it that it's my wallet address or eoa we'll need the token that we want to transfer that's going to be the address of the ccip bnm token we'll get that from the documentation and the am amount that we want to transfer remember we gave it 02 B&M tokens and we'll be sending 0.1 along so paste that in here and we'll click transact and confirm this transaction now once this is confirmed we can switch our wallet over to polygon Mumbai and we can look at our tokens you'll notice I already have 01 ccip B&M in here this is from previous time that I ran this example so we're looking to change that one to a two we send that additional point1 but how do we see this transaction if we go back to our spoo wallet we can see our activity and we can see our contract that we interacted with here now if we click on the copy transaction ID we'll need this in just a moment if we go back to the documentation it'll let you know that there is a CCI IP Explorer basically this is an Explorer but for cross-chain interactions if we paste our transaction ID in here and press return we'll see our transaction details and I'll tell you this can take a little bit so we'll come back in a few moments once this is completed we'll watch the status here and that's where we'll see the progress being made in this transaction all right so our status is now success that means our transaction has gone through we've been able to transfer our tokens from ethereum pooia to polygon mumi if we take a look at our wallet now and we take a look at the tokens we'll notice that I'm still connected to sepolia so I need to change to Mumbai here we can see the ccip B&M token amount and it's changed from 01 to 02 to we have successfully sent tokens across chains that's awesome let's take a look at how we can do the same process but pay with a native token so again this process is going to be basically the same we'll need to change our Network back to sapoia we've funded our contract this time we'll need to fund it with eth so we'll transfer 01 to our contract we'll head back to remix we'll copy the address of our Transformer contract we'll send it some eth so we paste the address in here we'll send it 0.1 eth and confirm we'll wait for this transaction to be finalized all right so our contract now has some eth that has been sent to it we'll open up the transfer and pay native we'll take a look at the documentation one more time again our destination chain is going to be the same the receiver is going to be the same the token will be the same the amounts will also be the same so we'll head back to remix we can just copy that information from the send it with link token above so we'll copy these values in here we'll click on transact and we will confirm this transaction that's been submitted we can either use our wallet or down here in remix I'll open up this menu in the debug menu we can see the transaction hash here we'll copy this and we'll head back to the Explorer we'll paste in our transaction here press return and we'll see once again waiting for finality on this transaction as well so if we go to our wallet change to Mumbai and take a look at our token tokens you can go to remember that we have 02 ccip bnm that should increase to 03 once this transaction has finalized all right so you can see that we have success now on our transaction if we take a look at our wallet we can also see that our ccip B&M has increased to 03 so that's it we've been able to transfer tokens from spolia to Mumbai using ccip paying in both native token and Link that's really powerful we've been able to do cross chain token transfers with ccip let's take a look at the documentation and one more example here let's send data from zolia to Mumbai the process will be very similar we're going to take this contract and this time though we're be deploying it on both sapoia and Mumbai and we'll be doing the example of paying with link tokens not both examples cuz as we've seen using native gas is a very similar process as using link tokens this one again will have the added challenge of needing to deploy your contract in two different places and keeping track of where they're deployed and which wallet you're on is kind of a challenge so let's scroll through this contract and at the bottom we will once again see our open and remix button we'll click on that and I'm going to go and close the Explorer mint functionality for our ccip B&M token and the other remix that we were using just to keep things simple all right so here we have our contract again and once again this contract is documented amazingly well we'll walk through the basics of it we've got our contract we're calling this one messenger this contract is both the sender and receiver of messages so you can see here we have two events one for the message being sent one for the message being received now this contract is set up so that is basically agnostic when it comes to what chain you're deploying on and whether you're the sender or receiver that's why it's such a large contract you could reduce the amount of code in here um if you wanted one that was specifically just a sender and another contract that was specifically just a receiver so back to the documentation when you need to do is you need to compile it again just like we did with the other one and we'll need to make sure that we are on sapoia and this is where things start to get a little bit tricky when it comes to managing both networks because we need to deploy this on Mumbai as well I'll do that by opening a separate window in a private browser mode just to keep things separate there we'll start with zolia so in remix we'll need to add Zia's router address and the link contract address so we'll head back to remix we'll go to the deploy tab we'll make sure that we have our injected provider selected and we will deploy so we have our router address here and I'm head back to the documentation we'll go ahead and grab the link token address as well paste that in here and we'll click transact this will deploy our smart contract for us on sepolia so now that we have this contract deployed on sapoia what's next well we'll need to enable our contract to send messages to Mumbai so we'll whitelist the destination chain we'll copy our destination chain selector here we'll head back to remix in our deployed contract here we'll need to whitelist the destination chain we'll paste in our chain selector click transact confirm this and after that we will need to deploy our contract on Mumbai so wait for this transaction to be confirmed all right transaction is confirmed we've got our messenger sender contract deployed on spolia and we whitelisted the destination chain of Mumbai now we need to deploy on Mumbai easiest way I found to do this and keep track of things is we'll copy this link here and open a remix Link in a new browser window again I'm doing a private window here in Brave in my mind this make things really easy because in Brave it makes it purple and that just kind of keeps me in the mind set of purple polygon and we're good to go put the link to remix in here we'll load all of this up this time we'll need to go to the compiler turn on the autoc compile or click the compile button and then we'll need to deploy this with the injected metamask provider we need to remember to change our Network to Mumbai that is key here like I said that's probably the hardest part so we'll head back to the documentation scroll back to the part about deploying on [Music] Mumbai we' got to open in remix we'll need to put in the router address again remember that's chain specific expand this we'll add our router address and we'll go and get the link token address add it here and we'll click transact again making sure that we are on Mumbai confirm our transaction here wait for this to be all right so our transaction has gone through our contract is deployed head back to the documentation you'll now need to whitelist The Source chain so in our contract we're going to be sending messages and we're whitelisting the destination in this contract we're whitelisting the source chain so we need to whitelist The Source chain here run this transaction confirm also need to enable the actual sending contract so this will be the contract address from sepolia so grab that from this window we'll use the contract address here we'll come back you can see that our white list the source chain has gone through we also need to whitelist the sender paste that value in here transact confirm this transaction again all right so at this point we've set up sapoia to send the message we set up polygam and buy to receive the message and we should be able to use the send function to send a message between the two so again we'll be paying with link so first we'll need to give our spolia contract some link token head to the spolia version of the contract grab its address we'll switch our change toia and we need to send some link to our contract so we'll grab that address here go into metamask send we'll paste in the contact address here we'll send it some link point 001 link tokens confirm this transaction and wait for it to be completed all right our contract has some link tokens now and now we can run the send message pay link function so we'll need our destination chain value remember these selectors are chain dependent so if you look at the sported networks page and determine which one you need for specific use case you will need to put a different value in there so we'll need that we'll need our receiver contract address which is going to be the address of Our receiver contract and then we'll need the text so we'll send a message and pay with link pasting our chain selector here we'll paste in the address from the other window we'll grab our contract address from polygon Mumbai paste that in here and then we'll say hello world and now we can click transact confirm this transaction but before this goes through let's take a quick look at our contract here we'll use this get last message received details you can see the ID in the string it's empty now so in polygon we have no information and first we need to switch over to polygon like I mentioned this is the hardest part I forgot to switch to polygon so we use polygon click on this get latest received message we see that this blank we don't have one right now so this is where we'll be able to see our string of text when it comes from sapoia but we need to watch it so we use the transaction ID here go back to the documentation open up the ccip Explorer we'll put our transaction ID in there and press return and similar we'll see that transaction is waiting for finality so this is where we'll wait until this completes and then we should be able to see our message cross chain so our transaction has completed successfully let's take a look at polygon Mumbai version of our contract remember when we got the last received message previously our string was blank we sent hello world if we check it now we can see Hello World and we can see our message ID here so we have taken a string of text a message and we have sent it from sapoia to Mumbai now this doesn't just have to be a text message like this it could be a function call or something like that I remember the message is received you can do what you like with it all right in this tutorial you've learned what ccip is a protocol that allows you to create cross-chain interoperability in your smart contract you've gotten some hands-on experience with ccip and learned how it works from here the sky is the limit on what you can create I can't wait to see what you build by leveraging the power of connecting blockchain ecosystems thanks for watching and I'll catch to you in the next one

Automatic transcript — names and jargon may be misspelled.