VestingWallet.behavior.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 ? ['ERC20Released', { token: token.address, amount }] : ['EtherReleased', { amount }];
  6. }
  7. function shouldBehaveLikeVesting(beneficiary) {
  8. it('check vesting schedule', async function () {
  9. const [vestedAmount, releasable, ...args] = this.token
  10. ? ['vestedAmount(address,uint64)', 'releasable(address)', this.token.address]
  11. : ['vestedAmount(uint64)', 'releasable()'];
  12. for (const timestamp of this.schedule) {
  13. await time.increaseTo(timestamp);
  14. const vesting = this.vestingFn(timestamp);
  15. expect(await this.mock.methods[vestedAmount](...args, timestamp)).to.be.bignumber.equal(vesting);
  16. expect(await this.mock.methods[releasable](...args)).to.be.bignumber.equal(vesting);
  17. }
  18. });
  19. it('execute vesting schedule', async function () {
  20. const [release, ...args] = this.token ? ['release(address)', this.token.address] : ['release()'];
  21. let released = web3.utils.toBN(0);
  22. const before = await this.getBalance(beneficiary);
  23. {
  24. const receipt = await this.mock.methods[release](...args);
  25. await expectEvent.inTransaction(receipt.tx, this.mock, ...releasedEvent(this.token, '0'));
  26. await this.checkRelease(receipt, beneficiary, '0');
  27. expect(await this.getBalance(beneficiary)).to.be.bignumber.equal(before);
  28. }
  29. for (const timestamp of this.schedule) {
  30. await time.setNextBlockTimestamp(timestamp);
  31. const vested = this.vestingFn(timestamp);
  32. const receipt = await this.mock.methods[release](...args);
  33. await expectEvent.inTransaction(receipt.tx, this.mock, ...releasedEvent(this.token, vested.sub(released)));
  34. await this.checkRelease(receipt, beneficiary, vested.sub(released));
  35. expect(await this.getBalance(beneficiary)).to.be.bignumber.equal(before.add(vested));
  36. released = vested;
  37. }
  38. });
  39. }
  40. module.exports = {
  41. shouldBehaveLikeVesting,
  42. };