Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 43 additions & 24 deletions test/ERC20_Token_Sample.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ describe("ERC20_Token_Sample", function () {
"ERC20_Token_Sample",
deployer
);
token = await Token.deploy(recipient.address);
const recipientAddr = await recipient.getAddress();
token = await Token.deploy(recipientAddr);
await token.waitForDeployment();
});

Expand All @@ -25,13 +26,15 @@ describe("ERC20_Token_Sample", function () {
});

it("mints the complete supply to the explicit recipient", async function () {
expect(await token.balanceOf(recipient.address)).to.equal(INITIAL_SUPPLY);
const recipientAddr = await recipient.getAddress();
expect(await token.balanceOf(recipientAddr)).to.equal(INITIAL_SUPPLY);
expect(await token.totalSupply()).to.equal(INITIAL_SUPPLY);
expect(await token.INITIAL_SUPPLY()).to.equal(INITIAL_SUPPLY);
});

it("does not mint to the deployer", async function () {
expect(await token.balanceOf(deployer.address)).to.equal(0);
const deployerAddr = await deployer.getAddress();
expect(await token.balanceOf(deployerAddr)).to.equal(0);
});

it("rejects the zero-address recipient", async function () {
Expand All @@ -46,58 +49,69 @@ describe("ERC20_Token_Sample", function () {
describe("Transfers", function () {
it("transfers from the explicit recipient", async function () {
const amount = ethers.parseUnits("1000", 18);
await expect(token.connect(recipient).transfer(alice.address, amount))
const recipientAddr = await recipient.getAddress();
const aliceAddr = await alice.getAddress();
await expect(token.connect(recipient).transfer(aliceAddr, amount))
.to.emit(token, "Transfer")
.withArgs(recipient.address, alice.address, amount);
.withArgs(recipientAddr, aliceAddr, amount);

expect(await token.balanceOf(alice.address)).to.equal(amount);
expect(await token.balanceOf(recipient.address)).to.equal(
expect(await token.balanceOf(aliceAddr)).to.equal(amount);
expect(await token.balanceOf(recipientAddr)).to.equal(
INITIAL_SUPPLY - amount
);
});

it("reverts for an insufficient balance", async function () {
const aliceAddr = await alice.getAddress();
const bobAddr = await bob.getAddress();
Comment on lines 64 to +66
await expect(
token.connect(alice).transfer(bob.address, ethers.parseUnits("1", 18))
token.connect(alice).transfer(bobAddr, ethers.parseUnits("1", 18))
).to.be.reverted;
});
});

describe("Allowances", function () {
it("supports approve and transferFrom", async function () {
const amount = ethers.parseUnits("100", 18);
await token.connect(recipient).approve(alice.address, amount);
const recipientAddr = await recipient.getAddress();
const aliceAddr = await alice.getAddress();
const bobAddr = await bob.getAddress();
await token.connect(recipient).approve(aliceAddr, amount);
expect(
await token.allowance(recipient.address, alice.address)
await token.allowance(recipientAddr, aliceAddr)
).to.equal(amount);

await token
.connect(alice)
.transferFrom(recipient.address, bob.address, amount);
expect(await token.balanceOf(bob.address)).to.equal(amount);
.transferFrom(recipientAddr, bobAddr, amount);
expect(await token.balanceOf(bobAddr)).to.equal(amount);
});

it("reverts when allowance is exceeded", async function () {
const amount = ethers.parseUnits("100", 18);
await token.connect(recipient).approve(alice.address, amount);
const recipientAddr = await recipient.getAddress();
const aliceAddr = await alice.getAddress();
const bobAddr = await bob.getAddress();
await token.connect(recipient).approve(aliceAddr, amount);
await expect(
token
.connect(alice)
.transferFrom(recipient.address, bob.address, amount + 1n)
.transferFrom(recipientAddr, bobAddr, amount + 1n)
).to.be.reverted;
});
});

describe("burnTokens", function () {
it("burns from the caller and reduces supply", async function () {
const amount = ethers.parseUnits("500", 18);
const recipientAddr = await recipient.getAddress();
await expect(token.connect(recipient).burnTokens(amount))
.to.emit(token, "TokensBurned")
.withArgs(recipient.address, amount)
.withArgs(recipientAddr, amount)
.and.to.emit(token, "Transfer")
.withArgs(recipient.address, ethers.ZeroAddress, amount);
.withArgs(recipientAddr, ethers.ZeroAddress, amount);

expect(await token.balanceOf(recipient.address)).to.equal(
expect(await token.balanceOf(recipientAddr)).to.equal(
INITIAL_SUPPLY - amount
);
expect(await token.totalSupply()).to.equal(INITIAL_SUPPLY - amount);
Expand All @@ -115,28 +129,33 @@ describe("ERC20_Token_Sample", function () {
const amount = ethers.parseUnits("300", 18);

beforeEach(async function () {
const aliceAddr = await alice.getAddress();
const bobAddr = await bob.getAddress();
await token
.connect(recipient)
.transfer(alice.address, ethers.parseUnits("1000", 18));
await token.connect(alice).approve(bob.address, amount);
.transfer(aliceAddr, ethers.parseUnits("1000", 18));
await token.connect(alice).approve(bobAddr, amount);
});

it("burns approved tokens and consumes allowance", async function () {
const supplyBefore = await token.totalSupply();
await expect(token.connect(bob).burnFrom(alice.address, amount))
const aliceAddr = await alice.getAddress();
const bobAddr = await bob.getAddress();
await expect(token.connect(bob).burnFrom(aliceAddr, amount))
.to.emit(token, "TokensBurned")
.withArgs(alice.address, amount);
.withArgs(aliceAddr, amount);

expect(await token.allowance(alice.address, bob.address)).to.equal(0);
expect(await token.allowance(aliceAddr, bobAddr)).to.equal(0);
expect(await token.totalSupply()).to.equal(supplyBefore - amount);
});

it("rejects zero and excessive allowance burns", async function () {
const aliceAddr = await alice.getAddress();
await expect(
token.connect(bob).burnFrom(alice.address, 0)
token.connect(bob).burnFrom(aliceAddr, 0)
).to.be.revertedWithCustomError(token, "ZeroBurnAmount");
await expect(
token.connect(bob).burnFrom(alice.address, amount + 1n)
token.connect(bob).burnFrom(aliceAddr, amount + 1n)
).to.be.reverted;
});
});
Expand Down
Loading