Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Templates #7

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
28 changes: 28 additions & 0 deletions Templates-voting/Fairvoting.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Fairvoting {
string public question = "who is cooler";
string public optionA = "superman";
string public optionB = "spiderman";
uint public optionAVotes;
uint public optionBVotes;

mapping(address => bool) public hasVoted;
mapping(address => string) public userVotes;

function voteForOptionA() public {
require(!hasVoted[msg.sender], "You have already voted.");
optionAVotes++;
hasVoted[msg.sender] = true;
userVotes[msg.sender] = optionA;
}

function voteForOptionB() public {
require(!hasVoted[msg.sender], "You have already voted.");
optionBVotes++;
hasVoted[msg.sender] = true;
userVotes[msg.sender] = optionB;
}

}
56 changes: 56 additions & 0 deletions Templates-voting/Fairvotingcommented.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: MIT
// This line specifies the license under which the code is distributed. In this case, it uses the MIT license.

pragma solidity ^0.8.0;
// This line specifies the version of the Solidity compiler that should be used to compile the code. The caret (^) symbol indicates that any compatible compiler version greater than or equal to 0.8.0 can be used.

contract Fairvotingcommented {
string public question = "who is cooler";
// This line declares a public string variable named "question" and assigns it the value "who is cooler". Public variables can be accessed from outside the contract.

string public optionA = "superman";
// This line declares a public string variable named "optionA" and assigns it the value "superman". Public variables can be accessed from outside the contract.

string public optionB = "spiderman";
// This line declares a public string variable named "optionB" and assigns it the value "spiderman". Public variables can be accessed from outside the contract.

uint public optionAVotes;
// This line declares a public uint (unsigned integer) variable named "optionAVotes". Public variables can be accessed from outside the contract.

uint public optionBVotes;
// This line declares a public uint (unsigned integer) variable named "optionBVotes". Public variables can be accessed from outside the contract.

mapping(address => bool) public hasVoted;
// This line declares a public mapping variable named "hasVoted" that maps addresses to booleans. It is used to keep track of whether an address has voted. Public mappings can be accessed from outside the contract.

mapping(address => string) public userVotes;
// This line declares a public mapping variable named "userVotes" that maps addresses to strings. It is used to store the voting choice of each address. Public mappings can be accessed from outside the contract.

function voteForOptionA() public {
require(!hasVoted[msg.sender], "You have already voted.");
// This line uses the require statement to check if the sender's address has already voted. If the condition is false, it throws an exception with the error message "You have already voted."

optionAVotes++;
// This line increments the vote count for option A by one.

hasVoted[msg.sender] = true;
// This line marks the sender's address as having voted by setting its corresponding value in the "hasVoted" mapping to true.

userVotes[msg.sender] = optionA;
// This line stores the sender's vote choice (option A) in the "userVotes" mapping using their address as the key.
}

function voteForOptionB() public {
require(!hasVoted[msg.sender], "You have already voted.");
// This line uses the require statement to check if the sender's address has already voted. If the condition is false, it throws an exception with the error message "You have already voted."

optionBVotes++;
// This line increments the vote count for option B by one.

hasVoted[msg.sender] = true;
// This line marks the sender's address as having voted by setting its corresponding value in the "hasVoted" mapping to true.

userVotes[msg.sender] = optionB;
// This line stores the sender's vote choice (option B) in the "userVotes" mapping using their address as the key.
}
}
32 changes: 32 additions & 0 deletions Templates-voting/Votingcommented.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: MIT
// This line specifies the license under which the code is distributed. In this case, it uses the MIT license.

pragma solidity ^0.8.0;
// This line specifies the version of the Solidity compiler that should be used to compile the code. The caret (^) symbol indicates that any compatible compiler version greater than or equal to 0.8.0 can be used.

contract Voting {
string public question = "who is cooler";
// This line declares a public string variable named "question" and assigns it the value "who is cooler". Public variables can be accessed from outside the contract.

string public optionA = "superman";
// This line declares a public string variable named "optionA" and assigns it the value "superman". Public variables can be accessed from outside the contract.

string public optionB = "spiderman";
// This line declares a public string variable named "optionB" and assigns it the value "spiderman". Public variables can be accessed from outside the contract.

uint public optionAVotes;
// This line declares a public uint (unsigned integer) variable named "optionAVotes". Public variables can be accessed from outside the contract.

uint public optionBVotes;
// This line declares a public uint (unsigned integer) variable named "optionBVotes". Public variables can be accessed from outside the contract.

function voteForOptionA() public {
optionAVotes++;
// This line increments the vote count for option A by one.
}

function voteForOptionB() public {
optionBVotes++;
// This line increments the vote count for option B by one.
}
}
34 changes: 34 additions & 0 deletions Templates-voting/Votingwinputs.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
//here the options and voting is through frontend


contract Fairvoting {
string public question = "Who is cooler?";
string public optionA;
string public optionB;
uint public optionAVotes;
uint public optionBVotes;

mapping(address => bool) public hasVoted;
mapping(address => string) public userVotes;

function setOptions(string memory _optionA, string memory _optionB) public {
optionA = _optionA;
optionB = _optionB;
}

function vote(string memory choice) public {
require(!hasVoted[msg.sender], "You have already voted.");
require(keccak256(bytes(choice)) == keccak256(bytes(optionA)) || keccak256(bytes(choice)) == keccak256(bytes(optionB)), "Invalid choice.");

if (keccak256(bytes(choice)) == keccak256(bytes(optionA))) {
optionAVotes++;
} else if (keccak256(bytes(choice)) == keccak256(bytes(optionB))) {
optionBVotes++;
}

hasVoted[msg.sender] = true;
userVotes[msg.sender] = choice;
}
}
54 changes: 54 additions & 0 deletions Templates-voting/Votingwinputscomm.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-License-Identifier: MIT
// This comment specifies the license under which the code is distributed. In this case, it uses the MIT license.

pragma solidity ^0.8.0;
// This line specifies the version of the Solidity compiler that should be used to compile the code. The caret (^) symbol indicates that any compatible compiler version greater than or equal to 0.8.0 can be used.

contract Fairvoting {
string public question = "Who is cooler?";
// This line declares a public string variable named "question" and assigns it the value "Who is cooler?". Public variables can be accessed from outside the contract.

string public optionA;
// This line declares a public string variable named "optionA" to store the first voting option. Public variables can be accessed from outside the contract.

string public optionB;
// This line declares a public string variable named "optionB" to store the second voting option. Public variables can be accessed from outside the contract.

uint public optionAVotes;
// This line declares a public uint (unsigned integer) variable named "optionAVotes" to track the number of votes for option A. Public variables can be accessed from outside the contract.

uint public optionBVotes;
// This line declares a public uint (unsigned integer) variable named "optionBVotes" to track the number of votes for option B. Public variables can be accessed from outside the contract.

mapping(address => bool) public hasVoted;
// This line declares a public mapping variable named "hasVoted" that maps addresses to booleans. It is used to keep track of whether an address has voted. Public mappings can be accessed from outside the contract.

mapping(address => string) public userVotes;
// This line declares a public mapping variable named "userVotes" that maps addresses to strings. It is used to store the voting choice of each address. Public mappings can be accessed from outside the contract.

function setOptions(string memory _optionA, string memory _optionB) public {
optionA = _optionA;
optionB = _optionB;
}
// This function allows the contract owner or authorized parties to set the options (option A and option B) by passing them as parameters.

function vote(string memory choice) public {
require(!hasVoted[msg.sender], "You have already voted.");
// This line uses the require statement to check if the sender's address has already voted. If the condition is false, it throws an exception with the error message "You have already voted."

require(keccak256(bytes(choice)) == keccak256(bytes(optionA)) || keccak256(bytes(choice)) == keccak256(bytes(optionB)), "Invalid choice.");
// This line uses the require statement to check if the choice matches either option A or option B. If the condition is false, it throws an exception with the error message "Invalid choice."

if (keccak256(bytes(choice)) == keccak256(bytes(optionA))) {
optionAVotes++;
// If the choice matches option A, this line increments the vote count for option A by one.
} else if (keccak256(bytes(choice)) == keccak256(bytes(optionB))) {
optionBVotes++;
// If the choice matches option B, this line increments the vote count for option B by one.
}

hasVoted[msg.sender] = true;
// This line marks the sender's address as having voted by setting its corresponding value in the "hasVoted" mapping to true.

userVotes[msg.sender] = choice;
//
19 changes: 19 additions & 0 deletions Templates-voting/Votiong.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Voting {
string public question="who is cooler";
string public optionA="superman";
string public optionB="spiderman";
uint public optionAVotes;
uint public optionBVotes;

function voteForOptionA() public {
optionAVotes++;
}

function voteForOptionB() public {
optionBVotes++;
}

}
29 changes: 29 additions & 0 deletions Templates-voting/run.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

To run the provided Solidity code in Remix using MetaMask, you can follow these steps:

1. Open Remix: Open the Remix IDE (https://remix.ethereum.org/) in your web browser.

2. Create a New File: Click on the folder "contracts" create a new file Name it "Voting.sol" (or any desired name First letter capital, .sol extension).

3. Copy the Code: Copy and paste the Solidity code into the newly created "Voting.sol" file in the Remix IDE.

4. Choose Compiler Version: In the "Solidity Compiler" tab, select the desired compiler version (e.g., 0.8.0). Make sure it matches the pragma statement in the code.

5. Compile the Code: Click on the "Compile Voting.sol" button to compile the code. The compiler will generate the bytecode and ABI (Application Binary Interface) for the contract.

6. Switch to the "Deploy & Run Transactions" tab: Click on the "Deploy & Run Transactions" tab on the left side of the Remix interface.

7. Connect to MetaMask: Ensure that you have the MetaMask extension installed in your browser and that it is connected to Mumbai testnet or any other EVM compatible chain you want to deploy on.

8. Deploy the Contract: In the "Deploy & Run Transactions" tab, under the "Environment" dropdown, select "Injected Web3" to connect to MetaMask. This will populate the "Account" dropdown with your MetaMask account address.

9. Deploy the Contract: Click on the "Deploy" button below the contract details. MetaMask will prompt you to confirm the deployment transaction. Adjust the gas limit if necessary and confirm the transaction.

10. Interact with the Contract: Once the contract is deployed, you can interact with its functions. You will see the contract instance in the "Deployed Contracts" section below. Expand the contract instance to access its functions and state variables.

11. Vote for Option A or Option B: Click on the "voteForOptionA" or "voteForOptionB" function, depending on your preferred choice. Confirm the transaction in MetaMask to execute the function.

12. View Vote Count: After voting, you can check the updated vote counts by accessing the optionAVotes and optionBVotes public variables in the deployed contract instance.

13. for the file where we are using the smartcomtract with frontend we can use Web3.js library and input our ABI and contract address to use the contract.
for any further doubts feel free to reach out to twitter: @momosdo or github:Ananyaagupta