VestingWallet.behavior.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const time = require('../helpers/time');
  4. async function envSetup(mock, beneficiary, token) {
  5. return {
  6. eth: {
  7. checkRelease: async (tx, amount) => {
  8. await expect(tx).to.changeEtherBalances([mock, beneficiary], [-amount, amount]);
  9. },
  10. setupFailure: async () => {
  11. const beneficiaryMock = await ethers.deployContract('EtherReceiverMock');
  12. await beneficiaryMock.setAcceptEther(false);
  13. await mock.connect(beneficiary).transferOwnership(beneficiaryMock);
  14. return { args: [], error: [mock, 'FailedCall'] };
  15. },
  16. releasedEvent: 'EtherReleased',
  17. args: [],
  18. },
  19. token: {
  20. checkRelease: async (tx, amount) => {
  21. await expect(tx).to.emit(token, 'Transfer').withArgs(mock, beneficiary, amount);
  22. await expect(tx).to.changeTokenBalances(token, [mock, beneficiary], [-amount, amount]);
  23. },
  24. setupFailure: async () => {
  25. const pausableToken = await ethers.deployContract('$ERC20Pausable', ['Name', 'Symbol']);
  26. await pausableToken.$_pause();
  27. return {
  28. args: [ethers.Typed.address(pausableToken)],
  29. error: [pausableToken, 'EnforcedPause'],
  30. };
  31. },
  32. releasedEvent: 'ERC20Released',
  33. args: [ethers.Typed.address(token)],
  34. },
  35. };
  36. }
  37. function shouldBehaveLikeVesting() {
  38. it('check vesting schedule', async function () {
  39. for (const timestamp of this.schedule) {
  40. await time.increaseTo.timestamp(timestamp);
  41. const vesting = this.vestingFn(timestamp);
  42. expect(await this.mock.vestedAmount(...this.args, timestamp)).to.equal(vesting);
  43. expect(await this.mock.releasable(...this.args)).to.equal(vesting);
  44. }
  45. });
  46. it('execute vesting schedule', async function () {
  47. let released = 0n;
  48. {
  49. const tx = await this.mock.release(...this.args);
  50. await expect(tx)
  51. .to.emit(this.mock, this.releasedEvent)
  52. .withArgs(...this.args, 0);
  53. await this.checkRelease(tx, 0n);
  54. }
  55. for (const timestamp of this.schedule) {
  56. await time.increaseTo.timestamp(timestamp, false);
  57. const vested = this.vestingFn(timestamp);
  58. const tx = await this.mock.release(...this.args);
  59. await expect(tx).to.emit(this.mock, this.releasedEvent);
  60. await this.checkRelease(tx, vested - released);
  61. released = vested;
  62. }
  63. });
  64. it('should revert on transaction failure', async function () {
  65. const { args, error } = await this.setupFailure();
  66. for (const timestamp of this.schedule) {
  67. await time.increaseTo.timestamp(timestamp);
  68. await expect(this.mock.release(...args)).to.be.revertedWithCustomError(...error);
  69. }
  70. });
  71. }
  72. module.exports = {
  73. envSetup,
  74. shouldBehaveLikeVesting,
  75. };