SampleCrowdsale.test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. const { BN, balance, ether, shouldFail, 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 shouldFail.reverting(this.crowdsale.send(ether('1')));
  34. await shouldFail.reverting(this.crowdsale.buyTokens(investor, { from: investor, value: ether('1') }));
  35. });
  36. it('should accept payments during the sale', async function () {
  37. const investmentAmount = ether('1');
  38. const expectedTokenAmount = RATE.mul(investmentAmount);
  39. await time.increaseTo(this.openingTime);
  40. await this.crowdsale.buyTokens(investor, { value: investmentAmount, from: investor });
  41. (await this.token.balanceOf(investor)).should.be.bignumber.equal(expectedTokenAmount);
  42. (await this.token.totalSupply()).should.be.bignumber.equal(expectedTokenAmount);
  43. });
  44. it('should reject payments after end', async function () {
  45. await time.increaseTo(this.afterClosingTime);
  46. await shouldFail.reverting(this.crowdsale.send(ether('1')));
  47. await shouldFail.reverting(this.crowdsale.buyTokens(investor, { value: ether('1'), from: investor }));
  48. });
  49. it('should reject payments over cap', async function () {
  50. await time.increaseTo(this.openingTime);
  51. await this.crowdsale.send(CAP);
  52. await shouldFail.reverting(this.crowdsale.send(1));
  53. });
  54. it('should allow finalization and transfer funds to wallet if the goal is reached', async function () {
  55. await time.increaseTo(this.openingTime);
  56. await this.crowdsale.send(GOAL);
  57. const balanceTracker = await balance.tracker(wallet);
  58. await time.increaseTo(this.afterClosingTime);
  59. await this.crowdsale.finalize({ from: owner });
  60. (await balanceTracker.delta()).should.be.bignumber.equal(GOAL);
  61. });
  62. it('should allow refunds if the goal is not reached', async function () {
  63. const balanceTracker = await balance.tracker(investor);
  64. await time.increaseTo(this.openingTime);
  65. await this.crowdsale.sendTransaction({ value: ether('1'), from: investor, gasPrice: 0 });
  66. await time.increaseTo(this.afterClosingTime);
  67. await this.crowdsale.finalize({ from: owner });
  68. await this.crowdsale.claimRefund(investor, { gasPrice: 0 });
  69. (await balanceTracker.delta()).should.be.bignumber.equal('0');
  70. });
  71. describe('when goal > cap', function () {
  72. // goal > cap
  73. const HIGH_GOAL = ether('30');
  74. it('creation reverts', async function () {
  75. await shouldFail.reverting(SampleCrowdsale.new(
  76. this.openingTime, this.closingTime, RATE, wallet, CAP, this.token.address, HIGH_GOAL
  77. ));
  78. });
  79. });
  80. });