Skip to content

Commit

Permalink
isUserMod function added
Browse files Browse the repository at this point in the history
  • Loading branch information
tempe-techie committed Sep 24, 2024
1 parent 72f0658 commit 27cc1cb
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
6 changes: 5 additions & 1 deletion contracts/chat/ChatContextV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ contract ChatContextV1 is Ownable {
address public modTokenAddress; // NFT, SBT, or ERC-20 token which is used to determine if an address is a mod
uint256 public modMinBalance; // minimum balance of mod token required to be considered a mod
bool public paused = false; // whether the contract is paused or not
uint256 public price = 0.0001 ether; // price to post a message (can serve as anti-spam measure; can be set to 0 to disable)
uint256 public price = 0.00001 ether; // price to post a message (can serve as anti-spam measure; can be set to 0 to disable)

Message[] public mainMessages; // array of main messages
mapping(uint256 => Message[]) public replies; // mapping from main message index to array of replies
Expand Down Expand Up @@ -166,6 +166,10 @@ contract ChatContextV1 is Ownable {
return replies[mainMsgIndex_].length;
}

function isUserMod(address user_) external view returns (bool) {
return IModToken(modTokenAddress).balanceOf(user_) >= modMinBalance || user_ == owner();
}

// WRITE FUNCTIONS

/**
Expand Down
6 changes: 5 additions & 1 deletion contracts/chat/CommentsContextV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ contract CommentsContextV1 is Ownable {
address public modTokenAddress; // NFT, SBT, or ERC-20 token which is used to determine if an address is a mod
uint256 public modMinBalance; // minimum balance of mod token required to be considered a mod
bool public paused = false; // whether the contract is paused or not
uint256 public price = 0.0001 ether; // price of a comment
uint256 public price = 0.00001 ether; // price of a comment

mapping(address => Comment[]) public comments; // mapping from subject address to array of comments
mapping(address => bool) public suspended; // whether an address is suspended from posting or not
Expand Down Expand Up @@ -116,6 +116,10 @@ contract CommentsContextV1 is Ownable {
return comments[subjectAddress_].length;
}

function isUserMod(address user_) external view returns (bool) {
return IModToken(modTokenAddress).balanceOf(user_) >= modMinBalance || user_ == owner();
}

// WRITE FUNCTIONS

/**
Expand Down
2 changes: 1 addition & 1 deletion hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ module.exports = {
gasPrice: 1000000000, // 1 gwei
},
sepolia: { // Sepolia testnet
url: 'https://rpc2.sepolia.org',
url: 'https://rpc.sepolia.org',
chainId: 11155111,
accounts: [process.env.DEPLOYER_PRIVATE_KEY],
gas: "auto", // gas limit
Expand Down
35 changes: 32 additions & 3 deletions test/chat/chatContextV1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("ChatContextV1", function () {
let chatContract;
let modTokenContract;
let owner, user1, user2, user3;
const price = ethers.utils.parseEther("0.0001");
const price = ethers.utils.parseEther("0.00001");

beforeEach(async function () {
[owner, user1, user2, user3] = await ethers.getSigners();
Expand Down Expand Up @@ -425,12 +425,16 @@ describe("ChatContextV1", function () {
});

it("allows owner to withdraw revenue", async function () {
// change price to 0.01
const newPrice = ethers.utils.parseEther("0.01");
await chatContract.connect(owner).setPrice(newPrice);

// Create a message to generate revenue
await chatContract.connect(user2).createMessage("ipfs://message1", { value: price });
await chatContract.connect(user2).createMessage("ipfs://message1", { value: newPrice });

// Check contract balance
const contractBalance = await ethers.provider.getBalance(chatContract.address);
expect(contractBalance).to.equal(price);
expect(contractBalance).to.equal(newPrice);

// Withdraw revenue
const initialOwnerBalance = await ethers.provider.getBalance(owner.address);
Expand All @@ -442,4 +446,29 @@ describe("ChatContextV1", function () {
expect(finalOwnerBalance).to.be.above(initialOwnerBalance);
});

it("correctly identifies mods and non-mods", async function () {
// Check if owner is considered a mod
expect(await chatContract.isUserMod(owner.address)).to.be.true;

// Check if user1 (who has a mod token) is considered a mod
expect(await chatContract.isUserMod(user1.address)).to.be.true;

// Check if user2 (who doesn't have a mod token) is not considered a mod
expect(await chatContract.isUserMod(user2.address)).to.be.false;

// Mint a mod token to user2 and check again
await modTokenContract.mint(user2.address);
expect(await chatContract.isUserMod(user2.address)).to.be.true;

// Change the minimum balance required to be a mod
await chatContract.connect(owner).setModMinBalance(2);

// Check if user2 (who now has 1 token) is no longer considered a mod
expect(await chatContract.isUserMod(user2.address)).to.be.false;

// Mint another token to user2 and check again
await modTokenContract.mint(user2.address);
expect(await chatContract.isUserMod(user2.address)).to.be.true;
});

});
27 changes: 26 additions & 1 deletion test/chat/commentsContextV1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe("CommentsContextV1", function () {
let modTokenContract;
let owner, user1, user2, user3;
let subjectAddress;
const price = ethers.utils.parseEther("0.0001");
const price = ethers.utils.parseEther("0.00001");

beforeEach(async function () {
[owner, user1, user2, user3] = await ethers.getSigners();
Expand Down Expand Up @@ -406,4 +406,29 @@ describe("CommentsContextV1", function () {
expect(await commentsContract.price()).to.equal(maxPrice);
});

it("correctly identifies mods and non-mods", async function () {
// Check if owner is considered a mod
expect(await commentsContract.isUserMod(owner.address)).to.be.true;

// Check if user1 (who has a mod token) is considered a mod
expect(await commentsContract.isUserMod(user1.address)).to.be.true;

// Check if user2 (who doesn't have a mod token) is not considered a mod
expect(await commentsContract.isUserMod(user2.address)).to.be.false;

// Mint a mod token to user2 and check again
await modTokenContract.mint(user2.address);
expect(await commentsContract.isUserMod(user2.address)).to.be.true;

// Change the minimum balance required to be a mod
await commentsContract.connect(owner).setModMinBalance(2);

// Check if user2 (who now has 1 token) is no longer considered a mod
expect(await commentsContract.isUserMod(user2.address)).to.be.false;

// Mint another token to user2 and check again
await modTokenContract.mint(user2.address);
expect(await commentsContract.isUserMod(user2.address)).to.be.true;
});

});

0 comments on commit 27cc1cb

Please sign in to comment.