SampleCrowdsale.test.js 4.3 KB

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