ERC6909.test.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { shouldBehaveLikeERC6909 } = require('./ERC6909.behavior');
  5. async function fixture() {
  6. const [holder, operator, recipient, other] = await ethers.getSigners();
  7. const token = await ethers.deployContract('$ERC6909');
  8. return { token, holder, operator, recipient, other };
  9. }
  10. describe('ERC6909', function () {
  11. beforeEach(async function () {
  12. Object.assign(this, await loadFixture(fixture));
  13. });
  14. shouldBehaveLikeERC6909();
  15. describe('internal functions', function () {
  16. const tokenId = 1990n;
  17. const mintValue = 9001n;
  18. const burnValue = 3000n;
  19. describe('_mint', function () {
  20. it('reverts with a zero destination address', async function () {
  21. await expect(this.token.$_mint(ethers.ZeroAddress, tokenId, mintValue))
  22. .to.be.revertedWithCustomError(this.token, 'ERC6909InvalidReceiver')
  23. .withArgs(ethers.ZeroAddress);
  24. });
  25. describe('with minted tokens', function () {
  26. beforeEach(async function () {
  27. this.tx = await this.token.connect(this.operator).$_mint(this.holder, tokenId, mintValue);
  28. });
  29. it('emits a Transfer event from 0 address', async function () {
  30. await expect(this.tx)
  31. .to.emit(this.token, 'Transfer')
  32. .withArgs(this.operator, ethers.ZeroAddress, this.holder, tokenId, mintValue);
  33. });
  34. it('credits the minted token value', async function () {
  35. await expect(this.token.balanceOf(this.holder, tokenId)).to.eventually.be.equal(mintValue);
  36. });
  37. });
  38. });
  39. describe('_transfer', function () {
  40. it('reverts when transferring from the zero address', async function () {
  41. await expect(this.token.$_transfer(ethers.ZeroAddress, this.holder, 1n, 1n))
  42. .to.be.revertedWithCustomError(this.token, 'ERC6909InvalidSender')
  43. .withArgs(ethers.ZeroAddress);
  44. });
  45. it('reverts when transferring to the zero address', async function () {
  46. await expect(this.token.$_transfer(this.holder, ethers.ZeroAddress, 1n, 1n))
  47. .to.be.revertedWithCustomError(this.token, 'ERC6909InvalidReceiver')
  48. .withArgs(ethers.ZeroAddress);
  49. });
  50. });
  51. describe('_burn', function () {
  52. it('reverts with a zero from address', async function () {
  53. await expect(this.token.$_burn(ethers.ZeroAddress, tokenId, burnValue))
  54. .to.be.revertedWithCustomError(this.token, 'ERC6909InvalidSender')
  55. .withArgs(ethers.ZeroAddress);
  56. });
  57. describe('with burned tokens', function () {
  58. beforeEach(async function () {
  59. await this.token.connect(this.operator).$_mint(this.holder, tokenId, mintValue);
  60. this.tx = await this.token.connect(this.operator).$_burn(this.holder, tokenId, burnValue);
  61. });
  62. it('emits a Transfer event to 0 address', async function () {
  63. await expect(this.tx)
  64. .to.emit(this.token, 'Transfer')
  65. .withArgs(this.operator, this.holder, ethers.ZeroAddress, tokenId, burnValue);
  66. });
  67. it('debits the burned token value', async function () {
  68. await expect(this.token.balanceOf(this.holder, tokenId)).to.eventually.be.equal(mintValue - burnValue);
  69. });
  70. });
  71. });
  72. describe('_approve', function () {
  73. it('reverts when the owner is the zero address', async function () {
  74. await expect(this.token.$_approve(ethers.ZeroAddress, this.recipient, 1n, 1n))
  75. .to.be.revertedWithCustomError(this.token, 'ERC6909InvalidApprover')
  76. .withArgs(ethers.ZeroAddress);
  77. });
  78. });
  79. describe('_setOperator', function () {
  80. it('reverts when the owner is the zero address', async function () {
  81. await expect(this.token.$_setOperator(ethers.ZeroAddress, this.operator, true))
  82. .to.be.revertedWithCustomError(this.token, 'ERC6909InvalidApprover')
  83. .withArgs(ethers.ZeroAddress);
  84. });
  85. });
  86. });
  87. });