智能合约遇到的问题整合

参考文档:

  手把手参考文档链接:https://blog.csdn.net/liyuechun520/article/details/78036363

  Solidity 编译合约错误 No visibility specified. Did you intend to add "public":https://www.twle.cn/t/640#reply1

  https://blog.csdn.net/weixin_45112822/article/details/90480872

第一个报错:Solidity 编译合约错误 No visibility specified. Did you intend to add "public"

/root/SmartContractDemo/HelloWord/contracts/HelloWord.sol:5:3: SyntaxError: No visibility specified. Did you intend to add "public"?
  function sayHello() returns (string) {
  ^ (Relevant source part starts here and spans across multiple lines).
,/root/SmartContractDemo/HelloWord/contracts/HelloWord.sol:5:32: TypeError: Data location must be "memory" for return parameter in function, but none was given.
  function sayHello() returns (string) {
                               ^----^
Compilation failed. See above.
Truffle v5.2.2 (core: 5.2.2)
Node v12.12.0

----------------------------改成下面的就好了-------------

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

contract HelloWord {
  function  sayHi() public pure returns (string memory _greeting){
    _greeting = 'Hello World';
  }

第二个错误:truffle部署智能合约报错:Could not connect to your Ethereum client with the following parameters

执行truffle migrate命令时提示如下图报错:

 

就是因为你的ganache监听的端口和你的truffle-config.js文件里的配置不一样,我打开truffle-config.js如图,将network下的参数去除注释,就可以部署成功了。

第三个错误:VM Exception while processing transaction: invalid opcode

网上查了下,说是testrpc老了,不太支持,我后面换了一个以太坊客户端就好了。

使用命令安装Ganache,
$ sudo npm install ganache-cli -g
安装完成后,使用命令$ ganache-cli

解决问题。

第四个错误:could not find artifacts for HelloWorld from any sources

(1)将编译好的build删掉,重新编译一下试试

(2)我的这个是我自己粗心的问题,我再合约源码里写的合约名称写错了。写成helloword了。我去,修改后,重新编译一下,别忘了migrantion下的那个部署脚本里合约名字也要对应起来,这样就能成功了。如下图哈哈

第五个报错  :ParserError: The state mutability modifier "constant" was removed in version 0.5.0. Use "view" or "pure" instead.

解决办法:

Solidity 旧版本只有constant,新版本将constant拆成了view和pure。view的作用和constant一模一样,可以读取状态变量但是不能改;pure则更为严格,pure修饰的函数不能改也不能读状态变量,否则编译通不过。
所以,我们这里需要把constant改成view就可以了:

 


版权声明:本文为qq_39441086原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。