Skip to content

Commit d5321c8

Browse files
committed
feat: test contract functions
1 parent 7d761b7 commit d5321c8

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed

test/minting.test.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";
2+
import { expect } from "chai";
3+
import { ethers } from "hardhat";
4+
5+
import { deployTokenFixture } from "./token.fixture";
6+
import { Signers } from "./types";
7+
8+
describe("Minting", function () {
9+
before(async function () {
10+
this.signers = {} as Signers;
11+
const signers = await ethers.getSigners();
12+
this.signers.admin = signers[0];
13+
this.loadFixture = loadFixture;
14+
});
15+
16+
describe("Initial State", function () {
17+
beforeEach(async function () {
18+
const { token, token_address } = await this.loadFixture(deployTokenFixture);
19+
this.token = token;
20+
this.token_address = token_address;
21+
});
22+
23+
it("owner is the community steward");
24+
25+
it("should allow the community steward to mint to themselves", async function () {
26+
const initialSupply = await this.token.totalSupply();
27+
await this.token.mint(this.signers.admin.address, 100);
28+
expect(await this.token.totalSupply()).to.equal(initialSupply + 100n);
29+
});
30+
31+
it("should allow the community steward to mint to others", async function () {
32+
const [_, newOwner] = await ethers.getSigners();
33+
const initialSupply = await this.token.totalSupply();
34+
await this.token.mint(newOwner.address, 100);
35+
expect(await this.token.totalSupply()).to.equal(initialSupply + 100n);
36+
});
37+
38+
it("should not allow non-comunity stewards to mint", async function () {
39+
const [_, nonOwner] = await ethers.getSigners();
40+
await expect(this.token.connect(nonOwner).mint(nonOwner.address, 100)).to.be.reverted;
41+
});
42+
});
43+
});

test/ownership.test.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";
2+
import { expect } from "chai";
3+
import { ethers } from "hardhat";
4+
5+
import { deployTokenFixture } from "./token.fixture";
6+
import { Signers } from "./types";
7+
8+
describe("Ownership", () => {
9+
before(async function () {
10+
this.signers = {} as Signers;
11+
const signers = await ethers.getSigners();
12+
this.signers.admin = signers[0];
13+
this.loadFixture = loadFixture;
14+
});
15+
16+
describe("Initial State", () => {
17+
beforeEach(async function () {
18+
const { token, token_address } = await this.loadFixture(deployTokenFixture);
19+
this.token = token;
20+
this.token_address = token_address;
21+
});
22+
23+
it("should be owned by the community steward", async function () {
24+
expect(await this.token.owner()).to.equal(this.signers.admin.address);
25+
});
26+
27+
it("should be able to transfer ownership", async function () {
28+
const [_, newOwner] = await ethers.getSigners();
29+
await this.token.transferOwnership(newOwner.address);
30+
expect(await this.token.owner()).to.equal(newOwner.address);
31+
});
32+
33+
it("only community steward can pause transfers");
34+
});
35+
});

test/token.fixture.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { ethers } from "hardhat";
2+
3+
import { KolektivoTTD, KolektivoTTD__factory } from "../types";
4+
5+
export async function deployTokenFixture() {
6+
// Contracts are deployed using the first signer/account by default
7+
const [_] = await ethers.getSigners();
8+
const Token = (await ethers.getContractFactory("KolektivoTTD")) as KolektivoTTD__factory;
9+
const token = (await Token.deploy()) as KolektivoTTD;
10+
const token_address = await token.getAddress();
11+
12+
return { token, token_address };
13+
}

test/transfer.test.ts

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";
2+
import { expect } from "chai";
3+
import { ethers } from "hardhat";
4+
5+
import { deployTokenFixture } from "./token.fixture";
6+
import { Signers } from "./types";
7+
8+
describe("Transfering", function () {
9+
before(async function () {
10+
this.signers = {} as Signers;
11+
const signers = await ethers.getSigners();
12+
this.signers.admin = signers[0];
13+
this.loadFixture = loadFixture;
14+
});
15+
16+
describe("Initial State", function () {
17+
beforeEach(async function () {
18+
const { token, token_address } = await this.loadFixture(deployTokenFixture);
19+
this.token = token;
20+
this.token_address = token_address;
21+
const [alice, ..._] = await ethers.getSigners();
22+
const transferAmount = 100n;
23+
await token.connect(this.signers.admin).mint(alice.address, transferAmount);
24+
});
25+
26+
it("comunity members can transfer to each other", async function () {
27+
const { token } = this;
28+
const transferAmount = 100n;
29+
const [alice, bob] = await ethers.getSigners();
30+
await token.connect(alice).transfer(bob.address, transferAmount);
31+
expect(await token.balanceOf(alice.address)).to.equal(0n);
32+
expect(await token.balanceOf(bob.address)).to.equal(transferAmount);
33+
});
34+
});
35+
36+
describe("Paying @ Impact Partner", function () {
37+
beforeEach(async function () {
38+
const { token, token_address } = await this.loadFixture(deployTokenFixture);
39+
this.token = token;
40+
this.token_address = token_address;
41+
});
42+
43+
it("can check if a partner is valid", async function () {
44+
const { token } = this;
45+
const [_, partner, customer] = await ethers.getSigners();
46+
await token.connect(this.signers.admin).addPartner(partner.address);
47+
expect(await token.isPartner(partner.address)).to.be.true;
48+
expect(await token.isPartner(customer.address)).to.be.false;
49+
});
50+
51+
it("emits event for partner transfers", async function () {
52+
const { token } = this;
53+
const [_, partner, customer] = await ethers.getSigners();
54+
const transferAmount = 100n;
55+
await token.connect(this.signers.admin).addPartner(partner.address);
56+
await token.connect(this.signers.admin).mint(customer.address, transferAmount);
57+
await expect(token.connect(customer).transfer(partner.address, transferAmount))
58+
.to.emit(token, "ImpactPartnerTransfer")
59+
.withArgs(customer.address, partner.address, transferAmount);
60+
expect(await token.balanceOf(partner.address)).to.equal(transferAmount);
61+
expect(await token.balanceOf(customer.address)).to.equal(0n);
62+
});
63+
});
64+
});

0 commit comments

Comments
 (0)