aptos について
aptos は、Aptos ブロックチェーンと Move 開発向けのスキルです。コントラクトの実装、フレームワークモジュールの利用、dApp のテストを支援し、リソースと能力、トークン規格、Gas 最適化、TypeScript SDK を扱います。
主な機能
- 01Move の型システムとリソース操作
- 02Aptos フレームワークのモジュールと定番パターン
- 03Coin、Fungible Asset、NFT の規格
- 04コントラクトテストと CLI コマンド
- 05Gas 最適化と TypeScript SDK
活用シーン
- 01Aptos の Move スマートコントラクト開発
- 02トークンや NFT 機能の実装
- 03コントラクトのテストと Gas 使用量の最適化
- 04TypeScript SDK による dApp 連携
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ raintree-technology/ claude-starter --skill aptosインストール後、Claude Code で/aptosを入力して読み込まれたか確認します。
詳細
Aptos ブロックチェーンと Move 言語のエキスパート
Aptos は、Move プログラミング言語を採用したレイヤー 1 ブロックチェーンで、安全かつスケーラブルなスマートコントラクトを実現するよう設計されています。
使用する場面
- Move スマートコントラクトを記述する
- Move の型システム(アビリティ、ジェネリクス、リソース)を理解する
- Aptos フレームワークのモジュール(coin、fungible_asset、object)を使用する
- トークン規格(Coin、Fungible Asset、Digital Asset/NFT)を扱う
- ガスを最適化し、テストする
- TypeScript SDK を使って dApp と連携する
Move 言語の基礎
アビリティ — Move の中核機能
すべての型は、値に対してできる操作を制御するアビリティを持ちます。
| アビリティ | 意味 | 例 |
|---|---|---|
copy | 複製できます | プリミティブ型、設定 |
drop | 破棄できます | 一時データ |
store | 構造体に保存できます | ほとんどのデータ型 |
key | トップレベルのリソースになれます | アカウントリソース |
// Resource that can be stored globally
struct Balance has key, store {
coins: u64
}
// Value type that behaves like a primitive
struct Point has copy, drop, store {
x: u64,
y: u64
}
// No abilities = must be explicitly handled (hot potato)
struct Receipt {
amount: u64
}
ジェネリクスとファントム型
// Type parameter with ability constraint
struct Box<T: store> has key {
value: T
}
// Phantom type - zero runtime cost, type safety only
struct Coin<phantom CoinType> has store {
value: u64
}
// Different types can't be mixed
let btc: Coin<BTC> = Coin { value: 100 };
let eth: Coin<ETH> = Coin { value: 50 };
// btc + eth → compile error!
グローバルストレージの操作
// Store resource at signer's address
public fun initialize(account: &signer) {
move_to(account, MyResource { value: 0 });
}
// Read resource (requires acquires annotation)
public fun get_value(addr: address): u64 acquires MyResource {
borrow_global<MyResource>(addr).value
}
// Modify resource
public fun set_value(addr: address, val: u64) acquires MyResource {
borrow_global_mut<MyResource>(addr).value = val;
}
// Check existence
public fun exists_at(addr: address): bool {
exists<MyResource>(addr)
}
// Remove and return resource
public fun destroy(addr: address): MyResource acquires MyResource {
move_from<MyResource>(addr)
}
Signer — 認証
use std::signer;
// Only the account owner can call this
public entry fun transfer(
sender: &signer,
recipient: address,
amount: u64
) acquires Balance {
let sender_addr = signer::address_of(sender);
// sender proves ownership of sender_addr
}
可視性修飾子
module example {
// Private (default) - only within module
fun internal() { }
// Public - callable from anywhere
public fun library_fn() { }
// Friend - only from declared friend modules
public(friend) fun restricted() { }
// Entry - transaction entry point
public entry fun user_action(account: &signer) { }
}
Aptos フレームワーク(0x1)
コアモジュール
| モジュール | 目的 |
|---|---|
account | アカウントの作成、認証キーのローテーション |
coin | 従来の代替可能トークン規格 |
fungible_asset | 新しい柔軟なトークン規格 |
object | コンポーザブルなアセットのオブジェクトモデル |
aptos_coin | ネイティブ APT トークン |
timestamp | ブロックのタイムスタンプ |
event | イベントの発行 |
Coin 規格(0x1::coin)
use aptos_framework::coin;
// Register to receive a coin type
public entry fun register<CoinType>(account: &signer) {
coin::register<CoinType>(account);
}
// Transfer coins
public entry fun transfer<CoinType>(
from: &signer,
to: address,
amount: u64
) {
coin::transfer<CoinType>(from, to, amount);
}
// Check balance
public fun balance<CoinType>(addr: address): u64 {
coin::balance<CoinType>(addr)
}
Fungible Asset 規格(0x1::fungible_asset)
最新の柔軟なトークン規格です。
use aptos_framework::fungible_asset::{Self, FungibleAsset, Metadata};
use aptos_framework::primary_fungible_store;
// Create fungible asset metadata
public fun create_fa(creator: &signer): Object<Metadata> {
let constructor_ref = object::create_named_object(creator, b"MY_FA");
primary_fungible_store::create_primary_store_enabled_fungible_asset(
&constructor_ref,
option::none(), // max supply
utf8(b"My Token"),
utf8(b"MTK"),
8, // decimals
utf8(b"https://example.com/icon.png"),
utf8(b"https://example.com"),
);
object::object_from_constructor_ref(&constructor_ref)
}
// Transfer FA
public entry fun transfer_fa(
sender: &signer,
metadata: Object<Metadata>,
recipient: address,
amount: u64
) {
primary_fungible_store::transfer(sender, metadata, recipient, amount);
}
オブジェクトモデル(0x1::object)
コンポーザブルで譲渡可能なオブジェクトです。
use aptos_framework::object::{Self, Object, ConstructorRef};
struct MyObject has key {
value: u64
}
// Create object
public fun create(creator: &signer): Object<MyObject> {
let constructor_ref = object::create_object(signer::address_of(creator));
let object_signer = object::generate_signer(&constructor_ref);
move_to(&object_signer, MyObject { value: 42 });
object::object_from_constructor_ref(&constructor_ref)
}
// Transfer object
public entry fun transfer_object(
owner: &signer,
obj: Object<MyObject>,
recipient: address
) {
object::transfer(owner, obj, recipient);
}
トークン規格
Digital Asset(NFT)規格
use aptos_token_objects::collection;
use aptos_token_objects::token;
// Create collection
public entry fun create_collection(creator: &signer) {
collection::create_unlimited_collection(
creator,
utf8(b"My Collection Description"),
utf8(b"My Collection"),
option::none(), // royalty
utf8(b"https://example.com/collection"),
);
}
// Mint token
public entry fun mint_token(creator: &signer, to: address) {
let token_constructor_ref = token::create(
creator,
utf8(b"My Collection"),
utf8(b"Token description"),
utf8(b"Token #1"),
option::none(), // royalty
utf8(b"https://example.com/token/1"),
);
let transfer_ref = object::generate_transfer_ref(&token_constructor_ref);
let linear_transfer_ref = object::generate_linear_transfer_ref(&transfer_ref);
object::transfer_with_ref(linear_transfer_ref, to);
}
よく使われるパターン
ケイパビリティパターン
struct AdminCap has key, store {}
public fun init_module(deployer: &signer) {
move_to(deployer, AdminCap {});
}
public entry fun admin_only(admin: &signer) acquires AdminCap {
assert!(exists<AdminCap>(signer::address_of(admin)), E_NOT_ADMIN);
// Admin-only logic
}
リソースアカウント
use aptos_framework::resource_account;
// Create resource account for deterministic addresses
public fun create_resource_account(creator: &signer): signer {
let (resource_signer, _cap) = resource_account::create_resource_account(
creator,
b"seed"
);
resource_signer
}
イベント
use aptos_framework::event;
#[event]
struct TransferEvent has drop, store {
from: address,
to: address,
amount: u64,
}
public fun emit_transfer(from: address, to: address, amount: u64) {
event::emit(TransferEvent { from, to, amount });
}
テスト
#[test]
fun test_basic() {
let x = 1 + 1;
assert!(x == 2, 0);
}
#[test(account = @0x123)]
fun test_with_signer(account: &signer) {
let addr = signer::address_of(account);
assert!(addr == @0x123, 0);
}
#[test]
#[expected_failure(abort_code = E_NOT_FOUND)]
fun test_expected_failure() {
abort E_NOT_FOUND
}
CLI コマンド
# Initialize project
aptos init
# Compile
aptos move compile
# Test
aptos move test
# Publish
aptos move publish --profile mainnet
# Run function
aptos move run --function-id 'addr::module::function'
ガスの最適化
- 頻繁に呼び出される小さな関数には
inlineを使用します - 大規模なコレクションでは
vectorよりもTableを優先します - ストレージ操作(最もコストが高い操作)を最小限に抑えます
- 並列実行には
SmartTableを使用します - 可能な場合は操作をまとめて実行します
TypeScript SDK
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.MAINNET });
const aptos = new Aptos(config);
// Query account balance
const balance = await aptos.getAccountAPTAmount({
accountAddress: "0x1"
});
// Submit transaction
const txn = await aptos.transaction.build.simple({
sender: account.accountAddress,
data: {
function: "0x1::coin::transfer",
typeArguments: ["0x1::aptos_coin::AptosCoin"],
functionArguments: [recipientAddress, amount],
},
});
const result = await aptos.signAndSubmitTransaction({
signer: account,
transaction: txn,
});
リソース
コメント
Markdown 対応