Solidity-ERC20代币的锁仓与释放-2

此篇文章主要介绍如何部署一个ERC20代币,和写一个智能合约的锁仓 ,释放功能,最后通过合约去管理,这些Token的流通。

在上一篇文章已经介绍并且部署成功了一个ERC20Token,现在开始实现锁仓,和释放的功能。

 一般锁仓分两种模式:

  • TimeLock
  • Vesting

今天我们主要一TimeLock来实现:

主要实现逻辑:

  •   先打开remix
  •  然后编写锁仓,释放的业务功能(里面采用的利用区块的时间戳来做判断条件block.timestamp)
  • 编写成功之后部署
  • 编写机器人去定时触发合约执行某个功能

1.打开网页版Remix

2.创建新文件夹编写锁仓核心业务(TimeLock 锁仓方 式

     代码参考:ERC20/TokenTimelock.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.0 (token/ERC20/utils/TokenTimelock.sol)

pragma solidity ^0.8.0;

import "./SafeERC20.sol";
import "./IERC20.sol";
 
contract TokenTimelock {
    using SafeERC20 for IERC20;

    // ERC20 basic token contract being held
    IERC20 private immutable _token;

    // beneficiary of tokens after they are released
    address private immutable _beneficiary;

    // timestamp when token release is enabled
    uint256 private immutable _releaseTime;

    constructor(
        IERC20 token_,
        address beneficiary_,
        uint256 releaseTime_
    ) {
        require(releaseTime_ > block.timestamp, "TokenTimelock: release time is before current time");
        _token = token_;
        _beneficiary = beneficiary_;
        _releaseTime = releaseTime_;
    }

    /**
     * @return the token being held.
     */
    function token() public view virtual returns (IERC20) {
        return _token;
    }

    /**
     * @return the beneficiary of the tokens.
     */
    function beneficiary() public view virtual returns (address) {
        return _beneficiary;
    }

    /**
     * @return the time when the tokens are released.
     */
    function releaseTime() public view virtual returns (uint256) {
        return _releaseTime;
    }

    /**
     * @notice Transfers tokens held by timelock to beneficiary.
     */
    function release() public virtual {
        require(block.timestamp >= releaseTime(), "TokenTimelock: current time is before release time");

        uint256 amount = token().balanceOf(address(this));
        require(amount > 0, "TokenTimelock: no tokens to release");

        token().safeTransfer(beneficiary(), amount);
    }
}

编写成功后

3.第三步

点击左上角的--Complie编译合约--编译通过后点击--左上角的DEPLOY部署自己的ERC20合约,在这里要注意先链接好自己的钱包地址,并且里面有测试代币等,最后点击部署即可完成全部操作。

接着合约就会按照你制定的规则执行。

4.第四步

编写智能合约调用的release方法,因为智能合约目前还有个天花板是不能自己触犯自己,必须要从外部触发,当合约里面的释放条件规则满足之后,他需要有机器人去触犯执行操作释放。

优点:

  • 这种TimeLock锁仓方式简洁明了,

  • _releaseTime 时间到了之后调用 release() 就会把token发放到_beneficiary的账户中。

缺点:

  • 不过TimeLock方式有局限性

  • 例如需求是:锁仓4年,1年之后一次性解冻25%,后面按时间线性解冻

  • 这样复杂的锁仓需求 TimeLock是搞不定的,这时候就要用 Vesting 方式,这种方式会下次在详细讲解。

 

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>