VestingWallet.test.js 2.4 KB

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