牢固性:ParserError:预期的编译指示,导入指令或合同/接口/库定义

编程入门 行业动态 更新时间:2024-10-26 19:35:56
本文介绍了牢固性:ParserError:预期的编译指示,导入指令或合同/接口/库定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在编写简单合约时也遇到了最新的solc(0.5.2版本)和0.4.25错误

I am getting error with both latest solc (0.5.2 version) and 0.4.25 too while I am writing Simple contract

我尝试了以下步骤

  • 未安装的Solc:npm卸载solc
  • 已安装的目标版本:npm install --save solc@0.4。 25
  • 节点compile.js(代码如下)

  • 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 = solcpile(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; } }

    代码在Remix上运行良好 ,对于版本0.5.2,我添加了内存标记以使其在Remix上编译。

    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)

    推荐答案

    solc< = v0.4.25

    您的主要使用Solidity / solc v0.4.25 的问题是您的构造函数定义。

    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

    但是,在Solidity中已弃用了与合同同名的定义构造函数。尝试使用 constructor 关键字定义构造函数。

    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

    如果您使用的是solc v0.4.25 ,请参阅到文档以便了解如何正确处理将输入传递给 compile 函数。见下面我的参考:

    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= solcpile({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

    如果使用的是Solidity / solc v0.5.2 ,则还需要修复构造函数的定义。此外,您需要将 memory 关键字添加到每个返回或接受的函数中字符串类型。

    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.

    例如:

    function setMessage(string newMsg) public

    应声明为:

    function setMessage(string memory newMsg) public

    更多,请参阅最新文档,以便了解最新的Solidity编译器之间的差异和旧版本。请参阅下面的参考资料,了解如何使用最新的编译器为 compile 函数定义输入:

    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(solcpile(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)}`); }

    更多推荐

    牢固性:ParserError:预期的编译指示,导入指令或合同/接口/库定义

    本文发布于:2023-11-13 11:59:37,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1584299.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:牢固   指令   指示   接口   定义

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!