主网NFT的发布合约

1.什么是nft?

NFT:Non-fungible-token

非同质化货币

2.新建suimove项目

使用sui move new 项目名命令新建sui move项目

rust 复制代码
sui move new nft_qyx

项目结构如下:

3.写nft合约

bash 复制代码
module qyx123::nft{
    use sui::object::{Self, UID};
    use sui::transfer;
    use sui::tx_context::{Self, TxContext};
    use sui::display;
    use sui::package;
    use std::string::utf8;
    struct NFT has drop {}

	//我的nft结构体,属性id和tokenid
    struct MyNFT has key,store{
        id: UID,
        tokenId: u64
    }

    struct State has key {
        id: UID,
        count: u64
    }

    fun init(witness: NFT, ctx:&mut TxContext){
    	//键值对,键名和值一一对应
    	//键名
        let keys = vector[
            utf8(b"name"),
            utf8(b"collection"),
            utf8(b"image_url"),
            utf8(b"description")
        ];
		//value值
        let values = vector[
            utf8(b"MyNFT #{tokenId}"),
            utf8(b"MyNFT Collection"),
            utf8(b"自己的头像地址"),
            utf8(b"This is My NFT")
        ];
        
		//使用 packge和display发布地址
		
		//创建发布者
        let publisher = package::claim(witness,ctx);
        //创建display
        let display = display::new_with_fields<MyNFT>(&publisher, keys, values, ctx);
        display::update_version(&mut display);
        
        transfer::public_transfer(publisher, tx_context::sender(ctx));
        transfer::public_transfer(display, tx_context::sender(ctx));
		//计数器
        transfer::share_object(State{
            id: object::new(ctx),
            count: 0
        });
    
    }

    entry public fun mint( state:&mut State, ctx: &mut TxContext){
        let sender = tx_context::sender(ctx);
        //计数器
        state.count = state.count + 1;
        let nft = MyNFT {
            id: object::new(ctx),
            tokenId: state.count,
        };
        transfer::public_transfer(nft, sender);
    }

}

4.引入图片

找到一张图片,转为base64,代码放到上面的value中的图像地址中

bash 复制代码
		//value值
        let values = vector[
            utf8(b"MyNFT #{tokenId}"),
            utf8(b"MyNFT Collection"),
            utf8(b"自己的头像地址"),//此处
            utf8(b"This is My NFT")
        ];

5.发布到开发网testnet

5.1列出当前所有网络别名和当前网络:

bash 复制代码
sui client envs

此处显示我目前为devnet

5.2切换网络命令可以切换到对应网络

切换网络:

bash 复制代码
sui client switch --env [network alias]

5.3编译代码

跳过拉取最新git依赖

rust 复制代码
sui move build --skip-fetch-latest-git-deps

编辑不通过记得在mint函数前加上

bash 复制代码
  #[lint_allow(self_transfer)]

编译通过

5.4发布到testnet

bash 复制代码
sui move test  --skip-fetch-latest-git-deps

sui client publish --skip-fetch-latest-git-deps --skip-dependency-verification --gas-budget 500000000

不成功在init函数前加上

bash 复制代码
    #[allow(unused_function)]

发布成功

通过sui浏览器查询packageID

https://suiexplorer.com/

6.mint

复制计数器id到mint处执行

支付后mint成功

相关推荐
Sakura_j_h_8 分钟前
Python 在定时任务里输出 emoji,UnicodeEncodeError 从哪来
开发语言·字符编码·windows·python·定时任务
小成202303202655 小时前
C++~07补充
开发语言·c++
2401_885885048 小时前
国际语音C#接口怎么写?.NET环境下实现国际语音验证码批量下发
开发语言·c#·.net
程序猿乐锅9 小时前
【从0-1学习Python | 第七篇】Python 面向对象基础
开发语言·笔记·后端·python·学习·1024程序员节
Wang's Blog9 小时前
Java 项目实战: 交友项目 - 查询圈子个人动态:查询我的动态需求分析
java·开发语言·交友
凯瑟琳.奥古斯特9 小时前
力扣1016高效解法:剪枝优化技巧
开发语言·c++·算法·leetcode·职场和发展
坤坤子吖10 小时前
C++继承:友元、静态成员、多继承与组合
开发语言·c++·笔记·学习·1024程序员节
技术小甜甜10 小时前
【Cursor】Skill 被标黄色警告,怎么改成合格的 SKILL.md
开发语言·javascript·ai编程·cursor·1024程序员节·ai agent·skills
小小龙学IT10 小时前
Python Polars 高性能数据处理引擎深度解析
开发语言·python·rust·1024程序员节