MintableToken.behavior.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. const { assertRevert } = require('../../helpers/assertRevert');
  2. const expectEvent = require('../../helpers/expectEvent');
  3. const BigNumber = web3.BigNumber;
  4. require('chai')
  5. .use(require('chai-bignumber')(BigNumber))
  6. .should();
  7. function shouldBehaveLikeMintableToken (owner, minter, [anyone]) {
  8. const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
  9. describe('as a basic mintable token', function () {
  10. describe('after token creation', function () {
  11. it('sender should be token owner', async function () {
  12. (await this.token.owner({ from: owner })).should.equal(owner);
  13. });
  14. });
  15. describe('minting finished', function () {
  16. describe('when the token minting is not finished', function () {
  17. it('returns false', async function () {
  18. (await this.token.mintingFinished()).should.equal(false);
  19. });
  20. });
  21. describe('when the token is minting finished', function () {
  22. beforeEach(async function () {
  23. await this.token.finishMinting({ from: owner });
  24. });
  25. it('returns true', async function () {
  26. (await this.token.mintingFinished()).should.equal(true);
  27. });
  28. });
  29. });
  30. describe('finish minting', function () {
  31. describe('when the sender is the token owner', function () {
  32. const from = owner;
  33. describe('when the token minting was not finished', function () {
  34. it('finishes token minting', async function () {
  35. await this.token.finishMinting({ from });
  36. (await this.token.mintingFinished()).should.equal(true);
  37. });
  38. it('emits a mint finished event', async function () {
  39. const { logs } = await this.token.finishMinting({ from });
  40. logs.length.should.be.equal(1);
  41. logs[0].event.should.eq('MintFinished');
  42. });
  43. });
  44. describe('when the token minting was already finished', function () {
  45. beforeEach(async function () {
  46. await this.token.finishMinting({ from });
  47. });
  48. it('reverts', async function () {
  49. await assertRevert(this.token.finishMinting({ from }));
  50. });
  51. });
  52. });
  53. describe('when the sender is not the token owner', function () {
  54. const from = anyone;
  55. describe('when the token minting was not finished', function () {
  56. it('reverts', async function () {
  57. await assertRevert(this.token.finishMinting({ from }));
  58. });
  59. });
  60. describe('when the token minting was already finished', function () {
  61. beforeEach(async function () {
  62. await this.token.finishMinting({ from: owner });
  63. });
  64. it('reverts', async function () {
  65. await assertRevert(this.token.finishMinting({ from }));
  66. });
  67. });
  68. });
  69. });
  70. describe('mint', function () {
  71. const amount = 100;
  72. describe('when the sender has the minting permission', function () {
  73. const from = minter;
  74. describe('when the token minting is not finished', function () {
  75. it('mints the requested amount', async function () {
  76. await this.token.mint(owner, amount, { from });
  77. (await this.token.balanceOf(owner)).should.be.bignumber.equal(amount);
  78. });
  79. it('emits a mint and a transfer event', async function () {
  80. const { logs } = await this.token.mint(owner, amount, { from });
  81. const mintEvent = expectEvent.inLogs(logs, 'Mint', {
  82. to: owner,
  83. });
  84. mintEvent.args.amount.should.be.bignumber.equal(amount);
  85. const transferEvent = expectEvent.inLogs(logs, 'Transfer', {
  86. from: ZERO_ADDRESS,
  87. to: owner,
  88. });
  89. transferEvent.args.value.should.be.bignumber.equal(amount);
  90. });
  91. });
  92. describe('when the token minting is finished', function () {
  93. beforeEach(async function () {
  94. await this.token.finishMinting({ from: owner });
  95. });
  96. it('reverts', async function () {
  97. await assertRevert(this.token.mint(owner, amount, { from }));
  98. });
  99. });
  100. });
  101. describe('when the sender has not the minting permission', function () {
  102. const from = anyone;
  103. describe('when the token minting is not finished', function () {
  104. it('reverts', async function () {
  105. await assertRevert(this.token.mint(owner, amount, { from }));
  106. });
  107. });
  108. describe('when the token minting is already finished', function () {
  109. beforeEach(async function () {
  110. await this.token.finishMinting({ from: owner });
  111. });
  112. it('reverts', async function () {
  113. await assertRevert(this.token.mint(owner, amount, { from }));
  114. });
  115. });
  116. });
  117. });
  118. });
  119. }
  120. module.exports = {
  121. shouldBehaveLikeMintableToken,
  122. };