Trying to compile below solidity code:
Deploy.js
let path = require('path');
let fs = require('fs'); let solc = require('solc');
let voterPath = path.resolve(__dirname,'Voter.sol'); let srcCodeObj = fs.readFileSync(voterPath, 'UTF-8');
let voterInput = { language:'Solidity', sources:{ 'Voter.sol':{ content:srcCodeObj, }, }, settings:{ outputSelection:{ '':{ '':["abi", "evm.bytecode"], }, }, }, };
try { console.log("Compiling"); let output = solc.compile(JSON.stringify(voterInput)); console.log(output); console.log("Compiled"); } catch(Ex) { console.log("Exception occured"); console.log(Ex); }
Voter.sol
pragma solidity >0.4.2;
contract Voter {
struct OptionPos {
uint pos;
bool exists;
}
/*constructor(string memory passedName) public {
}*/
// Fields
uint[] public votes;
string[] public options;
mapping(address=>bool) hasVoted;
mapping(string=>OptionPos) posOfOption;
bool votingStarted;
function addOption(string memory option) public {
require(!votingStarted);
options.push(option);
}
function startVoting() public{
require(!votingStarted);
//votes.length = options.length;
for (uint cnt = 0; cnt < options.length; cnt++) {
OptionPos memory option = OptionPos(cnt, true);
posOfOption[options[cnt]] = option;
}
votingStarted = true;
}
function vote(uint option) public {
require(option >= 0 && option < options.length);
require(!(hasVoted[msg.sender]));
votes[option] += 1;
hasVoted[msg.sender] = true;
}
function vote(string memory optionName) public {
require(!(hasVoted[msg.sender]));
OptionPos memory optionPos = posOfOption[optionName];
require(optionPos.exists);
votes[optionPos.pos] += 1;
hasVoted[msg.sender] = true;
}
function getVotes() public view returns (uint[] memory) {
return votes;
}
}
Facing below error: { contracts: {}, errors: [ ':1:1: ParserError: Expected pragma, import directive or contract/interface/library definition.\n' + ' ... { ... \n' + ' ^\n' ], sourceList: [ '' ], sources: {} }