SampleCrowdsale.test.js 4.6 KB

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