'Getting an error when trying to execute the deploy script

I'm getting a Transaction reverted without a reason string when I try to execute the deploy script.

Smart Contract:

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/access/Ownable.sol";

contract DecentralizedEVoting is Ownable {

    struct Candidate {
        uint256 Id;
        bytes32 Name;
        uint256 Votes;
    }

    bool hasEnded;
    mapping(address => bool) public hasVoted;
    Candidate[] public candidates;

    constructor(bytes32[] memory _candidateNames) payable {
        for(uint256 i = 0; i < _candidateNames.length; i++) {
            candidates[i] = Candidate(i, _candidateNames[i], 0);
        }
    }

    function viewCandidates() public view returns(Candidate[] memory){
        return candidates;
    }

    function vote(uint256 _ID) public {
        require(!hasEnded);
        require(!hasVoted[msg.sender], "This address has been voted before." );
        hasVoted[msg.sender] == true;
        candidates[_ID].Votes++;
    }

    function endElection() public onlyOwner {
        hasEnded = true;
    }

}

deploy.js

async function main() {

  const DecentralizedEVoting = await ethers.getContractFactory("DecentralizedEVoting");
  const decentralizedEVoting = await DecentralizedEVoting.deploy(["0x61686d6574000000000000000000000000000000000000000000000000000000", "0x6d65686d65740000000000000000000000000000000000000000000000000000"]); // [ahmet, mehmet]

  await decentralizedEVoting.deployed();

  console.log("DecentralizedEVoting contract deployed to:", decentralizedEVoting.address);
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

Getting this error when trying to run the script:

web3_clientVersion
eth_chainId
eth_accounts
eth_blockNumber
eth_chainId (2)
eth_estimateGas
  Contract deployment: <UnrecognizedContract>
  From:                0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
  Value:               0 ETH

  **Error: Transaction reverted without a reason string**
      at <UnrecognizedContract>.constructor (unknown)
      at EthModule._estimateGasAction (/home/user/dev/decentralized-e-voting/node_modules/hardhat/src/internal/hardhat-network/provider/modules/eth.ts:425:7)
      at HardhatNetworkProvider._sendWithLogging (/home/user/dev/decentralized-e-voting/node_modules/hardhat/src/internal/hardhat-network/provider/provider.ts:139:22)
      at HardhatNetworkProvider.request (/home/user/dev/decentralized-e-voting/node_modules/hardhat/src/internal/hardhat-network/provider/provider.ts:116:18)
      at JsonRpcHandler._handleRequest (/home/user/dev/decentralized-e-voting/node_modules/hardhat/src/internal/hardhat-network/jsonrpc/handler.ts:188:20)
      at JsonRpcHandler._handleSingleRequest (/home/user/dev/decentralized-e-voting/node_modules/hardhat/src/internal/hardhat-network/jsonrpc/handler.ts:167:17)
      at Server.JsonRpcHandler.handleHttp (/home/user/dev/decentralized-e-voting/node_modules/hardhat/src/internal/hardhat-network/jsonrpc/handler.ts:52:21)

Thanks in advance, have a nice day :)



Solution 1:[1]

Instead of using:

candidates[i] = Candidate(i, _candidateNames[i], 0);

use this:

candidates.push(Candidate(i, _candidateNames[i], 0));

the reason being, you are using dynamically sized array of candidates and not fixed sized!!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tur461