Solidity

無法通過 web3.py 訪問合約功能

  • May 15, 2018

我已經創建了合約並部署到了乙太坊主網。現在我想讀日誌。但我不能打電話print(contract_instance.address)contract_instance.eventFilter('Transfers')我看錯誤日誌。我究竟做錯了什麼?

import web3

from web3 import Web3, KeepAliveRPCProvider, IPCProvider
from web3.contract import ConciseContract
from solc import compile_source


ADDRESS = "..."
START_BLOCK = 4551217
END_BLOCK = 4599662

contract_source_code = '''...
'''


compiled_sol = compile_source(contract_source_code) # Compiled source code
contract_interface = compiled_sol['<stdin>:CustomToken']


web3 = Web3(KeepAliveRPCProvider(host='...', port='8545'))

contract_instance = web3.eth.contract(contract_interface['abi'], ADDRESS, ContractFactoryClass=ConciseContract)
print(contract_instance.address)

transfer_filter = contract_instance.eventFilter('Transfers')

錯誤日誌:

Traceback (most recent call last):
 File "analysis.py", line 488, in <module>
   print(contract_instance.creator())
 File "/usr/local/lib/python3.6/site-packages/web3/contract.py", line 803, in __call__
   return self.__prepared_function(**kwargs)(*args)
 File "/usr/local/lib/python3.6/site-packages/web3/contract.py", line 832, in call_contract_function
   transaction=transaction,
 File "/usr/local/lib/python3.6/site-packages/web3/utils/decorators.py", line 13, in _wrapper
   return self.method(obj, *args, **kwargs)
 File "/usr/local/lib/python3.6/site-packages/web3/contract.py", line 692, in _prepare_transaction
   fn_kwargs,
 File "/usr/local/lib/python3.6/site-packages/eth_utils/string.py", line 85, in inner
   return force_obj_to_text(fn(*args, **kwargs))
 File "/usr/local/lib/python3.6/site-packages/web3/contract.py", line 731, in _encode_transaction_data
   fn_name, args, kwargs,
 File "/usr/local/lib/python3.6/site-packages/web3/contract.py", line 660, in _get_function_info
   fn_abi = cls._find_matching_fn_abi(fn_name, args, kwargs)
 File "/usr/local/lib/python3.6/site-packages/web3/contract.py", line 624, in _find_matching_fn_abi
   raise ValueError("No matching functions found")
ValueError: No matching functions found

v3 與 v4

*更新:*當 v3 是最新的穩定版本時,問題是使用 web3.py v3 編寫的。v4 現在是穩定的,並且是首選。請注意,兩個版本都有可用的文件:v3 (old)v4 (current)

eventFilter在 v3 中

eventFilter僅在 v4 中可用。現在最簡單的解決方案是升級,使用pip install --upgrade web3. 如果不能,請使用v3 事件方法

經典與ConciseContract

看起來你想使用經典的合約介面,因為ConciseContract它更適合簡單的合約呼叫。所以刪除ConciseContract這一行中的變體:

contract_instance = web3.eth.contract(contract_interface['abi'], ADDRESS, ContractFactoryClass=ConciseContract)

更改後的行(如下)將提供對onpastEvents方法的訪問。

contract_instance = web3.eth.contract(contract_interface['abi'], ADDRESS)

合約參數順序

此外,位置 ABI 參數選項在 v3 中已棄用,並在 v4 中刪除。所以你應該改用這一行,它適用於 v3 和 v4:

contract_instance = web3.eth.contract(ADDRESS, abi=contract_interface['abi'])

引用自:https://ethereum.stackexchange.com/questions/31428