123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- const { ethers } = require('hardhat');
- const { expect } = require('chai');
- const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
- const name = 'My Token';
- const symbol = 'MTKN';
- const initialSupply = 100n;
- async function fixture() {
- const [holder, recipient, approved] = await ethers.getSigners();
- const token = await ethers.deployContract('$ERC20Pausable', [name, symbol]);
- await token.$_mint(holder, initialSupply);
- return { holder, recipient, approved, token };
- }
- describe('ERC20Pausable', function () {
- beforeEach(async function () {
- Object.assign(this, await loadFixture(fixture));
- });
- describe('pausable token', function () {
- describe('transfer', function () {
- it('allows to transfer when unpaused', async function () {
- await expect(this.token.connect(this.holder).transfer(this.recipient, initialSupply)).to.changeTokenBalances(
- this.token,
- [this.holder, this.recipient],
- [-initialSupply, initialSupply],
- );
- });
- it('allows to transfer when paused and then unpaused', async function () {
- await this.token.$_pause();
- await this.token.$_unpause();
- await expect(this.token.connect(this.holder).transfer(this.recipient, initialSupply)).to.changeTokenBalances(
- this.token,
- [this.holder, this.recipient],
- [-initialSupply, initialSupply],
- );
- });
- it('reverts when trying to transfer when paused', async function () {
- await this.token.$_pause();
- await expect(
- this.token.connect(this.holder).transfer(this.recipient, initialSupply),
- ).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
- });
- });
- describe('transfer from', function () {
- const allowance = 40n;
- beforeEach(async function () {
- await this.token.connect(this.holder).approve(this.approved, allowance);
- });
- it('allows to transfer from when unpaused', async function () {
- await expect(
- this.token.connect(this.approved).transferFrom(this.holder, this.recipient, allowance),
- ).to.changeTokenBalances(this.token, [this.holder, this.recipient], [-allowance, allowance]);
- });
- it('allows to transfer when paused and then unpaused', async function () {
- await this.token.$_pause();
- await this.token.$_unpause();
- await expect(
- this.token.connect(this.approved).transferFrom(this.holder, this.recipient, allowance),
- ).to.changeTokenBalances(this.token, [this.holder, this.recipient], [-allowance, allowance]);
- });
- it('reverts when trying to transfer from when paused', async function () {
- await this.token.$_pause();
- await expect(
- this.token.connect(this.approved).transferFrom(this.holder, this.recipient, allowance),
- ).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
- });
- });
- describe('mint', function () {
- const value = 42n;
- it('allows to mint when unpaused', async function () {
- await expect(this.token.$_mint(this.recipient, value)).to.changeTokenBalance(this.token, this.recipient, value);
- });
- it('allows to mint when paused and then unpaused', async function () {
- await this.token.$_pause();
- await this.token.$_unpause();
- await expect(this.token.$_mint(this.recipient, value)).to.changeTokenBalance(this.token, this.recipient, value);
- });
- it('reverts when trying to mint when paused', async function () {
- await this.token.$_pause();
- await expect(this.token.$_mint(this.recipient, value)).to.be.revertedWithCustomError(
- this.token,
- 'EnforcedPause',
- );
- });
- });
- describe('burn', function () {
- const value = 42n;
- it('allows to burn when unpaused', async function () {
- await expect(this.token.$_burn(this.holder, value)).to.changeTokenBalance(this.token, this.holder, -value);
- });
- it('allows to burn when paused and then unpaused', async function () {
- await this.token.$_pause();
- await this.token.$_unpause();
- await expect(this.token.$_burn(this.holder, value)).to.changeTokenBalance(this.token, this.holder, -value);
- });
- it('reverts when trying to burn when paused', async function () {
- await this.token.$_pause();
- await expect(this.token.$_burn(this.holder, value)).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
- });
- });
- });
- });
|