ERC721MintBurn.behavior.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. const { assertRevert } = require('../../helpers/assertRevert');
  2. const BigNumber = web3.BigNumber;
  3. require('chai')
  4. .use(require('chai-bignumber')(BigNumber))
  5. .should();
  6. function shouldBehaveLikeMintAndBurnERC721 (accounts) {
  7. const firstTokenId = 1;
  8. const secondTokenId = 2;
  9. const unknownTokenId = 3;
  10. const creator = accounts[0];
  11. const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
  12. describe('like a mintable and burnable ERC721', function () {
  13. beforeEach(async function () {
  14. await this.token.mint(creator, firstTokenId, { from: creator });
  15. await this.token.mint(creator, secondTokenId, { from: creator });
  16. });
  17. describe('mint', function () {
  18. const to = accounts[1];
  19. const tokenId = unknownTokenId;
  20. let logs = null;
  21. describe('when successful', function () {
  22. beforeEach(async function () {
  23. const result = await this.token.mint(to, tokenId);
  24. logs = result.logs;
  25. });
  26. it('assigns the token to the new owner', async function () {
  27. (await this.token.ownerOf(tokenId)).should.be.equal(to);
  28. });
  29. it('increases the balance of its owner', async function () {
  30. (await this.token.balanceOf(to)).should.be.bignumber.equal(1);
  31. });
  32. it('emits a transfer event', async function () {
  33. logs.length.should.be.equal(1);
  34. logs[0].event.should.be.equal('Transfer');
  35. logs[0].args._from.should.be.equal(ZERO_ADDRESS);
  36. logs[0].args._to.should.be.equal(to);
  37. logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
  38. });
  39. });
  40. describe('when the given owner address is the zero address', function () {
  41. it('reverts', async function () {
  42. await assertRevert(this.token.mint(ZERO_ADDRESS, tokenId));
  43. });
  44. });
  45. describe('when the given token ID was already tracked by this contract', function () {
  46. it('reverts', async function () {
  47. await assertRevert(this.token.mint(accounts[1], firstTokenId));
  48. });
  49. });
  50. });
  51. describe('burn', function () {
  52. const tokenId = firstTokenId;
  53. const sender = creator;
  54. let logs = null;
  55. describe('when successful', function () {
  56. beforeEach(async function () {
  57. const result = await this.token.burn(tokenId, { from: sender });
  58. logs = result.logs;
  59. });
  60. it('burns the given token ID and adjusts the balance of the owner', async function () {
  61. await assertRevert(this.token.ownerOf(tokenId));
  62. (await this.token.balanceOf(sender)).should.be.bignumber.equal(1);
  63. });
  64. it('emits a burn event', async function () {
  65. logs.length.should.be.equal(1);
  66. logs[0].event.should.be.equal('Transfer');
  67. logs[0].args._from.should.be.equal(sender);
  68. logs[0].args._to.should.be.equal(ZERO_ADDRESS);
  69. logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
  70. });
  71. });
  72. describe('when there is a previous approval', function () {
  73. beforeEach(async function () {
  74. await this.token.approve(accounts[1], tokenId, { from: sender });
  75. const result = await this.token.burn(tokenId, { from: sender });
  76. logs = result.logs;
  77. });
  78. it('clears the approval', async function () {
  79. (await this.token.getApproved(tokenId)).should.be.equal(ZERO_ADDRESS);
  80. });
  81. });
  82. describe('when the given token ID was not tracked by this contract', function () {
  83. it('reverts', async function () {
  84. await assertRevert(this.token.burn(unknownTokenId, { from: creator }));
  85. });
  86. });
  87. });
  88. });
  89. }
  90. module.exports = {
  91. shouldBehaveLikeMintAndBurnERC721,
  92. };