Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BT-564 map topic to string #108

Merged
merged 8 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
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
26 changes: 26 additions & 0 deletions contracts/TopicIdMapping.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.17;

import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";

/// @notice This contract is used to map a claim topic id to its name
/** @dev this contract stores and returns the names for different topics
*/
contract TopicIdMapping is Ownable {
mapping(uint256 => string) public topicToName;

/// @notice Saves the name for a given topic
function setTopicName(
uint256 _topic,
string memory _name
) external onlyOwner {
topicToName[_topic] = _name;
}

/// @notice Returns the name for a given topic
function getTopicName(
uint256 _topic
) external view returns (string memory _name) {
return topicToName[_topic];
}
}
46 changes: 46 additions & 0 deletions test/topic-id-mapping/topic-id-mapping.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { ethers } from "hardhat";
import { expect } from "chai";

describe("TopicIdMapping", () => {
const TEST_TOPIC_ID = 10101000100000;
const TEST_TOPIC_NAME = "INDIVIDUAL_INVESTOR";

it("should deploy", async () => {
await ethers.getSigners();
const topicIdMapping = await ethers.deployContract("TopicIdMapping", []);
await expect(topicIdMapping.deployed()).to.eventually.be.ok;
});

it("deployer should be owner", async () => {
const [deployerWallet] = await ethers.getSigners();
const topicIdMapping = await ethers.deployContract("TopicIdMapping", []);
await expect(topicIdMapping.owner()).to.eventually.equal(
deployerWallet.address
);
});

it("only owner can add/modify topic", async () => {
const [deployerWallet, otherWallet] = await ethers.getSigners();
const topicIdMapping = await ethers.deployContract("TopicIdMapping", []);
await expect(
topicIdMapping.connect(otherWallet).setTopicName(TEST_TOPIC_ID, TEST_TOPIC_NAME)
).to.be.revertedWith("Ownable: caller is not the owner");

await expect(
topicIdMapping
.connect(deployerWallet)
.setTopicName(TEST_TOPIC_ID, TEST_TOPIC_NAME)
).to.be.ok;
});

it("should set and get topic content correctly", async () => {
const [deployerWallet] = await ethers.getSigners();
const topicIdMapping = await ethers.deployContract("TopicIdMapping", []);
await topicIdMapping
.connect(deployerWallet)
.setTopicName(TEST_TOPIC_ID, TEST_TOPIC_NAME);
await expect(
topicIdMapping.getTopicName(TEST_TOPIC_ID)
).to.eventually.equal(TEST_TOPIC_NAME);
});
});
Loading