本文转自本人微信公众号的文章,所以带有wx水印,希望大家能理解一下。后续新文会自己留存不带水印。
1.1 创建合约stomnft
module stomnft::stomnft{
use sui::tx_context::{sender, TxContext};
use std::string::{utf8, String};
use sui::transfer;
use sui::object::{Self, UID};
use sui::package;
use sui::display;
struct StomNFT has key, store {
id: UID,
tokenId: u64
}
struct STOMNFT has drop {}
struct State has key {
id: UID,
count: u64
}
#[allow(unused_function)]
fun init(witness: STOMNFT, ctx: &mut TxContext) {
let keys = vector[
utf8(b"name"),
utf8(b"image_url"),
utf8(b"description"),
];
let values = vector[
// For `name` we can use the `.name` property
utf8(b"STOMNFT"),
utf8(b"https://avatars.githubusercontent.com/baicaiyihao"),
utf8(b"github:baicaiyihao"),
];
let publisher = package::claim(witness, ctx);
let display = display::new_with_fields<StomNFT>(
&publisher, keys, values, ctx
);
display::update_version(&mut display);
transfer::public_transfer(publisher, sender(ctx));
transfer::public_transfer(display, sender(ctx));
transfer::share_object(State{
id: object::new(ctx),
count: 0
});
}
#[lint_allow(self_transfer)]
public entry fun mint(state: &mut State, ctx: &mut TxContext){
state.count = state.count + 1;
let nft = StomNFT {
id:object::new(ctx),
tokenId:state.count
};
transfer::public_transfer(nft,sender(ctx));
}
}
发布合约,获取State objectid和packageid
│ ┌── │
│ │ ObjectID: 0xf9071210aa73f66e42deaf8bd7825f0d5154d3fb4a9476b17af76e0fa1ebfe52 │
│ │ Sender: 0x21f62d821142d1a72d2b78ce0e3fee2ae01f38293e583c46e9a4be3f91544767 │
│ │ Owner: Shared │
│ │ ObjectType: 0x8319b92f529ce43d8fc796dbe879336fc7c67e28d3c7f5dc99087d2ef50b67c::stomnft::State │
│ │ Version: 22846776 │
│ │ Digest: BuTPyykpvX2hkyQ6vbfCnvTBoDHFgDNdqu4D3sXPJQ9j │
│ └── │
│ │
│ Published Objects: │
│ ┌── │
│ │ PackageID: 0x08319b92f529ce43d8fc796dbe879336fc7c67e28d3c7f5dc99087d2ef50b67c │
│ │ Version: 1 │
│ │ Digest: GdkoaZdjTKknv9x92wsbmiQ2WnDjhiTHqGVywE8wV4Sb │
│ │ Modules: stomnft │
│ └──
然后mint
sui client call --package 0x08319b92f529ce43d8fc796dbe879336fc7c67e28d3c7f5dc99087d2ef50b67c --module stomnft --function mint --args 0xf9071210aa73f66e42deaf8bd7825f0d5154d3fb4a9476b17af76e0fa1ebfe52 --gas-budget 10000000
Transaction Digest: F5PfTeotQTDY5QW4526v8QnJqj7gCErVmTLroMPjgZK5
│ │
│ Created Objects: │
│ ┌── │
│ │ ObjectID: 0x5b589da5f09eb03c2a9dbbd1bffb13e393a57eac4b09fb0248d0a2e20845efd2 │
│ │ Sender: 0x21f62d821142d1a72d2b78ce0e3fee2ae01f38293e583c46e9a4be3f91544767 │
│ │ Owner: Account Address ( 0x21f62d821142d1a72d2b78ce0e3fee2ae01f38293e583c46e9a4be3f91544767 ) │
│ │ ObjectType: 0x8319b92f529ce43d8fc796dbe879336fc7c67e28d3c7f5dc99087d2ef50b67c::stomnft::StomNFT │
│ │ Version: 22846777 │
│ │ Digest: EvTcvKnSv7fP8mcdt3UewifAEcpsMKDag7EFzuudksTr
浏览器查看
1.2 实现自定义参数传入
module stomnft::stomnft{
use sui::tx_context::{sender, TxContext};
use std::string::{utf8, String};
use sui::transfer;
use sui::object::{Self, UID};
use sui::package;
use sui::display;
struct StomNFT has key, store {
id: UID,
name:String,
description:String,
image_url:String,
tokenId: u64
}
struct STOMNFT has drop {}
struct State has key {
id: UID,
count: u64
}
#[allow(unused_function)]
fun init(witness: STOMNFT, ctx: &mut TxContext) {
let keys = vector[
utf8(b"name"),
utf8(b"image_url"),
utf8(b"description"),
];
let values = vector[
utf8(b"{name}"),
utf8(b"{image_url}"),
utf8(b"{description}"),
];
let publisher = package::claim(witness, ctx);
let display = display::new_with_fields<StomNFT>(
&publisher, keys, values, ctx
);
display::update_version(&mut display);
transfer::public_transfer(publisher, sender(ctx));
transfer::public_transfer(display, sender(ctx));
transfer::share_object(State{
id: object::new(ctx),
count: 0
});
}
#[lint_allow(self_transfer)]
public entry fun mint(state: &mut State,name:String,description:String,image_url:String, ctx: &mut TxContext){
state.count = state.count + 1;
let nft = StomNFT {
id:object::new(ctx),
name:name,
description:description,
image_url:image_url,
tokenId:state.count
};
transfer::public_transfer(nft,sender(ctx));
}
}
修改了StomNFT{},新增了name,description,image_url实现自己自定义发布NFT,通过自己传入参数来创建想要的NFT name和image等。
发布合约
│ ┌── │
│ │ ObjectID: 0x1aaa5fc0c9d2cb97f124b16989ac50fdb188918aa695545a54fc508f17649f3a │
│ │ Sender: 0x21f62d821142d1a72d2b78ce0e3fee2ae01f38293e583c46e9a4be3f91544767 │
│ │ Owner: Shared │
│ │ ObjectType: 0x2ab3a7d840fd2b13573e1d9cad4826b3300b355b990905d4df2c1dd9e876492b::stomnft::State │
│ │ Version: 22846778 │
│ │ Digest: FE9epZDNPyBLJHzBq8bS8UqV64wfEZ9btfh2At4Jw8Yv │
│ └── │
│ Published Objects: │
│ ┌── │
│ │ PackageID: 0x2ab3a7d840fd2b13573e1d9cad4826b3300b355b990905d4df2c1dd9e876492b │
│ │ Version: 1 │
│ │ Digest: 9CiaVjozNmX91r7gtNRe4BdVhQ3CPK8BfiQ2SoMjzVLq │
│ │ Modules: stomnft │
调用mint
sui client call --package 0x2ab3a7d840fd2b13573e1d9cad4826b3300b355b990905d4df2c1dd9e876492b --module stomnft --function mint --args 0x1aaa5fc0c9d2cb97f124b16989ac50fdb188918aa695545a54fc508f17649f3a STOMNFT github:baicaiyihao https://avatars.githubusercontent.com/baicaiyihao --gas-budget 10000000
然后获取NFT的objectid
│ Created Objects: │
│ ┌── │
│ │ ID: 0x2058f902e71baa1e69167b395773e5ba61dea383b2edc048a7ed494da8bd7e91 │
│ │ Owner: Account Address ( 0x21f62d821142d1a72d2b78ce0e3fee2ae01f38293e583c46e9a4be3f91544767 ) │
│ │ Version: 22846779 │
│ │ Digest: 3yFwegVmkG3ayNc7MopPzm86DxUV7SyE2DSqXsF7GJKx │
│ └── │
display参考链接:
https://examples.sui.io/basics/display.html
https://www.youtube.com/embed/iVlvx-On59w?si=tM1VJKBqd83Xc-G5
Sui move_cn社交账号
telegram: https://t.me/move_cn
X(twitter): https://twitter.com/move_cn
QQ群: 79489587
微信公众号:Move中文
Sui中文开发群: https://t.me/sui_dev_cn
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!