Crowdsale.js 5.2 KB

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