I recently started using this course on Udemy to learn more about blockchain technologies and smart contracts. The main problem is that this course is based on older versions of solidity, truffle, geth, node.js etc. and therefore it is difficult to follow sometimes. Other people have posted my issue in the Q&A section but I don't think it has been solved so I'm trying here. I only have background in Python 3, C and C .
I first run sourceCode = fs.readFileSync('Greetings.sol').toString()
and get the following:
'\r\n'
'contract Greetings {\r\n'
' string message;\r\n'
'\r\n'
' constructor(){\r\n'
` message = "I'm ready";\r\n`
' }\r\n'
'\r\n'
' function setGreetings(string memory _message) public {\r\n'
' message = _message;\r\n'
' }\r\n'
'\r\n'
' function getGreetings() public view returns (string memory){\r\n'
' return message;\r\n'
' }\r\n'
'}'
Then I run compiledCode = solc.compile(sourceCode)
and get
'{"errors":[{"component":"general","formattedMessage":"* Line 1, Column 1\\n Syntax error: value, object or array expected.\\n* Line 1, Column 2\\n Extra non-whitespace after JSON value.\\n","message":"* Line 1, Column 1\\n Syntax error: value, object or array expected.\\n* Line 1, Column 2\\n Extra non-whitespace after JSON value.\\n","severity":"error","type":"JSONError"}]}'
From what I understand it is not in the correct format for it to be compiled. How do I resolve this?
I have the most recent versions of Geth, Truffle, Web3, Solc, Node.js etc. and any libraries. The Solc I have installed is the same as the one in the pragma
statement. I code using VS Code with the Solidity extension and use Windows Powershell for executing the commands.
My Grettings.sol file:
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.15;
contract Greetings {
string message;
constructor(){
message = "I'm ready";
}
function setGreetings(string memory _message) public {
message = _message;
}
function getGreetings() public view returns (string memory){
return message;
}
}
Full log:
> sourceCode = fs.readFileSync('Greetings.sol').toString()
'pragma solidity ^0.8.15;\r\n'
'\r\n'
'contract Greetings {\r\n'
' string message;\r\n'
'\r\n'
' constructor(){\r\n'
` message = "I'm ready";\r\n`
' }\r\n'
'\r\n'
' function setGreetings(string memory _message) public {\r\n'
' message = _message;\r\n'
' }\r\n'
'\r\n'
' function getGreetings() public view returns (string memory){\r\n'
' return message;\r\n'
' }\r\n'
'}'
> compiledCode = solc.compile(sourceCode)
'{"errors":[{"component":"general","formattedMessage":"* Line 1, Column 1\\n Syntax error: value, object or array expected.\\n* Line 1, Column 2\\n Extra non-whitespace after JSON value.\\n","message":"* Line 1, Column 1\\n Syntax error: value, object or array expected.\\n* Line 1, Column 2\\n Extra non-whitespace after JSON value.\\n","severity":"error","type":"JSONError"}]}'
CodePudding user response:
Through the help in the comments and the link they posted I have figured it out and will post the commands I used to move on to the next steps. I am stuck somewhere else now so I'll make another post for that.
sourceCode = fs.readFileSync('Greetings.sol').toString()
Convert the text in the .sol file to a string
input = {language: 'Solidity', sources: {['Greetings.sol']: {content: sourceCode}},settings:{outputSelection:{'*':{'*':['*']}}}}
Set it as an input in the correct JSON format
output = JSON.parse(solc.compile(JSON.stringify(input)))
Convert input to JSON string, compile it with the solc compiler, and parse it to a new variable.
contractABI = output.contracts['Greetings.sol']['Greetings'].abi
byteCode = output.contracts['Greetings.sol']['Greetings'].evm['bytecode'].object
Collect the ABI and bytecode data and store them into variables.