Web3.py 在 ganache 上调用函数时出现 abi keyerror

Web3.py abi keyerror while calling a function on ganache

我已经在 remix 中编译了我的代码 ide。它在那里工作正常。我将字节码和 adi 从编译细节复制到我的 python 代码中。我正在使用 web3.py 作为外部库。但是当我尝试 运行 我的函数时,出现错误 keyerror。我想我需要在函数中传递一些键作为参数,但我找不到方法。

这是我的代码

from web3 import Web3,HTTPProvider
import json

web_link="http://127.0.0.1:7545"


class FunderContract:
    web3 = Web3(Web3.HTTPProvider(web_link))

    def start(self,account_number):
        print("local host is ",self.web3.isConnected())
        print("first block at",self.web3.eth.blockNumber)

        self.web3.eth.defaultAccount=account_number


        abi = json.loads('[{"inputs":[],"stateMutability":"payable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"address","name":"from","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"FundsSended","type":"event"},{"inputs":[{"internalType":"address","name":"add","type":"address"}],"name":"endWithdrawal","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"add","type":"address"}],"name":"initiateWithdrawal","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"id","type":"address"}],"name":"isAllowedToWithdraw","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"string","name":"name","type":"string"}],"name":"register","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address payable","name":"add","type":"address"}],"name":"registerFundi","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"add","type":"address"}],"name":"sendFunds","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"stage","outputs":[{"internalType":"enum Funders.Stage","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"fundee_id","type":"address"},{"internalType":"enum Funders.voteDetail","name":"v","type":"uint8"}],"name":"vote","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]')


        byte_code='608060405260026000556000600160006101000a81548160ff0219169083600281111561002857fe5b021790555060006001806101000a81548160ff0219169083600181111561004b57fe5b021790555060006003556000600560146101000a81548160ff021916908360ff16021790555033600560006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555061137b806100c16000396000f3fe60806040526004361061007f5760003560e01c8063c040e6b81161004e578063c040e6b8146101da578063c92cd1b214610213578063cfe52bdd14610264578063f2c298be146102a857610086565b80632a4c80a41461008b57806358041a48146100e75780637eeb17731461012b578063bceb514d1461017c57610086565b3661008657005b600080fd5b6100cd600480360360208110156100a157600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610370565b604051808215151515815260200191505060405180910390f35b610129600480360360208110156100fd57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506104b6565b005b34801561013757600080fd5b5061017a6004803603602081101561014e57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610786565b005b34801561018857600080fd5b506101d86004803603604081101561019f57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803560ff1690602001909291905050506108cc565b005b3480156101e657600080fd5b506101ef610c40565b604051808260028111156101ff57fe5b60ff16815260200191505060405180910390f35b34801561021f57600080fd5b506102626004803603602081101561023657600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610c53565b005b6102a66004803603602081101561027a57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610d98565b005b3480156102b457600080fd5b5061036e600480360360208110156102cb57600080fd5b81019080803590602001906401000000008111156102e857600080fd5b8201836020820111156102fa57600080fd5b8035906020019184600183028401116401000000008311171561031c57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192905050506110d3565b005b600081600280600281111561038157fe5b600260008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060030160009054906101000a900460ff1660028111156103dc57fe5b1461044f576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260198152602001807f6e6f7420696e207468652072657175697265642073746174650000000000000081525060200191505060405180910390fd5b6000600260008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060040154905060005481106104a95760019350506104af565b60009350505b5050919050565b3373ffffffffffffffffffffffffffffffffffffffff16600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060020160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16146105b9576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601a8152602001807f796f75206e65656420746f20726567697374657220666972737400000000000081525060200191505060405180910390fd5b600354600260008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000018190555080600260008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060010160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506000600260008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600201819055506000600260008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060030160006101000a81548160ff0219169083600281111561072a57fe5b02179055506000600260008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206004018190555060016003540160038190555050565b80600080600281111561079557fe5b600260008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060030160009054906101000a900460ff1660028111156107f057fe5b14610863576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260198152602001807f6e6f7420696e207468652072657175697265642073746174650000000000000081525060200191505060405180910390fd5b6001600260008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060030160006101000a81548160ff021916908360028111156108c257fe5b0217905550505050565b8160018060028111156108db57fe5b600260008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060030160009054906101000a900460ff16600281111561093657fe5b146109a9576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260198152602001807f6e6f7420696e207468652072657175697265642073746174650000000000000081525060200191505060405180910390fd5b3373ffffffffffffffffffffffffffffffffffffffff16600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060020160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1614610aac576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601a8152602001807f796f75206e65656420746f20726567697374657220666972737400000000000081525060200191505060405180910390fd5b600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060020160149054906101000a900460ff1615610b6f576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260168152602001807f796f75206861766520616c726561647920766f7465640000000000000000000081525060200191505060405180910390fd5b600180811115610b7b57fe5b836001811115610b8757fe5b1415610bdf576001600260008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600401600082825401925050819055505b6001600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060020160146101000a81548160ff02191690831515021790555050505050565b600160009054906101000a900460ff1681565b806001806002811115610c6257fe5b600260008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060030160009054906101000a900460ff166002811115610cbd57fe5b14610d30576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260198152602001807f6e6f7420696e207468652072657175697265642073746174650000000000000081525060200191505060405180910390fd5b60028060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060030160006101000a81548160ff02191690836002811115610d8e57fe5b0217905550505050565b3373ffffffffffffffffffffffffffffffffffffffff16600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060020160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1614610e9b576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601a8152602001807f796f75206e65656420746f20726567697374657220666972737400000000000081525060200191505060405180910390fd5b600260008273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060010160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc349081150290604051600060405180830381858888f19350505050158015610f43573d6000803e3d6000fd5b5034600260008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206002015401600260008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600201819055507f7c85e8630bb4531f4492b73593e689399a8f085c9bf902152cb608deedc05cde600260008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060010160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff163334604051808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001828152602001935050505060405180910390a150565b600560149054906101000a900460ff16600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060000160006101000a81548160ff021916908360ff16021790555080600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060010190805190602001906111939291906112a0565b5033600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060020160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506000600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060020160146101000a81548160ff0219169083151502179055506001600560149054906101000a900460ff1601600560146101000a81548160ff021916908360ff16021790555050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106112e157805160ff191683800117855561130f565b8280016001018555821561130f579182015b8281111561130e5782518255916020019190600101906112f3565b5b50905061131c9190611320565b5090565b61134291905b8082111561133e576000816000905550600101611326565b5090565b9056fea2646970667358221220faca3dcd964f4917dbe19837b69f1ca4e4060e27338abe360253fc8353a2ab5964736f6c63430006010033'
        # address = web3.toChecksumAddress(address)
       Greeter = self.web3.eth.contract(abi=abi, bytecode=bytecode)
       tx_hash = Greeter.constructor().transact()
       tx_receipt = self.web3.eth.waitForTransactionReceipt(tx_hash)
       contract = self.web3.eth.contract(
         address=tx_receipt.contractAddress,
         abi=abi,
       )

       print(tx_receipt.contractAddress)

       tx_hash = contract.functions.register('name').transact()

if __name__ == "__main__":


        b=FunderContract()

        b.start("0x35286618aD7Ca934b0AA53FE110ec09569CAd507")
        a=b.funder_contract.functions.register('my').transact()
      `

我遇到了错误

File "main.py", line 41, in <module>
    b.start("0x35286618aD7Ca934b0AA53FE110ec09569CAd507")
  File "main.py", line 25, in start
    self.funder_contract.functions.register('my').transact()
  File "D:\mywork\ML\anaconda\envs\ethe_v\lib\site-packages\web3\contract.py", line 819, in __call__
    clone._set_function_info()
  File "D:\mywork\ML\anaconda\envs\ethe_v\lib\site-packages\web3\contract.py", line 829, in _set_function_info
    self.kwargs
  File "D:\mywork\ML\anaconda\envs\ethe_v\lib\site-packages\web3\_utils\contracts.py", line 119, in find_matching_fn_abi
    function_candidates = pipe(abi, name_filter, arg_count_filter, encoding_filter)
  File "cytoolz/functoolz.pyx", line 669, in cytoolz.functoolz.pipe
    return c_pipe(data, funcs)
  File "cytoolz/functoolz.pyx", line 644, in cytoolz.functoolz.c_pipe
    data = func(data)
  File "D:\mywork\ML\anaconda\envs\ethe_v\lib\site-packages\web3\_utils\abi.py", line 98, in filter_by_name
    in contract_abi
  File "D:\mywork\ML\anaconda\envs\ethe_v\lib\site-packages\web3\_utils\abi.py", line 102, in <listcomp>
    abi['name'] == name  # type: ignore
KeyError: 'name'

编辑 它适用于 solidity 0.5.x..

web3py 仍然不完全兼容 solidity 版本 0.6.x。 查看 link。 https://github.com/ethereum/web3.py/issues/1566 到那时你可以使用 solidity 0.5.x 并通过 link.

跟踪进度