VestingWallet.behavior.js 2.0 KB

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