SampleCrowdsale.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import ether from './helpers/ether'
  2. import advanceToBlock from './helpers/advanceToBlock'
  3. import EVMThrow from './helpers/EVMThrow'
  4. const BigNumber = web3.BigNumber;
  5. const should = require('chai')
  6. .use(require('chai-as-promised'))
  7. .use(require('chai-bignumber')(BigNumber))
  8. .should();
  9. const SampleCrowdsale = artifacts.require('SampleCrowdsale');
  10. const SampleCrowdsaleToken = artifacts.require('SampleCrowdsaleToken');
  11. contract('Crowdsale', function ([owner, wallet, investor]) {
  12. const RATE = new BigNumber(10);
  13. const GOAL = ether(10);
  14. const CAP = ether(20);
  15. beforeEach(async function () {
  16. this.startBlock = web3.eth.blockNumber + 10;
  17. this.endBlock = web3.eth.blockNumber + 20;
  18. this.crowdsale = await SampleCrowdsale.new(this.startBlock, this.endBlock, RATE, GOAL, CAP, wallet);
  19. this.token = SampleCrowdsaleToken.at(await this.crowdsale.token());
  20. });
  21. it('should create crowdsale with correct parameters', async function () {
  22. this.crowdsale.should.exist;
  23. this.token.should.exist;
  24. (await this.crowdsale.startBlock()).should.be.bignumber.equal(this.startBlock);
  25. (await this.crowdsale.endBlock()).should.be.bignumber.equal(this.endBlock);
  26. (await this.crowdsale.rate()).should.be.bignumber.equal(RATE);
  27. (await this.crowdsale.wallet()).should.be.bignumber.equal(wallet);
  28. (await this.crowdsale.goal()).should.be.bignumber.equal(GOAL);
  29. (await this.crowdsale.cap()).should.be.bignumber.equal(CAP);
  30. });
  31. it('should not accept payments before start', async function () {
  32. await this.crowdsale.send(ether(1)).should.be.rejectedWith(EVMThrow);
  33. await this.crowdsale.buyTokens(investor, {from: investor, value: ether(1)}).should.be.rejectedWith(EVMThrow);
  34. });
  35. it('should accept payments during the sale', async function () {
  36. const investmentAmount = ether(1);
  37. const expectedTokenAmount = RATE.mul(investmentAmount);
  38. await advanceToBlock(this.startBlock - 1);
  39. await this.crowdsale.buyTokens(investor, {value: investmentAmount, from: investor}).should.be.fulfilled;
  40. (await this.token.balanceOf(investor)).should.be.bignumber.equal(expectedTokenAmount);
  41. (await this.token.totalSupply()).should.be.bignumber.equal(expectedTokenAmount);
  42. });
  43. it('should reject payments after end', async function () {
  44. await advanceToBlock(this.endBlock);
  45. await this.crowdsale.send(ether(1)).should.be.rejectedWith(EVMThrow);
  46. await this.crowdsale.buyTokens(investor, {value: ether(1), from: investor}).should.be.rejectedWith(EVMThrow);
  47. });
  48. it('should reject payments over cap', async function () {
  49. await advanceToBlock(this.startBlock - 1);
  50. await this.crowdsale.send(CAP);
  51. await this.crowdsale.send(1).should.be.rejectedWith(EVMThrow);
  52. });
  53. it('should allow finalization and transfer funds to wallet if the goal is reached', async function () {
  54. await advanceToBlock(this.endBlock - 1);
  55. await this.crowdsale.send(GOAL);
  56. const beforeFinalization = web3.eth.getBalance(wallet);
  57. await this.crowdsale.finalize({from: owner});
  58. const afterFinalization = web3.eth.getBalance(wallet);
  59. afterFinalization.minus(beforeFinalization).should.be.bignumber.equal(GOAL);
  60. });
  61. it('should allow refunds if the goal is not reached', async function () {
  62. const balanceBeforeInvestment = web3.eth.getBalance(investor);
  63. await advanceToBlock(this.startBlock - 1);
  64. await this.crowdsale.sendTransaction({value: ether(1), from: investor, gasPrice: 0});
  65. await advanceToBlock(this.endBlock);
  66. await this.crowdsale.finalize({from: owner});
  67. await this.crowdsale.claimRefund({from: investor, gasPrice: 0}).should.be.fulfilled;
  68. const balanceAfterRefund = web3.eth.getBalance(investor);
  69. balanceBeforeInvestment.should.be.bignumber.equal(balanceAfterRefund);
  70. });
  71. });