VestingWallet.test.js 3.5 KB

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