VestingWallet.test.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. const { constants, expectEvent, time } = require('@openzeppelin/test-helpers');
  2. const { web3 } = require('@openzeppelin/test-helpers/src/setup');
  3. const { expect } = require('chai');
  4. const { BNmin } = require('../helpers/math');
  5. const { expectRevertCustomError } = require('../helpers/customError');
  6. const VestingWallet = artifacts.require('VestingWallet');
  7. const ERC20 = artifacts.require('$ERC20');
  8. const { shouldBehaveLikeVesting } = require('./VestingWallet.behavior');
  9. contract('VestingWallet', function (accounts) {
  10. const [sender, beneficiary] = accounts;
  11. const amount = web3.utils.toBN(web3.utils.toWei('100'));
  12. const duration = web3.utils.toBN(4 * 365 * 86400); // 4 years
  13. beforeEach(async function () {
  14. this.start = (await time.latest()).addn(3600); // in 1 hour
  15. this.mock = await VestingWallet.new(beneficiary, this.start, duration);
  16. });
  17. it('rejects zero address for beneficiary', async function () {
  18. await expectRevertCustomError(
  19. VestingWallet.new(constants.ZERO_ADDRESS, this.start, duration),
  20. 'VestingWalletInvalidBeneficiary',
  21. [constants.ZERO_ADDRESS],
  22. );
  23. });
  24. it('check vesting contract', async function () {
  25. expect(await this.mock.beneficiary()).to.be.equal(beneficiary);
  26. expect(await this.mock.start()).to.be.bignumber.equal(this.start);
  27. expect(await this.mock.duration()).to.be.bignumber.equal(duration);
  28. expect(await this.mock.end()).to.be.bignumber.equal(this.start.add(duration));
  29. });
  30. describe('vesting schedule', function () {
  31. beforeEach(async function () {
  32. this.schedule = Array(64)
  33. .fill()
  34. .map((_, i) => web3.utils.toBN(i).mul(duration).divn(60).add(this.start));
  35. this.vestingFn = timestamp => BNmin(amount, amount.mul(timestamp.sub(this.start)).div(duration));
  36. });
  37. describe('Eth vesting', function () {
  38. beforeEach(async function () {
  39. await web3.eth.sendTransaction({ from: sender, to: this.mock.address, value: amount });
  40. this.getBalance = account => web3.eth.getBalance(account).then(web3.utils.toBN);
  41. this.checkRelease = () => {};
  42. });
  43. shouldBehaveLikeVesting(beneficiary);
  44. });
  45. describe('ERC20 vesting', function () {
  46. beforeEach(async function () {
  47. this.token = await ERC20.new('Name', 'Symbol');
  48. this.getBalance = account => this.token.balanceOf(account);
  49. this.checkRelease = (receipt, to, value) =>
  50. expectEvent.inTransaction(receipt.tx, this.token, 'Transfer', { from: this.mock.address, to, value });
  51. await this.token.$_mint(this.mock.address, amount);
  52. });
  53. shouldBehaveLikeVesting(beneficiary);
  54. });
  55. });
  56. });