Skip to content

Commit 1e9689b

Browse files
committed
feat: add new tests
1 parent e3b09de commit 1e9689b

File tree

1 file changed

+56
-8
lines changed

1 file changed

+56
-8
lines changed

test/truthpost.js

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { expect } = require("chai");
22
const { ethers } = require("hardhat");
33
const { BigNumber } = ethers;
44
const crypto = require("crypto");
5-
const { constants } = require("ethers");
5+
const { constants, utils } = require("ethers");
66

77
const EXAMPLE_IPFS_CIDv1 = "bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi";
88
const ANOTHER_EXAMPLE_IPFS_CIDv1 = "bafybeigdyrzt5sfp7OKOKAHLSKASLK2LK3JLlqabf3oclgtqy55fbzdi";
@@ -13,7 +13,7 @@ const APPEAL_WINDOW = 1000;
1313
const ONE_ETH = BigNumber.from(BigInt(1e18));
1414
const TWO_ETH = BigNumber.from(2).mul(BigNumber.from(BigInt(1e18)));
1515
const FIVE_ETH = BigNumber.from(5).mul(BigNumber.from(BigInt(1e18)));
16-
const TEN_ETH = BigNumber.from(1000).mul(BigNumber.from(BigInt(1e18)));
16+
const TEN_ETH = BigNumber.from(10).mul(BigNumber.from(BigInt(1e18)));
1717

1818
const TIMELOCK_PERIOD = 1000000;
1919

@@ -28,8 +28,8 @@ function sleep(ms) {
2828

2929
describe("The Truth Post", () => {
3030
before("Deploying", async () => {
31-
[deployer, author, supporter, challenger, innocentBystander] = await ethers.getSigners();
32-
({ arbitrator, truthPost } = await deployContracts(deployer));
31+
[deployer, newAdmin, author, supporter, challenger, innocentBystander, treasury] = await ethers.getSigners();
32+
({ arbitrator, truthPost } = await deployContracts(deployer, treasury));
3333
await sleep(9000); // To wait for eth gas reporter to fetch data. Remove this line when the issue is fixed. https://github.com/cgewecke/hardhat-gas-reporter/issues/72
3434
NUMBER_OF_LEAST_SIGNIFICANT_BITS_TO_IGNORE = await truthPost.connect(deployer).NUMBER_OF_LEAST_SIGNIFICANT_BITS_TO_IGNORE();
3535
MULTIPLIER_DENOMINATOR = await truthPost.connect(deployer).MULTIPLIER_DENOMINATOR();
@@ -94,7 +94,7 @@ describe("The Truth Post", () => {
9494
}
9595
});
9696

97-
it("Should challenge a article", async () => {
97+
it("Should challenge an article", async () => {
9898
const ARTICLE_ADDRESS = 0;
9999

100100
const challengeFee = await truthPost.connect(deployer).challengeFee(ARTICLE_ADDRESS);
@@ -113,7 +113,6 @@ describe("The Truth Post", () => {
113113

114114
it("Should fund appeal of a dispute", async () => {
115115
const DISPUTE_ID = disputeCounter - 1;
116-
const ARTICLE_ADDRESS = 0;
117116

118117
await arbitrator.connect(deployer).giveRuling(DISPUTE_ID, RULING_OUTCOMES.ChallengeFailed, APPEAL_WINDOW);
119118
await ethers.provider.send("evm_increaseTime", [10]);
@@ -258,10 +257,59 @@ describe("The Truth Post", () => {
258257
expect(await truthPost.getAmountRemainsToBeRaised(DISPUTE_ID, RULING_OUTCOMES.Debunked)).to.eq(WINNER_FUNDING);
259258
expect(await truthPost.getAmountRemainsToBeRaised(DISPUTE_ID, RULING_OUTCOMES.ChallengeFailed)).to.eq(LOSER_FUNDING);
260259
});
260+
261+
it("Should collect correct amount of taxes", async () => {
262+
const vacantSlotIndex = await truthPost.findVacantStorageSlot(0);
263+
const bounty = TEN_ETH;
264+
265+
const args = [crypto.randomBytes(30).toString("hex"), 0, vacantSlotIndex];
266+
await truthPost.connect(deployer).initializeArticle(...args, { value: bounty });
267+
268+
const challengeFee = await truthPost.challengeFee(vacantSlotIndex);
269+
270+
let storageTreasuryBalanceBefore = await truthPost.treasuryBalance();
271+
await truthPost.connect(challenger).challenge(vacantSlotIndex, { value: challengeFee });
272+
const storageTreasuryBalanceAfter = await truthPost.treasuryBalance();
273+
const storageTreasuryBalanceDelta = storageTreasuryBalanceAfter.sub(storageTreasuryBalanceBefore);
274+
275+
const challengeTaxRate = await truthPost.challengeTaxRate();
276+
const denominator = await truthPost.MULTIPLIER_DENOMINATOR();
277+
278+
expect(challengeTaxRate.mul(10000).div(denominator)).equal(storageTreasuryBalanceDelta.mul(10000).div(bounty));
279+
280+
storageTreasuryBalanceBefore = storageTreasuryBalanceAfter;
281+
const treasuryBalanceBefore = await ethers.provider.getBalance(treasury.address);
282+
await truthPost.connect(deployer).transferBalanceToTreasury();
283+
const treasuryBalanceAfter = await ethers.provider.getBalance(treasury.address);
284+
const treasuryBalanceDelta = treasuryBalanceAfter.sub(treasuryBalanceBefore);
285+
286+
expect(storageTreasuryBalanceBefore).equal(treasuryBalanceDelta);
287+
expect(await truthPost.treasuryBalance()).equal(BigNumber.from(0));
288+
});
289+
290+
it("should not allow to set a new admin", async () => {
291+
await expect(truthPost.connect(newAdmin).changeAdmin(newAdmin.address)).to.be.reverted;
292+
});
293+
294+
it("Should set a new admin", async () => {
295+
await truthPost.connect(deployer).changeAdmin(newAdmin.address);
296+
expect(await truthPost.admin()).equal(newAdmin.address);
297+
});
298+
299+
it("Should allow only admin to set new tax rate", async () => {
300+
const newTaxRate = 128;
301+
await truthPost.connect(newAdmin).updateChallengeTaxRate(newTaxRate);
302+
expect(await truthPost.challengeTaxRate()).equal(BigNumber.from(newTaxRate));
303+
});
304+
305+
it("Should not allow to increase the tax rate more than 25%", async () => {
306+
await expect(truthPost.connect(newAdmin).updateChallengeTaxRate(512))
307+
.to.be.revertedWith("The tax rate can only be increased by a maximum of 25%");
308+
});
261309
});
262310
});
263311

264-
async function deployContracts(deployer) {
312+
async function deployContracts(deployer, treasury) {
265313
const SHARE_DENOMINATOR = 1_000_000;
266314
const MIN_FUND_INCREASE_PERCENT = 25;
267315
const MIN_BOUNTY = ONE_ETH.div(BigNumber.from(1e3));
@@ -272,7 +320,7 @@ async function deployContracts(deployer) {
272320

273321
const TruthPost = await ethers.getContractFactory("TruthPost", deployer);
274322
// const truthPost = await PMW.deploy({ arbitrator: arbitrator.address, arbitratorExtraData: "0x00" }, SHARE_DENOMINATOR, MIN_FUND_INCREASE_PERCENT, MIN_BOUNTY);
275-
const truthPost = await TruthPost.deploy(arbitrator.address, "0x00", "Metaevidence", TIMELOCK_PERIOD, WINNER_STAKE_MULTIPLIER, LOSER_STAKE_MULTIPLIER);
323+
const truthPost = await TruthPost.deploy(arbitrator.address, "0x00", "Metaevidence", TIMELOCK_PERIOD, WINNER_STAKE_MULTIPLIER, LOSER_STAKE_MULTIPLIER, treasury.address);
276324

277325
await truthPost.deployed();
278326

0 commit comments

Comments
 (0)