如何从web3py获取事件数据?

时间:2018-04-16 09:42:56

标签: python web3

pragma solidity ^0.4.17

contract Test {
    event greeting(string name);
    function say() pure public {
        greeting('jack');
    }
}

如何获取活动数据' jack'什么时候在python版web3.py中调用say()函数?下面是我的python代码。

contractAddress = '0x345ca3e014aaf5dca488057592ee47305d9b3e10'
contract = w3.eth.contract(address=contractAddress, abi=abiJson['abi'])
accounts = w3.eth.accounts

def handle_event(event):
    print(event)

def log_loop(event_filter, poll_interval):
    while True:
        for event in event_filter.get_new_entries():
            handle_event(event)
            time.sleep(poll_interval)

block_filter = w3.eth.filter({'fromBlock':'latest', 'address':contractAddress})
log_loop(block_filter, 2)

2 个答案:

答案 0 :(得分:0)

问题已解决,正确的代码应如下所示:

contractAddress = '0x345ca3e014aaf5dca488057592ee47305d9b3e10'
contract = w3.eth.contract(address=contractAddress, abi=abiJson['abi'])
accounts = w3.eth.accounts

def handle_event(event):
    receipt = w3.eth.waitForTransactionReceipt(event['transactionHash'])
    result = contract.events.greeting.processReceipt(receipt)
    print(result[0]['args'])

def log_loop(event_filter, poll_interval):
    while True:
        for event in event_filter.get_new_entries():
            handle_event(event)
            time.sleep(poll_interval)

block_filter = w3.eth.filter({'fromBlock':'latest', 'address':contractAddress})
log_loop(block_filter, 2)

答案 1 :(得分:0)

我发现您的代码非常有助于了解如何提取事件信息,但我遇到了一个问题。我会把它写在这里,以防它可以帮助任何人。

线 AGPBI: {"kind":"error","text":"Cannot fit requested classes in a single dex file (# methods: 77644 > 65536)","sources":[{}],"tool":"D8"} com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: The number of method references in a .dex file cannot exceed 64K. Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html at com.android.builder.dexing.D8DexArchiveMerger.getExceptionToRethrow(D8DexArchiveMerger.java:132) at com.android.builder.dexing.D8DexArchiveMerger.mergeDexArchives(D8DexArchiveMerger.java:119) at com.android.build.gradle.internal.transforms.DexMergerTransformCallable.call(DexMergerTransformCallable.java:102) at com.android.build.gradle.internal.tasks.DexMergingTaskRunnable.run(DexMergingTask.kt:432) at com.android.build.gradle.internal.tasks.Workers$ActionFacade.run(Workers.kt:242) at org.gradle.workers.internal.AdapterWorkAction.execute(AdapterWorkAction.java:57) at org.gradle.workers.internal.DefaultWorkerServer.execute(DefaultWorkerServer.java:63) at org.gradle.workers.internal.NoIsolationWorkerFactory$1$1.create(NoIsolationWorkerFactory.java:67) at org.gradle.workers.internal.NoIsolationWorkerFactory$1$1.create(NoIsolationWorkerFactory.java:63) at org.gradle.internal.classloader.ClassLoaderUtils.executeInClassloader(ClassLoaderUtils.java:97) at org.gradle.workers.internal.NoIsolationWorkerFactory$1.lambda$execute$0(NoIsolationWorkerFactory.java:63) at org.gradle.workers.internal.AbstractWorker$1.call(AbstractWorker.java:44) at org.gradle.workers.internal.AbstractWorker$1.call(AbstractWorker.java:41) at org.gradle.internal.operations.DefaultBuildOperationExecutor$CallableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:409) at org.gradle.internal.operations.DefaultBuildOperationExecutor$CallableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:399) at org.gradle.internal.operations.DefaultBuildOperationExecutor$1.execute(DefaultBuildOperationExecutor.java:157) at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:242) at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:150) at org.gradle.internal.operations.DefaultBuildOperationExecutor.call(DefaultBuildOperationExecutor.java:94) at org.gradle.internal.operations.DelegatingBuildOperationExecutor.call(DelegatingBuildOperationExecutor.java:36) at org.gradle.workers.internal.AbstractWorker.executeWrappedInBuildOperation(AbstractWorker.java:41) at org.gradle.workers.internal.NoIsolationWorkerFactory$1.execute(NoIsolationWorkerFactory.java:60) at org.gradle.workers.internal.DefaultWorkerExecutor.lambda$submitWork$2(DefaultWorkerExecutor.java:200) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.runExecution(DefaultConditionalExecutionQueue.java:215) at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.runBatch(DefaultConditionalExecutionQueue.java:164) at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.run(DefaultConditionalExecutionQueue.java:131) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64) at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56) at java.lang.Thread.run(Thread.java:748) Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete, position: null at Version.fakeStackEntry(Version_2.1.86.java:0) at com.android.tools.r8.utils.Y.a(SourceFile:78) at com.android.tools.r8.D8.run(D8.java:11) at com.android.builder.dexing.D8DexArchiveMerger.mergeDexArchives(D8DexArchiveMerger.java:117) ... 33 more Caused by: com.android.tools.r8.utils.b: Cannot fit requested classes in a single dex file (# methods: 77644 > 65536) at com.android.tools.r8.utils.T0.error(SourceFile:1) at com.android.tools.r8.utils.T0.a(SourceFile:2) at com.android.tools.r8.dex.P.a(SourceFile:746) at com.android.tools.r8.dex.P$h.a(SourceFile:7) at com.android.tools.r8.dex.b.a(SourceFile:14) at com.android.tools.r8.dex.b.b(SourceFile:25) at com.android.tools.r8.D8.d(D8.java:133) at com.android.tools.r8.D8.b(D8.java:1) at com.android.tools.r8.utils.Y.a(SourceFile:36) ... 35 more Cannot fit requested classes in a single dex file (# methods: 77644 > 65536) Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete, position: null Caused by: com.android.tools.r8.utils.b: Cannot fit requested classes in a single dex file (# methods: 77644 > 65536) Execution failed for task ':app:mergeDexDebug'. > A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade > com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: The number of method references in a .dex file cannot exceed 64K. Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html 抛出错误: result = contract.events.greeting.processReceipt(receipt)

在一些 digging 之后,它似乎是 Web3py 的一个限制,它可以通过事件的先前实例化来修复。所以,代码将是:

_parse_logs() missing 1 required positional argument: 'errors'