ERC721MintBurn.behavior.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. const { BN, constants, expectEvent, expectRevert } = require('openzeppelin-test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. const { expect } = require('chai');
  4. function shouldBehaveLikeMintAndBurnERC721 (
  5. creator,
  6. minter,
  7. [owner, newOwner, approved]
  8. ) {
  9. const firstTokenId = new BN(1);
  10. const secondTokenId = new BN(2);
  11. const thirdTokenId = new BN(3);
  12. const unknownTokenId = new BN(4);
  13. const MOCK_URI = 'https://example.com';
  14. const data = '0x42';
  15. describe('like a mintable and burnable ERC721', function () {
  16. beforeEach(async function () {
  17. await this.token.mint(owner, firstTokenId, { from: minter });
  18. await this.token.mint(owner, secondTokenId, { from: minter });
  19. });
  20. describe('mint', function () {
  21. let logs = null;
  22. describe('when successful', function () {
  23. beforeEach(async function () {
  24. const result = await this.token.mint(newOwner, thirdTokenId, { from: minter });
  25. logs = result.logs;
  26. });
  27. it('assigns the token to the new owner', async function () {
  28. expect(await this.token.ownerOf(thirdTokenId)).to.equal(newOwner);
  29. });
  30. it('increases the balance of its owner', async function () {
  31. expect(await this.token.balanceOf(newOwner)).to.be.bignumber.equal('1');
  32. });
  33. it('emits a transfer and minted event', async function () {
  34. expectEvent.inLogs(logs, 'Transfer', {
  35. from: ZERO_ADDRESS,
  36. to: newOwner,
  37. tokenId: thirdTokenId,
  38. });
  39. });
  40. });
  41. describe('when the given owner address is the zero address', function () {
  42. it('reverts', async function () {
  43. await expectRevert(
  44. this.token.mint(ZERO_ADDRESS, thirdTokenId, { from: minter }),
  45. 'ERC721: mint to the zero address'
  46. );
  47. });
  48. });
  49. describe('when the given token ID was already tracked by this contract', function () {
  50. it('reverts', async function () {
  51. await expectRevert(this.token.mint(owner, firstTokenId, { from: minter }),
  52. 'ERC721: token already minted.'
  53. );
  54. });
  55. });
  56. });
  57. describe('mintWithTokenURI', function () {
  58. it('can mint with a tokenUri', async function () {
  59. await this.token.mintWithTokenURI(newOwner, thirdTokenId, MOCK_URI, {
  60. from: minter,
  61. });
  62. });
  63. });
  64. describe('safeMint', function () {
  65. it('it can safely mint with data', async function () {
  66. await this.token.methods['safeMint(address,uint256,bytes)'](...[newOwner, thirdTokenId, data],
  67. { from: minter });
  68. });
  69. it('it can safely mint without data', async function () {
  70. await this.token.methods['safeMint(address,uint256)'](...[newOwner, thirdTokenId],
  71. { from: minter });
  72. });
  73. });
  74. describe('burn', function () {
  75. const tokenId = firstTokenId;
  76. let logs = null;
  77. describe('when successful', function () {
  78. beforeEach(async function () {
  79. const result = await this.token.burn(tokenId, { from: owner });
  80. logs = result.logs;
  81. });
  82. it('burns the given token ID and adjusts the balance of the owner', async function () {
  83. await expectRevert(
  84. this.token.ownerOf(tokenId),
  85. 'ERC721: owner query for nonexistent token'
  86. );
  87. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('1');
  88. });
  89. it('emits a burn event', async function () {
  90. expectEvent.inLogs(logs, 'Transfer', {
  91. from: owner,
  92. to: ZERO_ADDRESS,
  93. tokenId: tokenId,
  94. });
  95. });
  96. });
  97. describe('when there is a previous approval burned', function () {
  98. beforeEach(async function () {
  99. await this.token.approve(approved, tokenId, { from: owner });
  100. const result = await this.token.burn(tokenId, { from: owner });
  101. logs = result.logs;
  102. });
  103. context('getApproved', function () {
  104. it('reverts', async function () {
  105. await expectRevert(
  106. this.token.getApproved(tokenId), 'ERC721: approved query for nonexistent token'
  107. );
  108. });
  109. });
  110. });
  111. describe('when the given token ID was not tracked by this contract', function () {
  112. it('reverts', async function () {
  113. await expectRevert(
  114. this.token.burn(unknownTokenId, { from: creator }), 'ERC721: operator query for nonexistent token'
  115. );
  116. });
  117. });
  118. });
  119. });
  120. }
  121. module.exports = {
  122. shouldBehaveLikeMintAndBurnERC721,
  123. };