繁体   English   中英

使用 web3.py 时出现 ContractLogicError

[英]ContractLogicError while using web3.py

我正在尝试通过使用 Python 的 web3 模块来了解更多关于 dapp 的信息。 Web3 可以很好地连接到 Ganache,我可以使用web3.eth.accounts[0]查看我的帐户,并且可以检索我的合同。 但是,当我尝试从我的合同中调用 function 时,我得到以下信息: web3.exceptions.ContractLogicError: execution reverted: VM Exception while processing transaction: revert

这是我的 python 代码:

from web3 import Web3
import json

w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:7545'))
w3.eth.defaultAccount = w3.eth.accounts[0]
print(w3.eth.defaultAccount)

compiled_contract_path = './build/contracts/Greeter.json'
deployed_contract_address = '0x54BB58167CDB31A98F56E8Fc3CfbAC43bf867000'

with open(compiled_contract_path) as file:
    contract_json = json.load(file)  # load contract info as JSON
    contract_abi = contract_json['abi']

contract = w3.eth.contract(address=deployed_contract_address, abi=contract_abi)

print(contract.functions.greet().call())

这是我的合同:

pragma solidity ^0.5.0;

contract Greeter {
  uint public taskCount = 0;
  string public greeting;

  constructor() public {
    greeting = 'Hello';
  }

  function greet() public returns (string memory) {
    return greeting;
  }
}

任何帮助理解错误将不胜感激。

从已编译的合约文件中读取 abi 和字节码是有效的。

代替:

print(contract.functions.greet().call())

尝试:

callGreeting = contract.functions.greet().call()
print(callGreeting)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM