ERC20Capped.test.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const name = 'My Token';
  5. const symbol = 'MTKN';
  6. const cap = 1000n;
  7. async function fixture() {
  8. const [user] = await ethers.getSigners();
  9. const token = await ethers.deployContract('$ERC20Capped', [name, symbol, cap]);
  10. return { user, token, cap };
  11. }
  12. describe('ERC20Capped', function () {
  13. beforeEach(async function () {
  14. Object.assign(this, await loadFixture(fixture));
  15. });
  16. it('requires a non-zero cap', async function () {
  17. const ERC20Capped = await ethers.getContractFactory('$ERC20Capped');
  18. await expect(ERC20Capped.deploy(name, symbol, 0))
  19. .to.be.revertedWithCustomError(ERC20Capped, 'ERC20InvalidCap')
  20. .withArgs(0);
  21. });
  22. describe('capped token', function () {
  23. it('starts with the correct cap', async function () {
  24. expect(await this.token.cap()).to.equal(this.cap);
  25. });
  26. it('mints when value is less than cap', async function () {
  27. const value = this.cap - 1n;
  28. await this.token.$_mint(this.user, value);
  29. expect(await this.token.totalSupply()).to.equal(value);
  30. });
  31. it('fails to mint if the value exceeds the cap', async function () {
  32. await this.token.$_mint(this.user, this.cap - 1n);
  33. await expect(this.token.$_mint(this.user, 2))
  34. .to.be.revertedWithCustomError(this.token, 'ERC20ExceededCap')
  35. .withArgs(this.cap + 1n, this.cap);
  36. });
  37. it('fails to mint after cap is reached', async function () {
  38. await this.token.$_mint(this.user, this.cap);
  39. await expect(this.token.$_mint(this.user, 1))
  40. .to.be.revertedWithCustomError(this.token, 'ERC20ExceededCap')
  41. .withArgs(this.cap + 1n, this.cap);
  42. });
  43. });
  44. });