SampleCrowdsale.test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. const { BN, balance, ether, expectRevert, time } = require('openzeppelin-test-helpers');
  2. const { expect } = require('chai');
  3. const SampleCrowdsale = artifacts.require('SampleCrowdsale');
  4. const SampleCrowdsaleToken = artifacts.require('SampleCrowdsaleToken');
  5. contract('SampleCrowdsale', function ([_, deployer, owner, wallet, investor]) {
  6. const RATE = new BN(10);
  7. const GOAL = ether('10');
  8. const CAP = ether('20');
  9. before(async function () {
  10. // Advance to the next block to correctly read time in the solidity "now" function interpreted by ganache
  11. await time.advanceBlock();
  12. });
  13. beforeEach(async function () {
  14. this.openingTime = (await time.latest()).add(time.duration.weeks(1));
  15. this.closingTime = this.openingTime.add(time.duration.weeks(1));
  16. this.afterClosingTime = this.closingTime.add(time.duration.seconds(1));
  17. this.token = await SampleCrowdsaleToken.new({ from: deployer });
  18. this.crowdsale = await SampleCrowdsale.new(
  19. this.openingTime, this.closingTime, RATE, wallet, CAP, this.token.address, GOAL,
  20. { from: owner }
  21. );
  22. await this.token.addMinter(this.crowdsale.address, { from: deployer });
  23. await this.token.renounceMinter({ from: deployer });
  24. });
  25. it('should create crowdsale with correct parameters', async function () {
  26. expect(await this.crowdsale.openingTime()).to.be.bignumber.equal(this.openingTime);
  27. expect(await this.crowdsale.closingTime()).to.be.bignumber.equal(this.closingTime);
  28. expect(await this.crowdsale.rate()).to.be.bignumber.equal(RATE);
  29. expect(await this.crowdsale.wallet()).to.equal(wallet);
  30. expect(await this.crowdsale.goal()).to.be.bignumber.equal(GOAL);
  31. expect(await this.crowdsale.cap()).to.be.bignumber.equal(CAP);
  32. });
  33. it('should not accept payments before start', async function () {
  34. await expectRevert(this.crowdsale.send(ether('1')), 'TimedCrowdsale: not open');
  35. await expectRevert(this.crowdsale.buyTokens(investor, { from: investor, value: ether('1') }),
  36. 'TimedCrowdsale: not open'
  37. );
  38. });
  39. it('should accept payments during the sale', async function () {
  40. const investmentAmount = ether('1');
  41. const expectedTokenAmount = RATE.mul(investmentAmount);
  42. await time.increaseTo(this.openingTime);
  43. await this.crowdsale.buyTokens(investor, { value: investmentAmount, from: investor });
  44. expect(await this.token.balanceOf(investor)).to.be.bignumber.equal(expectedTokenAmount);
  45. expect(await this.token.totalSupply()).to.be.bignumber.equal(expectedTokenAmount);
  46. });
  47. it('should reject payments after end', async function () {
  48. await time.increaseTo(this.afterClosingTime);
  49. await expectRevert(this.crowdsale.send(ether('1')), 'TimedCrowdsale: not open');
  50. await expectRevert(this.crowdsale.buyTokens(investor, { value: ether('1'), from: investor }),
  51. 'TimedCrowdsale: not open'
  52. );
  53. });
  54. it('should reject payments over cap', async function () {
  55. await time.increaseTo(this.openingTime);
  56. await this.crowdsale.send(CAP);
  57. await expectRevert(this.crowdsale.send(1), 'CappedCrowdsale: cap exceeded');
  58. });
  59. it('should allow finalization and transfer funds to wallet if the goal is reached', async function () {
  60. await time.increaseTo(this.openingTime);
  61. await this.crowdsale.send(GOAL);
  62. const balanceTracker = await balance.tracker(wallet);
  63. await time.increaseTo(this.afterClosingTime);
  64. await this.crowdsale.finalize({ from: owner });
  65. expect(await balanceTracker.delta()).to.be.bignumber.equal(GOAL);
  66. });
  67. it('should allow refunds if the goal is not reached', async function () {
  68. const balanceTracker = await balance.tracker(investor);
  69. await time.increaseTo(this.openingTime);
  70. await this.crowdsale.sendTransaction({ value: ether('1'), from: investor, gasPrice: 0 });
  71. await time.increaseTo(this.afterClosingTime);
  72. await this.crowdsale.finalize({ from: owner });
  73. await this.crowdsale.claimRefund(investor, { gasPrice: 0 });
  74. expect(await balanceTracker.delta()).to.be.bignumber.equal('0');
  75. });
  76. describe('when goal > cap', function () {
  77. // goal > cap
  78. const HIGH_GOAL = ether('30');
  79. it('creation reverts', async function () {
  80. await expectRevert(SampleCrowdsale.new(
  81. this.openingTime, this.closingTime, RATE, wallet, CAP, this.token.address, HIGH_GOAL
  82. ), 'SampleCrowdSale: goal is greater than cap');
  83. });
  84. });
  85. });