SampleCrowdsale.test.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. const RefundVault = artifacts.require('RefundVault');
  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 = 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.vault = await RefundVault.new(wallet, { from: owner });
  29. this.crowdsale = await SampleCrowdsale.new(
  30. this.openingTime, this.closingTime, RATE, wallet, CAP, this.token.address, GOAL
  31. );
  32. await this.token.transferOwnership(this.crowdsale.address);
  33. await this.vault.transferOwnership(this.crowdsale.address);
  34. });
  35. it('should create crowdsale with correct parameters', async function () {
  36. this.crowdsale.should.exist;
  37. this.token.should.exist;
  38. const openingTime = await this.crowdsale.openingTime();
  39. const closingTime = await this.crowdsale.closingTime();
  40. const rate = await this.crowdsale.rate();
  41. const walletAddress = await this.crowdsale.wallet();
  42. const goal = await this.crowdsale.goal();
  43. const cap = await this.crowdsale.cap();
  44. openingTime.should.be.bignumber.equal(this.openingTime);
  45. closingTime.should.be.bignumber.equal(this.closingTime);
  46. rate.should.be.bignumber.equal(RATE);
  47. walletAddress.should.be.equal(wallet);
  48. goal.should.be.bignumber.equal(GOAL);
  49. cap.should.be.bignumber.equal(CAP);
  50. });
  51. it('should not accept payments before start', async function () {
  52. await this.crowdsale.send(ether(1)).should.be.rejectedWith(EVMRevert);
  53. await this.crowdsale.buyTokens(investor, { from: investor, value: ether(1) }).should.be.rejectedWith(EVMRevert);
  54. });
  55. it('should accept payments during the sale', async function () {
  56. const investmentAmount = ether(1);
  57. const expectedTokenAmount = RATE.mul(investmentAmount);
  58. await increaseTimeTo(this.openingTime);
  59. await this.crowdsale.buyTokens(investor, { value: investmentAmount, from: investor }).should.be.fulfilled;
  60. (await this.token.balanceOf(investor)).should.be.bignumber.equal(expectedTokenAmount);
  61. (await this.token.totalSupply()).should.be.bignumber.equal(expectedTokenAmount);
  62. });
  63. it('should reject payments after end', async function () {
  64. await increaseTimeTo(this.afterClosingTime);
  65. await this.crowdsale.send(ether(1)).should.be.rejectedWith(EVMRevert);
  66. await this.crowdsale.buyTokens(investor, { value: ether(1), from: investor }).should.be.rejectedWith(EVMRevert);
  67. });
  68. it('should reject payments over cap', async function () {
  69. await increaseTimeTo(this.openingTime);
  70. await this.crowdsale.send(CAP);
  71. await this.crowdsale.send(1).should.be.rejectedWith(EVMRevert);
  72. });
  73. it('should allow finalization and transfer funds to wallet if the goal is reached', async function () {
  74. await increaseTimeTo(this.openingTime);
  75. await this.crowdsale.send(GOAL);
  76. const beforeFinalization = web3.eth.getBalance(wallet);
  77. await increaseTimeTo(this.afterClosingTime);
  78. await this.crowdsale.finalize({ from: owner });
  79. const afterFinalization = web3.eth.getBalance(wallet);
  80. afterFinalization.minus(beforeFinalization).should.be.bignumber.equal(GOAL);
  81. });
  82. it('should allow refunds if the goal is not reached', async function () {
  83. const balanceBeforeInvestment = web3.eth.getBalance(investor);
  84. await increaseTimeTo(this.openingTime);
  85. await this.crowdsale.sendTransaction({ value: ether(1), from: investor, gasPrice: 0 });
  86. await increaseTimeTo(this.afterClosingTime);
  87. await this.crowdsale.finalize({ from: owner });
  88. await this.crowdsale.claimRefund({ from: investor, gasPrice: 0 }).should.be.fulfilled;
  89. const balanceAfterRefund = web3.eth.getBalance(investor);
  90. balanceBeforeInvestment.should.be.bignumber.equal(balanceAfterRefund);
  91. });
  92. describe('when goal > cap', function () {
  93. // goal > cap
  94. const HIGH_GOAL = ether(30);
  95. it('creation reverts', async function () {
  96. await assertRevert(SampleCrowdsale.new(
  97. this.openingTime, this.closingTime, RATE, wallet, CAP, this.token.address, HIGH_GOAL
  98. ));
  99. });
  100. });
  101. });