简体   繁体   中英

ContractLogicError while using web3.py

I'm trying to learn more about dapps by using Python's web3 module. Web3 connects to Ganache just fine, I can see my account by using web3.eth.accounts[0] and I can retrieve my contract. However when I try to call a function from my contract I get the following: web3.exceptions.ContractLogicError: execution reverted: VM Exception while processing transaction: revert

Here is my python code:

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())

And here is my contract:

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;
  }
}

Any help in understanding the error would be appreciated.

Reading in the abi and bytecode from the compiled contract file worked.

instead of:

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

try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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