VestingWallet.test.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { min } = require('../helpers/math');
  5. const time = require('../helpers/time');
  6. const { envSetup, shouldBehaveLikeVesting } = require('./VestingWallet.behavior');
  7. async function fixture() {
  8. const amount = ethers.parseEther('100');
  9. const duration = time.duration.years(4);
  10. const start = (await time.clock.timestamp()) + time.duration.hours(1);
  11. const [sender, beneficiary] = await ethers.getSigners();
  12. const mock = await ethers.deployContract('VestingWallet', [beneficiary, start, duration]);
  13. const token = await ethers.deployContract('$ERC20', ['Name', 'Symbol']);
  14. await token.$_mint(mock, amount);
  15. await sender.sendTransaction({ to: mock, value: amount });
  16. const env = await envSetup(mock, beneficiary, token);
  17. const schedule = Array.from({ length: 64 }, (_, i) => (BigInt(i) * duration) / 60n + start);
  18. const vestingFn = timestamp => min(amount, (amount * (timestamp - start)) / duration);
  19. return { mock, duration, start, beneficiary, schedule, vestingFn, env };
  20. }
  21. describe('VestingWallet', function () {
  22. beforeEach(async function () {
  23. Object.assign(this, await loadFixture(fixture));
  24. });
  25. it('rejects zero address for beneficiary', async function () {
  26. await expect(ethers.deployContract('VestingWallet', [ethers.ZeroAddress, this.start, this.duration]))
  27. .revertedWithCustomError(this.mock, 'OwnableInvalidOwner')
  28. .withArgs(ethers.ZeroAddress);
  29. });
  30. it('check vesting contract', async function () {
  31. expect(await this.mock.owner()).to.equal(this.beneficiary);
  32. expect(await this.mock.start()).to.equal(this.start);
  33. expect(await this.mock.duration()).to.equal(this.duration);
  34. expect(await this.mock.end()).to.equal(this.start + this.duration);
  35. });
  36. describe('vesting schedule', function () {
  37. describe('Eth vesting', function () {
  38. beforeEach(async function () {
  39. Object.assign(this, this.env.eth);
  40. });
  41. shouldBehaveLikeVesting();
  42. });
  43. describe('ERC20 vesting', function () {
  44. beforeEach(async function () {
  45. Object.assign(this, this.env.token);
  46. });
  47. shouldBehaveLikeVesting();
  48. });
  49. });
  50. });