ERC721MintBurn.behavior.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. const { BN, constants, expectEvent, shouldFail } = require('openzeppelin-test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. function shouldBehaveLikeMintAndBurnERC721 (
  4. creator,
  5. minter,
  6. [owner, newOwner, approved, anyone]
  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 shouldFail.reverting(this.token.mint(ZERO_ADDRESS, thirdTokenId, { from: minter }));
  42. });
  43. });
  44. describe('when the given token ID was already tracked by this contract', function () {
  45. it('reverts', async function () {
  46. await shouldFail.reverting(this.token.mint(owner, firstTokenId, { from: minter }));
  47. });
  48. });
  49. });
  50. describe('mintWithTokenURI', function () {
  51. it('can mint with a tokenUri', async function () {
  52. await this.token.mintWithTokenURI(newOwner, thirdTokenId, MOCK_URI, {
  53. from: minter,
  54. });
  55. });
  56. });
  57. describe('burn', function () {
  58. const tokenId = firstTokenId;
  59. let logs = null;
  60. describe('when successful', function () {
  61. beforeEach(async function () {
  62. const result = await this.token.burn(tokenId, { from: owner });
  63. logs = result.logs;
  64. });
  65. it('burns the given token ID and adjusts the balance of the owner', async function () {
  66. await shouldFail.reverting(this.token.ownerOf(tokenId));
  67. (await this.token.balanceOf(owner)).should.be.bignumber.equal('1');
  68. });
  69. it('emits a burn event', async function () {
  70. expectEvent.inLogs(logs, 'Transfer', {
  71. from: owner,
  72. to: ZERO_ADDRESS,
  73. tokenId: tokenId,
  74. });
  75. });
  76. });
  77. describe('when there is a previous approval burned', function () {
  78. beforeEach(async function () {
  79. await this.token.approve(approved, tokenId, { from: owner });
  80. const result = await this.token.burn(tokenId, { from: owner });
  81. logs = result.logs;
  82. });
  83. context('getApproved', function () {
  84. it('reverts', async function () {
  85. await shouldFail.reverting(this.token.getApproved(tokenId));
  86. });
  87. });
  88. });
  89. describe('when the given token ID was not tracked by this contract', function () {
  90. it('reverts', async function () {
  91. await shouldFail.reverting(
  92. this.token.burn(unknownTokenId, { from: creator })
  93. );
  94. });
  95. });
  96. });
  97. });
  98. }
  99. module.exports = {
  100. shouldBehaveLikeMintAndBurnERC721,
  101. };