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

chiranjeev13_NFT-AgeVerification #5

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
112 changes: 0 additions & 112 deletions README.md

This file was deleted.

11 changes: 11 additions & 0 deletions chiranjeev13_Projects/DAO-Governance/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
node_modules
.env
coverage
coverage.json
typechain
typechain-types

# Hardhat files
cache
artifacts

81 changes: 81 additions & 0 deletions chiranjeev13_Projects/DAO-Governance/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Governance Contract

This is the README file for the Governance contract deployed on the Polygon zkEVM testnet at address `0x0138A6124eCb4741058Faeef5e38C26638a4Ae26`.

## Introduction

The Governance contract is designed to facilitate decentralized governance within a system. It allows members to propose and vote on various proposals, as well as participate in the election of administrators. The contract provides functionality for adding members, creating proposals, voting on proposals, and contesting for administrator positions.

## Contract Structure

The Governance contract consists of the following main components:

1. **Members**: A struct representing the members of the governance system. It contains the member's public key, points, and role.

2. **Proposals**: A struct representing the proposals made within the governance system. Each proposal has a unique identifier, a title, an initiator, vote counts, percentages, status, and timestamp.

3. **AdminAllocations**: A struct representing the administrator contestants. It includes the contestant's identifier, address, name, vote counts, percentages, total votes, and status.

4. **AdminContestants**: An array storing all the admin contestants.

5. **Members**: An array storing all the members of the governance system.

6. **Proposals**: An array storing all the proposals made within the system.

7. **SortContestants**: An array used for sorting the admin contestants based on vote percentages.

8. **SortedValue**: An array storing the sorted values of the admin contestants.

9. **registered**: A mapping to keep track of registered members.

10. **isAdmin**: A mapping to determine if an address has admin privileges.

11. **voted**: A mapping to track if a member has voted on a specific proposal.

12. **adminVoted**: A mapping to track if a member has voted in the admin contest.

## Functionality

The Governance contract provides the following functionality:

1. **addMembers**: Allows new members to join the governance system by paying a membership fee.

2. **addProposal**: Enables registered members to create new proposals.

3. **votingOnProposals**: Allows registered members to vote on proposals.

4. **stopProposal**: Allows the proposal initiator or an admin to stop a proposal and calculate the vote percentages.

5. **contestForAdmin**: Allows registered members to contest for admin positions by paying a fee.

6. **voteAdmin**: Enables registered members to vote for admin contestants.

7. **closeVoting**: Closes the voting for admin contestants and calculates the vote percentages.

8. **setAdmin**: Sets the address with the highest vote percentage as the new admin.

## Deployment

The Governance contract has been deployed on the Polygon zkEVM testnet at address `0x0138A6124eCb4741058Faeef5e38C26638a4Ae26`. To interact with the contract, you can use any Ethereum-compatible tool or library.

## Usage

To utilize the Governance contract, follow these steps:

1. Start by adding members to the governance system. Call the `addMembers` function, ensuring that the membership fee of 0.002 ETH is paid. This function registers a new member with their address, assigns them a role as "member," and initializes their points as 0.

2. Create proposals within the governance system. Only registered members can create proposals. Use the `addProposal` function, providing the title of the proposal as a parameter. This function assigns a unique proposal ID, sets the initiator as the calling address, initializes the vote counts to 0, and marks the proposal as active.

3. Vote on proposals. Use the `votingOnProposals` function to cast your vote on a specific proposal. Provide the vote (0 for against, 1 for in favor) and the proposal ID as parameters. Ensure you are a registered member and haven't voted on the same proposal before.

4. Stop a proposal. If you are the initiator of a proposal or an admin, you can stop it using the `stopProposal` function. Pass the proposal ID as a parameter. This function calculates the voting percentages, marks the proposal as inactive, and prevents further voting.

5. Contest for admin positions. Registered members can participate in admin elections by using the `contestForAdmin` function. Pay the required fee of 0.003 ETH and provide your name as a parameter. This function adds you as a contestant with a unique contestant ID, associates your address and name, and sets the initial vote counts to 0.

6. Vote for admin contestants. Use the `voteAdmin` function to cast your vote for a specific admin contestant. Provide the contestant ID and your vote (0 for against, 1 for in favor) as parameters. Ensure you are a registered member and the contestant exists.

7. Close the voting for admin contestants. Call the `closeVoting` function to end the voting period. This function calculates the voting percentages for each admin contestant based on the received votes.

8. Set the new admin. Invoke the `setAdmin` function to determine the admin with the highest vote percentage. This address will be designated as the new admin.

Note: Ensure that you interact with the deployed Governance contract on the Polygon zkEVM testnet at address `0x0138A6124eCb4741058Faeef5e38C26638a4Ae26`. Use appropriate tools or libraries compatible with Ethereum for contract interaction.
183 changes: 183 additions & 0 deletions chiranjeev13_Projects/DAO-Governance/contracts/governance.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;


import "./sortUtils.sol";
contract Governance is QuickSort{
struct members {
address publicKey;
uint256 points;
string role;
}

struct proposals {
uint256 proposalId;
string title;
address initiator;
uint256 forVotes;
uint256 againstVotes;
uint256 forPercentage;
uint256 againstPercentage;
bool status;
uint256 timeInitiated;
uint256 totalVotes;
}

struct adminAllocations {
uint256 contestantId;
address candidate;
string name;
uint256 forVotes;
uint256 againstVotes;
uint256 forPercentage;
uint256 againstPercentage;
uint256 totalVotes;
bool status;
}

adminAllocations[] public adminContestants;
members[] public Members;
proposals[] public Proposals;
uint256[] public sortContestants;
uint[] public sortedValue;

uint256 _proposalId = 0;
uint256 _contestantId = 0;

mapping(address => bool) registered;
mapping(address => bool) isAdmin;
mapping(address => mapping(uint256 => bool)) voted;
mapping(address => mapping(uint256 => bool)) adminVoted;

//mapping(address => adminAllocations) adminContestants;

constructor() {
isAdmin[msg.sender] = true;
}

function addMembers() public payable returns (bool) {
require(registered[msg.sender] != true, "Already In");
require(msg.value == 2000000000000000, "e"); // 0.002 eth
Members.push(members(msg.sender, 0, "member"));
return true;
}

function addProposal(string memory title) public returns (bool) {
require(registered[msg.sender] == true, "Not registered");
Proposals.push(
proposals(
_proposalId,
title,
msg.sender,
0,
0,
0,
0,
true,
block.timestamp,
0
)
);
_proposalId++;
}

function votingOnProposals(
uint256 vote,
uint256 proposalId
)
public
returns (
bool // vote = 0-against 1-for
)
{
require(registered[msg.sender] == true, "Not registered");
require(
Proposals[proposalId].status == true,
"Proposal Not valid or ended"
);
require(voted[msg.sender][proposalId] == false, "Already Voted");
if (vote == 0) {
Proposals[proposalId].againstVotes++;
Proposals[proposalId].totalVotes++;
return true;
} else if (vote == 1) {
Proposals[proposalId].forVotes++;
Proposals[proposalId].totalVotes++;
return true;
}
voted[msg.sender][proposalId] == true;
}

function stopProposal(uint256 proposalId) public returns (bool) {
require(
Proposals[proposalId].initiator == msg.sender || isAdmin[msg.sender],
"Not the propsal initiator or admin"
);
Proposals[proposalId].forPercentage =
Proposals[proposalId].forVotes /
Proposals[proposalId].totalVotes;
Proposals[proposalId].againstPercentage =
Proposals[proposalId].againstVotes /
Proposals[proposalId].totalVotes;
Proposals[proposalId].status = false;
return true;
}

function contestForAdmin(string memory _name) public payable returns (bool) {
require(registered[msg.sender] == true, "Not Member");
require(msg.value == 3000000000000000, "Not enough");

adminContestants.push(
adminAllocations(_contestantId, msg.sender, _name, 0, 0, 0, 0, 0, true)
);
_contestantId++;
return true;
}

function voteAdmin(
uint256 _contestantId,
uint256 vote
) public returns (bool) {
require(registered[msg.sender] == true, "Not registered");
require(adminContestants[_contestantId].status == true, "Not existing");

if (vote == 1) {
adminContestants[_contestantId].forVotes++;
adminContestants[_contestantId].totalVotes++;
return true;
} else if (vote == 0) {
adminContestants[_contestantId].againstVotes++;
adminContestants[_contestantId].totalVotes++;
return true;
}
}

function closeVoting() public {
for (uint256 i = 0; i <= _contestantId; i++) {
adminContestants[i].forPercentage =
adminContestants[i].forVotes /
adminContestants[i].totalVotes;
adminContestants[i].againstPercentage =
adminContestants[i].againstVotes /
adminContestants[i].totalVotes;
}

setAdmin();
}

function setAdmin() public {
for (uint256 i = 0; i <= _contestantId; i++) {
sortContestants.push(adminContestants[i].forPercentage);
}
sortedValue = sort(sortContestants); //top votes


for (uint256 i = 0; i <= _contestantId; i++) {
if(sortedValue[_contestantId]==adminContestants[i].forPercentage)
{
isAdmin[adminContestants[i].candidate] = true; // admin set
break;
}
}
}
}
Loading