VestingWalletCliff.test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 { 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 cliffDuration = time.duration.years(1);
  12. const cliff = start + cliffDuration;
  13. const [sender, beneficiary] = await ethers.getSigners();
  14. const mock = await ethers.deployContract('$VestingWalletCliff', [beneficiary, start, duration, cliffDuration]);
  15. const token = await ethers.deployContract('$ERC20', ['Name', 'Symbol']);
  16. await token.$_mint(mock, amount);
  17. await sender.sendTransaction({ to: mock, value: amount });
  18. const pausableToken = await ethers.deployContract('$ERC20Pausable', ['Name', 'Symbol']);
  19. const beneficiaryMock = await ethers.deployContract('EtherReceiverMock');
  20. const env = {
  21. eth: {
  22. checkRelease: async (tx, amount) => {
  23. await expect(tx).to.emit(mock, 'EtherReleased').withArgs(amount);
  24. await expect(tx).to.changeEtherBalances([mock, beneficiary], [-amount, amount]);
  25. },
  26. setupFailure: async () => {
  27. await beneficiaryMock.setAcceptEther(false);
  28. await mock.connect(beneficiary).transferOwnership(beneficiaryMock);
  29. return { args: [], error: [mock, 'FailedInnerCall'] };
  30. },
  31. releasedEvent: 'EtherReleased',
  32. argsVerify: [],
  33. args: [],
  34. },
  35. token: {
  36. checkRelease: async (tx, amount) => {
  37. await expect(tx).to.emit(token, 'Transfer').withArgs(mock, beneficiary, amount);
  38. await expect(tx).to.changeTokenBalances(token, [mock, beneficiary], [-amount, amount]);
  39. },
  40. setupFailure: async () => {
  41. await pausableToken.$_pause();
  42. return {
  43. args: [ethers.Typed.address(pausableToken)],
  44. error: [pausableToken, 'EnforcedPause'],
  45. };
  46. },
  47. releasedEvent: 'ERC20Released',
  48. argsVerify: [token],
  49. args: [ethers.Typed.address(token)],
  50. },
  51. };
  52. const schedule = Array(64)
  53. .fill()
  54. .map((_, i) => (BigInt(i) * duration) / 60n + start);
  55. const vestingFn = timestamp => min(amount, timestamp < cliff ? 0n : (amount * (timestamp - start)) / duration);
  56. return { mock, duration, start, beneficiary, cliff, schedule, vestingFn, env };
  57. }
  58. describe('VestingWalletCliff', function () {
  59. beforeEach(async function () {
  60. Object.assign(this, await loadFixture(fixture));
  61. });
  62. it('rejects a larger cliff than vesting duration', async function () {
  63. await expect(
  64. ethers.deployContract('$VestingWalletCliff', [this.beneficiary, this.start, this.duration, this.duration + 1n]),
  65. )
  66. .revertedWithCustomError(this.mock, 'InvalidCliffDuration')
  67. .withArgs(this.duration + 1n, this.duration);
  68. });
  69. it('check vesting contract', async function () {
  70. expect(await this.mock.owner()).to.equal(this.beneficiary);
  71. expect(await this.mock.start()).to.equal(this.start);
  72. expect(await this.mock.duration()).to.equal(this.duration);
  73. expect(await this.mock.end()).to.equal(this.start + this.duration);
  74. expect(await this.mock.cliff()).to.equal(this.cliff);
  75. });
  76. describe('vesting schedule', function () {
  77. describe('Eth vesting', function () {
  78. beforeEach(async function () {
  79. Object.assign(this, this.env.eth);
  80. });
  81. shouldBehaveLikeVesting();
  82. });
  83. describe('ERC20 vesting', function () {
  84. beforeEach(async function () {
  85. Object.assign(this, this.env.token);
  86. });
  87. shouldBehaveLikeVesting();
  88. });
  89. });
  90. });