SampleCrowdsale.test.js 4.3 KB

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