ERC721MintBurn.behaviour.js 3.9 KB

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