SampleCrowdsale.test.js 4.3 KB

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