forked from pointer-gg/comments-with-polygon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4946912
commit 622e46e
Showing
6 changed files
with
158 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,3 +45,4 @@ typechain | |
#Hardhat files | ||
cache | ||
artifacts | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//SPDX-License-Identifier: Unlicense | ||
pragma solidity ^0.8.0; | ||
|
||
import "hardhat/console.sol"; | ||
|
||
contract Comments { | ||
struct Comment { | ||
uint32 id; | ||
string topic; | ||
address creator_address; | ||
string message; | ||
uint created_at; | ||
} | ||
|
||
uint32 private idCounter; | ||
mapping(string => Comment[]) private commentsByTopic; | ||
|
||
event CommentAdded(Comment comment); | ||
|
||
function getComments(string calldata topic) public view returns(Comment[] memory) { | ||
return commentsByTopic[topic]; | ||
} | ||
|
||
function addComment(string calldata topic, string calldata message) public { | ||
Comment memory comment = Comment({ | ||
id: idCounter, | ||
topic: topic, | ||
creator_address: msg.sender, | ||
message: message, | ||
created_at: block.timestamp | ||
}); | ||
commentsByTopic[topic].push(comment); | ||
idCounter++; | ||
emit CommentAdded(comment); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require("@nomiclabs/hardhat-waffle"); | ||
|
||
// This is a sample Hardhat task. To learn how to create your own go to | ||
// https://hardhat.org/guides/create-task.html | ||
task("accounts", "Prints the list of accounts", async (taskArgs, hre) => { | ||
const accounts = await hre.ethers.getSigners(); | ||
|
||
for (const account of accounts) { | ||
console.log(account.address); | ||
} | ||
}); | ||
|
||
// You need to export an object to set up your config | ||
// Go to https://hardhat.org/config/ to learn more | ||
|
||
/** | ||
* @type import('hardhat/config').HardhatUserConfig | ||
*/ | ||
module.exports = { | ||
solidity: "0.8.4", | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// We require the Hardhat Runtime Environment explicitly here. This is optional | ||
// but useful for running the script in a standalone fashion through `node <script>`. | ||
// | ||
// When running the script with `npx hardhat run <script>` you'll find the Hardhat | ||
// Runtime Environment's members available in the global scope. | ||
const hre = require("hardhat"); | ||
|
||
async function main() { | ||
// Hardhat always runs the compile task when running scripts with its command | ||
// line interface. | ||
// | ||
// If this script is run directly using `node` you may want to call compile | ||
// manually to make sure everything is compiled | ||
// await hre.run('compile'); | ||
|
||
// We get the contract to deploy | ||
const CommentsContract = await hre.ethers.getContractFactory("Comments"); | ||
const contract = await CommentsContract.deploy(); | ||
|
||
await contract.deployed(); | ||
|
||
const tx1 = await contract.addComment("my-blog-post", "My first comment"); | ||
await tx1.wait(); | ||
|
||
const tx2 = await contract.addComment("my-blog-post", "My second comment"); | ||
await tx2.wait(); | ||
|
||
console.log("Contract deployed to:", contract.address); | ||
} | ||
|
||
// We recommend this pattern to be able to use async/await everywhere | ||
// and properly handle errors. | ||
main() | ||
.then(() => process.exit(0)) | ||
.catch((error) => { | ||
console.error(error); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const { expect } = require("chai"); | ||
const { ethers } = require("hardhat"); | ||
|
||
describe("Comments", function () { | ||
it("Should add and fetch successfully", async function () { | ||
const Comments = await ethers.getContractFactory("Comments"); | ||
const comments = await Comments.deploy(); | ||
await comments.deployed(); | ||
|
||
expect(await comments.getComments("my-first-blog-post")).to.be.lengthOf(0); | ||
|
||
const tx1 = await comments.addComment( | ||
"my-first-blog-post", | ||
"my first comment" | ||
); | ||
await tx1.wait(); | ||
|
||
expect(await comments.getComments("my-first-blog-post")).to.be.lengthOf(1); | ||
expect(await comments.getComments("my-second-blog-post")).to.be.lengthOf(0); | ||
|
||
const tx2 = await comments.addComment( | ||
"my-second-blog-post", | ||
"this comment is on a different thread" | ||
); | ||
await tx2.wait(); | ||
|
||
expect(await comments.getComments("my-first-blog-post")).to.be.lengthOf(1); | ||
expect(await comments.getComments("my-second-blog-post")).to.be.lengthOf(1); | ||
}); | ||
}); |