Please point me to the right direction.
Below is the code for my voting contract that I have created in remix IDE. When interacting with addCandidate
Method on remix VM it is working properly but when I try to use metamask with sepolia test network, I am always getting the error Transaction Failed! Tranasaction replaced or dropped
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
error CandidateAlreadyExists();
error AlreadyVoted();
error InvalidCandidate();
error NotRegisteredVoter();
error VoterAlreadyRegistered();
error OnlyManagerCanCall();
error InvalidAddress();
error InvalidName();
error InsufficientCandidates();
contract BlockchainVoting {
address private manager;
mapping(address => Voter) private voters;
mapping(address => Candidate) private candidateLookup;
Candidate[] private candidates;
address[] private voterAddresses;
constructor() {
manager = msg.sender;
}
struct Voter {
string name;
address voterAddress;
bool hasVoted;
}
struct Candidate {
string name;
address candidateAddress;
uint256 votes;
}
function registerVoter(string memory voterName, address voterAddress) external onlyManager {
if (voterAddress == address(0)) {
revert InvalidAddress();
}
if (bytes(voterName).length == 0) {
revert InvalidName();
}
if (voters[voterAddress].voterAddress != address(0)) {
revert VoterAlreadyRegistered();
}
voters[voterAddress] = Voter(voterName, voterAddress, false);
voterAddresses.push(voterAddress);
}
function registerVoters(string[] memory voterNames, address[] memory voterAddressesArray) external onlyManager {
require(voterNames.length == voterAddressesArray.length, "Array length mismatch");
for (uint256 i = 0; i < voterAddressesArray.length; i++) {
address voterAddress = voterAddressesArray[i];
string memory voterName = voterNames[i];
if (voterAddress == address(0)) {
revert InvalidAddress();
}
if (bytes(voterName).length == 0) {
revert InvalidName();
}
if (voters[voterAddress].voterAddress != address(0)) {
revert VoterAlreadyRegistered();
}
voters[voterAddress] = Voter(voterName, voterAddress, false);
voterAddresses.push(voterAddress);
}
}
function addCandidate(address candidateAddress, string memory name) external onlyManager {
if (candidateAddress == address(0)) {
revert InvalidAddress();
}
if (bytes(name).length == 0) {
revert InvalidName();
}
if (candidateLookup[candidateAddress].candidateAddress != address(0)) {
revert CandidateAlreadyExists();
}
Candidate memory newCandidate = Candidate({ name: name, candidateAddress: candidateAddress, votes: 0 });
candidates.push(newCandidate);
candidateLookup[candidateAddress] = newCandidate;
}
function addCandidates(address[] memory candidateAddresses, string[] memory names) external onlyManager {
require(candidateAddresses.length == names.length, "Array length mismatch");
for (uint256 i = 0; i < candidateAddresses.length; i++) {
address candidateAddress = candidateAddresses[i];
string memory name = names[i];
if (candidateAddress == address(0)) {
revert InvalidAddress();
}
if (bytes(name).length == 0) {
revert InvalidName();
}
if (candidateLookup[candidateAddress].candidateAddress != address(0)) {
revert CandidateAlreadyExists();
}
Candidate memory newCandidate = Candidate({ name: name, candidateAddress: candidateAddress, votes: 0 });
candidates.push(newCandidate);
candidateLookup[candidateAddress] = newCandidate;
}
}
function vote(address candidateAddress) external {
if (candidates.length < 2) {
revert InsufficientCandidates();
}
Voter storage senderVoter = voters[msg.sender];
if (senderVoter.voterAddress == address(0)) {
revert NotRegisteredVoter();
}
if (senderVoter.hasVoted) {
revert AlreadyVoted();
}
Candidate storage candidate = candidateLookup[candidateAddress];
if (candidate.candidateAddress == address(0)) {
revert InvalidCandidate();
}
candidate.votes++;
senderVoter.hasVoted = true;
}
function getAllCandidates() external view returns (Candidate[] memory) {
return candidates;
}
function getAllVoters() external view returns (Voter[] memory) {
Voter[] memory voterList = new Voter[](voterAddresses.length);
for (uint256 i = 0; i < voterAddresses.length; i++) {
voterList[i] = voters[voterAddresses[i]];
}
return voterList;
}
modifier onlyManager() {
if (msg.sender != manager) {
revert OnlyManagerCanCall();
}
_;
}
}