Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions VotingDapp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
node_modules
.env

# Hardhat files
/cache
/artifacts

# TypeChain files
/typechain
/typechain-types

# solidity-coverage files
/coverage
/coverage.json

# Hardhat Ignition default folder for deployments against a local node
ignition/deployments/chain-31337
13 changes: 13 additions & 0 deletions VotingDapp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Sample Hardhat Project

This project demonstrates a basic Hardhat use case. It comes with a sample contract, a test for that contract, and a Hardhat Ignition module that deploys that contract.

Try running some of the following tasks:

```shell
npx hardhat help
npx hardhat test
REPORT_GAS=true npx hardhat test
npx hardhat node
npx hardhat ignition deploy ./ignition/modules/Lock.js
```
34 changes: 34 additions & 0 deletions VotingDapp/contracts/Lock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.27;

// Uncomment this line to use console.log
// import "hardhat/console.sol";

contract Lock {
uint public unlockTime;
address payable public owner;

event Withdrawal(uint amount, uint when);

constructor(uint _unlockTime) payable {
require(
block.timestamp < _unlockTime,
"Unlock time should be in the future"
);

unlockTime = _unlockTime;
owner = payable(msg.sender);
}

function withdraw() public {
// Uncomment this line, and the import of "hardhat/console.sol", to print a log in your terminal
// console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp);

require(block.timestamp >= unlockTime, "You can't withdraw yet");
require(msg.sender == owner, "You aren't the owner");

emit Withdrawal(address(this).balance, block.timestamp);

owner.transfer(address(this).balance);
}
}
66 changes: 66 additions & 0 deletions VotingDapp/contracts/Voting.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.27;

contract Voting {
struct Voter {
bool isRegistered;
bool hasVoted;
uint vote;
}

struct Candidate {
string name;
uint voteCount;
}

address public admin;
mapping(address => Voter) public voters;
Candidate[] public candidates;
uint256 public c = 0;
uint256 public num = 100;

constructor() {
admin = msg.sender;
}

modifier onlyAdmin() {
require(msg.sender == admin, "Only admin can call this.");
_;
}

function getCount() public view returns (uint256) {
return num;
}


function addCandidate(string memory candidateName) public onlyAdmin {
c = c+1;
candidates.push(Candidate({
name: candidateName,
voteCount: 0
}));
}

function registerVoter(address voterAddress) public onlyAdmin {
require(!voters[voterAddress].isRegistered, "Voter already registered.");
voters[voterAddress].isRegistered = true;
voters[voterAddress].hasVoted = false;
}

function vote(uint candidateId) public {
require(voters[msg.sender].isRegistered, "You are not a registered voter.");
require(!voters[msg.sender].hasVoted, "You have already voted.");
require(candidateId < candidates.length, "Candidate does not exist.");

voters[msg.sender].hasVoted = true;
voters[msg.sender].vote = candidateId;
candidates[candidateId].voteCount += 1;
}

function getVoteCount(uint candidateId) public view returns (uint) {
require(candidateId < candidates.length, "Candidate does not exist.");
return candidates[candidateId].voteCount;
}


}
11 changes: 11 additions & 0 deletions VotingDapp/hardhat.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require("@nomiclabs/hardhat-ethers");

module.exports = {
networks: {
goerli: {
url: `https://eth-goerli.g.alchemy.com/v2/nH3RNEH5wOfGy6DjMi5k70oEG4aCkisF`,
accounts: [`0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80`]
},
},
solidity: "0.8.27",
};
18 changes: 18 additions & 0 deletions VotingDapp/ignition/modules/Lock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// This setup uses Hardhat Ignition to manage smart contract deployments.
// Learn more about it at https://hardhat.org/ignition

const { buildModule } = require("@nomicfoundation/hardhat-ignition/modules");

const JAN_1ST_2030 = 1893456000;
const ONE_GWEI = 1_000_000_000n;

module.exports = buildModule("LockModule", (m) => {
const unlockTime = m.getParameter("unlockTime", JAN_1ST_2030);
const lockedAmount = m.getParameter("lockedAmount", ONE_GWEI);

const lock = m.contract("Lock", [unlockTime], {
value: lockedAmount,
});

return { lock };
});
Loading