Creating and managing your own NFTs using the 0x1::simple_nft module involves several steps, including creating a collection, minting NFTs, checking minted NFTs, and transferring NFTs. This tutorial will guide you through these steps, focusing on syntax and essential commands to interact with the blockchain.
Tutorial
Step 1: Create Collection
To mint NFTs, you first need to create a collection. You'll use the 0x1::simple_nft::create_collection function for this purpose. This function requires numerous parameters, including descriptions, maximum supply, collection name, and various flags to allow mutability in your collection's properties.
After creating a collection, you can mint NFTs within this collection using the 0x1::simple_nft::mint function. This function also requires various parameters, including the collection name, a description of the NFT, the token ID, and an optional recipient address.
public entry fun mint(
creator: &signer,
collection: String,
description: String,
token_id: String,
uri: String,
property_keys: vector<String>,
property_types: vector<String>,
property_values: vector<vector<u8>>,
to: Option<address>,
)
import {
bcs,
LCDClient,
MnemonicKey,
MsgExecute,
Wallet,
} from '@initia/initia.js';
async function mintNft() {
const lcd = new LCDClient('[rest-url]', {
gasPrices: '0.15uinit',
gasAdjustment: '1.5',
});
const key = new MnemonicKey({
mnemonic: 'beauty sniff protect ...',
});
const wallet = new Wallet(lcd, key);
const msgs = [
new MsgExecute(
key.accAddress,
'0x1',
'simple_nft',
'mint',
[],
[
bcs.string().serialize('my_collection').toBase64(), // collection name
bcs.string().serialize('nft_description').toBase64(), // nft description
bcs.string().serialize('nft_1').toBase64(), // nft token id
bcs.string().serialize('').toBase64(), // nft uri
bcs.vector(bcs.string()).serialize([]).toBase64(), // property keys
bcs.vector(bcs.string()).serialize([]).toBase64(), // property types
bcs.vector(bcs.vector(bcs.u8())).serialize([]).toBase64(), // property values
bcs.option(bcs.address()).serialize(key.accAddress).toBase64(), // to, if null mint to creator
]
),
];
// sign tx
const signedTx = await wallet.createAndSignTx({ msgs });
// send(broadcast) tx
lcd.tx.broadcastSync(signedTx).then(res => console.log(res));
// {
// height: 0,
// txhash: '162AA29DE237BD060EFEFFA862DBD07ECD1C562EBFDD965AD6C34DF856B53DC2',
// raw_log: '[]'
// }
}
mintNft();
Step 3: Check Minted NFTs
You can check the NFTs you've minted by accessing specific APIs. For checking collections and tokens owned by a specific address, use the following endpoints, replacing [addr] and [collection_address] with actual values:
Collections owned by a specific address:
https://api.initiation-1.initia.xyz/indexer/nft/v1/collections/by_account/[addr]
NFTs owned by a specific address:
https://api.initiation-1.initia.xyz/indexer/nft/v1/tokens/by_account/[addr]
Step 4: Transfer NFT
To transfer an NFT, use the 0x1::object::transfer_call function. This function allows transferring a single NFT from one account to another.
public entry fun transfer_call(
owner: &signer,
object: address,
to: address,
)