ERC20.test.js 7.7 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. // this.accounts is used by shouldBehaveLikeERC20
  19. const accounts = await ethers.getSigners();
  20. const [holder, recipient] = accounts;
  21. const token = await ethers.deployContract(Token, [name, symbol]);
  22. await token.$_mint(holder, initialSupply);
  23. return { accounts, holder, recipient, token };
  24. };
  25. beforeEach(async function () {
  26. Object.assign(this, await loadFixture(fixture));
  27. });
  28. shouldBehaveLikeERC20(initialSupply, { forcedApproval });
  29. it('has a name', async function () {
  30. expect(await this.token.name()).to.equal(name);
  31. });
  32. it('has a symbol', async function () {
  33. expect(await this.token.symbol()).to.equal(symbol);
  34. });
  35. it('has 18 decimals', async function () {
  36. expect(await this.token.decimals()).to.equal(18n);
  37. });
  38. describe('_mint', function () {
  39. const value = 50n;
  40. it('rejects a null account', async function () {
  41. await expect(this.token.$_mint(ethers.ZeroAddress, value))
  42. .to.be.revertedWithCustomError(this.token, 'ERC20InvalidReceiver')
  43. .withArgs(ethers.ZeroAddress);
  44. });
  45. it('rejects overflow', async function () {
  46. await expect(this.token.$_mint(this.recipient, ethers.MaxUint256)).to.be.revertedWithPanic(
  47. PANIC_CODES.ARITHMETIC_UNDER_OR_OVERFLOW,
  48. );
  49. });
  50. describe('for a non zero account', function () {
  51. beforeEach('minting', async function () {
  52. this.tx = await this.token.$_mint(this.recipient, value);
  53. });
  54. it('increments totalSupply', async function () {
  55. await expect(await this.token.totalSupply()).to.equal(initialSupply + value);
  56. });
  57. it('increments recipient balance', async function () {
  58. await expect(this.tx).to.changeTokenBalance(this.token, this.recipient, value);
  59. });
  60. it('emits Transfer event', async function () {
  61. await expect(this.tx).to.emit(this.token, 'Transfer').withArgs(ethers.ZeroAddress, this.recipient, 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.holder, initialSupply + 1n))
  74. .to.be.revertedWithCustomError(this.token, 'ERC20InsufficientBalance')
  75. .withArgs(this.holder, 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.holder, value);
  81. });
  82. it('decrements totalSupply', async function () {
  83. expect(await this.token.totalSupply()).to.equal(initialSupply - value);
  84. });
  85. it('decrements holder balance', async function () {
  86. await expect(this.tx).to.changeTokenBalance(this.token, this.holder, -value);
  87. });
  88. it('emits Transfer event', async function () {
  89. await expect(this.tx).to.emit(this.token, 'Transfer').withArgs(this.holder, 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.holder, value);
  104. await expect(tx).to.emit(this.token, 'Transfer').withArgs(ethers.ZeroAddress, this.holder, value);
  105. expect(await this.token.totalSupply()).to.equal(this.totalSupply + value);
  106. await expect(tx).to.changeTokenBalance(this.token, this.holder, value);
  107. });
  108. it('to is the zero address', async function () {
  109. const tx = await this.token.$_update(this.holder, ethers.ZeroAddress, value);
  110. await expect(tx).to.emit(this.token, 'Transfer').withArgs(this.holder, ethers.ZeroAddress, value);
  111. expect(await this.token.totalSupply()).to.equal(this.totalSupply - value);
  112. await expect(tx).to.changeTokenBalance(this.token, this.holder, -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.holder, this.holder, value);
  129. await expect(tx).to.changeTokenBalance(this.token, this.holder, 0n);
  130. await expect(tx).to.emit(this.token, 'Transfer').withArgs(this.holder, this.holder, 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. });