Crowdsale.js 4.9 KB

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