区块链入门HiBlock区块链社区Dapp开发

第十九课 代币锁仓后逐步释放的ERC20智能合约实践

2018-08-06  本文已影响586人  笔名辉哥

1,摘要

【本文目标】
通过本文学习,可以实现区块链私募,基金会员工期权(代币)激励时锁仓一定时间,逐步释放的方法。

【前置条件】
1)已经完成了一个ERC20的代币,本文以作者接触的CLB为样例。
2) 懂得在REMIX调试SOLIDITY语言,不熟悉的参考文章《第十课 Solidity语言编辑器REMIX指导大全》

2,需求实现描述

一般区块链项目在私募或者员工沟通时,都会明确代币发放的政策,一般来说都会要求项目上线后锁仓多久,分几年释放。如果通过合同的方式来人工操作,一个是实现比较麻烦或者存在不可控性,另一方面也存在无法取信私募机构或者员工的情况。
那么专业的团队会选择通过智能合约来实现,这是更可信、公开、且不可串改的最佳方式。
这个实现概括讲包括3步:
1)发布ERC20代币智能合约
2)配置锁仓合约参数, 发布锁仓的智能合约
3)把要锁仓的ERC20代币转入锁仓智能合约
其中锁仓智能合约的文章极少,很多人不知怎么实现,辉哥来做个分享。

3,锁仓智能合约分析

锁仓智能合约核心代码:

/**
 * @title TokenVesting
 * @dev A token holder contract that can release its token balance gradually like a
 * typical vesting scheme, with a cliff and vesting period. Optionally revocable by the
 * owner.
 */
contract TokenVesting is Ownable {
  using SafeMath for uint256;
  using SafeERC20 for Colorbay;

  event Released(uint256 amount);
  event Revoked();

  // beneficiary of tokens after they are released
  address public beneficiary;

  uint256 public cliff;
  uint256 public start;
  uint256 public duration;

  bool public revocable;

  mapping (address => uint256) public released;
  mapping (address => bool) public revoked;

  /**
   * @dev Creates a vesting contract that vests its balance of any ERC20 token to the
   * _beneficiary, gradually in a linear fashion until _start + _duration. By then all
   * of the balance will have vested.
   * @param _beneficiary address of the beneficiary to whom vested tokens are transferred
   * @param _cliff duration in seconds of the cliff in which tokens will begin to vest
   * @param _start the time (as Unix time) at which point vesting starts
   * @param _duration duration in seconds of the period in which the tokens will vest
   * @param _revocable whether the vesting is revocable or not
   */
  constructor(
    address _beneficiary,
    uint256 _start,
    uint256 _cliff,
    uint256 _duration,
    bool _revocable
  )
    public
  {
    require(_beneficiary != address(0));
    require(_cliff <= _duration);

    beneficiary = _beneficiary;
    revocable = _revocable;
    duration = _duration;
    cliff = _start.add(_cliff);
    start = _start;
  }

  /**
   * @notice Transfers vested tokens to beneficiary.
   * @param _token Colorbay token which is being vested
   */
  function release(Colorbay _token) public {
    uint256 unreleased = releasableAmount(_token);

    require(unreleased > 0);

    released[_token] = released[_token].add(unreleased);

    _token.safeTransfer(beneficiary, unreleased);

    emit Released(unreleased);
  }

  /**
   * @notice Allows the owner to revoke the vesting. Tokens already vested
   * remain in the contract, the rest are returned to the owner.
   * @param _token ERC20 token which is being vested
   */
  function revoke(Colorbay _token) public onlyOwner {
    require(revocable);
    require(!revoked[_token]);

    uint256 balance = _token.balanceOf(address(this));

    uint256 unreleased = releasableAmount(_token);
    uint256 refund = balance.sub(unreleased);

    revoked[_token] = true;

    _token.safeTransfer(owner, refund);

    emit Revoked();
  }

  /**
   * @dev Calculates the amount that has already vested but hasn't been released yet.
   * @param _token Colorbay token which is being vested
   */
  function releasableAmount(Colorbay _token) public view returns (uint256) {
    return vestedAmount(_token).sub(released[_token]);
  }

  /**
   * @dev Calculates the amount that has already vested.
   * @param _token ERC20 token which is being vested
   */
  function vestedAmount(Colorbay _token) public view returns (uint256) {
    uint256 currentBalance = _token.balanceOf(this);
    uint256 totalBalance = currentBalance.add(released[_token]);
    
    if (block.timestamp < cliff) {
      return 0;
    } else if (block.timestamp >= start.add(duration) || revoked[_token]) {
      return totalBalance;
    } else {
      return totalBalance.mul(block.timestamp.sub(start)).div(duration);
    }
  }
}

函数说明:
1,锁仓合约初始化函数constructor(...),包含5个参数:

2,期权代币释放函数release(...),包含1个参数:

3,期权代币回收函数revoke(...),包含1个参数:

完整的代码和CLB通证的调用函数,辉哥存放到知识星球,欢迎加入下载。

3, 测试用例验证

1] 管理员账号发布一个ERC20的ColorBay代币合约

CLB相关信息

2] 管理员账号转发500万给员工激励专用账号用于期权激励专用

3] 当前账号切换到员工激励专用账号下创建期权激励计划

4] 在CLB合约下,把CLB通证打给期权激励智能合约地址

5] [2018.08.06 17:31] 5分钟(300s)后测试分配期权

6] 离职员工激励计划实施

7] 在CLB合约下,通证打给期权激励智能合约地址

8] [2018.08.06 21:03] 3分钟(180s)后测试分配期权

9] [2018.08.06 21:05] 4分钟(240s)后,李四离职,收回分配期权
revoke("0x692a70d2e424a56d2c6c27aa97d1a86395877b3a")

4, 商用实现需求

辉哥给了一个命题需求,看看谁能把这个需求实现。除了功能,要提供网页查询页面,用户输入以太坊地址,可以查到对应账户的期权代币总额,已释放额度,未释放额度。

某项目方发布10亿的代币总额,其中:
1] 机构方私募为 1.5 亿枚, 上交易所后私募锁仓三个月,之后逐月释放 20%;
2] 基金会核心运作团队持有 2 亿枚, 锁定一年, 两年内逐步释放;
3] 社区建设 1 亿枚, 海外社区核心团队, 锁定一年,两年内逐步释放;

转化为具体的时间点动作为:
1] 私募总额1.5亿枚,2018.09.01上交易所,私募锁仓三个月到2018.12.01到期,之后逐月释放情况
[2019.01.01 - 20%, 3000万枚; 2019.02.01 - 20%, 3000万枚; 2019.03.01 - 20%, 3000万枚; 2019.04.01 20%, 3000万枚; 2019.05.01 20%, 3000万枚; ]
2] 基金会核心运作团队 2亿枚,2018.09.01上交易所,锁定一年到2019.09.01, 两年内按月逐步释放;
[2019.10.01 - 2亿1/24, 约833万枚; ...; 2020.10.01 - 2亿1/24, 约833万枚;]
3] 海外社区核心团队 1 亿枚,2018.09.01上交易所,锁定一年到2019.09.01,两年内逐步释放;
[2019.10.01 - 1亿1/24, 约416万枚; ...; 2020.10.01 - 1亿1/24, 约416万枚;]

5, 参考

1)关于ERC20代币的锁仓
2) 授权和释放合约
3) 定时释放合约


辉哥的技术博客即将搬运同步至腾讯云+社区,欢迎您点击关注:
https://cloud.tencent.com/developer/support-plan?invite_code=ipd7hcxeh5gk

上一篇 下一篇

猜你喜欢

热点阅读