从Java应用程序调用智能合约功能,而无需侦听事件 [英] invoke a smart contract function from java application without need to listen to events

查看:434
本文介绍了从Java应用程序调用智能合约功能,而无需侦听事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,要提取事件,必须使用TransactionReceipt.

As I understood that we have to use TransactionReceipt if we want to extract the events..

TransactionReceipt transactionReceipt = contract.someMethod(
         <param1>,
         ...).send();

但是例如,如果我有一个名为注册"的功能,并且需要许多帐户通过调用功能寄存器来注册自己,那该怎么办?

but what about for example if I have a function called "register" and need many accounts to register their self by invoking the function register.

如果TransactionReceipt没有参数(来自哪个帐户,加油站限额,.. etc)的参数,我如何定义帐户(许多凭证).

how I can define accounts ( many credentials ) if the TransactionReceipt doesn't have parameters for ( from which account, gas limit, ..etc).

还有另一件事,我使用TransactionReceipt如下调用了注册"功能:

One more thing that I invoked the "register" function using TransactionReceipt as the following:

 TransactionReceipt transactionReceipt = contract.register("John",BigInteger.valueOf(101)).send();

但出现此错误:

 Error processing transaction request: Error: Exceeds block gas limit

谢谢

推荐答案

据我了解,要提取事件,必须使用TransactionReceipt.

As I understood that we have to use TransactionReceipt if we want to extract the events..

TransactionReceipt不是侦听事件的唯一方法.您还可以设置可观察的过滤器:

TransactionReceipt is not the only way to listen for events. You can also set up an Observable filter:

contract.someEventObservable(startBlock, endBlock).subscribe(event -> ...);

TransactionReceipt是一种访问针对特定事务引发的事件的好方法.交易期间引发的所有事件均包含在收据中.但是,如果要跨多个事务处理一般的事件和/或使用过滤器,则要使用可观察的过滤器.有关事件过滤器的整节内容,请参见此处.

TransactionReceipt is a good way to get access to the events thrown for one specific transaction. All events thrown during the transaction are included in the receipt. However, if you want to process events in general across multiple transactions and/or use filters, you want to use an Observable filter. There's an entire section on event filters with examples here.

如果TransactionReceipt没有参数(来自哪个帐户,加油限额,.. etc),我该如何定义帐户(许多凭证).

how I can define accounts ( many credentials ) if the TransactionReceipt doesn't have parameters for ( from which account, gas limit, ..etc).

如果我正确地理解了这个问题,那么您想知道如何处理TransactionReceipt的事件部分吗? Web3j在合同实例中提供了一个助手方法,该方法将处理TransactionReceipt中的日志.

If I'm understanding this question correctly, you want to know how to process the events section of the TransactionReceipt? Web3j provides a helper method in the contract instance which will process the logs from TransactionReceipt.

EventValues eventValues = contract.processEVENT_NAMEEvent(transactionReceipt);

用感兴趣的事件类型替换EVENT_NAME.识别事件所需的任何帐户特定信息(地址,名称等)都应包括在事件本身中.

Replace EVENT_NAME with the event type you're interested in. Any account specific information you need to identify the event you want (address, name, etc) should be included in the event itself.

编辑:根据您的评论,我似乎误解了您的问题的这一部分.如果这对于处理事件和在下面解决您的问题很有用,请在此保留我以前的答案.

Based on your comment, it looks like I misunderstood this part of your question. I'll leave my previous answer here in case it's useful for processing events and address your question below.

在创建合同实例之后(通过deployload),您可以更改天然气限额和天然气价格.两者在包装器的父类中都有二传手.因此,您可以重复使用相同的包装器,以使用特定功能的适当气体参数来调用合同中的不同功能.

After you create your contract instance (either through deploy or load), you can change the gas limit and gas price. Both have setters in the wrapper's parent class. Therefore, you can reuse the same wrapper to call different functions in your contract using the appropriate gas parameters for that particular function.

但是,您不能更改基础的Credentials(至少在没有子类化或更改生成的包装器的情况下也是如此).对于不同的凭据,请使用.load创建不同的包装器对象.

However, you cannot change the underlying Credentials (at least, not without subclassing or changing the generated wrapper). For different credentials, create different wrapper objects using .load.

但出现此错误:

but this error appears:

Error processing transaction request: Error: Exceeds block gas limit

在没有看到用于调用该函数的合同和代码的情况下,我无能为力.

I can't help with this without seeing the contract and code used to call the function.

这篇关于从Java应用程序调用智能合约功能,而无需侦听事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆