Skip to content

Commit

Permalink
feat: test contract functions
Browse files Browse the repository at this point in the history
  • Loading branch information
shottah committed Jul 24, 2024
1 parent 7d761b7 commit d5321c8
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 0 deletions.
43 changes: 43 additions & 0 deletions test/minting.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";
import { expect } from "chai";
import { ethers } from "hardhat";

import { deployTokenFixture } from "./token.fixture";
import { Signers } from "./types";

describe("Minting", function () {
before(async function () {
this.signers = {} as Signers;
const signers = await ethers.getSigners();
this.signers.admin = signers[0];
this.loadFixture = loadFixture;
});

describe("Initial State", function () {
beforeEach(async function () {
const { token, token_address } = await this.loadFixture(deployTokenFixture);
this.token = token;
this.token_address = token_address;
});

it("owner is the community steward");

it("should allow the community steward to mint to themselves", async function () {
const initialSupply = await this.token.totalSupply();
await this.token.mint(this.signers.admin.address, 100);
expect(await this.token.totalSupply()).to.equal(initialSupply + 100n);
});

it("should allow the community steward to mint to others", async function () {
const [_, newOwner] = await ethers.getSigners();
const initialSupply = await this.token.totalSupply();
await this.token.mint(newOwner.address, 100);
expect(await this.token.totalSupply()).to.equal(initialSupply + 100n);
});

it("should not allow non-comunity stewards to mint", async function () {
const [_, nonOwner] = await ethers.getSigners();
await expect(this.token.connect(nonOwner).mint(nonOwner.address, 100)).to.be.reverted;
});
});
});
35 changes: 35 additions & 0 deletions test/ownership.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";
import { expect } from "chai";
import { ethers } from "hardhat";

import { deployTokenFixture } from "./token.fixture";
import { Signers } from "./types";

describe("Ownership", () => {
before(async function () {
this.signers = {} as Signers;
const signers = await ethers.getSigners();
this.signers.admin = signers[0];
this.loadFixture = loadFixture;
});

describe("Initial State", () => {
beforeEach(async function () {
const { token, token_address } = await this.loadFixture(deployTokenFixture);
this.token = token;
this.token_address = token_address;
});

it("should be owned by the community steward", async function () {
expect(await this.token.owner()).to.equal(this.signers.admin.address);
});

it("should be able to transfer ownership", async function () {
const [_, newOwner] = await ethers.getSigners();
await this.token.transferOwnership(newOwner.address);
expect(await this.token.owner()).to.equal(newOwner.address);
});

it("only community steward can pause transfers");
});
});
13 changes: 13 additions & 0 deletions test/token.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ethers } from "hardhat";

import { KolektivoTTD, KolektivoTTD__factory } from "../types";

export async function deployTokenFixture() {
// Contracts are deployed using the first signer/account by default
const [_] = await ethers.getSigners();
const Token = (await ethers.getContractFactory("KolektivoTTD")) as KolektivoTTD__factory;
const token = (await Token.deploy()) as KolektivoTTD;
const token_address = await token.getAddress();

return { token, token_address };
}
64 changes: 64 additions & 0 deletions test/transfer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";
import { expect } from "chai";
import { ethers } from "hardhat";

import { deployTokenFixture } from "./token.fixture";
import { Signers } from "./types";

describe("Transfering", function () {
before(async function () {
this.signers = {} as Signers;
const signers = await ethers.getSigners();
this.signers.admin = signers[0];
this.loadFixture = loadFixture;
});

describe("Initial State", function () {
beforeEach(async function () {
const { token, token_address } = await this.loadFixture(deployTokenFixture);
this.token = token;
this.token_address = token_address;
const [alice, ..._] = await ethers.getSigners();
const transferAmount = 100n;
await token.connect(this.signers.admin).mint(alice.address, transferAmount);
});

it("comunity members can transfer to each other", async function () {
const { token } = this;
const transferAmount = 100n;
const [alice, bob] = await ethers.getSigners();
await token.connect(alice).transfer(bob.address, transferAmount);
expect(await token.balanceOf(alice.address)).to.equal(0n);
expect(await token.balanceOf(bob.address)).to.equal(transferAmount);
});
});

describe("Paying @ Impact Partner", function () {
beforeEach(async function () {
const { token, token_address } = await this.loadFixture(deployTokenFixture);
this.token = token;
this.token_address = token_address;
});

it("can check if a partner is valid", async function () {
const { token } = this;
const [_, partner, customer] = await ethers.getSigners();
await token.connect(this.signers.admin).addPartner(partner.address);
expect(await token.isPartner(partner.address)).to.be.true;
expect(await token.isPartner(customer.address)).to.be.false;
});

it("emits event for partner transfers", async function () {
const { token } = this;
const [_, partner, customer] = await ethers.getSigners();
const transferAmount = 100n;
await token.connect(this.signers.admin).addPartner(partner.address);
await token.connect(this.signers.admin).mint(customer.address, transferAmount);
await expect(token.connect(customer).transfer(partner.address, transferAmount))
.to.emit(token, "ImpactPartnerTransfer")
.withArgs(customer.address, partner.address, transferAmount);
expect(await token.balanceOf(partner.address)).to.equal(transferAmount);
expect(await token.balanceOf(customer.address)).to.equal(0n);
});
});
});

0 comments on commit d5321c8

Please sign in to comment.