SampleCrowdsale.test.js 4.3 KB

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