Minting your own NFT collection on the Sui blockchain
One of the fundamental skills of a web3 developer is creating NFT collections. This walkthrough will teach you how to create your own NFT collection on the Sui blockchain.
Prerequisite
Before we can build and deploy our cryptocurrency, make sure to have set up your Sui wallet. We recommend using Martian or the Sui wallet for this walkthrough. Other wallets such as Suiet wallet are currently not supported by the tools we use in this walkthrough.
Sui objects
It is important to note that on the Sui blockchain, almost everything is an NFT. Everything deployed on Sui is an object that is unique, non-fungible, and owned. Check the Sui docs on objects to learn more! We will look more into the Sui objects later.
Creating the NFT contract
Setting up the environment
We will be using the Move Studio IDE to write and deploy our package. Head over to Move Studio to get started. Navigate to the build page and skip the tutorial if prompted (this walkthrough will cover what you need to know).
Once on the build page, create a new package called nftExample
. Select the new package after it is created. Move Studio automatically adds the Sui package as one of the package’s dependency. The other dependency is this package. Enter myNFT
in the new module box and click add. Enter the code below into the editor for the myNFT
module. Click compile to make sure the code has no syntax errors in it.
// This is an example module that creates a new NFT collection
// called MYNFT.
// This module code was provided by the Sui Move by Example book
// (https://examples.sui.io/samples/nft.html)
module nftExample::myNFT {
use sui::url::{Self, Url};
use std::string;
use sui::object::{Self, ID, UID};
use sui::event;
use sui::transfer;
use sui::tx_context::{Self, TxContext};
/// An example NFT that can be minted by anybody
struct MYNFT has key, store {
id: UID,
/// Name for the token
name: string::String,
/// Description of the token
description: string::String,
/// URL for the token
url: Url,
// TODO: allow custom attributes
}
// ===== Events =====
struct NFTMinted has copy, drop {
// The Object ID of the NFT
object_id: ID,
// The creator of the NFT
creator: address,
// The name of the NFT
name: string::String,
}
// ===== Public view functions =====
/// Get the NFT's `name`
public fun name(nft: &MYNFT): &string::String {
&nft.name
}
/// Get the NFT's `description`
public fun description(nft: &MYNFT): &string::String {
&nft.description
}
/// Get the NFT's `url`
public fun url(nft: &MYNFT): &Url {
&nft.url
}
// ===== Entrypoints =====
/// Create a new devnet_nft
public entry fun mint_to_sender(
name: vector<u8>,
description: vector<u8>,
url: vector<u8>,
ctx: &mut TxContext
) {
let sender = tx_context::sender(ctx);
let nft = MYNFT {
id: object::new(ctx),
name: string::utf8(name),
description: string::utf8(description),
url: url::new_unsafe_from_bytes(url)
};
event::emit(NFTMinted {
object_id: object::id(&nft),
creator: sender,
name: nft.name,
});
transfer::transfer(nft, sender);
}
/// Transfer `nft` to `recipient`
public entry fun transfer(
nft: MYNFT, recipient: address, _: &mut TxContext
) {
transfer::transfer(nft, recipient)
}
/// Update the `description` of `nft` to `new_description`
public entry fun update_description(
nft: &mut MYNFT,
new_description: vector<u8>,
_: &mut TxContext
) {
nft.description = string::utf8(new_description)
}
/// Permanently delete `nft`
public entry fun burn(nft: MYNFT, _: &mut TxContext) {
let MYNFT { id, name: _, description: _, url: _ } = nft;
object::delete(id)
}
}
What is the code doing?
The first section of this module is setting up the dependencies used. Information on the Sui library modules can be found in the docs.
Next is the struct that is defining our NFT. Sui objects are all deployed structs. It is important to note that the first attribute of any object struct must be the id
with type UID
. This is what provides the object with it’s address or objectId. The rest of the attributes are based on the criteria of the design. Object structs also must have the key
ability. More information of Sui objects can be found in the docs.
We have another struct. This struct represents an event that is emitted onto the Sui blockchain whenever an NFT has been minted. This struct does not need the key
ability since it is not designed to be an object.
The name
, description
, and url
functions are all public functions that can be used by other modules to get the data associated with a specific NFT object. Since these functions are not marked as entry
, they can only be called by a module and not from a normal Sui transaction.
Next, we have the mint_and_transfer
function which mints a new NFT and transfer ownership to the caller of the function. The function uses the TxContext
struct to retrieve the address of the function caller. More information on the tx_context
standard module can be found in the docs. The NFT is created, the NFTMinted event is emitted using the event
standard module, and the NFT is transferred to the caller using the transfer
standard module. More information on the event
and transfer
standard modules can be found in the docs: event docs, transfer docs.
The next function, is the transfer
function, which transfers a provided NFT to the provided recipient address.
We also have the update_description
which updates the description attribute of a provided NFT.
Finally, we have burn
function which uses the object
module to delete the provided NFT object. More information on the object
standard module can be found in the docs.
Deploying the package
Once the module is compiling, head over to the deploy page of the studio. Connect your Sui or Martian wallet to the site.
Select the nftExample
package and hit deploy. Confirm the deployment with your wallet.
Now, we can see the nftExample
package. Clicking the box and arrow icons in the objects or package will show it on the Sui explorer page (if there is an error with the Sui explorer page, make sure you are on the right network).
Note, that if you get disconnected from Move Studio, you can re-add your contracts and objects to the page with the manual section section. Just retrieve the objectIds from your account’s previous transactions and add them using the “add existing package or object” section.
Minting an NFT
Now, lets try minting our first NFT! We will be calling the mint_to_sender
function in the myNFT
module. Select the myNFT::mint_to_sender
function under the package details. This will give us the interface to input the arguments and execute the function.
The arguments are the name, description, and the image url. Enter NFT #1
for the name, This is my first NFT!
for the description, and this url: (https://cdn.discordapp.com/attachments/1016489500953808946/1070191436097650718/DanTheMan8300_blockchain_cryptocurrency_Sui_blockchain_banner_f_849965e4-e4c7-4675-bf5b-e1a22c37a93b.png) for the picture. Hit execute, then confirm the transaction on your wallet. Once the transaction has been confirmed, we can verify the NFT on the wallet. View the transaction on the Sui explorer by clicking the box and arrow icon. Copy the NFT’s address and paste it into the add existing object section. Click add. This will add the deployed NFT to our page. You can also view the NFT in your wallet!
Congrats! You have successfully minted your first NFT!
Updating the NFT
Lets update the NFT using the update_description
function. This function takes in the address of the NFT and the new description. Copy and paste the NFT address into the first argument, and enter a new description. Execute the call and confirm the transaction on the wallet. We can now see the NFT has the updated description.
Transferring the NFT
Let’s create another NFT and transfer it to another address. First mint a second NFT using the steps above (you can try to use your own input as well!). You can use this image url if you don’t have one (https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/ocean-quotes-index-1624414741.jpg). Once you have the second NFT in your account, use the transfer
function to send it to another account. Enter the address of the second NFT and the address of the recepient. Execute the call and confirm the transaction. Verify that the second NFT is now owned by the recepient account.
Burning the NFT
Finally, use the burn
function to permanently delete the NFT. Verify the NFT is delete on your wallet.
Conclusion
Now you know how to create your NFT collection on the Sui blockchain! You’ve practiced writing, deploying, and interacting with contracts using the Move Studio IDE as well as learn how objects work on the Sui blockchain!