ERC721MintBurn.behavior.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. describe('like a mintable and burnable ERC721', function () {
  15. beforeEach(async function () {
  16. await this.token.mint(owner, firstTokenId, { from: minter });
  17. await this.token.mint(owner, secondTokenId, { from: minter });
  18. });
  19. describe('mint', function () {
  20. let logs = null;
  21. describe('when successful', function () {
  22. beforeEach(async function () {
  23. const result = await this.token.mint(newOwner, thirdTokenId, { from: minter });
  24. logs = result.logs;
  25. });
  26. it('assigns the token to the new owner', async function () {
  27. expect(await this.token.ownerOf(thirdTokenId)).to.equal(newOwner);
  28. });
  29. it('increases the balance of its owner', async function () {
  30. expect(await this.token.balanceOf(newOwner)).to.be.bignumber.equal('1');
  31. });
  32. it('emits a transfer and minted event', async function () {
  33. expectEvent.inLogs(logs, 'Transfer', {
  34. from: ZERO_ADDRESS,
  35. to: newOwner,
  36. tokenId: thirdTokenId,
  37. });
  38. });
  39. });
  40. describe('when the given owner address is the zero address', function () {
  41. it('reverts', async function () {
  42. await expectRevert(
  43. this.token.mint(ZERO_ADDRESS, thirdTokenId, { from: minter }),
  44. 'ERC721: mint to the zero address'
  45. );
  46. });
  47. });
  48. describe('when the given token ID was already tracked by this contract', function () {
  49. it('reverts', async function () {
  50. await expectRevert(this.token.mint(owner, firstTokenId, { from: minter }),
  51. 'ERC721: token already minted.'
  52. );
  53. });
  54. });
  55. });
  56. describe('mintWithTokenURI', function () {
  57. it('can mint with a tokenUri', async function () {
  58. await this.token.mintWithTokenURI(newOwner, thirdTokenId, MOCK_URI, {
  59. from: minter,
  60. });
  61. });
  62. });
  63. describe('burn', function () {
  64. const tokenId = firstTokenId;
  65. let logs = null;
  66. describe('when successful', function () {
  67. beforeEach(async function () {
  68. const result = await this.token.burn(tokenId, { from: owner });
  69. logs = result.logs;
  70. });
  71. it('burns the given token ID and adjusts the balance of the owner', async function () {
  72. await expectRevert(
  73. this.token.ownerOf(tokenId),
  74. 'ERC721: owner query for nonexistent token'
  75. );
  76. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('1');
  77. });
  78. it('emits a burn event', async function () {
  79. expectEvent.inLogs(logs, 'Transfer', {
  80. from: owner,
  81. to: ZERO_ADDRESS,
  82. tokenId: tokenId,
  83. });
  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 expectRevert(
  95. this.token.getApproved(tokenId), 'ERC721: approved query for nonexistent token'
  96. );
  97. });
  98. });
  99. });
  100. describe('when the given token ID was not tracked by this contract', function () {
  101. it('reverts', async function () {
  102. await expectRevert(
  103. this.token.burn(unknownTokenId, { from: creator }), 'ERC721: operator query for nonexistent token'
  104. );
  105. });
  106. });
  107. });
  108. });
  109. }
  110. module.exports = {
  111. shouldBehaveLikeMintAndBurnERC721,
  112. };