Skip to content

Instantly share code, notes, and snippets.

@santhosh-reddy-6404
Last active February 17, 2022 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save santhosh-reddy-6404/87ddd39af0833b69e8cc838f54afa099 to your computer and use it in GitHub Desktop.
Save santhosh-reddy-6404/87ddd39af0833b69e8cc838f54afa099 to your computer and use it in GitHub Desktop.
/* Author : Santhosh Reddy */
//SPDX-License-Identifier: MIT
pragma solidity 0.8.*;
contract smartCityGov {
/* All required properties (variable, structs, mappings,
enums, events and modifiers) */
address addr = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;
address payable public mayor = payable(addr);
address adminAddr = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;
address public aadharAdmin = payable(adminAddr);
uint8 public population;
uint8 public totalTaxTxs;
uint8 public totalActivities;
function treasury() public view returns(uint) {
return address(this).balance;
}
struct person {
string name;
uint8 age;
uint256 tax;
}
mapping(address => person) citizens;
struct activity {
string title;
string description;
uint32 funds;
uint8 upvotes;
uint8 downvotes;
address[] voters;
activityStatus status;
uint256 time;
}
mapping(uint8 => activity) activities;
enum activityStatus{pending, approved, failed}
event taxPayed(address name, uint256 amount);
event voted(string activityTitle, address citizen);
event activityProposedOrApproved(uint8 id);
modifier onlyMayor() {
require (msg.sender == mayor, "You aren't the mayor");
_;
}
modifier onlyCitizen(address addrs) {
require (citizens[addrs].age != 0, "You aren't a citizen of this city");
_;
}
modifier onlyAadharAdmin() {
require (msg.sender == aadharAdmin, "You aren't the aadharAdmin of this city");
_;
}
modifier toVote(uint8 id) {
bool a = true;
for(uint8 i=0; i<activities[id].voters.length; i++) {
if(activities[id].voters[i]==msg.sender) {
a = false;
}
}
require(a == true, "You have already voted this activity");
_;
}
modifier approved(uint8 id) {
require(block.timestamp - activities[id].time > 86400, "The voting time period hasn't completed");
require(activities[id].upvotes > activities[id].downvotes, "The activity has failed due to majority of protesters");
_;
if(block.timestamp - activities[id].time > 86400 && activities[id].downvotes > activities[id].upvotes) {
activities[id].status = activityStatus.failed;
revert("The activity has failed due to majority of protesters");
}
}
/* Functions of aadharAdmin */
function addCitizen(address payable addrs, string memory name, uint8 age) public onlyAadharAdmin {
require(citizens[addrs].age == 0);
require(age != 0);
citizens[addrs] = person(name, age, 0);
population = population+1;
}
function readCitizen(address payable addrs) public view onlyAadharAdmin returns(person memory) {
return citizens[addrs];
}
function removeCitizen(address payable addrs) public onlyAadharAdmin {
delete citizens[addrs];
population = population-1;
}
/* Functions of Mayor */
function proposeActivity(string memory title, string memory description, uint32 funds) public onlyMayor {
address[] memory voters;
totalActivities++;
activities[totalActivities] = activity(title, description, funds, 0, 0, voters, activityStatus.pending, block.timestamp);
emit activityProposedOrApproved(totalActivities);
}
function getFunds(uint8 id) external approved(id) onlyMayor {
activities[id].status = activityStatus.approved;
payable(msg.sender).transfer(activities[id].funds);
}
function imposeTax(address citizen,uint256 tax) public onlyMayor {
citizens[citizen].tax = citizens[citizen].tax + tax;
}
/* Functions of Citizens*/
function payTax() public payable onlyCitizen(msg.sender) {
require(msg.value <= citizens[msg.sender].tax);
citizens[msg.sender].tax = citizens[msg.sender].tax - msg.value;
emit taxPayed(msg.sender, msg.value);
totalTaxTxs++;
payable(msg.sender).transfer(msg.value);
}
function voteActivity(uint8 id, bool vote) public onlyCitizen(msg.sender) toVote(id) {
activities[id].voters.push(msg.sender);
if(vote == true) {
activities[id].upvotes++;
} else {
activities[id].downvotes++;
}
emit voted(activities[id].title, msg.sender);
}
function myDetails() public view onlyCitizen(msg.sender) returns(person memory) {
return citizens[msg.sender];
}
/* Public Functions */
function getActivities(uint8 id) public view returns(activity memory) {
return activities[id];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment