ERC721MintBurn.behavior.js 4.1 KB

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