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

Add a VestingWallet #2748

Merged
merged 26 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* `Governor`: shift vote start and end by one block to better match Compound's GovernorBravo and prevent voting at the Governor level if the voting snapshot is not ready. ([#2892](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/#2892))
* `PaymentSplitter`: now supports ERC20 assets in addition to Ether. ([#2858](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/#2858))
* `ECDSA`: add a variant of `toEthSignedMessageHash` for arbitrary length message hashing. ([#2865](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/#2865))
* `MerkleProof`: add a `processProof` function that returns the rebuilt root hash given a leaf and a proof. ([#2841](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2841))
* `VestingWallet`: new contract that handles the vesting of Ether and ERC20 tokens following a customizable vesting schedule. ([#2748](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/#2748))

## 4.3.2 (2021-09-14)
Expand Down
14 changes: 8 additions & 6 deletions contracts/finance/VestingWallet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ contract VestingWallet is Context {
* Emits a {TokensReleased} event.
*/
function release() public virtual {
uint256 releasable = vestedAmount(block.timestamp) - released();
uint256 releasable = vestedAmount(uint64(block.timestamp)) - released();
_released += releasable;
emit EtherReleased(releasable);
Address.sendValue(payable(beneficiary()), releasable);
Expand All @@ -98,7 +98,7 @@ contract VestingWallet is Context {
* Emits a {TokensReleased} event.
*/
function release(address token) public virtual {
uint256 releasable = vestedAmount(token, block.timestamp) - released(token);
uint256 releasable = vestedAmount(token, uint64(block.timestamp)) - released(token);
_erc20Released[token] += releasable;
emit ERC20Released(token, releasable);
SafeERC20.safeTransfer(IERC20(token), beneficiary(), releasable);
Expand All @@ -107,26 +107,28 @@ contract VestingWallet is Context {
/**
* @dev Calculates the amount of ether that has already vested. Default implementation is a linear vesting curve.
*/
function vestedAmount(uint256 timestamp) public view virtual returns (uint256) {
function vestedAmount(uint64 timestamp) public view virtual returns (uint256) {
return _vestingSchedule(address(this).balance + released(), timestamp);
}

/**
* @dev Calculates the amount of tokens that has already vested. Default implementation is a linear vesting curve.
*/
function vestedAmount(address token, uint256 timestamp) public view virtual returns (uint256) {
function vestedAmount(address token, uint64 timestamp) public view virtual returns (uint256) {
return _vestingSchedule(IERC20(token).balanceOf(address(this)) + released(token), timestamp);
}

/**
* @dev Virtual implementation of the vesting formula. This returns the amout vested, as a function of time, for
* an asset given its total historical allocation.
*/
function _vestingSchedule(uint256 totalAllocation, uint256 timestamp) internal view virtual returns (uint256) {
function _vestingSchedule(uint256 totalAllocation, uint64 timestamp) internal view virtual returns (uint256) {
if (timestamp < start()) {
return 0;
} else if (timestamp > start() + duration()) {
return totalAllocation;
} else {
return Math.min(totalAllocation, (totalAllocation * (timestamp - start())) / duration());
return (totalAllocation * (timestamp - start())) / duration();
}
}
}
32 changes: 17 additions & 15 deletions test/finance/VestingWallet.behavior.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
const { expectEvent } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');

function releasedEvent (token, amount) {
return token
? [ 'ERC20Released', { token: token.address, amount } ]
: [ 'EtherReleased', { amount } ];
}

function shouldBehaveLikeVesting (beneficiary) {
it('check vesting schedule', async function () {
const args = this.token ? [ this.token.address ] : [];
const [ method, ...args ] = this.token
? [ 'vestedAmount(address,uint64)', this.token.address ]
: [ 'vestedAmount(uint64)' ];

for (const timestamp of this.schedule) {
expect(await this.mock.vestedAmount(...args, timestamp))
expect(await this.mock.methods[method](...args, timestamp))
frangio marked this conversation as resolved.
Show resolved Hide resolved
.to.be.bignumber.equal(this.vestingFn(timestamp));
}
});

it('execute vesting schedule', async function () {
const args = this.token ? [ this.token.address ] : [];
const [ method, ...args ] = this.token
? [ 'release(address)', this.token.address ]
: [ 'release()' ];

let released = web3.utils.toBN(0);
const before = await this.getBalance(beneficiary);

{
const receipt = await this.mock.release(...args);
const receipt = await this.mock.methods[method](...args);

await expectEvent.inTransaction(
receipt.tx,
this.mock,
this.token ? 'ERC20Released' : 'EtherReleased',
Object.fromEntries(Object.entries({
token: this.token && this.token.address,
amount: '0',
}).filter(x => x.every(Boolean))),
...releasedEvent(this.token, '0'),
);

await this.checkRelease(receipt, beneficiary, '0');
Expand All @@ -43,16 +49,12 @@ function shouldBehaveLikeVesting (beneficiary) {
params: [ timestamp.toNumber() ],
}, resolve));

const receipt = await this.mock.release(...args);
const receipt = await this.mock.methods[method](...args);

await expectEvent.inTransaction(
receipt.tx,
this.mock,
this.token ? 'ERC20Released' : 'EtherReleased',
Object.fromEntries(Object.entries({
token: this.token && this.token.address,
amount: vested.sub(released),
}).filter(x => x.every(Boolean))),
...releasedEvent(this.token, vested.sub(released)),
);

await this.checkRelease(receipt, beneficiary, vested.sub(released));
Expand Down