{
"path": "/posts/how-to-mint-on-sui",
"site": "at://did:plc:ia2zdnhjaokf5lazhxrmj6eu/site.standard.publication/3mbykzswhqc2x",
"tags": [
"web3",
"nfts",
"tutorials"
],
"$type": "site.standard.document",
"title": "How to Mint an NFT on Sui using Pinata and the Sui JS SDK",
"description": "Learn how to use the Sui SDK and Pinata to mint an NFT",
"publishedAt": "2023-02-27T05:00:00.000Z",
"textContent": "import medium from \"../../assets/medium.png\";\nimport OutLinkButton from \"@/components/common/OutLinkButton\";\n\n<OutLinkButton\n\tlink=\"https://medium.com/pinata/how-to-mint-an-nft-on-sui-using-pinata-and-the-sui-js-sdk-4386655e403\"\n\tsite=\"Medium\"\n\timage={medium}\n/>\n\nOne of my favorite pastimes is playing around with new blockchains and seeing what they’re like, what they offer, and how easy they are to build on. Recently I stumbled upon Sui and some of its unique features, such as dynamic metadata and goals to help onboard everyday people. After playing with it myself I was impressed with its speed and smoothness, which is saying something as it’s still in development!\n\nThe only thing I found lacking was some basic explanations of how to mint NFTs on Sui and some of the things you might have to tackle, so I thought I would share that experience and demonstrate this!\n\nTo get started, you should only need the basics such as NodeJS and a text editor like VSCode, as well as some fundamental knowledge about Javascript.\n\nSetting up your NFT with Pinata\n\nWhile Sui features dynamic metadata, you will still want to use a service to store images, video, or any other kind of content you want to turn into an NFT! Pinata is ideal as it uses IPFS which prevents tampering with content which you can read more about here.\n\nGetting started with Pinata is easy! Just visit the signup page here and start out with a free account. Now all you have to do is upload the image you want to use. Do that by visiting the main files page and clicking “Upload” and “Select File.”\n\n!image>\n\nAfter that just follow the steps of selecting your file, give it a name, then upload! Once done it should show up in your files page as seen below, and we will want to copy the CID for later.\n\n!image>\n\nCode Setup with the Sui JS SDK\n\nFirst let’s make a new directory where the project will live using the following terminal command:\n\nNow let’s install the Sui Javascript SDK:\n\nOne small change we need to make to the package.json file is adding the module type, so make sure your package.json looks like this.\n\nLastly, we will want to make a file to run our code which we’ll call mint-nft.js\n\nNow we can get into good stuff — writing the code to mint our NFT!\n\nMinting the NFT\n\nGo ahead and open up your mint-nft.js file in your text editor of choice, and the first thing we’re gonna do is import the following methods at the top of the page.\n\nAll we really need are four things, and you’ll see how they play a part as we start building. Next thing we need to do is create a wallet and get the public address for that wallet, which we can do like so.\n\nAccessing the keypair object makes it super simple to either get the public key or private key, and then turn it into usable data. The Sui address from the public key doesn’t include the leading “0x” so I’m adding that manually here. If you go into the terminal now and run “node mint-nft.js” then you should see an address like this!\n\nNow that we have a wallet address, it’s time to connect to the Sui network and get some test Sui coin! First we’ll declare a new provider and use the JsonRpcProvider, and pass in the Network “DEVNET.”\n\nThen we’ll use the provider to request some test Sui from the Devnet faucet and use our new address as the receiver like so.\n\nLet’s run the node mint-nft.js command now and see what we get!\n\nNice! Through this we can see the Sui we received which equals out to 0.05 Sui. Something you might be wondering is why we have 5 different items here with different ids, and that’s actually a very important piece of the Sui model that isn’t natural. Every coin in Sui has its own unique object id. These are all of the same type of currency which is the default Sui type, but our five pieces are in their own camp. This can cause a problem when we try to use our Sui to mint an NFT because its gonna try to pull from just one of these objects when ideally we want to pull from all of them like a normal blockchain (huge shoutout to Paul Fidika for helping me understand this bit!).\n\nThink of it like this. Let’s say you have a farm with five cow fields, and each field is separated by the type of cow: short haired, long haired, etc. Each field has 10 cows so you technically have 50 cows, but in order to trade for some goats you need 20 cows. You would have to empty two of those cow fields and combine them to trade them. That’s essentially what we’re doing in Sui with coins.\n\nWith that said, in order for us to mint, we need to combine some of our recently acquired Sui drop. We’re gonna do that with the following code.\n\nThere’s quite a bit going on here so let’s break it down.\n\nFirst we declare coin1 and coin2 from the airdrop we just received by accessing those object ids from our fund result. Then we need to declare our signer! This is what lets us use our private key from our keypair to transfer or mint on the chain, and we do that by declaring a new RawSigner and passing in our previously made keypair and provider to connect. Finally we use the mergeCoin method and pass in our two coins, along with a gas budget.\n\nBefore we run this, there is one small thing we’re missing. If you try to run this code now, you will likely get an error that it could not find the object id for the coin. That’s because its was just freshly created and we’re trying to access an object id that isn’t quite discoverable yet. To fix that we’re gonna make a small little helper function called “wait” like so.\n\nThis is really simple and just lets us pass in how many milliseconds we want to wait before continuing our function! Let’s use it after getting our airdrop and passing in 3 seconds. You code should look something like this now.\n\nIf you run this code you should see the result of the airdrop, a pause before merging the coins, and the successful coin merge! Now that we have all our cows in one heard, we can mint an NFT successfully. Let’s take a look!\n\nOur minting function is simply accessing a pre-built smart contract called ‘devnet_nft’ and we’re using the ‘mint’ function. All we have to pass into the arguments is the name of the NFT, the description, and then the asset link!\n\nBefore we mint, I’m gonna do one last console log that will let us take a look at the NFT in the browser. To do that , we need to access the results of the NFT mint and get the object id of the NFT, and then just pass that into a Sui explorer link like so.\n\nNow let’s look at our full code to make sure everything is good, then run it!\n\nIf all works as it should you’ll get a link and then you should see your final NFT!\n\n!image>\n\nYou did it!! 🎉\n\nYou successfully minted an NFT on Sui!! Of course this is only the beginning of what’s possible. Maybe a nice next step is transferring the NFT to another wallet, or maybe even trying to build a Sui marketplace 👀\n\nWith whatever you’re trying to do, Pinata is here to help with tools like our API, Dedicated Gateways to quickly stream images or video content for a marketplace, or token gated solutions using Submarine!\n\nIf you have any questions or want to learn more, feel free to join our Discord and say hi! :)",
"canonicalUrl": "https://stevedylan.dev/posts/how-to-mint-on-sui"
}