ERC721MintBurn.behavior.js 3.8 KB

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