Crowdsale.test.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. const should = require('chai')
  8. .use(require('chai-as-promised'))
  9. .use(require('chai-bignumber')(BigNumber))
  10. .should();
  11. const Crowdsale = artifacts.require('Crowdsale');
  12. const MintableToken = artifacts.require('MintableToken');
  13. contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
  14. const rate = new BigNumber(1000);
  15. const value = ether(42);
  16. const expectedTokenAmount = rate.mul(value);
  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.token = await MintableToken.new();
  26. this.crowdsale = await Crowdsale.new(this.startTime, this.endTime, rate, wallet, this.token.address);
  27. await this.token.transferOwnership(this.crowdsale.address);
  28. });
  29. it('should be token owner', async function () {
  30. const owner = await this.token.owner();
  31. owner.should.equal(this.crowdsale.address);
  32. });
  33. it('should be ended only after end', async function () {
  34. let ended = await this.crowdsale.hasEnded();
  35. ended.should.equal(false);
  36. await increaseTimeTo(this.afterEndTime);
  37. ended = await this.crowdsale.hasEnded();
  38. ended.should.equal(true);
  39. });
  40. describe('accepting payments', function () {
  41. it('should reject payments before start', async function () {
  42. await this.crowdsale.send(value).should.be.rejectedWith(EVMRevert);
  43. await this.crowdsale.buyTokens(investor, { from: purchaser, value: value }).should.be.rejectedWith(EVMRevert);
  44. });
  45. it('should accept payments after start', async function () {
  46. await increaseTimeTo(this.startTime);
  47. await this.crowdsale.send(value).should.be.fulfilled;
  48. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser }).should.be.fulfilled;
  49. });
  50. it('should reject payments after end', async function () {
  51. await increaseTimeTo(this.afterEndTime);
  52. await this.crowdsale.send(value).should.be.rejectedWith(EVMRevert);
  53. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser }).should.be.rejectedWith(EVMRevert);
  54. });
  55. });
  56. describe('high-level purchase', function () {
  57. beforeEach(async function () {
  58. await increaseTimeTo(this.startTime);
  59. });
  60. it('should log purchase', async function () {
  61. const { logs } = await this.crowdsale.sendTransaction({ value: value, from: investor });
  62. const event = logs.find(e => e.event === 'TokenPurchase');
  63. should.exist(event);
  64. event.args.purchaser.should.equal(investor);
  65. event.args.beneficiary.should.equal(investor);
  66. event.args.value.should.be.bignumber.equal(value);
  67. event.args.amount.should.be.bignumber.equal(expectedTokenAmount);
  68. });
  69. it('should increase totalSupply', async function () {
  70. await this.crowdsale.send(value);
  71. const totalSupply = await this.token.totalSupply();
  72. totalSupply.should.be.bignumber.equal(expectedTokenAmount);
  73. });
  74. it('should assign tokens to sender', async function () {
  75. await this.crowdsale.sendTransaction({ value: value, from: investor });
  76. let balance = await this.token.balanceOf(investor);
  77. balance.should.be.bignumber.equal(expectedTokenAmount);
  78. });
  79. it('should forward funds to wallet', async function () {
  80. const pre = web3.eth.getBalance(wallet);
  81. await this.crowdsale.sendTransaction({ value, from: investor });
  82. const post = web3.eth.getBalance(wallet);
  83. post.minus(pre).should.be.bignumber.equal(value);
  84. });
  85. });
  86. describe('low-level purchase', function () {
  87. beforeEach(async function () {
  88. await increaseTimeTo(this.startTime);
  89. });
  90. it('should log purchase', async function () {
  91. const { logs } = await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  92. const event = logs.find(e => e.event === 'TokenPurchase');
  93. should.exist(event);
  94. event.args.purchaser.should.equal(purchaser);
  95. event.args.beneficiary.should.equal(investor);
  96. event.args.value.should.be.bignumber.equal(value);
  97. event.args.amount.should.be.bignumber.equal(expectedTokenAmount);
  98. });
  99. it('should increase totalSupply', async function () {
  100. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  101. const totalSupply = await this.token.totalSupply();
  102. totalSupply.should.be.bignumber.equal(expectedTokenAmount);
  103. });
  104. it('should assign tokens to beneficiary', async function () {
  105. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  106. const balance = await this.token.balanceOf(investor);
  107. balance.should.be.bignumber.equal(expectedTokenAmount);
  108. });
  109. it('should forward funds to wallet', async function () {
  110. const pre = web3.eth.getBalance(wallet);
  111. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  112. const post = web3.eth.getBalance(wallet);
  113. post.minus(pre).should.be.bignumber.equal(value);
  114. });
  115. });
  116. });