Web3j – How to Send Transactions Continuously Using Web3j Generated Wrapper

javaquorumweb3j

I wrote a smart contract and deployed on my private Quorum. I use Web3j to generate java wrapper to call ABIs. For example,

// here is a smart contract function
function getMoney() external payable 
{
    Event_GotMoney(msg.value, now);
}

In my java, if I call like this

contract.getMoney(new BigInteger(100)).send();
// or
// contract.getMoney(new BigInteger(100));

I can always get Event_GotMoney event.
However, if I want to call it continuously, like this, (asynchronized)

for(int i=0; i<100; i++)
    contract.getMoney(new BigInteger(100));

I can't get 100 Event_GotMoney events. The interval of the timestamp in the event is 3 or 10 seconds(depends on the frequency of generating blocks). I guess the nonce isn't updated util a block generated. If my guess is correct, how can I call smart contract functions continuously? If not, is it a limitation or something?

Another small question, I use event observable functions provided by Web3j wrapper. It works well to get events, but it updates every 15 seconds. How could I modify the interval?

Any feedback would be appreciate.

Best Answer

I found the solution, please refer, #296

Use FastRawTransactionManager to speed up your transactions. Use RawTransactionManager to shorten the polling interval. If you need both, use the following code,

pollingInterval = 3000; // 3 seconds
FastRawTransactionManager fastRawTxMgr = new FastRawTransactionManager(web3, credentials, new PollingTransactionReceiptProcessor(web3j, pollingInterval, 40));
Contract contract = MyPersonalContract.load(contractAddr, web3, fastRawTxMgr, gasPrice, gasLimit);
Related Topic