SampleCrowdsale.test.js 4.9 KB

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