ERC721MintBurn.behavior.js 3.9 KB

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