【问题标题】:ContractLogicError while using web3.py使用 web3.py 时出现 ContractLogicError
【发布时间】:2021-07-01 11:03:17
【问题描述】:

我正在尝试通过使用 Python 的 web3 模块来了解更多关于 dapp 的信息。 Web3 可以很好地连接到 Ganache,我可以使用 web3.eth.accounts[0] 查看我的帐户,并且可以检索我的合同。但是,当我尝试从我的合同中调用一个函数时,我得到以下信息: 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;
  }
}

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

【问题讨论】:

    标签: python blockchain web3 web3py


    【解决方案1】:

    代替:

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

    尝试:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我得到了完全相同的错误信息。 “从编译后的合约中读取”是什么意思?
      猜你喜欢
      • 2022-01-10
      • 2020-05-18
      • 2021-11-04
      • 2021-07-28
      • 2022-12-10
      • 2020-03-23
      • 2022-06-25
      • 2020-10-05
      • 2018-09-13
      相关资源
      最近更新 更多