376 lines
12 KiB
Rust
376 lines
12 KiB
Rust
//! ACC-1155协议集成测试
|
||
//!
|
||
//! 测试ACC-1155多代币证书协议的完整功能
|
||
|
||
use nac_sdk::client::NacLensClient;
|
||
use nac_sdk::protocols::ACC1155;
|
||
use nac_sdk::error::{NACError, Result};
|
||
use nac_udm::primitives::{Address, Hash, Timestamp};
|
||
use nac_udm::l1_protocol::gnacs::{GNACSCode, AssetCategory, Jurisdiction, ComplianceLevel, RiskLevel};
|
||
use nac_udm::l1_protocol::acc::acc1155::{
|
||
TokenId, TokenType, SovereigntyType, TokenTypeMetadata, TokenTypeValuation,
|
||
};
|
||
|
||
/// 测试ACC-1155基本功能
|
||
#[tokio::test]
|
||
#[ignore] // 需要实际的NAC节点才能运行
|
||
async fn test_acc1155_create_and_mint() -> Result<()> {
|
||
// 创建客户端
|
||
let client = NacLensClient::new("https://rpc.newassetchain.io");
|
||
let acc1155 = ACC1155::new(client);
|
||
|
||
// 测试地址
|
||
let certificate_address = Address::from_hex("0x12345678901234567890123456789012345678901234567890123456789012345678901234567890")
|
||
.map_err(|e| NACError::InvalidAddress(e))?;
|
||
let owner_address = Address::from_hex("0x22345678901234567890123456789012345678902234567890123456789012345678901234567890")
|
||
.map_err(|e| NACError::InvalidAddress(e))?;
|
||
|
||
// 创建GNACS编码
|
||
let gnacs = GNACSCode::new(
|
||
AssetCategory::Digital,
|
||
1,
|
||
Jurisdiction::US,
|
||
ComplianceLevel::High,
|
||
RiskLevel::Low,
|
||
);
|
||
|
||
// 创建代币类型元数据
|
||
let metadata = TokenTypeMetadata {
|
||
token_id: 1,
|
||
name: "NAC Game Token".to_string(),
|
||
symbol: "NGT".to_string(),
|
||
token_type: TokenType::Fungible,
|
||
uri: "ipfs://QmGameToken123".to_string(),
|
||
max_supply: Some(1_000_000),
|
||
current_supply: 0,
|
||
created_at: Timestamp::now(),
|
||
};
|
||
|
||
// 1. 创建代币类型
|
||
let token_dna = acc1155.create_token_type(
|
||
&certificate_address,
|
||
1,
|
||
TokenType::Fungible,
|
||
gnacs,
|
||
SovereigntyType::A0,
|
||
metadata.clone(),
|
||
Some(1_000_000),
|
||
).await?;
|
||
|
||
println!("Token Type DNA: {:?}", token_dna);
|
||
assert_eq!(token_dna.token_id, 1);
|
||
assert_eq!(token_dna.token_type, TokenType::Fungible);
|
||
|
||
// 2. 批量铸造代币
|
||
let batch_mint = acc1155.mint_batch(
|
||
&certificate_address,
|
||
&owner_address,
|
||
vec![1],
|
||
vec![10000],
|
||
).await?;
|
||
|
||
println!("Batch Mint: {:?}", batch_mint);
|
||
assert_eq!(batch_mint.token_ids, vec![1]);
|
||
assert_eq!(batch_mint.amounts, vec![10000]);
|
||
|
||
// 3. 查询余额
|
||
let balance = acc1155.balance_of(&certificate_address, &owner_address, 1).await?;
|
||
assert_eq!(balance, 10000);
|
||
|
||
Ok(())
|
||
}
|
||
|
||
/// 测试ACC-1155批量操作
|
||
#[tokio::test]
|
||
#[ignore] // 需要实际的NAC节点才能运行
|
||
async fn test_acc1155_batch_operations() -> Result<()> {
|
||
let client = NacLensClient::new("https://rpc.newassetchain.io");
|
||
let acc1155 = ACC1155::new(client);
|
||
|
||
let certificate_address = Address::from_hex("0x12345678901234567890123456789012345678901234567890123456789012345678901234567890")
|
||
.map_err(|e| NACError::InvalidAddress(e))?;
|
||
let from_address = Address::from_hex("0x22345678901234567890123456789012345678902234567890123456789012345678901234567890")
|
||
.map_err(|e| NACError::InvalidAddress(e))?;
|
||
let to_address = Address::from_hex("0x32345678901234567890123456789012345678903234567890123456789012345678901234567890")
|
||
.map_err(|e| NACError::InvalidAddress(e))?;
|
||
|
||
// 批量转移
|
||
let batch_transfer = acc1155.safe_transfer_batch(
|
||
&certificate_address,
|
||
&from_address,
|
||
&to_address,
|
||
vec![1, 2, 3],
|
||
vec![100, 200, 300],
|
||
).await?;
|
||
|
||
println!("Batch Transfer: {:?}", batch_transfer);
|
||
assert_eq!(batch_transfer.token_ids, vec![1, 2, 3]);
|
||
assert_eq!(batch_transfer.amounts, vec![100, 200, 300]);
|
||
|
||
// 批量查询余额
|
||
let balances = acc1155.balance_of_batch(
|
||
&certificate_address,
|
||
vec![to_address, to_address, to_address],
|
||
vec![1, 2, 3],
|
||
).await?;
|
||
|
||
println!("Balances: {:?}", balances);
|
||
assert_eq!(balances.len(), 3);
|
||
|
||
Ok(())
|
||
}
|
||
|
||
/// 测试ACC-1155混合代币类型
|
||
#[tokio::test]
|
||
#[ignore] // 需要实际的NAC节点才能运行
|
||
async fn test_acc1155_hybrid_tokens() -> Result<()> {
|
||
let client = NacLensClient::new("https://rpc.newassetchain.io");
|
||
let acc1155 = ACC1155::new(client);
|
||
|
||
let certificate_address = Address::from_hex("0x12345678901234567890123456789012345678901234567890123456789012345678901234567890")
|
||
.map_err(|e| NACError::InvalidAddress(e))?;
|
||
let owner_address = Address::from_hex("0x22345678901234567890123456789012345678902234567890123456789012345678901234567890")
|
||
.map_err(|e| NACError::InvalidAddress(e))?;
|
||
|
||
let gnacs = GNACSCode::new(
|
||
AssetCategory::Digital,
|
||
1,
|
||
Jurisdiction::US,
|
||
ComplianceLevel::High,
|
||
RiskLevel::Low,
|
||
);
|
||
|
||
// 创建可替代代币
|
||
let fungible_metadata = TokenTypeMetadata {
|
||
token_id: 1,
|
||
name: "Gold Coin".to_string(),
|
||
symbol: "GOLD".to_string(),
|
||
token_type: TokenType::Fungible,
|
||
uri: "ipfs://QmGold".to_string(),
|
||
max_supply: None,
|
||
current_supply: 0,
|
||
created_at: Timestamp::now(),
|
||
};
|
||
|
||
let fungible_dna = acc1155.create_token_type(
|
||
&certificate_address,
|
||
1,
|
||
TokenType::Fungible,
|
||
gnacs,
|
||
SovereigntyType::A0,
|
||
fungible_metadata,
|
||
None,
|
||
).await?;
|
||
|
||
assert_eq!(fungible_dna.token_type, TokenType::Fungible);
|
||
|
||
// 创建不可替代代币
|
||
let nft_metadata = TokenTypeMetadata {
|
||
token_id: 2,
|
||
name: "Legendary Sword".to_string(),
|
||
symbol: "SWORD".to_string(),
|
||
token_type: TokenType::NonFungible,
|
||
uri: "ipfs://QmSword".to_string(),
|
||
max_supply: Some(1),
|
||
current_supply: 0,
|
||
created_at: Timestamp::now(),
|
||
};
|
||
|
||
let nft_dna = acc1155.create_token_type(
|
||
&certificate_address,
|
||
2,
|
||
TokenType::NonFungible,
|
||
gnacs,
|
||
SovereigntyType::A0,
|
||
nft_metadata,
|
||
Some(1),
|
||
).await?;
|
||
|
||
assert_eq!(nft_dna.token_type, TokenType::NonFungible);
|
||
|
||
// 批量铸造混合代币
|
||
let batch_mint = acc1155.mint_batch(
|
||
&certificate_address,
|
||
&owner_address,
|
||
vec![1, 2],
|
||
vec![1000, 1],
|
||
).await?;
|
||
|
||
println!("Hybrid Batch Mint: {:?}", batch_mint);
|
||
|
||
Ok(())
|
||
}
|
||
|
||
/// 测试ACC-1155授权功能
|
||
#[tokio::test]
|
||
#[ignore] // 需要实际的NAC节点才能运行
|
||
async fn test_acc1155_approval() -> Result<()> {
|
||
let client = NacLensClient::new("https://rpc.newassetchain.io");
|
||
let acc1155 = ACC1155::new(client);
|
||
|
||
let certificate_address = Address::from_hex("0x12345678901234567890123456789012345678901234567890123456789012345678901234567890")
|
||
.map_err(|e| NACError::InvalidAddress(e))?;
|
||
let owner_address = Address::from_hex("0x22345678901234567890123456789012345678902234567890123456789012345678901234567890")
|
||
.map_err(|e| NACError::InvalidAddress(e))?;
|
||
let operator_address = Address::from_hex("0x32345678901234567890123456789012345678903234567890123456789012345678901234567890")
|
||
.map_err(|e| NACError::InvalidAddress(e))?;
|
||
|
||
// 设置授权
|
||
acc1155.set_approval_for_all(
|
||
&certificate_address,
|
||
&owner_address,
|
||
&operator_address,
|
||
true,
|
||
).await?;
|
||
|
||
// 检查授权状态
|
||
let is_approved = acc1155.is_approved_for_all(
|
||
&certificate_address,
|
||
&owner_address,
|
||
&operator_address,
|
||
).await?;
|
||
|
||
assert!(is_approved);
|
||
|
||
println!("Approval set successfully");
|
||
|
||
Ok(())
|
||
}
|
||
|
||
/// 测试ACC-1155代币估值
|
||
#[tokio::test]
|
||
#[ignore] // 需要实际的NAC节点才能运行
|
||
async fn test_acc1155_valuation() -> Result<()> {
|
||
let client = NacLensClient::new("https://rpc.newassetchain.io");
|
||
let acc1155 = ACC1155::new(client);
|
||
|
||
let certificate_address = Address::from_hex("0x12345678901234567890123456789012345678901234567890123456789012345678901234567890")
|
||
.map_err(|e| NACError::InvalidAddress(e))?;
|
||
let valuation_provider = Address::from_hex("0x42345678901234567890123456789012345678904234567890123456789012345678901234567890")
|
||
.map_err(|e| NACError::InvalidAddress(e))?;
|
||
|
||
let token_id: TokenId = 1;
|
||
|
||
// 创建估值
|
||
let valuation = TokenTypeValuation {
|
||
token_id,
|
||
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, token_id, valuation.clone()).await?;
|
||
|
||
// 查询估值
|
||
let retrieved_valuation = acc1155.get_token_valuation(&certificate_address, token_id).await?;
|
||
assert_eq!(retrieved_valuation.value_per_unit_xtzh, valuation.value_per_unit_xtzh);
|
||
|
||
println!("Token valuation updated successfully");
|
||
|
||
Ok(())
|
||
}
|
||
|
||
/// 测试ACC-1155托管和保险
|
||
#[tokio::test]
|
||
#[ignore] // 需要实际的NAC节点才能运行
|
||
async fn test_acc1155_custody_and_insurance() -> Result<()> {
|
||
let client = NacLensClient::new("https://rpc.newassetchain.io");
|
||
let acc1155 = ACC1155::new(client);
|
||
|
||
let certificate_address = Address::from_hex("0x12345678901234567890123456789012345678901234567890123456789012345678901234567890")
|
||
.map_err(|e| NACError::InvalidAddress(e))?;
|
||
let token_id: TokenId = 1;
|
||
|
||
// 查询托管信息
|
||
let custody_info = acc1155.get_custody_info(&certificate_address, token_id).await?;
|
||
println!("Custody Info: {:?}", custody_info);
|
||
assert_eq!(custody_info.token_id, token_id);
|
||
|
||
// 查询保险信息
|
||
let insurance_info = acc1155.get_insurance_info(&certificate_address, token_id).await?;
|
||
println!("Insurance Info: {:?}", insurance_info);
|
||
assert_eq!(insurance_info.token_id, token_id);
|
||
|
||
Ok(())
|
||
}
|
||
|
||
/// 测试ACC-1155批量销毁
|
||
#[tokio::test]
|
||
#[ignore] // 需要实际的NAC节点才能运行
|
||
async fn test_acc1155_batch_burn() -> Result<()> {
|
||
let client = NacLensClient::new("https://rpc.newassetchain.io");
|
||
let acc1155 = ACC1155::new(client);
|
||
|
||
let certificate_address = Address::from_hex("0x12345678901234567890123456789012345678901234567890123456789012345678901234567890")
|
||
.map_err(|e| NACError::InvalidAddress(e))?;
|
||
let owner_address = Address::from_hex("0x22345678901234567890123456789012345678902234567890123456789012345678901234567890")
|
||
.map_err(|e| NACError::InvalidAddress(e))?;
|
||
|
||
// 批量销毁
|
||
let batch_burn = acc1155.burn_batch(
|
||
&certificate_address,
|
||
&owner_address,
|
||
vec![1, 2],
|
||
vec![100, 50],
|
||
).await?;
|
||
|
||
println!("Batch Burn: {:?}", batch_burn);
|
||
assert_eq!(batch_burn.token_ids, vec![1, 2]);
|
||
assert_eq!(batch_burn.amounts, vec![100, 50]);
|
||
|
||
Ok(())
|
||
}
|
||
|
||
/// 单元测试:测试ACC-1155接口创建
|
||
#[test]
|
||
fn test_acc1155_interface_creation() {
|
||
let client = NacLensClient::new("https://rpc.newassetchain.io");
|
||
let _acc1155 = ACC1155::new(client);
|
||
// 接口创建成功
|
||
}
|
||
|
||
/// 单元测试:测试代币类型
|
||
#[test]
|
||
fn test_token_types() {
|
||
use nac_udm::l1_protocol::acc::acc1155::TokenType;
|
||
|
||
let fungible = TokenType::Fungible;
|
||
let non_fungible = TokenType::NonFungible;
|
||
let semi_fungible = TokenType::SemiFungible;
|
||
|
||
assert_ne!(fungible, non_fungible);
|
||
assert_ne!(fungible, semi_fungible);
|
||
assert_ne!(non_fungible, semi_fungible);
|
||
}
|
||
|
||
/// 单元测试:测试主权类型
|
||
#[test]
|
||
fn test_sovereignty_types() {
|
||
use nac_udm::l1_protocol::acc::acc1155::SovereigntyType;
|
||
|
||
let a0 = SovereigntyType::A0;
|
||
let b1 = SovereigntyType::B1;
|
||
let c2 = SovereigntyType::C2;
|
||
|
||
assert_ne!(a0, b1);
|
||
assert_ne!(a0, c2);
|
||
assert_ne!(b1, c2);
|
||
}
|
||
|
||
/// 单元测试:测试GNACS编码
|
||
#[test]
|
||
fn test_gnacs_code() {
|
||
let gnacs = GNACSCode::new(
|
||
AssetCategory::Digital,
|
||
1,
|
||
Jurisdiction::US,
|
||
ComplianceLevel::High,
|
||
RiskLevel::Low,
|
||
);
|
||
|
||
assert!(gnacs.verify_checksum());
|
||
assert_eq!(gnacs.category(), AssetCategory::Digital);
|
||
}
|