Alva

Alva

programmer
github
telegram
email

Let's Move - Learn to Move Sui (3): Completing the Deployment of NFT on the Chain

letsmove

Let's Move is a motivational program to learn Move language and get SUI.

Learning Log (alva-lin)

Task 3 - Complete the deployment of NFT on the mainnet#

Task

  • Complete the learning of NFT-related knowledge

  • Deploy the contract that can mint NFT

  • Mint an NFT to your own address

  • Mint an NFT and send it to the address: 0x7b8e0864967427679b4e129f79dc332a885c6087ec9e187b53451a9006ee15f2

Refer to Creating NFT - Sui Move by Example

NFT Contract Code#

The NFT contract code is as follows, including the definition of NFT, various events, and entry functions.

module new_nft::simple_nft {
    use sui::url::{Url, Self};
    use std::string;
    use sui::object::{Self, ID, UID};
    use sui::event;
    use sui::transfer;
    use sui::tx_context::{TxContext, Self};

    struct SimpleNFT has key, store {
        id: UID,
        name: string::String,
        description: string::String,
        url: Url,
    }

    struct SimpleNftMintEvent has copy, drop  {
        object_id: ID,
        creator: address,
        name: string::String,
    }

    struct SimpleNftTransferEvent has copy, drop {
        object_id: ID,
        from: address,
        to: address,
    }

    struct SimpleNftBurnEvent has copy, drop {
        object_id: ID,
    }

    // Public view function
    public fun name(nft: &SimpleNFT): string::String {
        nft.name
    }

    public fun description(nft: &SimpleNFT): string::String {
        nft.description
    }

    public fun url(nft: &SimpleNFT): Url {
        nft.url
    }

    // Entrypoints
    public entry fun mint_nft(
        name: vector<u8>,
        description: vector<u8>,
        url: vector<u8>,
        ctx: &mut TxContext
    ) {
        let sender = tx_context::sender(ctx);
        let nft = SimpleNFT {
            id: object::new(ctx),
            name: string::utf8(name),
            description: string::utf8(description),
            url: url::new_unsafe_from_bytes(url),
        };

        event::emit(SimpleNftMintEvent {
            object_id: object::id(&nft),
            creator: sender,
            name: nft.name,
        });

        transfer::public_transfer(nft, sender);
    }

    public entry fun transfer_nft(
        nft: SimpleNFT,
        recipient: address,
        _: &mut TxContext
    ) {
        event::emit(SimpleNftTransferEvent {
            object_id: object::id(&nft),
            from: tx_context::sender(_),
            to: recipient,
        });

        transfer::public_transfer(nft, recipient);
    }

    public entry fun update_description(
        nft: &mut SimpleNFT,
        description: vector<u8>,
        _: &mut TxContext
    ) {
        nft.description = string::utf8(description);
    }

    public entry fun burn(
        nft: SimpleNFT,
        _: &mut TxContext
    ) {
        let SimpleNFT { id, name: _, description: _, url: _} = nft;

        event::emit(SimpleNftBurnEvent {
            object_id: object::uid_to_inner(&id),
        });

        object::delete(id);
    }
}

Mint NFT#

First switch to the mainnet and publish

 sui client switch --env mainnet
Active environment switched to [mainnet]

 sui client publish --gas-budget 200000000 --skip-dependency-verification
# ....

Get the published package ID, then call the public method

# Declare variables
export PACKAGE_ID=0xf78fb118efd9a86d8e2c54ac18766a1313bd8b0df80c85f859f83d01a5f78981
export NFT_NAME="\"joker\""
export DESCRIPTION="\"simple nft, power by alva-lin\""
export URL=<YOUR_NFT_IMAGE_URL>
sui client call --gas-budget 7500000 --package $PACKAGE_ID --module simple_nft --function mint_nft --args $NFT_NAME $DESCRIPTION $URL

Then transfer the minted NFT to the specified address

# NFT Object ID minted earlier
export NFT_ID=0xc1d108cdeef7666aa3f414bb3ead5faa7cd351e4dc75d0307e2888b640232787
export ADDRESS=0x7b8e0864967427679b4e129f79dc332a885c6087ec9e187b53451a9006ee15f2

# Call the method
sui client call --gas-budget 7500000 --package $PACKAGE_ID --module simple_nft --function transfer_nft --args $NFT_ID $ADDRESS
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.