A smart contract for a Smart City Government (Solidity)

A smart contract for a Smart City Government (Solidity)

👋 Hello world! This is Santhosh Reddy and I'm back again with a solidity smart contract project. The theme of the smart contract is, A Smart City Government and Administration.

Introduction

This smart contract created with Solidity is a sample that depicts how the government administration can work in a more transparent and secure way using a futuristic technology - Blockchain.

Reading the smart contract code helps new blockchain learners as this covers all the important concepts like structs, mappings, events, enums, modifiers, functions, payable functions etc...

Here is the solidity code of the smart contract...

Explanation

Now, I'm going to explain the functionalities of this smart contract and how it was built. Let's start...

Mayor and aadharAdmin

Mayor is the main administrator of the city, whereas the aadharAdmin is a character who has the responsibility of managing the citizens data (CRUD). These two characters are declared with a public addressees.

Structs & Mappings

The main data objects are Citizens and Activities. Citizens is like a database of people in the city and is created by mapping struct person (name, age, tax due) with address keys.

Activities are the data of activities initiated by the Mayor to govern & develop the city and is created by mapping struct activity (title, description, funds required, upvotes, downvotes, voters list, current status & time) with uint8 key.

Enums

The enum activityStatus has the elements - pending, approved & failed. The status in struct activity is created with this enum and will be updated in required functions.

Events

The 3 events created in this contract are taxPayed(address name, uint8 amount) which emits when a citizen pays an amount of tax, voted(string activityTitle, address citizen) which emits when a citizen votes and activity and activityProposedOrApproved(uint8 id) which emits when an activity is either proposed by the Mayor or approved under certain conditions.

Modifiers

The restricting modifiers used in the contract are, onlyMayor which restricts some functions only to the Mayor of the city, onlyAadharAdmin which restricts few functions only to the aadharAdmin, onlyCitizen which restricts some specific functions only to the people living in the city.

The other two modifiers are toVote which modifies the voting function of the citizens to prevent the duplication of votes and approved which checks the conditions to be met for making an activity approved and modifies the funds receiving function of the Mayor.

As per my knowledge, there is no much difference between using modifier() for a function and using require() directly a function, even in terms of gas fee.

The function revert() is used to throw an error message, when something failed.

functions

Let's discuss the functions in the smart contract separately for each character.

The functions of an aadharAdmin are, addCitizen which adds a citizen to the citizens' data in the contract, readCitizen which reads the data of every citizen by giving the citizen's address and removeCitizen which deletes a citizen from the citizens' data. All these functions are restricted to aadharAdmin.

The functions of the Mayor are, imposeTax which increments the tax due of the specified citizen address by the specified tax amount, proposeActivity which proposes an activity by adding the data (title, description, funds) to the activities data in the contract and the last function is getFunds which checks the required conditions to be met and transfers the ethers to the Mayor's wallet and sets the status of activity to approved if the conditions are met.

The conditions are a minimum of 24 hours (886400 sec) should be given as the voting time to the citizens and the main condition is that upvotes should be more than the downvotes. If the 24 hours is crossed and if the downvotes are more than upvotes, the status of the activity sets to failed.

The functions of Citizens are, payTax which pays the specified amount (=<tax due) to the government (here it's the smart contract), voteActivity which allows the citizen to vote a specified activity and a specified vote (boolean) and the last function is myDetails which shows the details of that citizen.

Payable functions

The only payable function is payTax which will be called by citizens to pay the tax amount to the smart contract using call(). The other function getFunds which isn't technically payable but transfers ethers from the smart contract to the Mayor using transfer() under certain conditions.

Public Functions & Variables

The public function in the smart contract which can be called by anyone is getActivities which returns the details of the specified activity.

The public variables are population population of the city, totalTaxTxs total number of tax transactions made till date, totalActivities total number of activities proposed till date and treasury the available funds of the government (smart contract balance).

Conclusion

Finally, smart-city-gov.sol is ready! This is my solidity smart contract project created to increase my experience and expertise around blockchain development.

If any blockchain expert or who is good at blockchain is reading this, I want to hear a small judgement from your side in the comments section and comment section is open to report bugs in the code.

If any blockchain learner reading this article, I hope reading this code helps you understand the smart contract coding.

Thanks for reading and for now, taking a leave!

👋 Bye...!