SampleCrowdsale.test.js 4.8 KB

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