279 lines
11 KiB
Rust
279 lines
11 KiB
Rust
//! ACC-1155多代币证书协议使用示例
|
|
//!
|
|
//! 本示例演示如何使用NAC SDK与ACC-1155多代币证书进行交互
|
|
|
|
use nac_sdk::client::NRPC3Client;
|
|
use nac_sdk::protocols::ACC1155;
|
|
use nac_sdk::error::Result;
|
|
use nac_udm::primitives::{Address, Timestamp};
|
|
use nac_udm::l1_protocol::gnacs::{GNACSCode, AssetCategory, Jurisdiction, ComplianceLevel, RiskLevel};
|
|
use nac_udm::l1_protocol::acc::acc1155::{
|
|
TokenType, SovereigntyType, TokenTypeMetadata, TokenTypeValuation,
|
|
};
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<()> {
|
|
println!("=== ACC-1155 多代币证书协议示例 ===\n");
|
|
|
|
// 1. 连接到NAC节点
|
|
println!("1. 连接到NAC节点...");
|
|
let client = NRPC3Client::new("https://rpc.newassetchain.io");
|
|
let acc1155 = ACC1155::new(client);
|
|
println!("✓ 已连接到NAC节点\n");
|
|
|
|
// 2. 准备测试数据
|
|
println!("2. 准备测试数据...");
|
|
let certificate_address = Address::from_hex("0x12345678901234567890123456789012345678901234567890123456789012345678901234567890")
|
|
.expect("Invalid certificate address");
|
|
let owner_address = Address::from_hex("0x22345678901234567890123456789012345678902234567890123456789012345678901234567890")
|
|
.expect("Invalid owner address");
|
|
let recipient_address = Address::from_hex("0x32345678901234567890123456789012345678903234567890123456789012345678901234567890")
|
|
.expect("Invalid recipient address");
|
|
|
|
println!("证书地址: {}", certificate_address);
|
|
println!("所有者地址: {}", owner_address);
|
|
println!("接收者地址: {}\n", recipient_address);
|
|
|
|
// 3. 创建GNACS编码
|
|
println!("3. 创建GNACS编码...");
|
|
let gnacs_fungible = GNACSCode::new(
|
|
AssetCategory::Digital,
|
|
1, // 游戏代币子类
|
|
Jurisdiction::US,
|
|
ComplianceLevel::High,
|
|
RiskLevel::Low,
|
|
);
|
|
|
|
let gnacs_nft = GNACSCode::new(
|
|
AssetCategory::Digital,
|
|
2, // 数字收藏品子类
|
|
Jurisdiction::US,
|
|
ComplianceLevel::High,
|
|
RiskLevel::Low,
|
|
);
|
|
|
|
println!("可替代代币GNACS: {}", gnacs_fungible.format_readable());
|
|
println!("不可替代代币GNACS: {}\n", gnacs_nft.format_readable());
|
|
|
|
// 4. 创建可替代代币类型
|
|
println!("4. 创建可替代代币类型(游戏金币)...");
|
|
let fungible_metadata = TokenTypeMetadata {
|
|
token_id: 1,
|
|
name: "NAC Game Gold".to_string(),
|
|
symbol: "NGG".to_string(),
|
|
token_type: TokenType::Fungible,
|
|
uri: "ipfs://QmGameGold123".to_string(),
|
|
max_supply: Some(10_000_000),
|
|
current_supply: 0,
|
|
created_at: Timestamp::now(),
|
|
};
|
|
|
|
let fungible_dna = acc1155.create_token_type(
|
|
&certificate_address,
|
|
1,
|
|
TokenType::Fungible,
|
|
gnacs_fungible,
|
|
SovereigntyType::A0,
|
|
fungible_metadata.clone(),
|
|
Some(10_000_000),
|
|
).await?;
|
|
|
|
println!("✓ 可替代代币类型已创建");
|
|
println!(" 代币ID: {}", fungible_dna.token_id);
|
|
println!(" DNA哈希: {}", fungible_dna.dna_hash);
|
|
println!(" 代币类型: {:?}", fungible_dna.token_type);
|
|
println!(" 主权类型: {:?}\n", fungible_dna.sovereignty_type);
|
|
|
|
// 5. 创建不可替代代币类型
|
|
println!("5. 创建不可替代代币类型(传奇武器)...");
|
|
let nft_metadata = TokenTypeMetadata {
|
|
token_id: 2,
|
|
name: "Legendary Sword".to_string(),
|
|
symbol: "LSWORD".to_string(),
|
|
token_type: TokenType::NonFungible,
|
|
uri: "ipfs://QmLegendarySword456".to_string(),
|
|
max_supply: Some(100),
|
|
current_supply: 0,
|
|
created_at: Timestamp::now(),
|
|
};
|
|
|
|
let nft_dna = acc1155.create_token_type(
|
|
&certificate_address,
|
|
2,
|
|
TokenType::NonFungible,
|
|
gnacs_nft,
|
|
SovereigntyType::A0,
|
|
nft_metadata.clone(),
|
|
Some(100),
|
|
).await?;
|
|
|
|
println!("✓ 不可替代代币类型已创建");
|
|
println!(" 代币ID: {}", nft_dna.token_id);
|
|
println!(" DNA哈希: {}", nft_dna.dna_hash);
|
|
println!(" 代币类型: {:?}", nft_dna.token_type);
|
|
println!(" 主权类型: {:?}\n", nft_dna.sovereignty_type);
|
|
|
|
// 6. 批量铸造代币
|
|
println!("6. 批量铸造代币(混合类型)...");
|
|
let batch_mint = acc1155.mint_batch(
|
|
&certificate_address,
|
|
&owner_address,
|
|
vec![1, 2],
|
|
vec![10000, 1], // 10000个金币 + 1把传奇武器
|
|
).await?;
|
|
|
|
println!("✓ 批量铸造成功");
|
|
println!(" 代币ID: {:?}", batch_mint.token_ids);
|
|
println!(" 数量: {:?}", batch_mint.amounts);
|
|
println!(" 铸造时间: {}", batch_mint.minted_at.as_secs());
|
|
println!(" 宪法收据: {}\n", batch_mint.constitutional_receipt);
|
|
|
|
// 7. 查询代币余额
|
|
println!("7. 查询代币余额...");
|
|
let balance_gold = acc1155.balance_of(&certificate_address, &owner_address, 1).await?;
|
|
let balance_sword = acc1155.balance_of(&certificate_address, &owner_address, 2).await?;
|
|
|
|
println!("✓ 余额查询成功");
|
|
println!(" 游戏金币余额: {}", balance_gold);
|
|
println!(" 传奇武器余额: {}\n", balance_sword);
|
|
|
|
// 8. 批量查询余额
|
|
println!("8. 批量查询余额...");
|
|
let balances = acc1155.balance_of_batch(
|
|
&certificate_address,
|
|
vec![owner_address, owner_address],
|
|
vec![1, 2],
|
|
).await?;
|
|
|
|
println!("✓ 批量余额查询成功");
|
|
println!(" 余额列表: {:?}\n", balances);
|
|
|
|
// 9. 查询代币元数据
|
|
println!("9. 查询代币元数据...");
|
|
let metadata = acc1155.get_token_metadata(&certificate_address, 1).await?;
|
|
|
|
println!("✓ 元数据查询成功");
|
|
println!(" 代币名称: {}", metadata.name);
|
|
println!(" 代币符号: {}", metadata.symbol);
|
|
println!(" 元数据URI: {}", metadata.uri);
|
|
println!(" 最大供应量: {:?}", metadata.max_supply);
|
|
println!(" 当前供应量: {}\n", metadata.current_supply);
|
|
|
|
// 10. 查询代币DNA
|
|
println!("10. 查询代币DNA...");
|
|
let token_dna = acc1155.get_token_dna(&certificate_address, 1).await?;
|
|
|
|
println!("✓ DNA查询成功");
|
|
println!(" DNA哈希: {}", token_dna.dna_hash);
|
|
println!(" GNACS编码: {}", token_dna.gnacs_code.format_readable());
|
|
println!(" 代币类型: {:?}", token_dna.token_type);
|
|
println!(" 主权类型: {:?}\n", token_dna.sovereignty_type);
|
|
|
|
// 11. 设置授权
|
|
println!("11. 设置授权...");
|
|
acc1155.set_approval_for_all(
|
|
&certificate_address,
|
|
&owner_address,
|
|
&recipient_address,
|
|
true,
|
|
).await?;
|
|
|
|
let is_approved = acc1155.is_approved_for_all(
|
|
&certificate_address,
|
|
&owner_address,
|
|
&recipient_address,
|
|
).await?;
|
|
|
|
println!("✓ 授权设置成功");
|
|
println!(" 授权状态: {}\n", is_approved);
|
|
|
|
// 12. 批量转移代币
|
|
println!("12. 批量转移代币...");
|
|
let batch_transfer = acc1155.safe_transfer_batch(
|
|
&certificate_address,
|
|
&owner_address,
|
|
&recipient_address,
|
|
vec![1, 2],
|
|
vec![1000, 1], // 转移1000个金币 + 1把传奇武器
|
|
).await?;
|
|
|
|
println!("✓ 批量转移成功");
|
|
println!(" 发送者: {}", batch_transfer.from);
|
|
println!(" 接收者: {}", batch_transfer.to);
|
|
println!(" 代币ID: {:?}", batch_transfer.token_ids);
|
|
println!(" 数量: {:?}", batch_transfer.amounts);
|
|
println!(" 转移时间: {}", batch_transfer.transferred_at.as_secs());
|
|
println!(" 宪法收据: {}\n", batch_transfer.constitutional_receipt);
|
|
|
|
// 13. 更新代币估值
|
|
println!("13. 更新代币估值...");
|
|
let valuation_provider = Address::from_hex("0x42345678901234567890123456789012345678904234567890123456789012345678901234567890")
|
|
.expect("Invalid valuation provider address");
|
|
|
|
let valuation = TokenTypeValuation {
|
|
token_id: 1,
|
|
value_per_unit_xtzh: 1_000_000_000_000_000u128, // 0.001 XTZH per token
|
|
valuation_provider,
|
|
valued_at: Timestamp::now(),
|
|
validity_period: 30 * 24 * 3600, // 30天
|
|
};
|
|
|
|
acc1155.update_token_valuation(&certificate_address, 1, valuation.clone()).await?;
|
|
|
|
println!("✓ 估值更新成功");
|
|
println!(" 单位估值: {} XTZH", valuation.value_per_unit_xtzh as f64 / 1e18);
|
|
println!(" 估值提供者: {}", valuation.valuation_provider);
|
|
println!(" 有效期: {} 天\n", valuation.validity_period / 86400);
|
|
|
|
// 14. 查询代币估值
|
|
println!("14. 查询代币估值...");
|
|
let retrieved_valuation = acc1155.get_token_valuation(&certificate_address, 1).await?;
|
|
|
|
println!("✓ 估值查询成功");
|
|
println!(" 单位估值: {} XTZH", retrieved_valuation.value_per_unit_xtzh as f64 / 1e18);
|
|
println!(" 估值提供者: {}", retrieved_valuation.valuation_provider);
|
|
println!(" 估值时间: {}", retrieved_valuation.valued_at.as_secs());
|
|
println!(" 有效期: {} 天\n", retrieved_valuation.validity_period / 86400);
|
|
|
|
// 15. 查询托管信息
|
|
println!("15. 查询托管信息...");
|
|
let custody_info = acc1155.get_custody_info(&certificate_address, 1).await?;
|
|
|
|
println!("✓ 托管信息查询成功");
|
|
println!(" 托管方: {}", custody_info.custodian);
|
|
println!(" 托管开始时间: {}", custody_info.custody_start.as_secs());
|
|
println!(" 托管状态: {}", if custody_info.is_active { "活跃" } else { "非活跃" });
|
|
println!(" 托管证明: {}\n", custody_info.custody_proof);
|
|
|
|
// 16. 查询保险信息
|
|
println!("16. 查询保险信息...");
|
|
let insurance_info = acc1155.get_insurance_info(&certificate_address, 1).await?;
|
|
|
|
println!("✓ 保险信息查询成功");
|
|
println!(" 保险提供者: {}", insurance_info.insurer);
|
|
println!(" 单位保险金额: {} XTZH", insurance_info.coverage_per_unit_xtzh as f64 / 1e18);
|
|
println!(" 保险开始时间: {}", insurance_info.insurance_start.as_secs());
|
|
println!(" 保险到期时间: {}", insurance_info.insurance_expiry.as_secs());
|
|
println!(" 保单号: {}\n", insurance_info.policy_number);
|
|
|
|
// 17. 批量销毁代币
|
|
println!("17. 批量销毁代币...");
|
|
let batch_burn = acc1155.burn_batch(
|
|
&certificate_address,
|
|
&owner_address,
|
|
vec![1],
|
|
vec![100], // 销毁100个金币
|
|
).await?;
|
|
|
|
println!("✓ 批量销毁成功");
|
|
println!(" 持有者: {}", batch_burn.from);
|
|
println!(" 代币ID: {:?}", batch_burn.token_ids);
|
|
println!(" 数量: {:?}", batch_burn.amounts);
|
|
println!(" 销毁时间: {}", batch_burn.burned_at.as_secs());
|
|
println!(" 宪法收据: {}\n", batch_burn.constitutional_receipt);
|
|
|
|
println!("=== ACC-1155 示例完成 ===");
|
|
|
|
Ok(())
|
|
}
|