ERC20Mintable.behavior.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 (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.equal('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. context('for a zero amount', function () {
  76. shouldMint(0);
  77. });
  78. context('for a non-zero amount', function () {
  79. shouldMint(amount);
  80. });
  81. function shouldMint (amount) {
  82. beforeEach(async function () {
  83. ({ logs: this.logs } = await this.token.mint(anyone, amount, { from }));
  84. });
  85. it('mints the requested amount', async function () {
  86. (await this.token.balanceOf(anyone)).should.be.bignumber.equal(amount);
  87. });
  88. it('emits a mint and a transfer event', async function () {
  89. const mintEvent = expectEvent.inLogs(this.logs, 'Mint', {
  90. to: anyone,
  91. });
  92. mintEvent.args.amount.should.be.bignumber.equal(amount);
  93. const transferEvent = expectEvent.inLogs(this.logs, 'Transfer', {
  94. from: ZERO_ADDRESS,
  95. to: anyone,
  96. });
  97. transferEvent.args.value.should.be.bignumber.equal(amount);
  98. });
  99. }
  100. });
  101. describe('when the token minting is finished', function () {
  102. beforeEach(async function () {
  103. await this.token.finishMinting({ from: owner });
  104. });
  105. it('reverts', async function () {
  106. await assertRevert(this.token.mint(anyone, amount, { from }));
  107. });
  108. });
  109. });
  110. describe('when the sender has not the minting permission', function () {
  111. const from = anyone;
  112. describe('when the token minting is not finished', function () {
  113. it('reverts', async function () {
  114. await assertRevert(this.token.mint(anyone, amount, { from }));
  115. });
  116. });
  117. describe('when the token minting is already finished', function () {
  118. beforeEach(async function () {
  119. await this.token.finishMinting({ from: owner });
  120. });
  121. it('reverts', async function () {
  122. await assertRevert(this.token.mint(anyone, amount, { from }));
  123. });
  124. });
  125. });
  126. });
  127. });
  128. }
  129. module.exports = {
  130. shouldBehaveLikeERC20Mintable,
  131. };