forked from OpenZeppelin/openzeppelin-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate finance tests to ethers.js (OpenZeppelin#4723)
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com> Co-authored-by: ernestognw <ernestognw@gmail.com>
- Loading branch information
1 parent
248be2f
commit f1f427d
Showing
2 changed files
with
59 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,79 @@ | ||
const { constants, expectEvent, time } = require('@openzeppelin/test-helpers'); | ||
const { web3 } = require('@openzeppelin/test-helpers/src/setup'); | ||
const { ethers } = require('hardhat'); | ||
const { expect } = require('chai'); | ||
const { BNmin } = require('../helpers/math'); | ||
const { expectRevertCustomError } = require('../helpers/customError'); | ||
|
||
const VestingWallet = artifacts.require('VestingWallet'); | ||
const ERC20 = artifacts.require('$ERC20'); | ||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers'); | ||
const { bigint: time } = require('../helpers/time'); | ||
const { min } = require('../helpers/math'); | ||
|
||
const { shouldBehaveLikeVesting } = require('./VestingWallet.behavior'); | ||
|
||
contract('VestingWallet', function (accounts) { | ||
const [sender, beneficiary] = accounts; | ||
async function fixture() { | ||
const amount = ethers.parseEther('100'); | ||
const duration = time.duration.years(4); | ||
const start = (await time.clock.timestamp()) + time.duration.hours(1); | ||
|
||
const amount = web3.utils.toBN(web3.utils.toWei('100')); | ||
const duration = web3.utils.toBN(4 * 365 * 86400); // 4 years | ||
const [sender, beneficiary] = await ethers.getSigners(); | ||
const mock = await ethers.deployContract('VestingWallet', [beneficiary, start, duration]); | ||
return { mock, amount, duration, start, sender, beneficiary }; | ||
} | ||
|
||
describe('VestingWallet', function () { | ||
beforeEach(async function () { | ||
this.start = (await time.latest()).addn(3600); // in 1 hour | ||
this.mock = await VestingWallet.new(beneficiary, this.start, duration); | ||
Object.assign(this, await loadFixture(fixture)); | ||
}); | ||
|
||
it('rejects zero address for beneficiary', async function () { | ||
await expectRevertCustomError( | ||
VestingWallet.new(constants.ZERO_ADDRESS, this.start, duration), | ||
'OwnableInvalidOwner', | ||
[constants.ZERO_ADDRESS], | ||
); | ||
await expect(ethers.deployContract('VestingWallet', [ethers.ZeroAddress, this.start, this.duration])) | ||
.revertedWithCustomError(this.mock, 'OwnableInvalidOwner') | ||
.withArgs(ethers.ZeroAddress); | ||
}); | ||
|
||
it('check vesting contract', async function () { | ||
expect(await this.mock.owner()).to.be.equal(beneficiary); | ||
expect(await this.mock.start()).to.be.bignumber.equal(this.start); | ||
expect(await this.mock.duration()).to.be.bignumber.equal(duration); | ||
expect(await this.mock.end()).to.be.bignumber.equal(this.start.add(duration)); | ||
expect(await this.mock.owner()).to.be.equal(this.beneficiary.address); | ||
expect(await this.mock.start()).to.be.equal(this.start); | ||
expect(await this.mock.duration()).to.be.equal(this.duration); | ||
expect(await this.mock.end()).to.be.equal(this.start + this.duration); | ||
}); | ||
|
||
describe('vesting schedule', function () { | ||
beforeEach(async function () { | ||
beforeEach(function () { | ||
this.schedule = Array(64) | ||
.fill() | ||
.map((_, i) => web3.utils.toBN(i).mul(duration).divn(60).add(this.start)); | ||
this.vestingFn = timestamp => BNmin(amount, amount.mul(timestamp.sub(this.start)).div(duration)); | ||
.map((_, i) => (BigInt(i) * this.duration) / 60n + this.start); | ||
this.vestingFn = timestamp => min(this.amount, (this.amount * (timestamp - this.start)) / this.duration); | ||
}); | ||
|
||
describe('Eth vesting', function () { | ||
beforeEach(async function () { | ||
await web3.eth.sendTransaction({ from: sender, to: this.mock.address, value: amount }); | ||
this.getBalance = account => web3.eth.getBalance(account).then(web3.utils.toBN); | ||
this.checkRelease = () => {}; | ||
await this.sender.sendTransaction({ to: this.mock, value: this.amount }); | ||
|
||
this.getBalance = signer => ethers.provider.getBalance(signer); | ||
this.checkRelease = (tx, amount) => expect(tx).to.changeEtherBalances([this.beneficiary], [amount]); | ||
|
||
this.releasedEvent = 'EtherReleased'; | ||
this.args = []; | ||
this.argsVerify = []; | ||
}); | ||
|
||
shouldBehaveLikeVesting(beneficiary); | ||
shouldBehaveLikeVesting(); | ||
}); | ||
|
||
describe('ERC20 vesting', function () { | ||
beforeEach(async function () { | ||
this.token = await ERC20.new('Name', 'Symbol'); | ||
this.token = await ethers.deployContract('$ERC20', ['Name', 'Symbol']); | ||
await this.token.$_mint(this.mock, this.amount); | ||
|
||
this.getBalance = account => this.token.balanceOf(account); | ||
this.checkRelease = (receipt, to, value) => | ||
expectEvent.inTransaction(receipt.tx, this.token, 'Transfer', { from: this.mock.address, to, value }); | ||
this.checkRelease = async (tx, amount) => { | ||
await expect(tx).to.emit(this.token, 'Transfer').withArgs(this.mock.target, this.beneficiary.address, amount); | ||
await expect(tx).to.changeTokenBalances(this.token, [this.mock, this.beneficiary], [-amount, amount]); | ||
}; | ||
|
||
await this.token.$_mint(this.mock.address, amount); | ||
this.releasedEvent = 'ERC20Released'; | ||
this.args = [ethers.Typed.address(this.token.target)]; | ||
this.argsVerify = [this.token.target]; | ||
}); | ||
|
||
shouldBehaveLikeVesting(beneficiary); | ||
shouldBehaveLikeVesting(); | ||
}); | ||
}); | ||
}); |