ERC20.test.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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)
  60. .to.emit(this.token, 'Transfer')
  61. .withArgs(ethers.ZeroAddress, this.recipient.address, value);
  62. });
  63. });
  64. });
  65. describe('_burn', function () {
  66. it('rejects a null account', async function () {
  67. await expect(this.token.$_burn(ethers.ZeroAddress, 1n))
  68. .to.be.revertedWithCustomError(this.token, 'ERC20InvalidSender')
  69. .withArgs(ethers.ZeroAddress);
  70. });
  71. describe('for a non zero account', function () {
  72. it('rejects burning more than balance', async function () {
  73. await expect(this.token.$_burn(this.initialHolder, initialSupply + 1n))
  74. .to.be.revertedWithCustomError(this.token, 'ERC20InsufficientBalance')
  75. .withArgs(this.initialHolder.address, initialSupply, initialSupply + 1n);
  76. });
  77. const describeBurn = function (description, value) {
  78. describe(description, function () {
  79. beforeEach('burning', async function () {
  80. this.tx = await this.token.$_burn(this.initialHolder, value);
  81. });
  82. it('decrements totalSupply', async function () {
  83. expect(await this.token.totalSupply()).to.equal(initialSupply - value);
  84. });
  85. it('decrements initialHolder balance', async function () {
  86. await expect(this.tx).to.changeTokenBalance(this.token, this.initialHolder, -value);
  87. });
  88. it('emits Transfer event', async function () {
  89. await expect(this.tx)
  90. .to.emit(this.token, 'Transfer')
  91. .withArgs(this.initialHolder.address, ethers.ZeroAddress, value);
  92. });
  93. });
  94. };
  95. describeBurn('for entire balance', initialSupply);
  96. describeBurn('for less value than balance', initialSupply - 1n);
  97. });
  98. });
  99. describe('_update', function () {
  100. const value = 1n;
  101. beforeEach(async function () {
  102. this.totalSupply = await this.token.totalSupply();
  103. });
  104. it('from is the zero address', async function () {
  105. const tx = await this.token.$_update(ethers.ZeroAddress, this.initialHolder, value);
  106. await expect(tx)
  107. .to.emit(this.token, 'Transfer')
  108. .withArgs(ethers.ZeroAddress, this.initialHolder.address, value);
  109. expect(await this.token.totalSupply()).to.equal(this.totalSupply + value);
  110. await expect(tx).to.changeTokenBalance(this.token, this.initialHolder, value);
  111. });
  112. it('to is the zero address', async function () {
  113. const tx = await this.token.$_update(this.initialHolder, ethers.ZeroAddress, value);
  114. await expect(tx)
  115. .to.emit(this.token, 'Transfer')
  116. .withArgs(this.initialHolder.address, ethers.ZeroAddress, value);
  117. expect(await this.token.totalSupply()).to.equal(this.totalSupply - value);
  118. await expect(tx).to.changeTokenBalance(this.token, this.initialHolder, -value);
  119. });
  120. describe('from and to are the same address', function () {
  121. it('zero address', async function () {
  122. const tx = await this.token.$_update(ethers.ZeroAddress, ethers.ZeroAddress, value);
  123. await expect(tx).to.emit(this.token, 'Transfer').withArgs(ethers.ZeroAddress, ethers.ZeroAddress, value);
  124. expect(await this.token.totalSupply()).to.equal(this.totalSupply);
  125. await expect(tx).to.changeTokenBalance(this.token, ethers.ZeroAddress, 0n);
  126. });
  127. describe('non zero address', function () {
  128. it('reverts without balance', async function () {
  129. await expect(this.token.$_update(this.recipient, this.recipient, value))
  130. .to.be.revertedWithCustomError(this.token, 'ERC20InsufficientBalance')
  131. .withArgs(this.recipient.address, 0n, value);
  132. });
  133. it('executes with balance', async function () {
  134. const tx = await this.token.$_update(this.initialHolder, this.initialHolder, value);
  135. await expect(tx).to.changeTokenBalance(this.token, this.initialHolder, 0n);
  136. await expect(tx)
  137. .to.emit(this.token, 'Transfer')
  138. .withArgs(this.initialHolder.address, this.initialHolder.address, value);
  139. });
  140. });
  141. });
  142. });
  143. describe('_transfer', function () {
  144. beforeEach(function () {
  145. this.transfer = this.token.$_transfer;
  146. });
  147. shouldBehaveLikeERC20Transfer(initialSupply);
  148. it('reverts when the sender is the zero address', async function () {
  149. await expect(this.token.$_transfer(ethers.ZeroAddress, this.recipient, initialSupply))
  150. .to.be.revertedWithCustomError(this.token, 'ERC20InvalidSender')
  151. .withArgs(ethers.ZeroAddress);
  152. });
  153. });
  154. describe('_approve', function () {
  155. beforeEach(function () {
  156. this.approve = this.token.$_approve;
  157. });
  158. shouldBehaveLikeERC20Approve(initialSupply);
  159. it('reverts when the owner is the zero address', async function () {
  160. await expect(this.token.$_approve(ethers.ZeroAddress, this.recipient, initialSupply))
  161. .to.be.revertedWithCustomError(this.token, 'ERC20InvalidApprover')
  162. .withArgs(ethers.ZeroAddress);
  163. });
  164. });
  165. });
  166. }
  167. });