VestingWallet.test.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 VestingWallet = artifacts.require('VestingWallet');
  6. const ERC20 = artifacts.require('$ERC20');
  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. expect(await this.mock.end()).to.be.bignumber.equal(this.start.add(duration));
  27. });
  28. describe('vesting schedule', function () {
  29. beforeEach(async function () {
  30. this.schedule = Array(64)
  31. .fill()
  32. .map((_, i) => web3.utils.toBN(i).mul(duration).divn(60).add(this.start));
  33. this.vestingFn = timestamp => BNmin(amount, amount.mul(timestamp.sub(this.start)).div(duration));
  34. });
  35. describe('Eth vesting', function () {
  36. beforeEach(async function () {
  37. await web3.eth.sendTransaction({ from: sender, to: this.mock.address, value: amount });
  38. this.getBalance = account => web3.eth.getBalance(account).then(web3.utils.toBN);
  39. this.checkRelease = () => {};
  40. });
  41. shouldBehaveLikeVesting(beneficiary);
  42. });
  43. describe('ERC20 vesting', function () {
  44. beforeEach(async function () {
  45. this.token = await ERC20.new('Name', 'Symbol');
  46. this.getBalance = account => this.token.balanceOf(account);
  47. this.checkRelease = (receipt, to, value) =>
  48. expectEvent.inTransaction(receipt.tx, this.token, 'Transfer', { from: this.mock.address, to, value });
  49. await this.token.$_mint(this.mock.address, amount);
  50. });
  51. shouldBehaveLikeVesting(beneficiary);
  52. });
  53. });
  54. });