ERC721MintBurn.behavior.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. const { assertRevert } = require('../../helpers/assertRevert');
  2. const expectEvent = require('../../helpers/expectEvent');
  3. const { ZERO_ADDRESS } = require('../../helpers/constants');
  4. const BigNumber = web3.BigNumber;
  5. require('chai')
  6. .use(require('chai-bignumber')(BigNumber))
  7. .should();
  8. function shouldBehaveLikeMintAndBurnERC721 (
  9. creator,
  10. minter,
  11. [owner, newOwner, approved, anyone]
  12. ) {
  13. const firstTokenId = 1;
  14. const secondTokenId = 2;
  15. const thirdTokenId = 3;
  16. const unknownTokenId = 4;
  17. const MOCK_URI = 'https://example.com';
  18. describe('like a mintable and burnable ERC721', function () {
  19. beforeEach(async function () {
  20. await this.token.mint(owner, firstTokenId, { from: minter });
  21. await this.token.mint(owner, secondTokenId, { from: minter });
  22. });
  23. describe('mint', function () {
  24. let logs = null;
  25. describe('when successful', function () {
  26. beforeEach(async function () {
  27. const result = await this.token.mint(newOwner, thirdTokenId, { from: minter });
  28. logs = result.logs;
  29. });
  30. it('assigns the token to the new owner', async function () {
  31. (await this.token.ownerOf(thirdTokenId)).should.be.equal(newOwner);
  32. });
  33. it('increases the balance of its owner', async function () {
  34. (await this.token.balanceOf(newOwner)).should.be.bignumber.equal(1);
  35. });
  36. it('emits a transfer and minted event', async function () {
  37. expectEvent.inLogs(logs, 'Transfer', {
  38. from: ZERO_ADDRESS,
  39. to: newOwner,
  40. tokenId: thirdTokenId,
  41. });
  42. });
  43. });
  44. describe('when the given owner address is the zero address', function () {
  45. it('reverts', async function () {
  46. await assertRevert(this.token.mint(ZERO_ADDRESS, thirdTokenId, { from: minter }));
  47. });
  48. });
  49. describe('when the given token ID was already tracked by this contract', function () {
  50. it('reverts', async function () {
  51. await assertRevert(this.token.mint(owner, firstTokenId, { from: minter }));
  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 assertRevert(this.token.ownerOf(tokenId));
  72. (await this.token.balanceOf(owner)).should.be.bignumber.equal(1);
  73. });
  74. it('emits a burn event', async function () {
  75. expectEvent.inLogs(logs, 'Transfer', {
  76. from: owner,
  77. to: ZERO_ADDRESS,
  78. tokenId: tokenId,
  79. });
  80. });
  81. });
  82. describe('when there is a previous approval burned', function () {
  83. beforeEach(async function () {
  84. await this.token.approve(approved, tokenId, { from: owner });
  85. const result = await this.token.burn(tokenId, { from: owner });
  86. logs = result.logs;
  87. });
  88. context('getApproved', function () {
  89. it('reverts', async function () {
  90. await assertRevert(this.token.getApproved(tokenId));
  91. });
  92. });
  93. });
  94. describe('when the given token ID was not tracked by this contract', function () {
  95. it('reverts', async function () {
  96. await assertRevert(
  97. this.token.burn(unknownTokenId, { from: creator })
  98. );
  99. });
  100. });
  101. });
  102. });
  103. }
  104. module.exports = {
  105. shouldBehaveLikeMintAndBurnERC721,
  106. };