-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create pi_network_module_3: Governance.sol
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
pi_network_smart_contract_architecture/pi_network_module_3: Governance.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,61 @@ | ||
pragma solidity ^0.8.0; | ||
|
||
import "./AccessControl.sol"; | ||
|
||
contract Governance { | ||
using AccessControl for address; | ||
|
||
// Mapping of proposals | ||
mapping (uint256 => Proposal) public proposals; | ||
|
||
// Struct to represent a proposal | ||
struct Proposal { | ||
uint256 id; | ||
address proposer; | ||
string description; | ||
uint256 voteCount; | ||
bool executed; | ||
} | ||
|
||
// Event emitted when a new proposal is created | ||
event NewProposal(uint256 indexed proposalId, address indexed proposer, string description); | ||
|
||
// Event emitted when a user votes on a proposal | ||
event Vote(uint256 indexed proposalId, address indexed voter, bool support); | ||
|
||
// Event emitted when a proposal is executed | ||
event ExecuteProposal(uint256 indexed proposalId); | ||
|
||
// Function to propose a new change to the Pi Network | ||
function propose(string memory _description) public onlyDeveloper { | ||
uint256 proposalId = uint256(keccak256(abi.encodePacked(block.timestamp, msg.sender))); | ||
Proposal storage proposal = proposals[proposalId]; | ||
proposal.id = proposalId; | ||
proposal.proposer = msg.sender; | ||
proposal.description = _description; | ||
proposal.voteCount = 0; | ||
proposal.executed = false; | ||
emit NewProposal(proposalId, msg.sender, _description); | ||
} | ||
|
||
// Function to vote on a proposal | ||
function vote(uint256 _proposalId, bool _support) public { | ||
Proposal storage proposal = proposals[_proposalId]; | ||
require(proposal.id != 0, "Proposal does not exist"); | ||
require(!proposal.executed, "Proposal has already been executed"); | ||
proposal.voteCount += 1; | ||
emit Vote(_proposalId, msg.sender, _support); | ||
} | ||
|
||
// Function to execute a proposal that has received sufficient votes | ||
function executeProposal(uint256 _proposalId) public onlyAdmin { | ||
Proposal storage proposal = proposals[_proposalId]; | ||
require(proposal.id != 0, "Proposal does not exist"); | ||
require(!proposal.executed, "Proposal has already been executed"); | ||
require(proposal.voteCount > 50, "Proposal has not received sufficient votes"); | ||
proposal.executed = true; | ||
emit ExecuteProposal(_proposalId); | ||
// Execute the proposal logic here | ||
// ... | ||
} | ||
} |