search-aptos-examples
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSearch Aptos Examples Skill
搜索Aptos示例Skill
Overview
概述
This skill helps you find relevant examples in official Aptos repositories before writing new contracts. Always
search examples first to follow established patterns.
Repositories:
- — 53+ official Move examples demonstrating best practices
aptos-labs/aptos-core/aptos-move/move-examples/ - — 17 curated educational examples covering design patterns, Move 2 features, composable NFTs, and more
aptos-labs/daily-move/snippets/
本Skill可帮助你在编写新合约前在Aptos官方仓库中查找相关示例。请务必优先搜索示例,遵循已验证的开发模式。
涉及仓库:
- — 53+个官方Move示例,展示最佳开发实践
aptos-labs/aptos-core/aptos-move/move-examples/ - — 17个精选的教学示例,覆盖设计模式、Move 2特性、可组合NFT等内容
aptos-labs/daily-move/snippets/
Core Workflow
核心工作流
Step 1: Identify What You're Building
步骤1:明确开发内容
Categorize your contract:
- NFTs/Tokens: NFT collections, digital assets, collectibles
- Fungible Assets: Coins, tokens, currencies
- DeFi: DEXs, AMMs, lending, staking
- Governance: DAOs, voting, proposals
- Marketplace: Trading, escrow, auctions
- Gaming: Items, characters, game logic
- Infrastructure: Registries, configs, utilities
对你的合约进行分类:
- NFTs/Tokens:NFT合集、数字资产、收藏品
- Fungible Assets:代币、通证、货币
- DeFi:DEX、AMM、借贷、质押
- Governance:DAO、投票、提案
- Marketplace:交易、托管、拍卖
- Gaming:道具、角色、游戏逻辑
- Infrastructure:注册表、配置、工具类
Step 2: Search Relevant Examples
步骤2:搜索相关示例
Priority Examples by Category:
按分类排序的优先级示例:
NFTs & Token Objects
NFT与Token对象
- - Modern object-based tokens (V2 pattern)
token_objects/ - - NFT minting patterns
mint_nft/ - - NFT-gated governance
nft_dao/ - - Collection management
collection_manager/ - (daily-move) - NFTs that contain other NFTs
composable-nfts/ - (daily-move) - Mutable NFT metadata patterns
modifying-nfts/ - (daily-move) - Concurrent NFT minting
parallel-nfts/ - (daily-move) - Fractionalized/liquid NFTs
liquid-nfts/
When to use: Building NFT collections, digital collectibles, tokenized assets
- - 基于对象的现代化代币(V2模式)
token_objects/ - - NFT铸造模式
mint_nft/ - - NFT门控治理
nft_dao/ - - 合集管理
collection_manager/ - (daily-move) - 可嵌套其他NFT的可组合NFT
composable-nfts/ - (daily-move) - 可变NFT元数据模式
modifying-nfts/ - (daily-move) - 并发NFT铸造
parallel-nfts/ - (daily-move) - 碎片化/流动型NFT
liquid-nfts/
适用场景: 开发NFT合集、数字收藏品、通证化资产
Fungible Assets
同质化资产
- - Modern fungible token standard
fungible_asset/ - - Basic coin implementation
coin/ - - Controlled fungible assets
managed_fungible_asset/ - (daily-move) - FA lockup and escrow patterns
fa-lockup-example/ - (daily-move) - Fractional token ownership
fractional-token/ - (daily-move) - Controlled minting with access control
controlled-mint/
When to use: Creating tokens, currencies, reward points
- - 现代化同质化代币标准
fungible_asset/ - - 基础代币实现
coin/ - - 受管控的同质化资产
managed_fungible_asset/ - (daily-move) - 同质化资产锁仓与托管模式
fa-lockup-example/ - (daily-move) - 碎片化代币所有权
fractional-token/ - (daily-move) - 带访问控制的受控铸造
controlled-mint/
适用场景: 创建代币、货币、奖励积分
DeFi & Trading
DeFi与交易
- - NFT marketplace patterns
marketplace/ - - Simple token swap
swap/ - - AMM pool implementation
liquidity_pool/ - - Staking mechanisms
staking/
When to use: Building DEXs, marketplaces, trading platforms
- - NFT交易市场模式
marketplace/ - - 简单代币兑换
swap/ - - AMM流动池实现
liquidity_pool/ - - 质押机制
staking/
适用场景: 开发DEX、交易市场、交易平台
Governance & DAOs
治理与DAO
- - DAO governance patterns
dao/ - - Voting mechanisms
voting/ - - Multi-signature accounts
multisig/
When to use: Building DAOs, governance systems, voting
- - DAO治理模式
dao/ - - 投票机制
voting/ - - 多签账户
multisig/
适用场景: 开发DAO、治理系统、投票功能
Basic Patterns
基础模式
- - Module structure basics
hello_blockchain/ - - Simple state management
message_board/ - - Resource patterns (legacy - avoid for new code)
resource_account/ - (daily-move) - Error code conventions and patterns
error-codes/ - (daily-move) - Function visibility and access
private-vs-public/ - (daily-move) - Object model fundamentals
objects/
When to use: Learning Move basics, simple contracts
- - 模块结构基础
hello_blockchain/ - - 简单状态管理
message_board/ - - 资源模式(已过时,新代码请勿使用)
resource_account/ - (daily-move) - 错误码规范与模式
error-codes/ - (daily-move) - 函数可见性与访问控制
private-vs-public/ - (daily-move) - 对象模型基础
objects/
适用场景: 学习Move基础、开发简单合约
Advanced Patterns
高级模式
- - Object model exploration
object_playground/ - - Capability-based security
capability/ - - Upgradeable contracts
upgradeable/ - (daily-move) - Autonomous objects and other design patterns
design-patterns/ - (daily-move) - Struct-based capability patterns
struct-capabilities/ - (daily-move) - Move 2 language features and idioms
move-2/ - (daily-move) - Storage layout and optimization patterns
storage/ - (daily-move) - Heap data structure implementation
data-structures/
When to use: Complex architectures, security patterns
- - 对象模型探索
object_playground/ - - 基于能力的安全机制
capability/ - - 可升级合约
upgradeable/ - (daily-move) - 自治对象及其他设计模式
design-patterns/ - (daily-move) - 基于结构体的能力模式
struct-capabilities/ - (daily-move) - Move 2语言特性与惯用写法
move-2/ - (daily-move) - 存储布局与优化模式
storage/ - (daily-move) - 堆数据结构实现
data-structures/
适用场景: 复杂架构、安全模式开发
Gaming
游戏
- (daily-move) - Randomized loot box mechanics
lootbox/
When to use: Building games, randomized rewards, loot systems
- (daily-move) - 随机盲盒机制
lootbox/
适用场景: 开发游戏、随机奖励、战利品系统
Step 3: Review Example Code
步骤3:审查示例代码
What to look for:
-
Module Structure:
- How are imports organized?
- What structs are defined?
- How are error codes structured?
-
Object Creation:
- How are objects created?
- Which refs are generated?
- How is ownership managed?
-
Access Control:
- How is signer authority verified?
- How is object ownership checked?
- What roles/permissions exist?
-
Operations:
- How are transfers handled?
- How are updates secured?
- What validations are performed?
-
Testing:
- What test patterns are used?
- How is coverage achieved?
重点关注内容:
-
模块结构:
- 导入是如何组织的?
- 定义了哪些结构体?
- 错误码是如何组织的?
-
对象创建:
- 对象是如何创建的?
- 生成了哪些引用?
- 所有权是如何管理的?
-
访问控制:
- 签名者权限是如何验证的?
- 对象所有权是如何检查的?
- 存在哪些角色/权限?
-
操作逻辑:
- 转账是如何处理的?
- 更新操作是如何保障安全的?
- 执行了哪些校验?
-
测试逻辑:
- 使用了哪些测试模式?
- 如何实现测试覆盖?
Step 4: Adapt Patterns to Your Use Case
步骤4:适配模式到你的业务场景
Don't copy blindly - adapt:
- Understand the pattern: Why is it structured this way?
- Identify core concepts: What security checks are critical?
- Adapt to your needs: Modify for your specific requirements
- Maintain security: Keep all security checks intact
- Test thoroughly: Ensure 100% coverage
不要盲目复制 — 按需适配:
- 理解模式: 为什么要这么设计结构?
- 识别核心概念: 哪些安全检查是关键的?
- 按需适配: 根据你的具体需求进行修改
- 保留安全性: 完整保留所有安全检查逻辑
- 充分测试: 确保100%测试覆盖
Example Discovery Table
示例查找对照表
| Building | Search For | Source | Key Files |
|---|---|---|---|
| NFT Collection | | aptos-core | |
| Fungible Token | | aptos-core | |
| Marketplace | | aptos-core | |
| DAO | | aptos-core | |
| Token Swap | | aptos-core | |
| Staking | | aptos-core | |
| Simple Contract | | aptos-core | |
| Object Patterns | | aptos-core | |
| Composable NFTs | | daily-move | |
| FA Lockup/Escrow | | daily-move | |
| Design Patterns | | daily-move | |
| Move 2 Features | | daily-move | |
| Data Structures | | daily-move | |
| Storage Patterns | | daily-move | |
| Loot Box Patterns | | daily-move | |
| 开发内容 | 搜索关键词 | 来源 | 核心文件 |
|---|---|---|---|
| NFT合集 | | aptos-core | |
| 同质化代币 | | aptos-core | |
| 交易市场 | | aptos-core | |
| DAO | | aptos-core | |
| 代币兑换 | | aptos-core | |
| 质押 | | aptos-core | |
| 简单合约 | | aptos-core | |
| 对象模式 | | aptos-core | |
| 可组合NFT | | daily-move | |
| 同质化资产锁仓/托管 | | daily-move | |
| 设计模式 | | daily-move | |
| Move 2特性 | | daily-move | |
| 数据结构 | | daily-move | |
| 存储模式 | | daily-move | |
| 盲盒模式 | | daily-move | |
How to Access Examples
如何访问示例
Option 1: GitHub Web Interface
方案1:GitHub网页界面
https://github.com/aptos-labs/aptos-core/tree/main/aptos-move/move-examplesBrowse online and read source files directly.
https://github.com/aptos-labs/aptos-core/tree/main/aptos-move/move-examples直接在线浏览阅读源代码文件。
Option 2: Clone Repository (Recommended)
方案2:克隆仓库(推荐)
bash
undefinedbash
undefinedClone Aptos core
克隆Aptos core仓库
Navigate to examples
进入示例目录
cd aptos-core/aptos-move/move-examples
cd aptos-core/aptos-move/move-examples
List all examples
列出所有示例
ls -la
ls -la
View specific example
查看指定示例
cd token_objects
cat sources/token.move
undefinedcd token_objects
cat sources/token.move
undefinedOption 3: daily-move Repository
方案3:daily-move仓库
Browse online:
https://github.com/aptos-labs/daily-move/tree/main/snippetsClone locally:
bash
git clone https://github.com/aptos-labs/daily-move.git
cd daily-move/snippets
ls -la在线浏览:
https://github.com/aptos-labs/daily-move/tree/main/snippets克隆到本地:
bash
git clone https://github.com/aptos-labs/daily-move.git
cd daily-move/snippets
ls -laOption 4: Search Aptos Documentation
方案4:搜索Aptos文档
https://aptos.dev/build/smart-contractsMany examples are documented with explanations.
https://aptos.dev/build/smart-contracts很多示例都附带文档说明。
Common Patterns from Examples
示例中的通用模式
Pattern 1: Object Creation (from token_objects)
模式1:对象创建(来自token_objects)
move
// From: token_objects/sources/token.move
public entry fun create_token(
creator: &signer,
collection_name: String,
description: String,
name: String,
uri: String,
) {
let constructor_ref = token::create_named_token(
creator,
collection_name,
description,
name,
option::none(),
uri,
);
// ... additional setup
}Takeaway: Use named tokens for collections, create_object for unique items.
move
// 来源: token_objects/sources/token.move
public entry fun create_token(
creator: &signer,
collection_name: String,
description: String,
name: String,
uri: String,
) {
let constructor_ref = token::create_named_token(
creator,
collection_name,
description,
name,
option::none(),
uri,
);
// ... 额外设置逻辑
}要点: 合集使用命名代币,独立道具使用create_object创建。
Pattern 2: Access Control (from dao)
模式2:访问控制(来自dao)
move
// From: dao/sources/dao.move
public entry fun execute_proposal(
proposer: &signer,
proposal_id: u64
) acquires DAO, Proposal {
let dao = borrow_global_mut<DAO>(@dao_addr);
let proposal = vector::borrow_mut(&mut dao.proposals, proposal_id);
// Verify proposal passed
assert!(proposal.votes_for > proposal.votes_against, E_PROPOSAL_NOT_PASSED);
// Execute actions
// ...
}Takeaway: Verify state conditions before executing critical operations.
move
// 来源: dao/sources/dao.move
public entry fun execute_proposal(
proposer: &signer,
proposal_id: u64
) acquires DAO, Proposal {
let dao = borrow_global_mut<DAO>(@dao_addr);
let proposal = vector::borrow_mut(&mut dao.proposals, proposal_id);
// 验证提案已通过
assert!(proposal.votes_for > proposal.votes_against, E_PROPOSAL_NOT_PASSED);
// 执行操作
// ...
}要点: 执行关键操作前先验证状态条件。
Pattern 3: Transfer Control (from fungible_asset)
模式3:转账控制(来自fungible_asset)
move
// From: fungible_asset/sources/fungible_asset.move
public fun transfer<T: key>(
from: &signer,
to: address,
amount: u64
) acquires FungibleStore {
// Verify sender has sufficient balance
let from_store = borrow_global_mut<FungibleStore<T>>(signer::address_of(from));
assert!(from_store.balance >= amount, E_INSUFFICIENT_BALANCE);
// Deduct from sender
from_store.balance = from_store.balance - amount;
// Add to recipient
let to_store = borrow_global_mut<FungibleStore<T>>(to);
to_store.balance = to_store.balance + amount;
}Takeaway: Check-effects-interactions pattern (verify, deduct, add).
move
// 来源: fungible_asset/sources/fungible_asset.move
public fun transfer<T: key>(
from: &signer,
to: address,
amount: u64
) acquires FungibleStore {
// 验证发送方余额充足
let from_store = borrow_global_mut<FungibleStore<T>>(signer::address_of(from));
assert!(from_store.balance >= amount, E_INSUFFICIENT_BALANCE);
// 扣除发送方余额
from_store.balance = from_store.balance - amount;
// 增加接收方余额
let to_store = borrow_global_mut<FungibleStore<T>>(to);
to_store.balance = to_store.balance + amount;
}要点: 遵循检查-效应-交互模式(先校验,再修改状态,最后执行交互)。
ALWAYS Rules
必须遵守的规则
- ✅ ALWAYS search examples before writing new contracts
- ✅ ALWAYS check both aptos-core (canonical) and daily-move (educational) repositories
- ✅ ALWAYS understand patterns before copying
- ✅ ALWAYS adapt patterns to your use case
- ✅ ALWAYS maintain security checks from examples
- ✅ ALWAYS reference which example you adapted from
- ✅ ALWAYS test adapted code thoroughly
- ✅ 编写新合约前务必先搜索示例
- ✅ 务必同时检查aptos-core(官方标准)和daily-move(教学内容)两个仓库
- ✅ 复制代码前务必先理解模式逻辑
- ✅ 务必根据你的业务场景适配模式
- ✅ 务必保留示例中的所有安全检查逻辑
- ✅ 务必在代码中注明你适配的来源示例
- ✅ 适配后的代码务必进行充分测试
NEVER Rules
禁止行为
- ❌ NEVER copy code without understanding it
- ❌ NEVER skip security checks from examples
- ❌ NEVER use deprecated patterns (resource accounts, address-based)
- ❌ NEVER assume examples are always up-to-date (verify against docs)
- ❌ NEVER mix V1 and V2 patterns
- ❌ NEVER include real private keys or credentials when adapting examples — use placeholders
"0x..."
- ❌ 不要不理解代码逻辑就直接复制
- ❌ 不要省略示例中的安全检查逻辑
- ❌ 不要使用已过时的模式(资源账户、基于地址的模式)
- ❌ 不要假设示例永远是最新的(请对照文档验证)
- ❌ 不要混用V1和V2模式
- ❌ 适配示例时不要包含真实私钥或凭证 — 使用占位符
"0x..."
Search Checklist
搜索检查清单
Before writing contract code:
- Identified category (NFT, DeFi, DAO, etc.)
- Found 2-3 relevant examples in aptos-core
- Checked daily-move snippets for educational examples
- Reviewed module structure
- Identified security patterns
- Understood object creation patterns
- Noted access control mechanisms
- Checked test patterns
- Ready to adapt to my use case
编写合约代码前:
- 已明确开发分类(NFT、DeFi、DAO等)
- 已在aptos-core中找到2-3个相关示例
- 已在daily-move代码片段中查找了教学示例
- 已审查模块结构
- 已识别安全模式
- 已理解对象创建模式
- 已明确访问控制机制
- 已检查测试模式
- 已准备好适配到你的业务场景
Example Adaptation Workflow
示例适配工作流
Step-by-Step: Building NFT Collection
分步指南:开发NFT合集
-
Search: Findexample
token_objects -
Review Structure:
token_objects/ ├── sources/ │ ├── collection.move # Collection management │ ├── token.move # Token operations │ └── property_map.move # Metadata handling └── tests/ └── token_tests.move -
Identify Key Patterns:
- Collection creation with
create_collection - Token minting with
create_named_token - Metadata storage using
PropertyMap - Transfer control with
TransferRef
- Collection creation with
-
Adapt to Your Needs:
- Keep object creation pattern
- Keep security checks
- Add your custom fields
- Add your business logic
- Write comprehensive tests
-
Reference in Code:move
// Adapted from: aptos-core/move-examples/token_objects module my_addr::custom_nft { // ... your implementation }
-
搜索: 找到示例
token_objects -
审查结构:
token_objects/ ├── sources/ │ ├── collection.move # 合集管理 │ ├── token.move # 代币操作 │ └── property_map.move # 元数据处理 └── tests/ └── token_tests.move -
识别核心模式:
- 使用创建合集
create_collection - 使用铸造NFT
create_named_token - 使用存储元数据
PropertyMap - 使用控制转账
TransferRef
- 使用
-
按需适配:
- 保留对象创建模式
- 保留安全检查逻辑
- 添加自定义字段
- 添加业务逻辑
- 编写完善的测试用例
-
代码中注明来源:move
// 适配自: aptos-core/move-examples/token_objects module my_addr::custom_nft { // ... 你的实现代码 }
References
参考资料
Official Examples:
- aptos-core: https://github.com/aptos-labs/aptos-core/tree/main/aptos-move/move-examples
- daily-move: https://github.com/aptos-labs/daily-move/tree/main/snippets
- Documentation: https://aptos.dev/build/smart-contracts
Related Skills:
- - Apply patterns after searching
write-contracts - - Verify security of adapted code
security-audit - - Test adapted patterns
generate-tests
Remember: Search examples first. Understand patterns. Adapt securely. Test thoroughly.
官方示例:
- aptos-core: https://github.com/aptos-labs/aptos-core/tree/main/aptos-move/move-examples
- daily-move: https://github.com/aptos-labs/daily-move/tree/main/snippets
- 文档: https://aptos.dev/build/smart-contracts
相关Skill:
- - 搜索示例后应用模式编写合约
write-contracts - - 验证适配后代码的安全性
security-audit - - 为适配后的模式编写测试
generate-tests
谨记: 先搜索示例,理解模式,安全适配,充分测试。