问题
I am getting error with both latest solc (0.5.2 version) and 0.4.25 too while I am writing Simple contract
I have tried following steps
- uninstalled Solc: npm uninstall solc
- Installed targeted version: npm install --save solc@0.4.25
node compile.js (code given below)
{ contracts: {}, errors: [ ':1:1: ParserError: Expected pragma, import directive or contract /interface/library definition.\nD:\\RND\\BlockChain\\contracts\\Inbox.sol\n^\n' ],sourceList: [ '' ],sources: {} }
Compile.js
const path = require('path');
const fs = require('fs');
const solc = require('solc');
const inPath = path.resolve(__dirname,'contracts','Inbox.sol');
const src = fs.readFileSync(inPath,'UTF-8');
const res = solc.compile(inPath, 1);
console.log(res);
Inbox.sol
pragma solidity ^0.4.25;
contract Inbox {
string message;
function Inbox(string passedName) public {
message = passedName;
}
function setMessage(string newMsg) public {
message = newMsg;
}
function getMessage() public view returns(string){
return message;
}
}
Code worked well on Remix, for version 0.5.2 I have added memory tag to make it compile on Remix.
ex: function setMessage(string **memory** newMsg)
回答1:
You don't seem to be using solc-js correctly. Please see the documentation: https://github.com/ethereum/solc-js
Specifically, you need to construct an input object and then stringify it and pass that to solc.compile()
.
回答2:
solc <= v0.4.25
Your primary issue using Solidity/solc v0.4.25 is your constructor definition.
You currently have your constructor defined as:
function Inbox(string passedName) public
However, defining constructors with the same name as the contract has been deprecated in Solidity. Try defining your constructor using the constructor
keyword instead.
constructor(string passedName) public
If you are using solc v0.4.25, please refer to the documentation in order to understand how to properly pass input to the compile
function. See my reference below:
const input = {
'Inbox.sol': fs.readFileSync(path.resolve(__dirname, 'contracts', 'Inbox.sol'), 'utf8')
}
const output= solc.compile({sources: input}, 1);
if(output.errors) {
output.errors.forEach(err => {
console.log(err);
});
} else {
const bytecode = output.contracts['Inbox.sol:Inbox'].bytecode;
const abi = output.contracts['Inbox.sol:Inbox'].interface;
console.log(`bytecode: ${bytecode}`);
console.log(`abi: ${JSON.stringify(JSON.parse(abi), null, 2)}`);
}
solc >= v0.5.0
If you are using Solidity/solc v0.5.2, you will also need to fix your constructor
definition. Furthermore, you will need to add the memory
keyword to each function
that returns or accepts the string
type.
For example:
function setMessage(string newMsg) public
should be declared as:
function setMessage(string memory newMsg) public
Futhermore, please see the latest documentation in order to understand the differences between the latest Solidity compiler and the older version. See my reference below for how to define the input for the compile
function utilizing the latest compiler:
const input = {
language: "Solidity",
sources: {
"Inbox.sol": {
content: fs.readFileSync(path.resolve(__dirname, "contracts", "Inbox.sol"), "utf8")
}
},
settings: {
outputSelection: {
"*": {
"*": [ "abi", "evm.bytecode" ]
}
}
}
}
const output = JSON.parse(solc.compile(JSON.stringify(input)));
if(output.errors) {
output.errors.forEach(err => {
console.log(err.formattedMessage);
});
} else {
const bytecode = output.contracts['Inbox.sol'].Inbox.evm.bytecode.object;
const abi = output.contracts['Inbox.sol'].Inbox.abi;
console.log(`bytecode: ${bytecode}`);
console.log(`abi: ${JSON.stringify(abi, null, 2)}`);
}
回答3:
The problem is that you've not save your contract in UTF-8 encoding. to solve that you can open your contract file in a text editor and just save it as UTF8 (you can easily do it in VS code)
来源:https://stackoverflow.com/questions/53941422/solidity-parsererror-expected-pragma-import-directive-or-contract-interface