Aleo 正在讨论中的ARC-20标准旨在为Aleo区块链提供代币互操作性和用户授权功能,类似于ERC-20。它允许代币轻松转账,并允许用户授权程序管理这些代币,特别适用于自动化金融应用。ARC-20标准的独特之处在于整合了Aleo的隐私特性,例如私有记录,提供用户在数字交易中的隐私和匿名性。这里我们将分别介绍这些方法。
程序结构
program token.aleo;
mapping account:
key as address.public;
value as u64.public;
record token:
owner as address.private;
amount as u64.private;
struct approval:
approver as address;
spender as address;
struct metadata:
name as u128; // 16 bytes -> 16 characters with ASCII encoding
symbol as u64; // 8 bytes -> 8 characters with ASCII encoding
decimals as u8;
total_supply as u64;
mapping approvals:
key as field.public;
value as u64.public;
1. 获取元数据
function get_metadata:
cast [name] [symbol] [decimals] [total_supply] into r0 as metadata;
output r0 as metadata.public;
此函数返回代币的元数据,包括名称、符号、小数位和总供应量。这些信息有助于钱包和dApp正确显示代币信息。
2. 授权公共代币使用
function approve_public:
input r0 as address.public; // 授权的地址
input r1 as u64.public; // 授权的代币数量
cast self.caller r0 into r2 as approval;
hash.bhp256 r2 into r3 as field;
async approve_public r3 r1 into r4;
output r4 as token.aleo/approve_public.future;
finalize approve_public:
input r0 as field.public;
input r1 as u64.public; // 增加授权的代币数量
get.or_use approvals[r0] 0u64 into r2;
add r1 r2 into r3;
set r3 into approvals[r0];
此函数允许代币持有者授权某个地址使用一定数量的代币。这个功能对于允许智能合约代表用户执行代币操作至关重要。
3. 取消授权
function unapprove_public:
input r0 as address.public; // 被取消授权的地址
input r1 as u64.public; // 取消的代币数量
cast self.caller r0 into r2 as approval;
hash.bhp256 r2 into r3 as field;
async unapprove_public r3 r1 into r4;
output r4 as token.aleo/unapprove_public.future;
finalize unapprove_public:
input r0 as field.public;
input r1 as u64.public; // 减少授权的代币数量
get approvals[r0] into r2;
sub r2 r1 into r3;
set r3 into approvals[r0];
此函数撤销之前授权的代币数量,用于减少或完全撤销对某个地址的授权。
4. 从公共账户转账
function transfer_from_public:
input r0 as address.public; // 授权者
input r1 as address.public; // 接收者
input r2 as u64.public; // 转账数量
cast r0 self.caller into r3 as approval;
hash.bhp256 r3 into r4 as field;
async transfer_from_public r4 r0 r1 r2 into r5;
output r5 as token.aleo/transfer_from_public.future;
finalize transfer_from_public:
input r0 as field.public;
input r1 as address.public;
input r2 as address.public;
input r3 as u64.public;
get approvals[r0] into r4;
sub r4 r3 into r5;
set r5 into approvals[r0];
get account[r1] into r6;
sub r6 r3 into r7;
set r7 into account[r1];
get.or_use account[r2] 0u64 into r8;
add r8 r3 into r9;
set r9 into account[r2];
此函数实现从授权者账户向接收者账户的代币转账,用于实现代币的自动化管理和支付。
5. 公共代币转账
function transfer_public:
input r0 as address.public; // 接收者地址
input r1 as u64.public; // 转账数量
async transfer_public self.caller r0 r1 into r2;
output r2 as token.aleo/transfer_public.future;
finalize transfer_public:
input r0 as address.public;
input r1 as address.public;
input r2 as u64.public;
get.or_use account[r0] 0u64 into r3;
sub r3 r2 into r4;
set r4 into account[r0];
get.or_use account[r1] 0u64 into r5;
add r5 r2 into r6;
set r6 into account[r1];
此函数将代币从调用者账户转移到指定的公共账户,实现简单的代币转账。
6. 私有代币转账
function transfer_private:
input r0 as token.record; // 代币记录
input r1 as address.private; // 接收者地址
input r2 as u64.private; // 转账数量
sub r0.amount r2 into r3;
cast r0.owner r3 into r4 as token.record;
cast r1 r2 into r5 as token.record;
output r4 as token.record;
output r5 as token.record;
此函数实现私有代币的转账,用于在保持交易隐私的情况下进行代币转移。
7. 私有代币转公共代币
function transfer_private_to_public:
input r0 as token.record; // 私有代币记录
input r1 as address.public; // 公共接收者地址
input r2 as u64.public; // 转账数量
sub r0.amount r2 into r3;
cast r0.owner r3 into r4 as token.record;
async transfer_private_to_public r1 r2 into r5;
output r4 as token.record;
output r5 as token.aleo/transfer_private_to_public.future;
finalize transfer_private_to_public:
input r0 as address.public;
input r1 as u64.public;
get.or_use account[r0] 0u64 into r2;
add r2 r1 into r3;
set r3 into account[r0];
此函数将私有代币转换为公共代币,允许在公共账户中持有之前私有的代币。
8. 公共代币转私有代币
function transfer_public_to_private:
input r0 as address.public; // 公共账户地址
input r1 as u64.public; // 转账数量
cast r0 r1 into r2 as token.record;
async transfer_public_to_private self.caller r1 into r3;
output r2 as token.record;
output r3 as token.aleo/transfer_public_to_private.future;
finalize transfer_public_to_private:
input r0 as address.public;
input r1 as u64.public;
get.or_use account[r0] 0u64 into r2;
sub r2 r1 into r3;
set r3 into account[r0];
此函数将公共代币转换为私有代币,以确保交易隐私和安全。
通过这些规范和示例代码,ARC-20标准在Aleo区块链上实现了兼容且安全的同质化代币管理。
Aleo 官方链接:
