Skip to content

Commit

Permalink
version 1
Browse files Browse the repository at this point in the history
  • Loading branch information
malerba118 committed Feb 14, 2022
1 parent 4946912 commit 622e46e
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ typechain
#Hardhat files
cache
artifacts

36 changes: 36 additions & 0 deletions contracts/Comments.sol
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);
}
}
21 changes: 21 additions & 0 deletions hardhat.config.js
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",
};
40 changes: 32 additions & 8 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,28 @@ import { QueryClient, QueryClientProvider } from "react-query";
import { ChakraProvider, Box, Heading } from "@chakra-ui/react";
import { Toaster } from "react-hot-toast";
import theme from "../theme";
import { Provider as WagmiProvider, InjectedConnector, chain } from "wagmi";
import { providers } from "ethers";

// Chains for connectors to support
const chains = [chain.polygonTestnetMumbai, chain.hardhat];

// Provide a fallback network while chainId is not yet defined
const provider = ({ chainId, connector }) => {
const selectedChain = connector?.chains.find((c) => c.id === chainId);
return providers.getDefaultProvider(
selectedChain?.rpcUrls?.[0] || "http://localhost:8545"
);
};

// Enables sign-in with Metamask
const connectors = () => {
return [
new InjectedConnector({
chains,
}),
];
};

// Create a client
const queryClient = new QueryClient({
Expand All @@ -16,14 +38,16 @@ const queryClient = new QueryClient({

const App: NextPage = () => {
return (
<ChakraProvider theme={theme}>
<QueryClientProvider client={queryClient}>
<Box p={8} maxW="600px" minW="320px" m="0 auto">
<Heading>Oops, no comments yet!</Heading>
<Toaster position="bottom-right" />
</Box>
</QueryClientProvider>
</ChakraProvider>
<WagmiProvider autoConnect provider={provider} connectors={connectors}>
<ChakraProvider theme={theme}>
<QueryClientProvider client={queryClient}>
<Box p={8} maxW="600px" minW="320px" m="0 auto">
<Heading>Oops, no comments yet!</Heading>
<Toaster position="bottom-right" />
</Box>
</QueryClientProvider>
</ChakraProvider>
</WagmiProvider>
);
};

Expand Down
38 changes: 38 additions & 0 deletions scripts/deploy-and-seed.js
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);
});
30 changes: 30 additions & 0 deletions test/basic.js
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);
});
});

0 comments on commit 622e46e

Please sign in to comment.