SampleCrowdsale.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 EVMThrow from './helpers/EVMThrow'
  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 SampleCrowdsale = artifacts.require('SampleCrowdsale');
  12. const SampleCrowdsaleToken = artifacts.require('SampleCrowdsaleToken');
  13. contract('Crowdsale', 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. (await this.crowdsale.startTime()).should.be.bignumber.equal(this.startTime);
  32. (await this.crowdsale.endTime()).should.be.bignumber.equal(this.endTime);
  33. (await this.crowdsale.rate()).should.be.bignumber.equal(RATE);
  34. (await this.crowdsale.wallet()).should.be.equal(wallet);
  35. (await this.crowdsale.goal()).should.be.bignumber.equal(GOAL);
  36. (await this.crowdsale.cap()).should.be.bignumber.equal(CAP);
  37. });
  38. it('should not accept payments before start', async function () {
  39. await this.crowdsale.send(ether(1)).should.be.rejectedWith(EVMThrow);
  40. await this.crowdsale.buyTokens(investor, {from: investor, value: ether(1)}).should.be.rejectedWith(EVMThrow);
  41. });
  42. it('should accept payments during the sale', async function () {
  43. const investmentAmount = ether(1);
  44. const expectedTokenAmount = RATE.mul(investmentAmount);
  45. await increaseTimeTo(this.startTime);
  46. await this.crowdsale.buyTokens(investor, {value: investmentAmount, from: investor}).should.be.fulfilled;
  47. (await this.token.balanceOf(investor)).should.be.bignumber.equal(expectedTokenAmount);
  48. (await this.token.totalSupply()).should.be.bignumber.equal(expectedTokenAmount);
  49. });
  50. it('should reject payments after end', async function () {
  51. await increaseTimeTo(this.afterEnd);
  52. await this.crowdsale.send(ether(1)).should.be.rejectedWith(EVMThrow);
  53. await this.crowdsale.buyTokens(investor, {value: ether(1), from: investor}).should.be.rejectedWith(EVMThrow);
  54. });
  55. it('should reject payments over cap', async function () {
  56. await increaseTimeTo(this.startTime);
  57. await this.crowdsale.send(CAP);
  58. await this.crowdsale.send(1).should.be.rejectedWith(EVMThrow);
  59. });
  60. it('should allow finalization and transfer funds to wallet if the goal is reached', async function () {
  61. await increaseTimeTo(this.startTime);
  62. await this.crowdsale.send(GOAL);
  63. const beforeFinalization = web3.eth.getBalance(wallet);
  64. await increaseTimeTo(this.afterEndTime);
  65. await this.crowdsale.finalize({from: owner});
  66. const afterFinalization = web3.eth.getBalance(wallet);
  67. afterFinalization.minus(beforeFinalization).should.be.bignumber.equal(GOAL);
  68. });
  69. it('should allow refunds if the goal is not reached', async function () {
  70. const balanceBeforeInvestment = web3.eth.getBalance(investor);
  71. await increaseTimeTo(this.startTime);
  72. await this.crowdsale.sendTransaction({value: ether(1), from: investor, gasPrice: 0});
  73. await increaseTimeTo(this.afterEndTime);
  74. await this.crowdsale.finalize({from: owner});
  75. await this.crowdsale.claimRefund({from: investor, gasPrice: 0}).should.be.fulfilled;
  76. const balanceAfterRefund = web3.eth.getBalance(investor);
  77. balanceBeforeInvestment.should.be.bignumber.equal(balanceAfterRefund);
  78. });
  79. });