AptosMoveNFT项目实操指南:从开发到部署全流程解析随着区块链技术的不断发展,非同质化代币(NFT)已经成为数字资产管理的关键组成部分,为资产所有权和交易方式带来了革命性的变化。Aptos提供了一种创新的数字资产(DA)标准,使开发者能够轻松创建和管理链上独特的资产。通过集合
随着区块链技术的不断发展,非同质化代币(NFT)已经成为数字资产管理的关键组成部分,为资产所有权和交易方式带来了革命性的变化。Aptos 提供了一种创新的数字资产 (DA) 标准,使开发者能够轻松创建和管理链上独特的资产。通过 集合 (Collection) 和 代币 (Token) 的组合,开发者不仅可以轻松创建 NFT,还能为这些资产附加丰富的元数据(如图像、视频链接等),以增强用户的互动性和体验。
本文深入介绍了 Aptos 数字资产 (DA) 标准的核心功能,重点展示了如何通过 集合 (Collection) 和 代币 (Token) 来创建和管理 NFT。我们将从项目初始化开始,详细讲解项目结构设计、智能合约编写、编译与部署过程,以及如何在 Aptos 区块链上调用和操作 NFT 项目。通过本教程的分步实践,读者将全面掌握 Aptos 上的 NFT 开发流程,学习如何通过智能合约实现链上资产管理与交易。
Aptos 数字资产 (DA) 标准
Aptos 的数字资产 (DA) 标准是其独特的非同质化代币 (NFT) 标准。NFT 代表链上唯一的数字资产,并通过集合的形式进行存储。
该标准由两个对象实现:
hello_aptos/AptosNFTFactory on main [?] via 🅒 base
➜ aptos init
Configuring for profile default
Choose network from [devnet, testnet, mainnet, local, custom | defaults to devnet]
testnet
Enter your private key as a hex literal (0x...) [Current: None | No input: Generate new key (or keep one if present)]
No key given, generating key...
Account 0xe6eba0c086998fbd5ad141e4b30942e38bc77e2d5696bf6b1786f7a7722415c8 doesn't exist, creating it and funding it with 100000000 Octas
Account 0xe6eba0c086998fbd5ad141e4b30942e38bc77e2d5696bf6b1786f7a7722415c8 funded successfully
---
Aptos CLI is now set up for account 0xe6eba0c086998fbd5ad141e4b30942e38bc77e2d5696bf6b1786f7a7722415c8 as profile default!
See the account here: https://explorer.aptoslabs.com/account/0xe6eba0c086998fbd5ad141e4b30942e38bc77e2d5696bf6b1786f7a7722415c8?network=testnet
Run `aptos --help` for more information about commands
{
"Result": "Success"
}
tree . -L 6 -I 'build'
.
├── Move.toml
├── scripts
├── sources
│ └── nft.move
└── tests
4 directories, 2 files
Move.toml
文件[package]
name = "AptosNFTFactory"
version = "1.0.0"
authors = []
[addresses]
contract = "_"
[dev-addresses]
[dependencies.AptosFramework]
git = "https://github.com/aptos-labs/aptos-core.git"
rev = "mainnet"
subdir = "aptos-move/framework/aptos-framework"
# https://github.com/aptos-labs/aptos-core/blob/main/aptos-move/framework/aptos-token-objects/Move.toml
[dependencies.AptosTokenObjects]
git = "https://github.com/aptos-labs/aptos-core.git"
rev = "mainnet"
subdir = "aptos-move/framework/aptos-token-objects"
[dev-dependencies]
nft.move
文件module contract::nft {
use aptos_token_objects::collection;
use std::option::Self;
use std::string;
use aptos_token_objects::token;
public entry fun create_collection(creator: &signer) {
let max_supply = 1000;
let royalty = option::none();
// Maximum supply cannot be changed after collection creation
collection::create_fixed_collection(
creator,
string::utf8(b"My Collection Description"),
max_supply,
string::utf8(b"Qiao Collection"),
royalty,
string::utf8(b"ipfs://Qma8NPQhCWjZZdi42e9ZmYjSPb76sgknCrztnCBXJHJczq/2.png")
);
}
public entry fun mint_token(creator: &signer) {
let royalty = option::none();
token::create(
creator,
string::utf8(b"Qiao Collection"),
string::utf8(b"My NFT Description"),
string::utf8(b"QiaoToken"),
royalty,
string::utf8(b"ipfs://Qma8NPQhCWjZZdi42e9ZmYjSPb76sgknCrztnCBXJHJczq/3.png")
);
}
}
hello_aptos/AptosNFTFactory on main [?] via 🅒 base took 1m 2.3s
➜ aptos move compile --named-addresses contract=default --skip-fetch-latest-git-deps
Compiling, may take a little while to download git dependencies...
INCLUDING DEPENDENCY AptosFramework
INCLUDING DEPENDENCY AptosStdlib
INCLUDING DEPENDENCY AptosTokenObjects
INCLUDING DEPENDENCY MoveStdlib
BUILDING AptosNFTFactory
warning[W09001]: unused alias
┌─ /Users/qiaopengjun/Code/Aptos/hello_aptos/AptosNFTFactory/sources/nft.move:3:29
│
3 │ use std::option::{Self, Option};
│ ^^^^^^ Unused 'use' of alias 'Option'. Consider removing it
warning: unused alias
┌─ /Users/qiaopengjun/Code/Aptos/hello_aptos/AptosNFTFactory/sources/nft.move:3:29
│
3 │ use std::option::{Self, Option};
│ ^^^^^^ Unused 'use' of alias 'Option'. Consider removing it
{
"Result": [
"e6eba0c086998fbd5ad141e4b30942e38bc77e2d5696bf6b1786f7a7722415c8::nft"
]
}
hello_aptos/AptosNFTFactory on main [?] via 🅒 base took 4.3s
➜ aptos move deploy-object --address-name contract
Compiling, may take a little while to download git dependencies...
UPDATING GIT DEPENDENCY https://github.com/aptos-labs/aptos-core.git
UPDATING GIT DEPENDENCY https://github.com/aptos-labs/aptos-core.git
INCLUDING DEPENDENCY AptosFramework
INCLUDING DEPENDENCY AptosStdlib
INCLUDING DEPENDENCY AptosTokenObjects
INCLUDING DEPENDENCY MoveStdlib
BUILDING AptosNFTFactory
Do you want to deploy this package at object address 0x05b86a98c7976f97000b08c1df62ddd3948128d64acf50a7fa13d78dfc67181b [yes/no] >
yes
package size 1708 bytes
Do you want to submit a transaction for a range of [212400 - 318600] Octas at a gas unit price of 100 Octas? [yes/no] >
yes
Transaction submitted: https://explorer.aptoslabs.com/txn/0xb6f8785228763792dd36280eaaf619e84e3c81bbc80f1a40f3ddfcfe0ba05333?network=testnet
Code was successfully deployed to object address {}. 0x05b86a98c7976f97000b08c1df62ddd3948128d64acf50a7fa13d78dfc67181b
{
"Result": "Success"
}
create_collection
方法mint_token
方法nft.move
文件module contract::nft {
use aptos_token_objects::collection;
use std::option::Self;
use std::string;
use aptos_token_objects::token;
use aptos_framework::object::{Self, Object};
#[resource_group_member(group = aptos_framework::object::ObjectGroup)]
/// The ambassador token
struct NFTToken has key {
/// Used to mutate the token uri
mutator_ref: token::MutatorRef,
/// Used to burn.
burn_ref: token::BurnRef
}
public entry fun create_collection(creator: &signer) {
let max_supply = 1000;
let royalty = option::none();
// Maximum supply cannot be changed after collection creation
collection::create_fixed_collection(
creator,
string::utf8(b"My Collection Description"),
max_supply,
string::utf8(b"Qiao Collection"),
royalty,
string::utf8(
b"https://learnblockchain.cn/image/avatar/18602_middle.jpg?GjIyGZqa"
)
);
}
public entry fun mint_token(creator: &signer) {
let royalty = option::none();
let token_constructor_ref =
token::create(
creator,
string::utf8(b"Qiao Collection"),
string::utf8(b"My NFT Description"),
string::utf8(b"QiaoToken"),
royalty,
string::utf8(
b"https://img.learnblockchain.cn/space/banner/18602/h7ljMtGq668b998356db8.jpg"
)
);
let object_signer = object::generate_signer(&token_constructor_ref);
let burn_ref = token::generate_burn_ref(&token_constructor_ref);
let mutator_ref = token::generate_mutator_ref(&token_constructor_ref);
let nft_token = NFTToken { mutator_ref, burn_ref };
move_to(&object_signer, nft_token);
}
public entry fun burn(token: Object<NFTToken>) acquires NFTToken {
let ambassador_token = move_from<NFTToken>(object::object_address(&token));
let NFTToken { mutator_ref: _, burn_ref } = ambassador_token;
token::burn(burn_ref);
}
}
/opt/homebrew/bin/aptos move publish --named-addresses contract=default
Compiling, may take a little while to download git dependencies...
UPDATING GIT DEPENDENCY https://github.com/aptos-labs/aptos-core.git
UPDATING GIT DEPENDENCY https://github.com/aptos-labs/aptos-core.git
INCLUDING DEPENDENCY AptosFramework
INCLUDING DEPENDENCY AptosStdlib
INCLUDING DEPENDENCY AptosTokenObjects
INCLUDING DEPENDENCY MoveStdlib
BUILDING AptosNFTFactory
package size 2436 bytes
Do you want to submit a transaction for a range of [188300 - 282400] Octas at a gas unit price of 100 Octas? [yes/no] >
yes
Transaction submitted: https://explorer.aptoslabs.com/txn/0x150f32b61ca082f3d659d57628cdfc489efed24157ffbd2dcee96cb78d358246?network=testnet
{
"Result": {
"transaction_hash": "0x150f32b61ca082f3d659d57628cdfc489efed24157ffbd2dcee96cb78d358246",
"gas_used": 1883,
"gas_unit_price": 100,
"sender": "e6eba0c086998fbd5ad141e4b30942e38bc77e2d5696bf6b1786f7a7722415c8",
"sequence_number": 1,
"success": true,
"timestamp_us": 1727510591901024,
"version": 6052675871,
"vm_status": "Executed successfully"
}
}
Process finished with exit code 0
mint_token
方法这里因为上面已经调用了create_collection
方法,该地址已经有collection,故无需调用。
如果调用则会报错:
create_collection
方法mint_token
方法查看NFT 详情
burn
方法查看钱包
通过本文的实践指南,读者应当已经对 Aptos 的数字资产标准及其 NFT 实现有了深入的理解。我们从项目的初始化到智能合约的部署与调用,完整展示了 Aptos 上 NFT 项目的开发全流程。Aptos 提供的灵活性和扩展性,使开发者能够高效地管理链上独特资产。希望本教程能够帮助开发者在未来的 Web3 生态中创造更多创新性项目。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!