0

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: {} }

1 Answer 1

0

You want to check if your .sol file is encoded in UTF-8 without BOM. You can open your contract file in a text editor such as VS Code or Notepad++ and go to encoding and covert it to UTF-8.

1
  • yes it was encoded without BOM
    – Sandy S
    Sep 11, 2022 at 3:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.