Skip to content

Commit

Permalink
Election factory (#24)
Browse files Browse the repository at this point in the history
* Created ElectionFactory.sol file

* Created the basic functions for the Election Factory contract

* Created a function to delete an election

* Created an onlyOwner modifier

* Done with the ElectionFactory.sol file

* Updated code

* Changd the indentation of the file
  • Loading branch information
theGeniusDennis authored Aug 23, 2024
1 parent 649ec86 commit a3bd480
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"editor.tabSize": 2
"editor.tabSize": 2,
"solidity.compileUsingRemoteVersion": "v0.8.26+commit.8a97fa7a"
}
63 changes: 63 additions & 0 deletions blockchain/contracts/ElectionFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "./Election.sol";

contract ElectionFactory {
address owner;

//Array to keep track of the elections
address[] elections;

constructor (){
owner = msg.sender;
}

modifier onlyOwner() {
require(msg.sender == owner, "You are not the owner");
_;
}

//Event emitted when an election is created
event ElectionCreated(address electionAddress);

//Function to create new election
function createElection(
string memory _title,
string memory _description,
bool _isPublic,
uint _startDate,
uint _endDate
) public {
//Create a new instance of the Election contract
Election newElection = new Election(
_title,
_description,
_isPublic,
_startDate,
_endDate
);

//Store the address of the newly created election
elections.push(address(newElection));

//Emit an event for the creation of the new contract
emit ElectionCreated(address(newElection));
}


//function to get addresses of all elections
function getElections() public view returns (address[] memory){
return elections;
}

//function to delete an election
function deleteElection(uint _electionID) public onlyOwner {
delete elections[_electionID];
}

//Function to retrieve the total number of elections
function getTotalElections() public view returns (uint) {
return elections.length;
}
}
2 changes: 1 addition & 1 deletion blockchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
"description": "",
"devDependencies": {
"@nomicfoundation/hardhat-toolbox": "^5.0.0",
"hardhat": "^2.22.8"
"hardhat": "^2.22.9"
}
}

0 comments on commit a3bd480

Please sign in to comment.