NAC_Blockchain/nac-bridge-contracts/src/wrapped_asset.charter

289 lines
7.3 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// ACC-20C包裹资产合约
// 版本: v1.0.0
// 描述: 实现跨链包裹资产的铸造、销毁和转移
module acc20c.wrapped_asset;
/// 包裹资产元数据
public struct WrappedAssetMetadata {
name: bytes32, // 资产名称
symbol: bytes32, // 资产符号
decimals: uint8, // 小数位数
original_chain_id: uint64, // 原始链ID
original_asset_address: bytes, // 原始资产地址
bridge_contract: [u8; 32], // 桥合约地址
gnacs: bytes12, // GNACS编码
sovereignty: bytes2 // 主权级别C2=跨链资产)
}
/// ACC-20C包裹资产合约
contract WrappedAsset {
storage {
// 元数据
metadata: WrappedAssetMetadata,
// 总供应量
total_supply: uint128,
// 余额映射
balances: map<[u8; 32], uint128>,
// 授权映射
allowances: map<[u8; 32], map<[u8; 32], uint128>>,
// 桥合约地址(只有桥合约可以铸造和销毁)
bridge: [u8; 32],
// 是否暂停
paused: bool
}
/// 构造函数
constructor(
name: bytes32,
symbol: bytes32,
decimals: uint8,
original_chain_id: uint64,
original_asset_address: bytes,
bridge_contract: [u8; 32],
gnacs: bytes12
) {
self.metadata = WrappedAssetMetadata {
name: name,
symbol: symbol,
decimals: decimals,
original_chain_id: original_chain_id,
original_asset_address: original_asset_address,
bridge_contract: bridge_contract,
gnacs: gnacs,
sovereignty: b"C2" // C2=跨链资产
};
self.bridge = bridge_contract;
self.total_supply = 0;
self.paused = false;
}
/// 查询余额
pub fn balance_of(account: [u8; 32]) -> uint128 {
self.balances.get(account).unwrap_or(0)
}
/// 查询总供应量
pub fn get_total_supply() -> uint128 {
self.total_supply
}
/// 查询元数据
pub fn get_metadata() -> WrappedAssetMetadata {
self.metadata
}
/// 转账
pub fn transfer(to: [u8; 32], amount: uint128) -> bool {
require(!self.paused, "Contract is paused");
require(to != [0u8; 32], "Invalid recipient");
require(amount > 0, "Amount must be positive");
let from = msg.sender;
let from_balance = self.balance_of(from);
require(from_balance >= amount, "Insufficient balance");
// 更新余额
self.balances[from] = from_balance - amount;
self.balances[to] = self.balance_of(to) + amount;
// 触发事件
emit Transfer {
from: from,
to: to,
amount: amount
};
true
}
/// 授权
pub fn approve(spender: [u8; 32], amount: uint128) -> bool {
require(!self.paused, "Contract is paused");
require(spender != [0u8; 32], "Invalid spender");
let owner = msg.sender;
if !self.allowances.contains(owner) {
self.allowances[owner] = map::new();
}
self.allowances[owner][spender] = amount;
emit Approval {
owner: owner,
spender: spender,
amount: amount
};
true
}
/// 查询授权额度
pub fn allowance(owner: [u8; 32], spender: [u8; 32]) -> uint128 {
if self.allowances.contains(owner) {
self.allowances[owner].get(spender).unwrap_or(0)
} else {
0
}
}
/// 从授权额度转账
pub fn transfer_from(
from: [u8; 32],
to: [u8; 32],
amount: uint128
) -> bool {
require(!self.paused, "Contract is paused");
require(to != [0u8; 32], "Invalid recipient");
require(amount > 0, "Amount must be positive");
let spender = msg.sender;
// 检查授权额度
let current_allowance = self.allowance(from, spender);
require(current_allowance >= amount, "Insufficient allowance");
// 检查余额
let from_balance = self.balance_of(from);
require(from_balance >= amount, "Insufficient balance");
// 更新余额
self.balances[from] = from_balance - amount;
self.balances[to] = self.balance_of(to) + amount;
// 更新授权额度
self.allowances[from][spender] = current_allowance - amount;
emit Transfer {
from: from,
to: to,
amount: amount
};
true
}
/// 铸造(只有桥合约可以调用)
pub fn mint(to: [u8; 32], amount: uint128) -> bool {
require(msg.sender == self.bridge, "Only bridge can mint");
require(!self.paused, "Contract is paused");
require(to != [0u8; 32], "Invalid recipient");
require(amount > 0, "Amount must be positive");
// 更新总供应量
self.total_supply += amount;
// 更新余额
self.balances[to] = self.balance_of(to) + amount;
emit Mint {
to: to,
amount: amount
};
emit Transfer {
from: [0u8; 32],
to: to,
amount: amount
};
true
}
/// 销毁(只有桥合约可以调用)
pub fn burn(from: [u8; 32], amount: uint128) -> bool {
require(msg.sender == self.bridge, "Only bridge can burn");
require(!self.paused, "Contract is paused");
require(amount > 0, "Amount must be positive");
let from_balance = self.balance_of(from);
require(from_balance >= amount, "Insufficient balance");
// 更新余额
self.balances[from] = from_balance - amount;
// 更新总供应量
self.total_supply -= amount;
emit Burn {
from: from,
amount: amount
};
emit Transfer {
from: from,
to: [0u8; 32],
amount: amount
};
true
}
/// 暂停合约(只有桥合约可以调用)
pub fn pause() -> bool {
require(msg.sender == self.bridge, "Only bridge can pause");
self.paused = true;
emit Paused {
by: msg.sender
};
true
}
/// 恢复合约(只有桥合约可以调用)
pub fn unpause() -> bool {
require(msg.sender == self.bridge, "Only bridge can unpause");
self.paused = false;
emit Unpaused {
by: msg.sender
};
true
}
}
/// 转账事件
event Transfer {
from: [u8; 32],
to: [u8; 32],
amount: uint128
}
/// 授权事件
event Approval {
owner: [u8; 32],
spender: [u8; 32],
amount: uint128
}
/// 铸造事件
event Mint {
to: [u8; 32],
amount: uint128
}
/// 销毁事件
event Burn {
from: [u8; 32],
amount: uint128
}
/// 暂停事件
event Paused {
by: [u8; 32]
}
/// 恢复事件
event Unpaused {
by: [u8; 32]
}