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 VestingWallet = artifacts.require('VestingWallet');
  5. const ERC20 = artifacts.require('$ERC20');
  6. const { shouldBehaveLikeVesting } = require('./VestingWallet.behavior');
  7. const min = (...args) => args.slice(1).reduce((x, y) => (x.lt(y) ? x : y), args[0]);
  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)
  30. .fill()
  31. .map((_, i) => web3.utils.toBN(i).mul(duration).divn(60).add(this.start));
  32. this.vestingFn = timestamp => min(amount, amount.mul(timestamp.sub(this.start)).div(duration));
  33. });
  34. describe('Eth vesting', function () {
  35. beforeEach(async function () {
  36. await web3.eth.sendTransaction({ from: sender, to: this.mock.address, value: amount });
  37. this.getBalance = account => web3.eth.getBalance(account).then(web3.utils.toBN);
  38. this.checkRelease = () => {};
  39. });
  40. shouldBehaveLikeVesting(beneficiary);
  41. });
  42. describe('ERC20 vesting', function () {
  43. beforeEach(async function () {
  44. this.token = await ERC20.new('Name', 'Symbol');
  45. this.getBalance = account => this.token.balanceOf(account);
  46. this.checkRelease = (receipt, to, value) =>
  47. expectEvent.inTransaction(receipt.tx, this.token, 'Transfer', { from: this.mock.address, to, value });
  48. await this.token.$_mint(this.mock.address, amount);
  49. });
  50. shouldBehaveLikeVesting(beneficiary);
  51. });
  52. });
  53. });