ERC721MintBurn.behavior.js 5.0 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 shouldBehaveLikeMintAndBurnERC721 (
  8. creator,
  9. minter,
  10. [owner, newOwner, approved, anyone]
  11. ) {
  12. const firstTokenId = 1;
  13. const secondTokenId = 2;
  14. const thirdTokenId = 3;
  15. const unknownTokenId = 4;
  16. const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
  17. const MOCK_URI = 'https://example.com';
  18. describe('like a mintable and burnable ERC721', function () {
  19. beforeEach(async function () {
  20. await this.token.mint(owner, firstTokenId, { from: minter });
  21. await this.token.mint(owner, secondTokenId, { from: minter });
  22. });
  23. describe('mint', function () {
  24. let logs = null;
  25. describe('when successful', function () {
  26. beforeEach(async function () {
  27. const result = await this.token.mint(newOwner, thirdTokenId, { from: minter });
  28. logs = result.logs;
  29. });
  30. it('assigns the token to the new owner', async function () {
  31. (await this.token.ownerOf(thirdTokenId)).should.be.equal(newOwner);
  32. });
  33. it('increases the balance of its owner', async function () {
  34. (await this.token.balanceOf(newOwner)).should.be.bignumber.equal(1);
  35. });
  36. it('emits a transfer and minted event', async function () {
  37. await expectEvent.inLogs(logs, 'Transfer', {
  38. from: ZERO_ADDRESS,
  39. to: newOwner,
  40. });
  41. logs[0].args.tokenId.should.be.bignumber.equal(thirdTokenId);
  42. await expectEvent.inLogs(logs, 'Minted', {
  43. to: newOwner,
  44. });
  45. logs[1].args.tokenId.should.be.bignumber.equal(thirdTokenId);
  46. });
  47. });
  48. describe('when the given owner address is the zero address', function () {
  49. it('reverts', async function () {
  50. await assertRevert(this.token.mint(ZERO_ADDRESS, thirdTokenId));
  51. });
  52. });
  53. describe('when the given token ID was already tracked by this contract', function () {
  54. it('reverts', async function () {
  55. await assertRevert(this.token.mint(owner, firstTokenId));
  56. });
  57. });
  58. });
  59. describe('mintWithTokenURI', function () {
  60. it('can mint with a tokenUri', async function () {
  61. await this.token.mintWithTokenURI(newOwner, thirdTokenId, MOCK_URI, {
  62. from: minter,
  63. });
  64. });
  65. });
  66. describe('burn', function () {
  67. const tokenId = firstTokenId;
  68. let logs = null;
  69. describe('when successful', function () {
  70. beforeEach(async function () {
  71. const result = await this.token.burn(tokenId, { from: owner });
  72. logs = result.logs;
  73. });
  74. it('burns the given token ID and adjusts the balance of the owner', async function () {
  75. await assertRevert(this.token.ownerOf(tokenId));
  76. (await this.token.balanceOf(owner)).should.be.bignumber.equal(1);
  77. });
  78. it('emits a burn event', async function () {
  79. logs.length.should.be.equal(1);
  80. logs[0].event.should.be.equal('Transfer');
  81. logs[0].args.from.should.be.equal(owner);
  82. logs[0].args.to.should.be.equal(ZERO_ADDRESS);
  83. logs[0].args.tokenId.should.be.bignumber.equal(tokenId);
  84. });
  85. });
  86. describe('when there is a previous approval burned', function () {
  87. beforeEach(async function () {
  88. await this.token.approve(approved, tokenId, { from: owner });
  89. const result = await this.token.burn(tokenId, { from: owner });
  90. logs = result.logs;
  91. });
  92. context('getApproved', function () {
  93. it('reverts', async function () {
  94. await assertRevert(this.token.getApproved(tokenId));
  95. });
  96. });
  97. });
  98. describe('when the given token ID was not tracked by this contract', function () {
  99. it('reverts', async function () {
  100. await assertRevert(
  101. this.token.burn(unknownTokenId, { from: creator })
  102. );
  103. });
  104. });
  105. });
  106. describe('finishMinting', function () {
  107. it('allows the minter to finish minting', async function () {
  108. const { logs } = await this.token.finishMinting({ from: minter });
  109. expectEvent.inLogs(logs, 'MintingFinished');
  110. });
  111. });
  112. context('mintingFinished', function () {
  113. beforeEach(async function () {
  114. await this.token.finishMinting({ from: minter });
  115. });
  116. describe('mint', function () {
  117. it('reverts', async function () {
  118. await assertRevert(
  119. this.token.mint(owner, thirdTokenId, { from: minter })
  120. );
  121. });
  122. });
  123. describe('mintWithTokenURI', function () {
  124. it('reverts', async function () {
  125. await assertRevert(
  126. this.token.mintWithTokenURI(owner, thirdTokenId, MOCK_URI, { from: minter })
  127. );
  128. });
  129. });
  130. });
  131. });
  132. }
  133. module.exports = {
  134. shouldBehaveLikeMintAndBurnERC721,
  135. };