Token Vesting Smart Contract
Introduction
Token vesting ensures fair distribution by releasing tokens to stakeholders over time. This tutorial demonstrates how to create a token vesting smart contract for a transparent release schedule.
Step 1: Setting Up Your Development Environment
Make sure you have these tools installed:
- Node.js
- Truffle or Hardhat for contract development
- Ganache for local testing
Step 2: Writing the Vesting Contract
In the contracts
folder, create TokenVesting.sol
:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract TokenVesting {
address public beneficiary;
uint256 public start;
uint256 public duration;
uint256 public released;
IERC20 public token;
constructor(address _beneficiary, uint256 _start, uint256 _duration, address _token) {
beneficiary = _beneficiary;
start = _start;
duration = _duration;
token = IERC20(_token);
}
function release() public {
require(block.timestamp >= start, "Vesting not started");
uint256 vestedAmount = (token.balanceOf(address(this)) * (block.timestamp - start)) / duration;
uint256 unreleased = vestedAmount - released;
require(unreleased > 0, "No tokens to release");
released += unreleased;
token.transfer(beneficiary, unreleased);
}
}
Step 3: Deploying the Contract
Configure your deployment script to set parameters such as the beneficiary address, start time, duration, and token address.
Step 4: Testing and Interaction
Use Ganache or testnets to simulate vesting scenarios. Call release()
after the vesting period begins to transfer vested tokens.
Recommendation
Developing a robust token vesting mechanism is critical for building trust in your project. For advanced features like cliff periods or multi-beneficiary vesting, Web3Dev’s professional services are here to help.
No Comments