generated from scaffold-eth/scaffold-eth-2
-
Notifications
You must be signed in to change notification settings - Fork 7
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
Showing
14 changed files
with
351 additions
and
118 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
117 changes: 117 additions & 0 deletions
117
packages/hardhat/contracts/VectorDBProposalGovernancer.sol
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,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; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -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); | ||
}); | ||
}); | ||
}); |
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
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
Oops, something went wrong.