SampleCrowdsale.test.js 4.4 KB

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