MintableToken.behavior.js 5.1 KB

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