|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { ethers } from 'hardhat'; |
| 3 | + |
| 4 | +describe('ERC20Mock', function () { |
| 5 | + beforeEach(async function () { |
| 6 | + const [deployer, alice, bob] = await ethers.getSigners(); |
| 7 | + const ERC20Mock = await ethers.getContractFactory('ERC20Mock'); |
| 8 | + const token = await ERC20Mock.deploy('Mock Token', 'MTK', 18); |
| 9 | + await token.waitForDeployment(); |
| 10 | + Object.assign(this, { token, deployer, alice, bob }); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should support custom decimals', async function () { |
| 14 | + const ERC20Mock = await ethers.getContractFactory('ERC20Mock'); |
| 15 | + const token6 = await ERC20Mock.deploy('Six Decimals', 'SIX', 6); |
| 16 | + expect(await token6.decimals()).to.equal(6); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should mint tokens', async function () { |
| 20 | + const amount = ethers.parseUnits('1000', 18); |
| 21 | + await this.token.mint(this.alice.address, amount); |
| 22 | + expect(await this.token.balanceOf(this.alice.address)).to.equal(amount); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should revert if mint amount exceeds max', async function () { |
| 26 | + const maxAmount = ethers.parseUnits('1000000', 18); |
| 27 | + const tooMuch = maxAmount + 1n; |
| 28 | + await expect(this.token.mint(this.alice.address, tooMuch)) |
| 29 | + .to.be.revertedWithCustomError(this.token, 'MintAmountExceedsMax') |
| 30 | + .withArgs(tooMuch, maxAmount); |
| 31 | + }); |
| 32 | +}); |
| 33 | + |
| 34 | +describe('USDTMock', function () { |
| 35 | + beforeEach(async function () { |
| 36 | + const [deployer, alice, bob] = await ethers.getSigners(); |
| 37 | + const USDTMock = await ethers.getContractFactory('USDTMock'); |
| 38 | + const usdt = await USDTMock.deploy(); |
| 39 | + await usdt.waitForDeployment(); |
| 40 | + Object.assign(this, { usdt, deployer, alice, bob }); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should have correct name, symbol, and decimals', async function () { |
| 44 | + expect(await this.usdt.name()).to.equal('Tether USD (Mock)'); |
| 45 | + expect(await this.usdt.symbol()).to.equal('USDTMock'); |
| 46 | + expect(await this.usdt.decimals()).to.equal(6); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should approve from zero allowance', async function () { |
| 50 | + const amount = ethers.parseUnits('100', 6); |
| 51 | + await this.usdt.connect(this.alice).approve(this.bob.address, amount); |
| 52 | + expect(await this.usdt.allowance(this.alice.address, this.bob.address)).to.equal(amount); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should approve to zero', async function () { |
| 56 | + const amount = ethers.parseUnits('100', 6); |
| 57 | + await this.usdt.connect(this.alice).approve(this.bob.address, amount); |
| 58 | + await this.usdt.connect(this.alice).approve(this.bob.address, 0); |
| 59 | + expect(await this.usdt.allowance(this.alice.address, this.bob.address)).to.equal(0); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should revert when changing non-zero allowance to non-zero', async function () { |
| 63 | + const amount1 = ethers.parseUnits('100', 6); |
| 64 | + const amount2 = ethers.parseUnits('200', 6); |
| 65 | + await this.usdt.connect(this.alice).approve(this.bob.address, amount1); |
| 66 | + await expect(this.usdt.connect(this.alice).approve(this.bob.address, amount2)).to.be.revertedWithoutReason(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should allow setting new allowance after resetting to zero', async function () { |
| 70 | + const amount1 = ethers.parseUnits('100', 6); |
| 71 | + const amount2 = ethers.parseUnits('200', 6); |
| 72 | + await this.usdt.connect(this.alice).approve(this.bob.address, amount1); |
| 73 | + await this.usdt.connect(this.alice).approve(this.bob.address, 0); |
| 74 | + await this.usdt.connect(this.alice).approve(this.bob.address, amount2); |
| 75 | + expect(await this.usdt.allowance(this.alice.address, this.bob.address)).to.equal(amount2); |
| 76 | + }); |
| 77 | +}); |
0 commit comments