ERC721MintBurn.behavior.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. const { assertRevert } = require('../../helpers/assertRevert');
  2. const expectEvent = require('../../helpers/expectEvent');
  3. const BigNumber = web3.BigNumber;
  4. require('chai')
  5. .use(require('chai-bignumber')(BigNumber))
  6. .should();
  7. function shouldBehaveLikeMintAndBurnERC721 (
  8. creator,
  9. minter,
  10. [owner, newOwner, approved, anyone]
  11. ) {
  12. const firstTokenId = 1;
  13. const secondTokenId = 2;
  14. const thirdTokenId = 3;
  15. const unknownTokenId = 4;
  16. const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
  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. await expectEvent.inLogs(logs, 'Transfer', {
  38. from: ZERO_ADDRESS,
  39. to: newOwner,
  40. });
  41. logs[0].args.tokenId.should.be.bignumber.equal(thirdTokenId);
  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));
  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));
  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. logs.length.should.be.equal(1);
  76. logs[0].event.should.be.equal('Transfer');
  77. logs[0].args.from.should.be.equal(owner);
  78. logs[0].args.to.should.be.equal(ZERO_ADDRESS);
  79. logs[0].args.tokenId.should.be.bignumber.equal(tokenId);
  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. };