From 51e127853edea8e03dec04a836e411dcaa012748 Mon Sep 17 00:00:00 2001 From: greyhao Date: Sat, 16 Nov 2024 09:47:48 +0800 Subject: [PATCH] nft add display; learn record; --- mover/greyhao/co-learn-2411/readme.md | 2 + .../code/nft_greyhao/sources/nft_greyhao.move | 42 ++++++++++++++++--- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/mover/greyhao/co-learn-2411/readme.md b/mover/greyhao/co-learn-2411/readme.md index a34a3fd39..e6a158cd9 100644 --- a/mover/greyhao/co-learn-2411/readme.md +++ b/mover/greyhao/co-learn-2411/readme.md @@ -29,6 +29,7 @@ - [x] 第二篇笔记 - [x] 第三篇笔记 - [x] 第四篇笔记 +- [x] 第五篇笔记 ## 对外输出学习笔记 @@ -36,6 +37,7 @@ - [x] 第二篇笔记 [学习笔记链接](https://learnblockchain.cn/article/9839) - [x] 第三篇笔记 [学习笔记链接](https://learnblockchain.cn/article/9866) - [x] 第四篇笔记 [学习笔记链接](https://learnblockchain.cn/article/9876) +- [x] 第五篇笔记 [学习笔记链接](https://learnblockchain.cn/article/9925) ## 在HOH社区公众号发布自己的技术文章 diff --git a/mover/greyhao/code/nft_greyhao/sources/nft_greyhao.move b/mover/greyhao/code/nft_greyhao/sources/nft_greyhao.move index e21ff15e2..9b3eb9a0a 100644 --- a/mover/greyhao/code/nft_greyhao/sources/nft_greyhao.move +++ b/mover/greyhao/code/nft_greyhao/sources/nft_greyhao.move @@ -4,14 +4,19 @@ module nft_greyhao::nft_greyhao; use std::string::{String , utf8}; use sui::url::{Self, Url}; use sui::event; +use sui::package; +use sui::display; public struct GreyhaoNFT has key, store { id: UID, name: String, description: String, - url: Url, + image_url: Url, } +// OTW for module +public struct NFT_GREYHAO has drop {} + // ====Evens ==== public struct NFTMinted has copy, drop { object_id: ID, @@ -32,8 +37,8 @@ public fun description(nft: &GreyhaoNFT): &String { } /// Get NFT's url -public fun url(nft: &GreyhaoNFT): &Url { - &nft.url +public fun image_url(nft: &GreyhaoNFT): &Url { + &nft.image_url } /// update the `description` of `nft` to `new_description` @@ -41,6 +46,33 @@ public fun update_description(nft: &mut GreyhaoNFT, new_description: vector, nft.description = utf8(new_description) } +fun init(otw: NFT_GREYHAO, ctx: &mut TxContext) { + // key包含的值就是 GreyhaoNFT 对象中除 id 外的字段 + let keys = vector[ + b"name".to_string(), + b"description".to_string(), + b"image_url".to_string(), + ]; + + let values = vector[ + b"{name}".to_string(), + b"{description}".to_string(), + b"{image_url}".to_string(), + ]; + + let publisher = package::claim(otw, ctx); + + let mut display = display::new_with_fields(&publisher, keys, values, ctx); + + display.update_version(); + + transfer::public_transfer(publisher, ctx.sender()); + transfer::public_transfer(display, ctx.sender()); + + let nft = nftInfo(ctx); + transfer::transfer(nft, tx_context::sender(ctx)); +} + #[allow(lint(self_transfer))] public fun mint(ctx: &mut TxContext){ let recipient = tx_context::sender(ctx); @@ -74,12 +106,12 @@ fun nftInfo(ctx: &mut TxContext): GreyhaoNFT { id: object::new(ctx), name: utf8(b"GREYHAO"), description: utf8(b"NFT was created by GreyHao."), - url: url, + image_url: url, } } /// delete `nft` public fun burn(nft: GreyhaoNFT, _: &mut TxContext) { - let GreyhaoNFT {id, name: _, description: _, url: _} = nft; + let GreyhaoNFT {id, name: _, description: _, image_url: _} = nft; id.delete() } \ No newline at end of file