ERC20Mintable.behavior.js 4.7 KB

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