ZRC2 同质化代币合约标准
ZRC | 标题 | 状态 | 类型 | 作者 | 创建时间 (yyyy-mm-dd) | 更新时间 (yyyy-mm-dd) |
---|---|---|---|---|---|---|
2 | 同质化代币标准 | 批准 | 标准 | Vaivaswatha Nagaraj vaivaswatha@zilliqa.com Chua Han Wen hanwen@zilliqa.com |
2019-11-18 | 2020-04-25 |
I. 什么是同质化代币?
同质化代币是用于创建货币的开放标准。同质化是商品的属性,其每个单元基本上都可以互换,并且其每个部分都与另一部分无法区分。
II. 摘要
ZRC-2 定义了智能合约必须实现的最小接口,以允许通过钱包或交易所点对点管理、跟踪、拥有和交易同质化代币。
III. 动机
同质化代币的标准可以作为开发人员创建货币、通用代币或稳定币的接口。通常,同质化代币可用于将可互换、相同和可分割的资产表示为代币。
IV. 规格
同质化代币合约规范描述了:
- 需要在合约库部分声明的全局错误代码;
- 不可变和可变变量(又名“字段”)的名称和类型;
- 允许改变可变变量值的 transition;
- 由它们发出的事件。
A. 角色
名称 | 说明 |
---|---|
contract_owner |
由合约创建者初始化的合约所有者。 |
token_owner |
拥有代币的用户(由地址标识)。 |
approved_spender |
可以代表 token_owner 转移代币的用户(由地址标识)。 |
operator |
一个用户(由地址标识)被批准操作另一个用户(由另一个地址标识)拥有的所有代币。 此角色是可选的。 |
default_operator |
一个特殊用户(由地址标识),被批准操作所有用户(由地址标识)拥有的所有代币。 此角色是可选的。 |
B. 错误代码
同质化代币合约必须定义以下常量以用作 Error
异常的错误代码。
名称 | 类型 | 代码 | 说明 |
---|---|---|---|
CodeIsSender |
Int32 |
-1 |
当地址与发件人相同时发出。 |
CodeInsufficientFunds |
Int32 |
-2 |
当余额不足以授权交易时发出。 |
CodeInsufficientAllowance |
Int32 |
-3 |
当没有足够的配额来授权交易时发出。 |
CodeNotOwner |
Int32 |
-4 |
当发送者不是 contract_owner 时发出。 此错误代码是可选的。 |
CodeNotApprovedOperator |
Int32 |
-5 |
当调用者不是批准的操作员或 default_operator 时发出。 此错误代码是可选的。 |
C. 不可变变量
名称 | 类型 | 说明 |
---|---|---|
contract_owner |
ByStr20 |
由合约创建者初始化的合约所有者。 |
name |
String |
同质化代币的名称。 |
symbol |
String |
同质化代币的符号。 |
decimals |
Uint32 |
代币可以被分割的小数位数。 |
init_supply |
Uint128 |
创建合约时同质化代币的初始供应量。 |
default_operators |
List ByStr20 |
由合约创建者初始化的默认操作员列表。 这个不可变变量是可选的。 |
D. 可变字段
名称 | 类型 | 说明 |
---|---|---|
total_supply |
Uint128 |
可用代币总量。 |
balances |
Map ByStr20 Uint128 |
代币所有者与拥有代币数量之间的映射。 |
allowances |
Map ByStr20 (Map ByStr20 Uint128) |
从代币所有者到批准的支出者地址的映射。 代币所有者可以允许一个地址将代币转移到其他地址。 |
operators |
Map ByStr20 (Map ByStr20 Unit) |
从代币所有者到指定操作员的映射。 代币所有者可以批准一个地址作为操作员(根据上面给出的操作员定义)。 此映射是可选的。 |
revoked_default_operators |
Map ByStr20 (Map ByStr20 Unit) |
从代币所有者映射到已撤销的默认操作员。 默认操作员由合约所有者初始化。 代币所有者可以随意撤销默认操作员(根据上面给出的默认操作员的定义)。 此映射是可选的。 |
E. Getter Transitions
1. IsOperatorFor() (可选)
(* @dev: Check if an address is an operator or default operator of a token_owner. Throw if not. *)
(* @param operator: Address of a potential operator. *)
(* @param token_owner: Address of a token_owner. *)
transition IsOperatorFor(token_owner: ByStr20, operator: ByStr20)
参数:
名称 | 类型 | 说明 | |
---|---|---|---|
@param | token_owner |
ByStr20 |
特定 token_owner 的地址。 |
@param | operator |
ByStr20 |
token_owner 的特定操作员的地址。 |
发送的消息:
名称 | 说明 | 回调参数 | |
---|---|---|---|
_tag |
IsOperatorForCallBack |
如果指定地址确实是指定 token_owner 的批准操作员,则向发送者提供回调。 | token_owner : ByStr20 , operator : ByStr20 , 其中 token_owner 是 token_owner 的地址,operator 是被批准的操作员的地址。 |
F. Interface Transitions
1. Mint() (可选)
(* @dev: Mint new tokens. Only contract_owner can mint. *)
(* @param recipient: Address of the recipient whose balance is to increase. *)
(* @param amount: Number of tokens to be minted. *)
transition Mint(recipient: ByStr20, amount: Uint128)
参数:
名称 | 类型 | 说明 | |
---|---|---|---|
@param | recipient |
ByStr20 |
余额要增加的收款人地址。 |
@param | amount |
Uint128 |
要铸造的代币数量。 |
发送的消息:
名称 | 说明 | 回调参数 | |
_tag |
RecipientAcceptMint |
虚拟回调以防止无效的接收者合约。 | minter : ByStr20 , recipient : ByStr20 , amount : Uint128 , 其中 minter 是铸币者的地址,recipient 是接收者的地址,amount 是铸造的同质化代币的数量。 |
_tag |
MintSuccessCallBack |
向发件人提供铸币厂的状态。 | minter : ByStr20 , recipient : ByStr20 , amount : Uint128 , 其中 minter 是铸币者的地址,recipient 是接收者的地址,amount 是铸造的同质化代币的数量。 |
事件/错误:
名称 | 说明 | 事件参数 | |
---|---|---|---|
_eventname |
Minted |
铸造成功。 | minter : ByStr20 , recipient : ByStr20 , amount : Uint128 , 其中 minter 是铸币者的地址,recipient 是余额将增加的地址,amount 是铸造的同质化代币的数量。 |
_eventname |
Error |
铸造不成功。 | – 如果 contract_owner 没有调用 transition,则发出CodeNotOwner 。 |
2. Burn()(可选)
(* @dev: Burn existing tokens. Only contract_owner can burn. *)
(* @param burn_account: Address of the token_owner whose balance is to decrease. *)
(* @param amount: Number of tokens to be burned. *)
transition Burn(burn_account: ByStr20, amount: Uint128)
参数:
名称 | 类型 | 说明 | |
---|---|---|---|
@param | burn_account |
ByStr20 |
余额要减少的 token_owner 的地址。 |
@param | amount |
Uint128 |
要销毁的代币数量。 |
发送的消息:
名称 | 说明 | 回调参数 | |
---|---|---|---|
_tag |
BurnSuccessCallBack |
向发送者提供销毁状态。 | burner : ByStr20 , burn_account : ByStr20 , amount : Uint128 , 其中 burner 是销毁者的地址,burn_account 是余额将减少的地址,amount 是销毁的同质化代币的数量。 |
事件/错误:
名称 | 说明 | 事件参数 | |
---|---|---|---|
_eventname |
Burnt |
销毁成功 | burner : ByStr20 , burn_account : ByStr20 , amount : Uint128 , 其中 burner 是销毁者的地址,burn_account 是余额将减少的地址,amount 是销毁的同质化代币的数量。 |
_eventname |
Error |
销毁不成功 | – 如果 contract_owner 没有调用 transition,则发出 CodeNotOwner 。 – 如果 token_owner 的余额不存在,则发出 CodeNoBalance 。 – 如果要销毁的金额超过 token_owner 的余额,则发出 CodeInsufficientFunds 。 |
3. AuthorizeOperator() (可选)
(* @dev: Make an address an operator of the caller. *)
(* @param operator: Address to be authorize as operator or *)
(* Re-authorize as default_operator. Cannot be calling address. *)
transition AuthorizeOperator(operator: ByStr20)
参数:
名称 | 类型 | 说明 | |
---|---|---|---|
@param | operator |
ByStr20 |
地址被授权为操作员或重新授权为default_operator。 不能调用地址。 |
事件/错误:
名称 | 说明 | 事件参数 | |
---|---|---|---|
_eventname |
AuthorizeOperatorSuccess |
授权成功。 | authorizer : ByStr20 这是调用者的地址,以及 authorized_operator : ByStr20 这是要被授权为 token_owner 的操作员的地址。 |
_eventname |
Error |
授权不成功。 | – 如果用户试图将自己授权为操作员,则发出 CodeIsSender 。 |
4. RevokeOperator() (可选)
(* @dev: Revoke an address from being an operator or default_operator of the caller. *)
(* @param operator: Address to be removed as operator or default_operator. *)
transition RevokeOperator(operator: ByStr20)
参数:
名称 | 类型 | 说明 | |
---|---|---|---|
@param | operator |
ByStr20 |
要取消设置为操作员的地址。 |
事件/错误:
名称 | 说明 | 事件参数 | |
---|---|---|---|
_eventname |
RevokeOperatorSuccess |
撤销成功。 | revoker : ByStr20 这是调用者的地址,以及 revoked_operator : ByStr20 这是要被撤销的 token_owner 的操作员的地址。 |
_eventname |
Error |
撤销不成功。 | – 如果指定的地址不是 token_owner 的现有操作员或 default_operator,则发出 CodeNotApprovedOperator 。 |
5. IncreaseAllowance()
(* @dev: Increase the allowance of an approved_spender over the caller tokens. Only token_owner allowed to invoke. *)
(* param spender: Address of the designated approved_spender. *)
(* param amount: Number of tokens to be increased as allowance for the approved_spender. *)
transition IncreaseAllowance(spender: ByStr20, amount: Uint128)
参数:
名称 | 类型 | 说明 | |
---|---|---|---|
@param | spender |
ByStr20 |
已批准的支出者的地址。 |
@param | amount |
Uint128 |
作为 approved_spender 的支出限额要增加的代币数量。 |
事件/错误:
名称 | 说明 | 事件参数 | |
---|---|---|---|
_eventname |
IncreasedAllowance |
被批准的支出者的限额增加成功。 | token_owner : ByStr20 这是 token_owner 的地址, spender : ByStr20 这是 token_owner 的 approved_spender 的地址,以及 new_allowance 是 token_owner 的 approved_spender 的新支出限额。 |
_eventname |
Error |
限额增加不成功 | – 如果用户尝试将自己授权为 approved_spender,则发出 CodeIsSelf 。 |
6. DecreaseAllowance()
(* @dev: Decrease the allowance of an approved_spender over the caller tokens. Only token_owner allowed to invoke. *)
(* param spender: Address of the designated approved_spender. *)
(* param amount: Number of tokens to be decreased as allowance for the approved_spender. *)
transition DecreaseAllowance(spender: ByStr20, amount: Uint128)
参数:
名称 | 类型 | 说明 | |
---|---|---|---|
@param | spender |
ByStr20 |
已批准的支出者的地址。 |
@param | amount |
Uint128 |
作为 approved_spender 的支出限额要减少的代币数量。 |
事件/错误:
名称 | 说明 | 事件参数 | |
---|---|---|---|
_eventname |
DecreasedAllowance |
已批准的支出者的限额减少成功。 | token_owner : ByStr20 这是 token_owner 的地址,spender : ByStr20 这是 token_owner 的 approved_spender 的地址,以及 new_allowance 是 token_owner 的 approved_spender 的新支出限额。 |
_eventname |
Error |
限额减少不成功 | – 如果用户尝试将自己授权为 approved_spender,则发出 CodeIsSelf 。 |
7. Transfer()
(* @dev: Moves an amount tokens from _sender to the recipient. Used by token_owner. *)
(* @dev: Balance of recipient will increase. Balance of _sender will decrease. *)
(* @param to: Address of the recipient whose balance is increased. *)
(* @param amount: Amount of tokens to be sent. *)
transition Transfer(to: ByStr20, amount: Uint128)
参数:
名称 | 类型 | 说明 | |
---|---|---|---|
@param | to |
ByStr20 |
余额要增加的收款人地址。 |
@param | amount |
Uint128 |
要发送的代币数量。 |
发送的消息:
名称 | 说明 | 回调参数 | |
---|---|---|---|
_tag |
RecipientAcceptTransfer |
虚拟回调以防止无效的接收者合约。 | sender : ByStr20 , recipient : ByStr20 , amount : Uint128 ,其中 sender 是发送者的地址,recipient 是接收者的地址,amount 是要转移的同质化代币的数量。 |
_tag |
TransferSuccessCallBack |
向发件人提供传输状态。 | sender : ByStr20 , recipient : ByStr20 , amount : Uint128 ,其中 sender 是发送者的地址,recipient 是接收者的地址,amount 是要转移的同质化代币的数量。 |
事件/错误:
名称 | 说明 | 回调参数 | |
---|---|---|---|
_eventname |
TransferSuccess |
发送成功。 | sender :ByStr20 是发送者的地址,recipient :ByStr20 是接收者的地址,amount :Uint128 是转移的同质化代币的数量。 |
_eventname |
Error |
发送不成功。 | -如果 token_owner 的余额小于要转移的指定金额,则发出 CodeInsufficientFunds 。 |
8. TransferFrom()
(* @dev: Move a given amount of tokens from one address to another using the allowance mechanism. The caller must be an approved_spender. *)
(* @dev: Balance of recipient will increase. Balance of token_owner will decrease. *)
(* @param from: Address of the token_owner whose balance is decreased. *)
(* @param to: Address of the recipient whose balance is increased. *)
(* @param amount: Amount of tokens to be transferred. *)
transition TransferFrom(from: ByStr20, to: ByStr20, amount: Uint128)
参数:
名称 | 类型 | 说明 | |
---|---|---|---|
@param | from |
ByStr20 |
余额要减少的 token_owner 的地址。 |
@param | to |
ByStr20 |
余额要增加的收款人地址。 |
@param | amount |
Uint128 |
要转移的代币数量。 |
发送的消息:
名称 | 说明 | 回调参数 | |
---|---|---|---|
_tag |
RecipientAcceptTransferFrom |
虚拟回调以防止无效的接收者合约。 | initiator :ByStr20 ,sender :ByStr20 ,recipient :ByStr20 ,amount :Uint128 ,其中initiator 是一个 approved_spender 的地址,sender 是 token_owner,recipient 是接收者的地址,amount 是要转移的同质化代币的数量。 |
_tag |
TransferFromSuccessCallBack |
向发起人提供传输状态。 | initiator :ByStr20 ,sender :ByStr20 ,recipient :ByStr20 ,amount :Uint128 ,其中initiator 是一个 approved_spender 的地址,sender 是 token_owner,recipient 是接收者的地址,amount 是要转移的同质化代币的数量。 |
事件:
名称 | 说明 | 事件参数 | |
---|---|---|---|
_eventname |
TransferFromSuccess |
发送成功。 | initiator : ByStr20 , sender : ByStr20 , recipient : ByStr20 , amount : Uint128 , 其中,initiator 是批准的支付者的地址,sender 是 token_owner 的地址,recipient 是接收者的地址,amount 是要转移的可替代代币的数量。 |
_eventname |
Error |
发送不成功。 | – 如果 approved_spender 的限额小于要转移的指定金额,则发出CodeInsufficientAllowance 。 – 如果 token_owner 的余额小于要转移的指定金额,则发出 CodeInsufficientFunds 。 |
9. OperatorSend() (可选)
(* @dev: Moves amount tokens from token_owner to recipient. _sender must be an operator of token_owner. *)
(* @dev: Balance of recipient will increase. Balance of token_owner will decrease. *)
(* @param from: Address of the token_owner whose balance is decreased. *)
(* @param to: Address of the recipient whose balance is increased. *)
(* @param amount: Amount of tokens to be sent. *)
transition OperatorSend(from: ByStr20, to: ByStr20, amount: Uint128)
参数:
名称 |
类型
|
说明
|
|
@param
|
from |
ByStr20 |
余额要减少的 token_owner 的地址。
|
@param
|
to |
ByStr20 |
余额要增加的收款人地址。
|
@param
|
amount |
Uint128 |
要发送的令牌数量。
|
发送的消息:
名称 | 说明 | 回调参数 | |
---|---|---|---|
_tag |
RecipientAcceptOperatorSend |
虚拟回调以防止无效的接收者合约。 | initiator :ByStr20 ,sender :ByStr20 ,recipient :ByStr20 ,amount :Uint128 ,其中 initiator 是操作员的地址,sender 是操作员的地址 token_owner,recipient 是接收者的地址,amount 是要转移的同质化代币的数量。 |
_tag |
OperatorSendSuccessCallBack |
向操作员提供传输状态。 | initiator :ByStr20 ,sender :ByStr20 ,recipient :ByStr20 ,amount :Uint128 ,其中initiator 是操作员的地址,sender 是操作员的地址 token_owner,recipient 是接收者的地址,amount 是要转移的同质化代币的数量。 |
事件/错误:
名称 | 说明 | 事件参数 | |
---|---|---|---|
_eventname |
OperatorSendSuccess |
发送成功。 | initiator :ByStr20 是操作员的地址,sender :ByStr20 是 token_owner 的地址,recipient :ByStr20 是接收者的地址,amount :Uint128 是要转移的同质化代币的数量。 |
_eventname |
Error |
发送不成功。 | – 如果发送者不是 token_owner 的批准操作员,则发出 CodeNotApprovedOperator – 如果 token_owner 的余额小于要转移的指定金额,则发出 CodeInsufficientFunds 。 |
V. 现有实施
要测试参考合约,只需转到 example
文件夹并运行其中一个 JS 脚本。 例如,要部署合约,请运行:
yarn deploy.js
注意: 请将脚本中的
privkey
更改为你自己的私钥。 你可以在 Nucleus Faucet 生成测试网钱包并请求测试网 ZIL。
VI. 版权
通过 CC0 放弃版权和相关权利。
VII. 原始英文链接
发布者:Babbage,转转请注明出处:https://www.china-zil.com/zilliqa-tech/zrc2-%e5%90%8c%e8%b4%a8%e5%8c%96%e4%bb%a3%e5%b8%81%e5%90%88%e7%ba%a6%e6%a0%87%e5%87%86/
订阅评论
登录
0 评论