智能合约遇到的问题整合

参考文档:

  手把手参考文档链接: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就可以了:

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智能合约是一种在区块链上执行的自动化合约。Python是一种常用的编程语言,也可以用于编写智能合约。在Python中,可以使用一些库来编写智能合约,例如Web3.py和Solidity.py。下面是一个使用Web3.py库编写智能合约的示例: ```python from web3 import Web3 # 连接到以太坊节点 w3 = Web3(Web3.HTTPProvider('https://mainnet.infura.io/v3/your-infura-project-id')) # 加载智能合约ABI contract_abi = [ { "constant": False, "inputs": [ { "name": "x", "type": "uint256" } ], "name": "set", "outputs": [], "payable": False, "stateMutability": "nonpayable", "type": "function" }, { "constant": True, "inputs": [], "name": "get", "outputs": [ { "name": "", "type": "uint256" } ], "payable": False, "stateMutability": "view", "type": "function" } ] # 部署智能合约 contract_address = '0x1234567890abcdef1234567890abcdef12345678' contract = w3.eth.contract(address=contract_address, abi=contract_abi) # 调用智能合约方法 transaction = contract.functions.set(42).buildTransaction({ 'from': w3.eth.accounts[0], 'gas': 100000, 'gasPrice': w3.toWei('1', 'gwei'), 'nonce': w3.eth.getTransactionCount(w3.eth.accounts[0]) }) signed_transaction = w3.eth.account.signTransaction(transaction, private_key='your-private-key') transaction_hash = w3.eth.sendRawTransaction(signed_transaction.rawTransaction) transaction_receipt = w3.eth.waitForTransactionReceipt(transaction_hash) # 获取智能合约状态 result = contract.functions.get().call() print("智能合约状态:", result) ``` 请注意,上述示例中的合约ABI和地址是虚构的,您需要根据您自己的合约来替换它们。此外,您还需要替换连接到以太坊节点的URL和私钥。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值