From a3bd480ec5e2afa9e9ab47cb8932a4ea06db3c7d Mon Sep 17 00:00:00 2001 From: Dennis Anim <150798169+theGeniusDennis@users.noreply.github.com> Date: Fri, 23 Aug 2024 08:46:08 +0000 Subject: [PATCH] Election factory (#24) * 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 --- .vscode/settings.json | 3 +- blockchain/contracts/ElectionFactory.sol | 63 ++++++++++++++++++++++++ blockchain/package.json | 2 +- 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 blockchain/contracts/ElectionFactory.sol diff --git a/.vscode/settings.json b/.vscode/settings.json index ff30c44..687923d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,4 @@ { - "editor.tabSize": 2 + "editor.tabSize": 2, + "solidity.compileUsingRemoteVersion": "v0.8.26+commit.8a97fa7a" } \ No newline at end of file diff --git a/blockchain/contracts/ElectionFactory.sol b/blockchain/contracts/ElectionFactory.sol new file mode 100644 index 0000000..3139c2e --- /dev/null +++ b/blockchain/contracts/ElectionFactory.sol @@ -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; + } +} diff --git a/blockchain/package.json b/blockchain/package.json index 1d8f7a1..37c1070 100644 --- a/blockchain/package.json +++ b/blockchain/package.json @@ -15,6 +15,6 @@ "description": "", "devDependencies": { "@nomicfoundation/hardhat-toolbox": "^5.0.0", - "hardhat": "^2.22.8" + "hardhat": "^2.22.9" } }