-->

invoke a smart contract function from java applica

2019-08-01 11:22发布

问题:

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.