Skip to content

Commit

Permalink
update with smart contract part
Browse files Browse the repository at this point in the history
  • Loading branch information
leeduckgo committed Oct 16, 2023
1 parent a618d34 commit 1f27745
Show file tree
Hide file tree
Showing 14 changed files with 351 additions and 118 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Visit your app on: `http://localhost:3000`. You can interact with your smart con

Run smart contract test with `yarn hardhat:test`

- Edit your smart contract `YourContract.sol` in `packages/hardhat/contracts`
- Edit your smart contract `VectorDBProposalGovernancer.sol` in `packages/hardhat/contracts`
- Edit your frontend in `packages/nextjs/pages`
- Edit your deployment scripts in `packages/hardhat/deploy`
## Reference
Expand Down
117 changes: 117 additions & 0 deletions packages/hardhat/contracts/VectorDBProposalGovernancer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

/**
* @title Ballot
* @dev Implements voting process along with vote delegation
*/
contract YourContract {

struct Voter {
uint weight; // weight is accumulated by delegation
bool voted; // if true, that person already voted
address delegate; // person delegated to
uint vote; // index of the voted proposal
}

struct Proposal {
// If you can limit the length to a certain number of bytes,
// always use one of bytes1 to bytes32 because they are much cheaper
string title; // title for vector dataset item
string content; // a link to the proposal content
address proposaler;
uint voteCount; // number of accumulated votes

}

address public chairperson;

mapping(address => Voter) public voters;
mapping(uint => Proposal) public proposals;

uint32 proposal_counter = 0;

constructor() {
chairperson = msg.sender;
voters[chairperson].weight = 1;
}

function createProposal(string memory _title, string memory _content) public {

proposals[proposal_counter].title = _title;
proposals[proposal_counter].content = _content;
proposals[proposal_counter].proposaler = msg.sender;
proposals[proposal_counter].voteCount = 0;

proposal_counter +=1;
}


function getProposal(uint32 id) public view returns (Proposal memory) {
return proposals[id];
}

/**
* @dev Give 'voter' the right to vote on this ballot. May only be called by 'chairperson'.
* @param voter address of voter
*/
function giveRightToVote(address voter) public {
require(
msg.sender == chairperson,
"Only chairperson can give right to vote."
);
require(
!voters[voter].voted,
"The voter already voted."
);
require(voters[voter].weight == 0);
voters[voter].weight = 1;
}

/**
* @dev Delegate your vote to the voter 'to'.
* @param to address to which vote is delegated
*/
function delegate(address to) public {
Voter storage sender = voters[msg.sender];
require(!sender.voted, "You already voted.");
require(to != msg.sender, "Self-delegation is disallowed.");

while (voters[to].delegate != address(0)) {
to = voters[to].delegate;

// We found a loop in the delegation, not allowed.
require(to != msg.sender, "Found loop in delegation.");
}
sender.voted = true;
sender.delegate = to;
Voter storage delegate_ = voters[to];
if (delegate_.voted) {
// If the delegate already voted,
// directly add to the number of votes
proposals[delegate_.vote].voteCount += sender.weight;
} else {
// If the delegate did not vote yet,
// add to her weight.
delegate_.weight += sender.weight;
}
}

/**
* @dev Give your vote (including votes delegated to you) to proposal 'proposals[proposal].name'.
* @param proposal index of proposal in the proposals array
*/
function vote(uint proposal) public {
Voter storage sender = voters[msg.sender];
require(sender.weight != 0, "Has no right to vote");
require(!sender.voted, "Already voted.");
sender.voted = true;
sender.vote = proposal;

// If 'proposal' is out of the range of the array,
// this will throw automatically and revert all
// changes.
proposals[proposal].voteCount += sender.weight;
}
}
87 changes: 0 additions & 87 deletions packages/hardhat/contracts/YourContract.sol

This file was deleted.

2 changes: 1 addition & 1 deletion packages/hardhat/deploy/00_deploy_your_contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const deployYourContract: DeployFunction = async function (hre: HardhatRuntimeEn
await deploy("YourContract", {
from: deployer,
// Contract constructor arguments
args: [deployer],
args: [],
log: true,
// autoMine: can be passed to the deploy function to make the deployment process faster on local networks by
// automatically mining the contract deployment transaction. There is no effect on live networks.
Expand Down
18 changes: 9 additions & 9 deletions packages/hardhat/test/YourContract.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { expect } from "chai";
import { ethers } from "hardhat";
import { YourContract } from "../typechain-types";
import { VectorDBProposalGovernancer } from "../typechain-types";

describe("YourContract", function () {
describe("VectorDBProposalGovernancer", function () {
// We define a fixture to reuse the same setup in every test.

let yourContract: YourContract;
let VectorDBProposalGovernancer: VectorDBProposalGovernancer;
before(async () => {
const [owner] = await ethers.getSigners();
const yourContractFactory = await ethers.getContractFactory("YourContract");
yourContract = (await yourContractFactory.deploy(owner.address)) as YourContract;
await yourContract.deployed();
const VectorDBProposalGovernancerFactory = await ethers.getContractFactory("VectorDBProposalGovernancer");
VectorDBProposalGovernancer = (await VectorDBProposalGovernancerFactory.deploy(owner.address)) as VectorDBProposalGovernancer;
await VectorDBProposalGovernancer.deployed();
});

describe("Deployment", function () {
it("Should have the right message on deploy", async function () {
expect(await yourContract.greeting()).to.equal("Building Unstoppable Apps!!!");
expect(await VectorDBProposalGovernancer.greeting()).to.equal("Building Unstoppable Apps!!!");
});

it("Should allow setting a new message", async function () {
const newGreeting = "Learn Scaffold-ETH 2! :)";

await yourContract.setGreeting(newGreeting);
expect(await yourContract.greeting()).to.equal(newGreeting);
await VectorDBProposalGovernancer.setGreeting(newGreeting);
expect(await VectorDBProposalGovernancer.greeting()).to.equal(newGreeting);
});
});
});
1 change: 1 addition & 0 deletions packages/nextjs/.env.example → packages/nextjs/.env
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
# More info: https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables
NEXT_PUBLIC_ALCHEMY_API_KEY=
NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID=
NEXT_PUBLIC_IGNORE_BUILD_ERROR="true"
18 changes: 9 additions & 9 deletions packages/nextjs/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ interface HeaderMenuLink {
}

export const menuLinks: HeaderMenuLink[] = [
// {
// label: "Home",
// href: "/",
// },
// {
// label: "Debug Contracts",
// href: "/debug",
// icon: <BugAntIcon className="h-4 w-4" />,
// },
{
label: "Home",
href: "/",
},
{
label: "vectorDBProposalGovernancer",
href: "/debug",
icon: <BugAntIcon className="h-4 w-4" />,
},
// {
// label: "Example UI",
// href: "/example-ui",
Expand Down
12 changes: 6 additions & 6 deletions packages/nextjs/components/example-ui/ContractData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ export const ContractData = () => {
const greetingRef = useRef<HTMLDivElement>(null);

const { data: totalCounter } = useScaffoldContractRead({
contractName: "YourContract",
contractName: "VectorDBProposalGovernancer",
functionName: "totalCounter",
});

const { data: currentGreeting, isLoading: isGreetingLoading } = useScaffoldContractRead({
contractName: "YourContract",
contractName: "VectorDBProposalGovernancer",
functionName: "greeting",
});

useScaffoldEventSubscriber({
contractName: "YourContract",
contractName: "VectorDBProposalGovernancer",
eventName: "GreetingChange",
listener: logs => {
logs.map(log => {
Expand All @@ -46,7 +46,7 @@ export const ContractData = () => {
isLoading: isLoadingEvents,
error: errorReadingEvents,
} = useScaffoldEventHistory({
contractName: "YourContract",
contractName: "VectorDBProposalGovernancer",
eventName: "GreetingChange",
fromBlock: process.env.NEXT_PUBLIC_DEPLOY_BLOCK ? BigInt(process.env.NEXT_PUBLIC_DEPLOY_BLOCK) : 0n,
filters: { greetingSetter: address },
Expand All @@ -55,8 +55,8 @@ export const ContractData = () => {

console.log("Events:", isLoadingEvents, errorReadingEvents, myGreetingChangeEvents);

const { data: yourContract } = useScaffoldContract({ contractName: "YourContract" });
console.log("yourContract: ", yourContract);
const { data: VectorDBProposalGovernancer } = useScaffoldContract({ contractName: "VectorDBProposalGovernancer" });
console.log("VectorDBProposalGovernancer: ", VectorDBProposalGovernancer);

const { showAnimation } = useAnimationConfig(totalCounter);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const ContractInteraction = () => {
const [newGreeting, setNewGreeting] = useState("");

const { writeAsync, isLoading } = useScaffoldContractWrite({
contractName: "YourContract",
contractName: "VectorDBProposalGovernancer",
functionName: "setGreeting",
args: [newGreeting],
value: parseEther("0.01"),
Expand Down
Loading

0 comments on commit 1f27745

Please sign in to comment.