Chainlink 🛠How to Mint CrossChain NFTs Using CCIP
ETHGlobal·Wed, Oct 9, 2024, 12:00 AM
Join Chainlink team on learning How to Mint CrossChain NFTs Using CCIP! Embark on a journey with ETHGlobal London, where innovation thrives, collaboration knows no bounds, and hierarchies fade into self-sovereign ownership. The most skilled web3 developers, designers and product builders from all around the globe come together for a weekend-long adventure to advance the Ethereum ecosystem at ETHGlobal London in March of 2024! ----------------- Follow us: https://x.com/ethglobal Check us out: https://ethglobal.co​m Email us at: hello@ethglobal.com
Transcript
[Music] hey there I'm Richard one of the developer Advocates here at chain link labs and today we're going to take a look at chain link cross chain interoperability protocol or ccip and we'll learn how to Mint an nft from one chain to another so let's dive in if we take a look at the code here for ccip cross chain inft we have an example that uses hard hat to do all this now we're going to be doing this with remix I'll show you how to kind of Port things over it should be pretty straightforward I didn't want to go through any complexities of dealing with hard hat in this tutorial so I just wanted to keep everything as basic as possible and do them in remix why would we want to Mint an nft on one chain and have it show up on another well in this example we'll be minting from sepolia onto the Avalanche Fuji Network and what if we had funds on zolia we didn't have any funds on Fuji this would allow us to do that process of a minting an nft on Fuji without actually transferring our funds over and then minting the nft and then if we had extra funds left maybe we want to transfer them back right so that's one use case could be very useful for your project how does chain link ccip actually work well to keep things very basic and simple we have in the repository a diagram that kind of talks through this basically the end user will interact with the DAP from there that dap talks to the ccip router you can think of ccip as a black box if you want we have more information our documentation if you want to know how ccip actually works behind the scenes really what you need to know is it's a secure way of transferring tokens and data from one chain to another on the destination chain then the router interacts with your contract there and passes that information those tokens along on the source chain we talk to the router the router talks to ccip on the destination chain another router reaches out to your contract and does the work for you there that's a gist of how this works we have in this documentation instructions for how to actually use it if you wanted to use hard hat you are more than welcome to use the documentation on GitHub and follow along with hard hat I'm going to be taking some of the contracts out of this documentation and putting them in remix like I mentioned so what are these contracts that were going to be looking at if we head back to the top we have our contracts folder here and from this contracts folder we have four contracts that we're actually interested in you may only see three right here but there's one more hidden under your tills that we'll need the first one I'm going to look at I'm just going to follow the flow of the actual process itself right so we're going to start the source then we'll go to the destination then we'll go to the actual nft itself so we'll start with Source Mentor we'll copy all of this information and we'll put it into remix before I get started there though I want to take take a moment to pause and say probably the most complicated part of dealing with ccip is keeping track of what network you're actually on I'm using one browser to do all of this so you'll see me switching back and forth between networks and honestly that's the hardest part when it comes to ccip is keeping track of that Network so we'll take this code here and we'll put it into remix I've already gone through the copy and paste so we have here a new remix project set up it was an empty project I've copied my source Mentor I've pasted it in here there's one thing that you may note it's this import withdrawal from utils withdraw so we'll need that file as well we'll need to create a utils folder we can do that with new folder right here and within that we'll create a new file called withdraw. Soul inside withdraw we'll use the utils withdraw code that is here this will enable us to withdraw funds from our sinder contract when we finish things up so back to remix and we'll take a look here so we have our Source mentor. Soul it's going to import a few things it's going to import the link token interface ccip router and a client for ccip as well now we're importing the link token interface because you can pay with either link or native tokens on the blockchain that you're using so if you wanted to use the link option you absolutely could do that in this example we're going to be going through using the native token though but this contract is set up to use either one and then we would import our withdraw function so let's take a look at that first so this withdraw function it's basic contract that lets you withdraw if you're the owner of this contract and you can see here we have an error we can't withdraw we then take a look at the actual function itself we're withdrawing and we will withdraw it to the beneficiary address I'm assuming that's probably going to be your address but you could send the remains to whoever you wanted we get the amount that the address has and then we attempt to actually withdraw that amount if something fails we go ahead and revert with that error message above and then we have our withdraw token which will allow us to withdraw specific tokens in this case that would be for the link that you funded this contract with you did that as well all right so let's take a look at source Mentor it's a fairly basic contract we have our contract here Source Mentor we have an enum paying fees in and we can either use native or link one thing to note when we're actually using this in our deployed contract that enum we're going to reference those with zero or one so zero will be native and one will be link it's what you want to pay in when you're actually doing the trans action we then set a couple of addresses one for the router and one for the link token address we go and set up those variables we use them in our Constructor we have an event for when the message is sent in our Constructor you can see here that we take in the router and the link token address now the router is going to be the address of the actual router for your specific chain we'll see that in the documentation where to find that information once we go to deploy this contract we also set up our link token interface and approve the router to spin tokens for us we have received fallback back and we have our mint function here this mint function is what will actually allow us to Mint an nft cross chain so our mint function takes the destination chain selector that's going to be a value that lets us know what the chain ID is that we're going to we have a receiver address that will be the address of the receiver on the destination chain in this case it's going to be the contract that we Deploy on the destination chain to receive this information we have our pay fees in value and again this is going to be whether it's native zero or link one once we have that we go ahead and create a message that will be sent to ccip so it has the receiver it has the data what is data it's going to be the actual message that's passed across chains in this case it's going to be a mint address and a message. sender so this will actually mint to the address that you pass in which will be the message sender so mint a token to whoever sends this message then we have the token amounts that we're passing along any extra args in this case we're hard coding that to nothing and then the fee token here which depending on what we passed in will either be link or the native token we'll retrieve the fee that's required for this cross chain communication so we'll get what the fee is in order to send it across we create a message ID and then if we are paying in link We Pass one message otherwise we send our message along with the value of the fee included and once this is all done we'll emit an event that our message was sent so it's pretty straightforward right we are creating a message and then we'll be sending it to the router address the router will then go to ccip and pass that information to the other chain so let's go ahead and actually get this deployed so I'll be deploying the first contract on spolia you can pick whatever contract you want you'll see how easy that is to do here in just a minute the first thing I need to do is I need to to make sure I am on sapoia all right so I'm on seoa so I'll go to the deploy tab you'll notice I've already got this compiled it's because I have Auto compile turned on and I'll need to make sure that I'm deploying the correct thing so I want to deploy Source mentor and you'll notice I need to pass in the router address and the link token address so where do I find this information if I had to docs. chain.
link and go to to ccip we can look at supported networks and I'm doing this on pooia so I'll go to ethereum sepolia we have our router address here and then we have our link token address right below so I'll click on the copy button I'll get the router address I'll head back to remix I'll put the router address in there then I'll get that link token address put it in here and that's all I need to do so I'll I'll click the trans transact button and confirm the transaction and just like that our contract has been deployed so that's all we need to do on sapoia we're ready to go we've got our source Mentor setup now we're going to switch over to another window just to help me keep track of everything I suggest you do something similar and we'll deploy the destination mentor and nft contracts there so we'll head back to remix and you can see I've done the same thing I've copied over two different solidity contracts where did I get them if we look here in our cross chain nfts repo under contracts I just copied the code straight over for Destination mentor and my nft so on the source chain we need two contracts we need Source mentor and the withdrawal contract on the destination chain we also need two contracts we need the destination mentor and my nft so I'm going and just copied those straight out of this repository pasted them into remix the first thing to look at here is destination Mentor so again we're importing in some ccip contracts the receiver and the client we're also importing in our nft so let's take a look at this nft and see what it looks like it's pretty basic we have erc721 it's ownable and then we've got our contract my nft we store everything for the URI and ipfs if you're interested you can take a look at it there it's basically for a medieval version of Babe Ruth in his rookie years we've got our token ID as you would expect on an nft our Constructor just sets up the basics of the nft giving it a name and a symbol and then we go ahead and create a mint function which safe mints a token to the address that's passed in and sets the URI and then increments the token ID that's it it's pretty straightforward right it's a pretty basic nft but I think it helps kind of get the point across about what's happening with ccip in the destination minor what happens here well we've got our Imports like we mentioned and then we have our destination Mentor contract which is a ccip receiver we declare an instance of the my contract we set up an event for the mint call successful we have our Constructor where we set up the nft is equal to my nft and we pass in an address so first we're going to deploy the my nft contract then when we deploy this destination Mentor will'll give it the nft contracts address and then we have our ccip receive function this is going to be receiving that message in from the router so what exactly does it do well it takes the nft address that we passed in before for and passes along the message data which will cause it to Mint an nft then it will ad mint the success if everything was successful it's pretty straightforward it's just receiving that message and doing a little bit of stuff with it in this case just passing along to the nftd contract now one thing to know when we deploy my inft we'll have the option to transfer owner when you deploy it you'll be the owner and only the owner is allowed to Mint nfts well destination Mentor contract needs to Mint the nfts so we'll need to transfer ownership once we've deployed destination mentor to our Mentor contract so let's go ahead and do both of those now the first thing that we'll need to do is we will need to change our Network remember I said this is probably the hardest part is remembering to change your network I'll change it to Avalanche Fuji I'll change the injected provider here and first I need to deploy my nft we'll get that deployed confirm this transaction once that's deployed I'll be deploying the destination Mentor so again we'll need the router and we are on Fuji so we'll go back to the documentation here to Avalanche Fuji we'll need the router address here and then we need the nft address so my nft is deployed now I'll copy the address for that edit here and so we'll deploy our destination mentor on Fuji once that's done remember we do need to do that one last step of copying our Mentor address coming to our nft contract and transfer ownership to our nft Mentor contract and at this point we should be good to go all right so we'll head back to sapoia we'll change back to sapoia we'll change our wallet back to seoa as well and we will mint an NT so we need a few things here we need the destination chain selector that's going to be from the documentation we need our receiver address and then we need to pay in and a value before we can pay though we have to fund this this actual contract will be paying for the ccip call so we need to send it some funds so we go to metamask go to send paste in the address of this contract which I just copied we can send it something like 01 eth that should be plenty for this transaction confirm that and we'll wait for this to be completed all right so that has been completed now let's go and get our destination chain selector so our destination chain is going to be Avalanche Fuji so we'll copy our chain selector paste that in here Our receiver address will be the destination Mentor so we'll copy that address here paste in there and we'll pay in and remember our enum here at the top we have native and we have link we want to pay in Native which would be the first instance would be zero we'll click transact and confirm our transaction how do we see this in ccip if we open up this debug menu we can grab the transaction hash right here and from here we can go to CC cp. chain. link and that'll take you to the ccip Explorer this is where we can actually see all of the ccip transactions if we paste in our transaction hash right there press return we should see our transaction here yes and so now we're waiting for finality so we'll wait for this to be completed and I'll meet you back here all right so we can see now our transaction has been successful how can we actually see our nft though well there's a couple ways to do this if you just head to test Nets . openc doio and connect your wallet you should be able to go to your wallets profile and from there you should be able to see your NFC so here's that rookie medieval Babe Ruth that I was talking about we can see it's actually on the Avalanche Network and we minted this nft on sepolia effectively right so we did a transaction on AA from there we actually were able to Mint an nft on avalanche now I think that's huge I think it's massively powerful to be able to do a transaction on one network and end up with results on another so from here you could use this tutorial to build your own cross chain nft or whatever product you want to bring to the blockchain environment I appreciate you watching this tutorial I can't wait to see what you build and I'll catch you in the next one [Music]
Automatic transcript — names and jargon may be misspelled.