Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vesting with inclusion of Cliff duration #101

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 17 additions & 109 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,121 +1,29 @@
[![Actions Status](https://github.com/abdelhamidbakhta/token-vesting-contracts/workflows/test/badge.svg)](https://github.com/abdelhamidbakhta/token-vesting-contracts/actions/workflows/test.yml)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![license](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![npm version](https://badge.fury.io/js/erc20-token-vesting.svg)](https://badge.fury.io/js/erc20-token-vesting)

# Token Vesting Contracts
---

## Overview

On-Chain vesting scheme enabled by smart contracts.

`TokenVesting` contract can release its token balance gradually like a typical vesting scheme, with a cliff and vesting period.
The vesting schedules are optionally revocable by the owner.

## 🎭🧑‍💻 Security audits

- [Security audit](https://github.com/abdelhamidbakhta/token-vesting-contracts/blob/main/audits/hacken_audit_report.pdf) from [Hacken](https://hacken.io)

This repository is compatible with both Forge and Hardhat.
Forge needs to be ran (install and build) before Hardhat is used in order to load dependency contracts.
You can find the specific instructions for each tool below.

### Forge

#### 📦 Installation

```console
forge install
```

#### ⛏️ Compile

```console
forge build
```

#### 🌡️ Testing

```console
$ forge test
```

### Hardhat

#### 📦 Installation
This project is a fork of the ERC20 token vesting contract from [AbdelStark's repository](https://github.com/AbdelStark/token-vesting-contracts/tree/main). The current implementation has been modified to make the cliff period inclusive of the vesting duration.

```console
$ yarn
```

#### ⛏️ Compile

```console
$ yarn compile
```

This task will compile all smart contracts in the `contracts` directory.
ABI files will be automatically exported in `build/abi` directory.

#### 📚 Documentation

Documentation is auto-generated after each build in `docs` directory.

The generated output is a static website containing smart contract documentation.

#### 🌡️ Testing

Note: make sure to have ran forge build and compile before you run tests.

```console
$ yarn test
```

#### 📊 Code coverage

```console
$ yarn coverage
```

The report will be printed in the console and a static website containing full report will be generated in `coverage` directory.

#### ✨ Code style

```console
$ yarn prettier
```

#### 🐱‍💻 Verify & Publish contract source code
## Overview

```console
$ npx hardhat verify --network mainnet $CONTRACT_ADDRESS $CONSTRUCTOR_ARGUMENTS
```
In the modified implementation, tokens are not vested during the cliff period, making it inclusive of the vesting duration. For example, if 1000 tokens are allocated for a 10-month duration with a 3-month cliff period, the vested/claimable amount for the first 3 months will be 0. On the first day of the 4th month, the claimable amount should be 300 tokens.

## 📄 License
## Deployed Contracts

**Token Vesting Contracts** is released under the [Apache-2.0](LICENSE).
- **Token Address:** [0x480222Fd55597BB7EFc414a9C6d4E103820520Eb](https://sepolia.etherscan.io/address/0x480222Fd55597BB7EFc414a9C6d4E103820520Eb)
- **TokenVesting Address:** [0xDFf34D3960804DA62734d095419fD19F65229b0C](https://sepolia.etherscan.io/address/0xDFf34D3960804DA62734d095419fD19F65229b0C)
- **Deployer Address:** [0x14E9511285a10950f3c968Fa371F3b87182C9ef5](https://sepolia.etherscan.io/address/0x14E9511285a10950f3c968Fa371F3b87182C9ef5)

## Contributors ✨
## Modifications

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
1. **Cliff Period Inclusion:**
- The logic has been modified to include the cliff period within the vesting duration.

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
<tbody>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/abdelhamidbakhta"><img src="https://avatars.githubusercontent.com/u/45264458?v=4?s=100" width="100px;" alt="Abdel @ StarkWare "/><br /><sub><b>Abdel @ StarkWare </b></sub></a><br /><a href="https://github.com/abdelhamidbakhta/token-vesting-contracts/commits?author=abdelhamidbakhta" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/vpavlin"><img src="https://avatars.githubusercontent.com/u/4759808?v=4?s=100" width="100px;" alt="Vaclav Pavlin"/><br /><sub><b>Vaclav Pavlin</b></sub></a><br /><a href="https://github.com/abdelhamidbakhta/token-vesting-contracts/commits?author=vpavlin" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/TheMightyYak1"><img src="https://avatars.githubusercontent.com/u/78634345?v=4?s=100" width="100px;" alt="Brendan Baker"/><br /><sub><b>Brendan Baker</b></sub></a><br /><a href="https://github.com/abdelhamidbakhta/token-vesting-contracts/commits?author=TheMightyYak1" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/owlen"><img src="https://avatars.githubusercontent.com/u/5638865?v=4?s=100" width="100px;" alt="Oren Gampel"/><br /><sub><b>Oren Gampel</b></sub></a><br /><a href="https://github.com/abdelhamidbakhta/token-vesting-contracts/commits?author=owlen" title="Code">💻</a></td>
</tr>
</tbody>
</table>
2. **Example Scenario:**
- For a 1000 tokens allocation over a 10-month duration with a 3-month cliff period:
- The claimable amount is 0 for the first 3 months.
- On the first day of the 4th month, the claimable amount is 300 tokens.

<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->
## Testing

<!-- ALL-CONTRIBUTORS-LIST:END -->
A test case has been written to pass the scenario mentioned above.

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
23 changes: 21 additions & 2 deletions hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require("hardhat-tracer");
require("hardhat-gas-reporter");
require("solidity-coverage");
require("hardhat-preprocessor");
require("dotenv").config();
const fs = require("fs");

const etherscanApiKey = getEtherscanApiKey();
Expand Down Expand Up @@ -52,8 +53,9 @@ module.exports = {
}),
},
networks: {
mainnet: mainnetNetworkConfig(),
goerli: goerliNetworkConfig(),
// mainnet: mainnetNetworkConfig(),
// goerli: goerliNetworkConfig(),
sepolia: sepoliaNetworkConfig(),
},
abiExporter: {
path: "./build/abi",
Expand Down Expand Up @@ -109,6 +111,23 @@ function goerliNetworkConfig() {
accounts: [accountPrivateKey],
};
}
function sepoliaNetworkConfig() {
// console.log("Hello");
let url = "https://goerli.infura.io/v3/";
let accountPrivateKey =
"0x0000000000000000000000000000000000000000000000000000000000000000";
if (process.env.SEPOLIA_ENDPOINT) {
url = `${process.env.SEPOLIA_ENDPOINT}`;
}

if (process.env.SEPOLIA_PRIVATE_KEY) {
accountPrivateKey = `${process.env.SEPOLIA_PRIVATE_KEY}`;
}
return {
url: url,
accounts: [accountPrivateKey],
};
}

function getEtherscanApiKey() {
let apiKey = "";
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"flatten": "npx hardhat flatten",
"check:format": "solhint contracts/*.sol --fix",
"deploy:mainnet": "npx hardhat run scripts/deploy.js --network mainnet",
"deploy:goerli": "npx hardhat run scripts/deploy.js --network goerli"
"deploy:goerli": "npx hardhat run scripts/deploy.js --network goerli",
"deploy:sepolia": "npx hardhat run scripts/deploy.js --network sepolia"
},
"files": [
"build/abi",
Expand All @@ -36,6 +37,7 @@
"homepage": "https://github.com/abdelhamidbakhta/token-vesting-contracts#readme",
"dependencies": {
"@openzeppelin/contracts": "^5.0.1",
"dotenv": "^16.4.5",
"prettier": "^2.3.0",
"solc": "npm:solc@^0.8.4",
"solidity-coverage": "^0.7.16",
Expand All @@ -51,14 +53,14 @@
"@nomiclabs/hardhat-etherscan": "^3.1.7",
"@nomiclabs/hardhat-solhint": "^2.0.0",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"hardhat-preprocessor": "^0.1.5",
"chai": "^4.3.4",
"ethereum-waffle": "^3.3.0",
"ethers": "^5.2.0",
"hardhat": "^2.3.0",
"hardhat-abi-exporter": "^2.2.1",
"hardhat-docgen": "^1.1.1",
"hardhat-gas-reporter": "^1.0.4",
"hardhat-preprocessor": "^0.1.5",
"hardhat-tracer": "^1.0.0-alpha.6"
}
}
84 changes: 53 additions & 31 deletions src/TokenVesting.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ contract TokenVesting is Owned, ReentrancyGuard {
getWithdrawableAmount() >= _amount,
"TokenVesting: cannot create vesting schedule because not sufficient tokens"
);
require(_start >= getCurrentTime(), "TokenVesting: Invalid start time for schedule vesting");
require(_duration > 0, "TokenVesting: duration must be > 0");
require(_amount > 0, "TokenVesting: amount must be > 0");
require(
Expand All @@ -116,10 +117,11 @@ contract TokenVesting is Owned, ReentrancyGuard {
0,
false
);
vestingSchedulesTotalAmount = vestingSchedulesTotalAmount + _amount;
vestingSchedulesTotalAmount += _amount;
vestingSchedulesIds.push(vestingScheduleId);
uint256 currentVestingCount = holdersVestingCount[_beneficiary];
holdersVestingCount[_beneficiary] = currentVestingCount + 1;
unchecked {
holdersVestingCount[_beneficiary]++;
}
}

/**
Expand Down Expand Up @@ -167,31 +169,31 @@ contract TokenVesting is Owned, ReentrancyGuard {
* @param amount the amount to release
*/
function release(
bytes32 vestingScheduleId,
uint256 amount
) public nonReentrant onlyIfVestingScheduleNotRevoked(vestingScheduleId) {
VestingSchedule storage vestingSchedule = vestingSchedules[
vestingScheduleId
];
bool isBeneficiary = msg.sender == vestingSchedule.beneficiary;
bytes32 vestingScheduleId,
uint256 amount
) public nonReentrant onlyIfVestingScheduleNotRevoked(vestingScheduleId) {
VestingSchedule storage vestingSchedule = vestingSchedules[vestingScheduleId];
address beneficiary = vestingSchedule.beneficiary;

require(
msg.sender == beneficiary || msg.sender == owner,
"TokenVesting: only beneficiary and owner can release vested tokens"
);

uint256 vestedAmount = _computeReleasableAmount(vestingSchedule);
require(
vestedAmount >= amount,
"TokenVesting: cannot release tokens, not enough vested tokens"
);

bool isReleasor = (msg.sender == owner);
require(
isBeneficiary || isReleasor,
"TokenVesting: only beneficiary and owner can release vested tokens"
);
uint256 vestedAmount = _computeReleasableAmount(vestingSchedule);
require(
vestedAmount >= amount,
"TokenVesting: cannot release tokens, not enough vested tokens"
);
vestingSchedule.released = vestingSchedule.released + amount;
address payable beneficiaryPayable = payable(
vestingSchedule.beneficiary
);
vestingSchedulesTotalAmount = vestingSchedulesTotalAmount - amount;
SafeTransferLib.safeTransfer(_token, beneficiaryPayable, amount);
unchecked {
vestingSchedule.released += amount;
vestingSchedulesTotalAmount -= amount;
}

SafeTransferLib.safeTransfer(_token, payable(beneficiary), amount);
}


/**
* @dev Returns the number of vesting schedules associated to a beneficiary.
Expand Down Expand Up @@ -337,21 +339,41 @@ contract TokenVesting is Owned, ReentrancyGuard {
) internal view returns (uint256) {
// Retrieve the current time.
uint256 currentTime = getCurrentTime();
// If the current time is before the cliff, no tokens are releasable.
if ((currentTime < vestingSchedule.cliff) || vestingSchedule.revoked) {
// If the current time is before or equals to the cliff, no tokens are releasable.
if ((currentTime <= vestingSchedule.cliff) || vestingSchedule.revoked) {

/* (block.timestamp)
------------------------------------------------------------------------------------
| | | |
Start-Time **Curent Timestamp** Cliff Vesting Duration end
(vesting started) (vesting ended)
*/
return 0;
}
// If the current time is after the vesting period, all tokens are releasable,
// minus the amount already released.
else if (
currentTime >= vestingSchedule.cliff + vestingSchedule.duration
else if (
/* (block.timestamp)
------------------------------------------------------------------------------------
| | | |
Start-Time Cliff Vesting Duration end **Curent Timestamp**
(vesting started) (vesting ended)
*/
currentTime > vestingSchedule.start + vestingSchedule.duration
) {
return vestingSchedule.amountTotal - vestingSchedule.released;
}
// Otherwise, some tokens are releasable.
else {

/* (block.timestamp)
------------------------------------------------------------------------------------
| | | |
Start-Time Cliff **Curent Timestamp** Vesting Duration end
(vesting started) (vesting ended)
*/
// Compute the number of full vesting periods that have elapsed.
uint256 timeFromStart = currentTime - vestingSchedule.cliff;
uint256 timeFromStart = currentTime - vestingSchedule.start;
uint256 secondsPerSlice = vestingSchedule.slicePeriodSeconds;
uint256 vestedSlicePeriods = timeFromStart / secondsPerSlice;
uint256 vestedSeconds = vestedSlicePeriods * secondsPerSlice;
Expand Down
Loading