使用Web3.py通过ABI调用智能合约功能时出错

时间:2019-04-20 04:56:58

标签: python-3.x ethereum smartcontracts json-rpc web3

我在通过松露生成的ABI调用智能合约功能时遇到问题。我正在使用JSON方法以python代码加载文件。文件已成功加载,但是在调用函数时会生成错误

智能合约代码

    pragma solidity >=0.4.22 <0.6.0;

    contract Greeter
      {

        string public greeting;

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

        function setGreeting(string memory _greeting) public
          {
            greeting = _greeting;
          }

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

    }

Web3.py脚本

    import sys
    import json
    from web3 import Web3, HTTPProvider

    ganache_cli="http://127.0.0.1:8545"

    web3=Web3(Web3.HTTPProvider(ganache_cli))

    print(web3.isConnected())

    with open("/home/khizar/tutorial/Tutorial4/build/contracts/Greeter.json") as f:
      info_json = json.load(f)
    abi = info_json["abi"]

    address=web3.toChecksumAddress("0x76a1bcb1803840602544d83212daec083d68fba2")

    contract = web3.eth.contract(address=address, abi=abi)
    '''print(contract.functions.getGreeting().call())'''

脚本输出:

True
Traceback (most recent call last):
  File "app4.py", line 24, in <module>
    print(contract.functions.getGreeting().call())
  File "/home/khizar/tutorial/lib/python3.6/site-packages/web3/contract.py", line 1106, in call
    **self.kwargs
  File "/home/khizar/tutorial/lib/python3.6/site-packages/web3/contract.py", line 1355, in call_contract_function
    return_data = web3.eth.call(call_transaction, block_identifier=block_id)
  File "/home/khizar/tutorial/lib/python3.6/site-packages/eth_utils/functional.py", line 22, in inner
    return callback(fn(*args, **kwargs))
  File "/home/khizar/tutorial/lib/python3.6/site-packages/web3/eth.py", line 293, in call
    [transaction, block_identifier],
  File "/home/khizar/tutorial/lib/python3.6/site-packages/web3/manager.py", line 110, in request_blocking
    raise ValueError(response["error"])
ValueError: {'message': 'VM Exception while processing transaction: revert', 'code': -32000, 'data': {'0x02595810ac6841fa8464521a96ce3ae9592f2a2dcab504ae8506d014820e8128': {'error': 'revert', 'program_counter': 103, 'return': '0x'}, 'stack': 'o: VM Exception while processing transaction: revert\n    at Function.o.fromResults (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:10:81931)\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:47:121235\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:61:1853218\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:26124\n    at i (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:41179)\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:61:1210468\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:105466\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:32:392\n    at c (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:32:5407)\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:32:317\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:61:1871644\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:23237\n    at o (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:26646)\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:26124\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:61:1864681\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:61:1862544\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:61:1889757\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:23237\n    at o (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:26646)\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:26124\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:5439\n    at FSReqWrap.args [as oncomplete] (fs.js:140:20)', 'name': 'o'}}

0 个答案:

没有答案