invoke a smart contract function from java application without need to listen to events

China☆狼群 提交于 2019-12-13 02:13:03

问题


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.

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

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

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

but this error appears:

 Error processing transaction request: Error: Exceeds block gas limit

Thanks


回答1:


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

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 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.

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

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);

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.

EDIT: 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.

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.

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.



来源:https://stackoverflow.com/questions/48905703/invoke-a-smart-contract-function-from-java-application-without-need-to-listen-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!