[提案] 最小 UDT 实现和扩展

本文译自: [Proposal] Minimal UDT Implementation and Extensions
作者:Cipher

我们已经有多篇文章讨论了在 CKB 上实现 UDT,比如 在 CKB 上设计一个 UDT 标准的方法:Part 1 ,以及 关于 UDT 标准评估规范的讨论 。根据前面的讨论,本文是一个最小 UDT 实现提案。

ERC20 标准

首先,ERC20 标准定义了一个接口,而不是一个实现,这意味着开发人员必须为每个新的 Token 部署一个新的 ERC20 可编译的智能合约。 而我们正在讨论的 UDT 标准是一个实现,这意味着所有 Token 可以共享相同的基础脚本代码。

// Grabbed from: https://github.com/ethereum/EIPs/issues/20
contract ERC20 {
   function totalSupply();
   function balanceOf(address _owner);
   function transfer(address _to, uint _value);
   function transferFrom(address _from, address _to, uint _value);
   function approve(address _spender, uint _value);
   function allowance(address _owner, address _spender);
}

这是一个标准的 ERC20 接口。 我们可以将这些函数拆分为三个类别,隐藏的 constructor functiontotalSupply 用于 token 管理;balanceOftransfer 用于日常使用;approveallowancetransferFrom 用于授权。

实际上,一个最小 UDT 实现必须包括 token 创建和 token 传输函数。开发者可以使用扩展来实现其他功能,比如未来管理和授权。

CKB 上的最小 UDT

本 UDT 提案只支持两个基础操作(什么是操作?) 在这个最小 UDT 实现中。 这两个操作用于处理两种 Cells 的状态转换。

# actions
Action create:
    input: [capacity_cell, ...]
    output: [info_cell, balance_cell]

Action transfer:
    input: [balance_cell, ...]
    output: [balance_cell, ...]

# cells
Cell info_cell:
    data: /* infomation including supply, decimal, symbol, full name, operator, ...*/
    type: {code: minimal_udt, args: uuid_for_udt}
    lock: /*operator defined*/

Cell balance_cell:
    data: /* 64bit UDT amount*/
    type: {code: minimal_udt, args: uuid_for_udt}
    lock: /*user defined*/

任何人都可以启动 create 操作,即在 CKB 上创建一个新的 UDT,使用 witness 中的基础 token 参数来定义新 UDT 的名称、符号、初始供应量和小数位数。这种最小 UDT 类型保证了每种 UDT 具备唯一的 uuid,并保证了余额的一致性。

UDT 实现的扩展

UDT 实现可以通过使用自定义锁脚本和相应的 Cell 来进行扩展。 这里有几个例子。

Token 管理扩展

/* Add mint and burn fucntions: 
    - setup info_cell's lock to operator defined lock 
    - operator unlocks info_cell to modify it
    - unlock rules are user-defined, like 2-of-3 multisig or token holder vote.
*/

// actions
Action mint:
    input: [info_cell]
    output: [info_cell, balance_cell, ...]

Action burn:
    input: [info_cell, balance_cell, ...]
    output: [info_cell, balance_cell]

授权

开发者也可以使用新的锁脚本来实现 ERC20 的 approvetransfer_from 等功能。

/* approve_logic description:
  - grantor could fully control this cell
  - grantee could be either a lock script or a type script
    - lock script: grantee must provide the unlock witness to the lock script to consume this cell.
    - type script: the TX that consume this cell must contains an output with specific type script.
  - approve_parameters consists of grantor, grantee and allowance amount.
*/

// actions
Action approve:
    input: [balance_cell, ...]
    output: [balance_with_approve_cell, ...]

Action transfer_from:
    input: [balance_with_approve_cell]
    output: [balance_with_approve_cell, balance_cell, ...]

// cells
Cell balance_with_approve_cell: /*standard udt cell with specific lock*/
    data: /* 64bit UDT amount*/
    type: {code: minimal_udt, args: uuid_for_udt}
    lock: {code: approve_logic, args: approve_parameters}

原子交换

/* Alice use 100 A tokens to exchange 10 B tokens.
  - Alice create a balance_cell_pending to describe her intention
  - Bob consume the balance_cell_pending with 10 B tokens and send the 100 A tokens to himself.

  lock atomic_swap_logic:
  - Alice could fully control this cell
  - Any TX consumed this cell (except Alice herself initialized) must follow these rules:
    -  There are 10 B tokens in output with lock = Alice's lock
*/

// actions
Action ask:
    input: [balance_cell_A, ...]
    output: [balance_cell_pending]

Action bid:
    input: [balance_cell_pending, balance_cell_B]
    output: [balance_cell_A, balance_cell_B]

// cells
Cell balance_cell_pending: /*standard udt cell with specific lock*/
    data: /* 64bit UDT amount*/
    type: {code: minimal_udt, args: uuid_for_udt}
    lock: {code: atomic_swap_logic, args: swap_parameters}

总结

在我看来,如果第三方可以提供新的锁脚本和新型的 cells,那么这个最小 UDT 模型可以覆盖大部分的 token 使用场景。社区在 CKB 上提供关于这个最小 UDT 方案的实现,并将扩展留给 dApp 开发者们。


推荐阅读:
1 Like

因本人翻译能力水平有限,如有错误或不足望大家及时批评指正 :smiling_face_with_three_hearts: