ERC20.test.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { PANIC_CODES } = require('@nomicfoundation/hardhat-chai-matchers/panic');
  5. const {
  6. shouldBehaveLikeERC20,
  7. shouldBehaveLikeERC20Transfer,
  8. shouldBehaveLikeERC20Approve,
  9. } = require('./ERC20.behavior');
  10. const TOKENS = [{ Token: '$ERC20' }, { Token: '$ERC20ApprovalMock', forcedApproval: true }];
  11. const name = 'My Token';
  12. const symbol = 'MTKN';
  13. const initialSupply = 100n;
  14. describe('ERC20', function () {
  15. for (const { Token, forcedApproval } of TOKENS) {
  16. describe(Token, function () {
  17. const fixture = async () => {
  18. const [initialHolder, recipient, anotherAccount] = await ethers.getSigners();
  19. const token = await ethers.deployContract(Token, [name, symbol]);
  20. await token.$_mint(initialHolder, initialSupply);
  21. return { initialHolder, recipient, anotherAccount, token };
  22. };
  23. beforeEach(async function () {
  24. Object.assign(this, await loadFixture(fixture));
  25. });
  26. shouldBehaveLikeERC20(initialSupply, { forcedApproval });
  27. it('has a name', async function () {
  28. expect(await this.token.name()).to.equal(name);
  29. });
  30. it('has a symbol', async function () {
  31. expect(await this.token.symbol()).to.equal(symbol);
  32. });
  33. it('has 18 decimals', async function () {
  34. expect(await this.token.decimals()).to.equal(18n);
  35. });
  36. describe('_mint', function () {
  37. const value = 50n;
  38. it('rejects a null account', async function () {
  39. await expect(this.token.$_mint(ethers.ZeroAddress, value))
  40. .to.be.revertedWithCustomError(this.token, 'ERC20InvalidReceiver')
  41. .withArgs(ethers.ZeroAddress);
  42. });
  43. it('rejects overflow', async function () {
  44. await expect(this.token.$_mint(this.recipient, ethers.MaxUint256)).to.be.revertedWithPanic(
  45. PANIC_CODES.ARITHMETIC_UNDER_OR_OVERFLOW,
  46. );
  47. });
  48. describe('for a non zero account', function () {
  49. beforeEach('minting', async function () {
  50. this.tx = await this.token.$_mint(this.recipient, value);
  51. });
  52. it('increments totalSupply', async function () {
  53. await expect(await this.token.totalSupply()).to.equal(initialSupply + value);
  54. });
  55. it('increments recipient balance', async function () {
  56. await expect(this.tx).to.changeTokenBalance(this.token, this.recipient, value);
  57. });
  58. it('emits Transfer event', async function () {
  59. await expect(this.tx).to.emit(this.token, 'Transfer').withArgs(ethers.ZeroAddress, this.recipient, value);
  60. });
  61. });
  62. });
  63. describe('_burn', function () {
  64. it('rejects a null account', async function () {
  65. await expect(this.token.$_burn(ethers.ZeroAddress, 1n))
  66. .to.be.revertedWithCustomError(this.token, 'ERC20InvalidSender')
  67. .withArgs(ethers.ZeroAddress);
  68. });
  69. describe('for a non zero account', function () {
  70. it('rejects burning more than balance', async function () {
  71. await expect(this.token.$_burn(this.initialHolder, initialSupply + 1n))
  72. .to.be.revertedWithCustomError(this.token, 'ERC20InsufficientBalance')
  73. .withArgs(this.initialHolder, initialSupply, initialSupply + 1n);
  74. });
  75. const describeBurn = function (description, value) {
  76. describe(description, function () {
  77. beforeEach('burning', async function () {
  78. this.tx = await this.token.$_burn(this.initialHolder, value);
  79. });
  80. it('decrements totalSupply', async function () {
  81. expect(await this.token.totalSupply()).to.equal(initialSupply - value);
  82. });
  83. it('decrements initialHolder balance', async function () {
  84. await expect(this.tx).to.changeTokenBalance(this.token, this.initialHolder, -value);
  85. });
  86. it('emits Transfer event', async function () {
  87. await expect(this.tx)
  88. .to.emit(this.token, 'Transfer')
  89. .withArgs(this.initialHolder, ethers.ZeroAddress, value);
  90. });
  91. });
  92. };
  93. describeBurn('for entire balance', initialSupply);
  94. describeBurn('for less value than balance', initialSupply - 1n);
  95. });
  96. });
  97. describe('_update', function () {
  98. const value = 1n;
  99. beforeEach(async function () {
  100. this.totalSupply = await this.token.totalSupply();
  101. });
  102. it('from is the zero address', async function () {
  103. const tx = await this.token.$_update(ethers.ZeroAddress, this.initialHolder, value);
  104. await expect(tx).to.emit(this.token, 'Transfer').withArgs(ethers.ZeroAddress, this.initialHolder, value);
  105. expect(await this.token.totalSupply()).to.equal(this.totalSupply + value);
  106. await expect(tx).to.changeTokenBalance(this.token, this.initialHolder, value);
  107. });
  108. it('to is the zero address', async function () {
  109. const tx = await this.token.$_update(this.initialHolder, ethers.ZeroAddress, value);
  110. await expect(tx).to.emit(this.token, 'Transfer').withArgs(this.initialHolder, ethers.ZeroAddress, value);
  111. expect(await this.token.totalSupply()).to.equal(this.totalSupply - value);
  112. await expect(tx).to.changeTokenBalance(this.token, this.initialHolder, -value);
  113. });
  114. describe('from and to are the same address', function () {
  115. it('zero address', async function () {
  116. const tx = await this.token.$_update(ethers.ZeroAddress, ethers.ZeroAddress, value);
  117. await expect(tx).to.emit(this.token, 'Transfer').withArgs(ethers.ZeroAddress, ethers.ZeroAddress, value);
  118. expect(await this.token.totalSupply()).to.equal(this.totalSupply);
  119. await expect(tx).to.changeTokenBalance(this.token, ethers.ZeroAddress, 0n);
  120. });
  121. describe('non zero address', function () {
  122. it('reverts without balance', async function () {
  123. await expect(this.token.$_update(this.recipient, this.recipient, value))
  124. .to.be.revertedWithCustomError(this.token, 'ERC20InsufficientBalance')
  125. .withArgs(this.recipient, 0n, value);
  126. });
  127. it('executes with balance', async function () {
  128. const tx = await this.token.$_update(this.initialHolder, this.initialHolder, value);
  129. await expect(tx).to.changeTokenBalance(this.token, this.initialHolder, 0n);
  130. await expect(tx).to.emit(this.token, 'Transfer').withArgs(this.initialHolder, this.initialHolder, value);
  131. });
  132. });
  133. });
  134. });
  135. describe('_transfer', function () {
  136. beforeEach(function () {
  137. this.transfer = this.token.$_transfer;
  138. });
  139. shouldBehaveLikeERC20Transfer(initialSupply);
  140. it('reverts when the sender is the zero address', async function () {
  141. await expect(this.token.$_transfer(ethers.ZeroAddress, this.recipient, initialSupply))
  142. .to.be.revertedWithCustomError(this.token, 'ERC20InvalidSender')
  143. .withArgs(ethers.ZeroAddress);
  144. });
  145. });
  146. describe('_approve', function () {
  147. beforeEach(function () {
  148. this.approve = this.token.$_approve;
  149. });
  150. shouldBehaveLikeERC20Approve(initialSupply);
  151. it('reverts when the owner is the zero address', async function () {
  152. await expect(this.token.$_approve(ethers.ZeroAddress, this.recipient, initialSupply))
  153. .to.be.revertedWithCustomError(this.token, 'ERC20InvalidApprover')
  154. .withArgs(ethers.ZeroAddress);
  155. });
  156. });
  157. });
  158. }
  159. });