VestingWallet.behavior.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. const { time } = require('@nomicfoundation/hardhat-network-helpers');
  2. const { expectEvent } = require('@openzeppelin/test-helpers');
  3. const { expect } = require('chai');
  4. function releasedEvent (token, amount) {
  5. return token
  6. ? [ 'ERC20Released', { token: token.address, amount } ]
  7. : [ 'EtherReleased', { amount } ];
  8. }
  9. function shouldBehaveLikeVesting (beneficiary) {
  10. it('check vesting schedule', async function () {
  11. const [ fnVestedAmount, fnReleasable, ...args ] = this.token
  12. ? [ 'vestedAmount(address,uint64)', 'releasable(address)', this.token.address ]
  13. : [ 'vestedAmount(uint64)', 'releasable()' ];
  14. for (const timestamp of this.schedule) {
  15. await time.increaseTo(timestamp);
  16. const vesting = this.vestingFn(timestamp);
  17. expect(await this.mock.methods[fnVestedAmount](...args, timestamp))
  18. .to.be.bignumber.equal(vesting);
  19. expect(await this.mock.methods[fnReleasable](...args))
  20. .to.be.bignumber.equal(vesting);
  21. }
  22. });
  23. it('execute vesting schedule', async function () {
  24. const [ fnRelease, ...args ] = this.token
  25. ? [ 'release(address)', this.token.address ]
  26. : [ 'release()' ];
  27. let released = web3.utils.toBN(0);
  28. const before = await this.getBalance(beneficiary);
  29. {
  30. const receipt = await this.mock.methods[fnRelease](...args);
  31. await expectEvent.inTransaction(
  32. receipt.tx,
  33. this.mock,
  34. ...releasedEvent(this.token, '0'),
  35. );
  36. await this.checkRelease(receipt, beneficiary, '0');
  37. expect(await this.getBalance(beneficiary)).to.be.bignumber.equal(before);
  38. }
  39. for (const timestamp of this.schedule) {
  40. await time.setNextBlockTimestamp(timestamp);
  41. const vested = this.vestingFn(timestamp);
  42. const receipt = await this.mock.methods[fnRelease](...args);
  43. await expectEvent.inTransaction(
  44. receipt.tx,
  45. this.mock,
  46. ...releasedEvent(this.token, vested.sub(released)),
  47. );
  48. await this.checkRelease(receipt, beneficiary, vested.sub(released));
  49. expect(await this.getBalance(beneficiary))
  50. .to.be.bignumber.equal(before.add(vested));
  51. released = vested;
  52. }
  53. });
  54. }
  55. module.exports = {
  56. shouldBehaveLikeVesting,
  57. };