0

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.

4

2 回答 2

1

代替:

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

尝试:

callGreeting = contract.functions.greet().call()
print(callGreeting)
于 2021-06-08T21:29:56.743 回答
0

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

于 2021-04-06T07:28:07.020 回答