Homework 2 – Voting Contract with MetaMask, Ganache & Remix
Install • Connect • Deploy • Interact • Validate • Count

A Simple Voting Smart Contract – Step‑by‑Step

Follow each step. Use your local chain from Ganache. Keep screenshots of the key actions and outputs.

Reference material
GitHub: Chapter 5 (Voting)

1. Install and Set Up MetaMask

Why: MetaMask manages accounts and signs transactions. It bridges your browser to Ethereum networks (Ganache in this HW).

  1. Install the MetaMask browser extension from the official site/store.
  2. Open MetaMask → create a new wallet → securely store the recovery phrase.
  3. After setup, you will see your first account (e.g., Account 1).
Expected Outcome: MetaMask is installed and your first Ethereum account is created.

2. Set Up Ganache

Why: Ganache simulates an Ethereum network locally. No real money needed.

  1. Download and install Ganache (GUI) from Truffle Suite.
  2. Open Ganache → Quickstart Ethereum.
  3. Ganache creates 10 accounts with 100 ETH each. RPC runs at http://127.0.0.1:7545.
Expected Outcome: Ganache is running locally with 10 funded accounts.

3. Connect MetaMask to Ganache

Why: MetaMask must point to your local blockchain to deploy and send transactions.

  1. In MetaMask, open the network selector → Add NetworkAdd a network manually.
  2. Fill in:
    • Network Name: Ganache
    • New RPC URL: http://127.0.0.1:7545
    • Chain ID: 1337
    • Currency Symbol: ETH
  3. Save, then switch MetaMask to the Ganache network.
Expected Outcome: MetaMask is now connected to your local Ganache network.

4. Import a Ganache Account into MetaMask

Why: Use a prefunded Ganache account to pay for deployments.

  1. In Ganache, click the key icon for the first account → copy its private key.
  2. In MetaMask: account icon → Import Account → paste the private key → Import.
Expected Outcome: The 100‑ETH Ganache account appears in MetaMask.

5–7. Open Remix, Write, Compile, and Deploy

5. Open Remix and Write the Contract

  1. Open Remix IDE in your browser.
  2. New file → Voting.sol → paste the code below.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Voting {
    string[] public candidateList;
    mapping(string => uint256) public votesReceived;

    constructor(string[] memory candidateNames) {
        candidateList = candidateNames;
    }

    function voteForCandidate(string memory candidate) public {
        require(validCandidate(candidate), "Candidate does not exist");
        votesReceived[candidate] += 1;
    }

    function totalVotesFor(string memory candidate) public view returns (uint256) {
        require(validCandidate(candidate), "Candidate does not exist");
        return votesReceived[candidate];
    }

    function validCandidate(string memory candidate) public view returns (bool) {
        for (uint i = 0; i < candidateList.length; i++) {
            if (keccak256(abi.encodePacked(candidateList[i])) == keccak256(abi.encodePacked(candidate))) {
                return true;
            }
        }
        return false;
    }
}
Expected Outcome: Voting.sol is ready in Remix.

6. Compile the Contract

  1. Click Solidity Compiler in Remix sidebar.
  2. Set compiler to 0.8.0 or higher.
  3. Click Compile Voting.sol. Look for the green checkmark.
Expected Outcome: Compilation succeeds.

7. Deploy to Ganache

  1. Click Deploy & Run Transactions in Remix.
  2. Environment: choose Injected Web3 (connects to MetaMask). (Alternatively, you may use Remix VM (Cancun) for practice, but homework requires Ganache.)
  3. Ensure MetaMask is on the Ganache network.
  4. Constructor args: ["Alice", "Bob", "Charlie"]
  5. Click Deploy → confirm the MetaMask transaction.
Expected Outcome: Contract is deployed and appears under Deployed Contracts.

8–10. Interact with the Contract

8. Cast a Vote

  1. Expand the deployed Voting instance in Remix.
  2. Find voteForCandidate. Enter "Alice" (quotes included; case‑sensitive).
  3. Click the function → confirm in MetaMask.
Expected Outcome: A vote transaction is confirmed for "Alice".

9. Validate a Candidate

  1. Find validCandidate.
  2. Enter "Bob" and click. Result appears below: true or false.
Expected Outcome: You can verify whether a name exists.

10. Count Votes

  1. Find totalVotesFor.
  2. Enter "Alice" and click to read the current vote count.
Expected Outcome: You see the number of votes recorded for a candidate.

Troubleshooting

Submission Checklist

File naming: HW2_FirstnameLastname.pdf with all screenshots in order.