区块链锁仓智能合约怎么实现

发布时间:2022-01-18 10:38:54 作者:iii
来源:亿速云 阅读:225

这篇文章主要介绍了区块链锁仓智能合约怎么实现的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇区块链锁仓智能合约怎么实现文章都会有所收获,下面我们一起来看看吧。

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

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

需求实现描述

一般区块链项目在私募或者员工沟通时,都会明确代币发放的政策,一般来说都会要求项目上线后锁仓多久,分几年释放。如果通过合同的方式来人工操作,一个是实现比较麻烦或者存在不可控性,另一方面也存在无法取信私募机构或者员工的情况。

那么专业的团队会选择通过智能合约来实现,这是更可信、公开、且不可串改的最佳方式。

这个实现概括讲包括3步: 1)发布ERC20代币智能合约 2)配置锁仓合约参数, 发布锁仓的智能合约 3)把要锁仓的ERC20代币转入锁仓智能合约

锁仓智能合约分析

锁仓智能合约核心代码:

/** 

 * @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个参数:

举例来说明:

如果 _cliff=半年 ,_duration=1年 具体解冻情况如下: Month 1: I get 0 tokens Month 2: I get 0 tokens Month 3: I get 0 tokens Month 4: I get 0 tokens Month 5: I get 0 tokens Month 6: I get 0 tokens --- End of cliff Month 7: I get 700 tokens (7/12th) Month 8: I get 100 tokens (8/12th) Month 9: I get 100 tokens (9/12th) Month 10: I get 100 tokens (10/12th) Month 11: I get 100 tokens (11/12th) Month 12: I get 100 tokens (12/12th)

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

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

4

测试用例验证

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")

区块链锁仓智能合约怎么实现

发现王五剩余未释放的额度全部返回到员工专用账号了。

关于“区块链锁仓智能合约怎么实现”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“区块链锁仓智能合约怎么实现”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 区块链里的智能合约安全
  2. solidity智能合约[56]-solc编译智能合约

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

区块链

上一篇:Tendermint核心是什么

下一篇:以太坊开发环境怎么配置

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》