We're currently developing an App for Android which uses web3j and its functions. We use the Generated SmartContract Wrapper which we created from our Smart Contract.
Now Contract creation can take some time (would appreciate tips on how high we should set Gas Prices on the Ropsten Testnet). We use this method:
TestContract contract = TestContract.deploy(web3, credentials,
GAS_PRICE, GAS_LIMIT, eth.toBigInteger(),
stringToBytes32(conditions), eth.toBigInteger(), Addresses, Roles).send(); // constructor params
Now this method is the one, that can take really long. In Android that's a problem. And we only seem to be able to access the Contract Address after it is done. (Well it's logical, that the Contract has to be mined first) We were wondering, if we can somehow get the Transaction Hash out beforehand, so in case of an Error (The Method seems to throw an Exception after 5 Minutes of the contract not having been deployed) we at least have a point where we can check it's progress and further it's address once it's deployed.
The generated wrapper classes purposely abstract the client away from knowing about some of the complexities on sending transactions including the transaction hash, signing the transaction, encoding the data, etc. If you want to get access to it, you have to interact with the TransactionManager
directly. The manager exposes a sendTransaction
method that returns EthSendTransaction
public abstract EthSendTransaction sendTransaction(BigInteger gasPrice, BigInteger gasLimit, String toAddress, String data, BigInteger value) throws IOException;
From there, you can call EthSendTransaction.getTransactionHash()
to get the transaction hash.
If the only thing you care about is increasing the timeout, the polling/timeout of transactions is controlled through the TransactionReceiptProcessor
used inside TransactionManager
:
public static final int DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH = 40;
public static final long DEFAULT_POLLING_FREQUENCY = 15000L;
You can override this by passing in your own instance of PollingTransactionReceiptProcessor
instead of using the default one created in TransactionManager
:
RawTransactionManager manager = new RawTransactionManager(web3, credentials, CHAIN_ID,
new PollingTransactionReceiptProcessor(web3, SLEEP_IN_MILLIS, MAX_POLL_ATTEMPTS));
TestContract contract = TestContract.deploy(web3, manager,
GAS_PRICE, GAS_LIMIT, eth.toBigInteger(),
stringToBytes32(conditions), eth.toBigInteger(), Addresses, Roles).send();
Speeding up the mining time of your transaction is going to vary depending on the load on the blockchain. Most of the time, you're looking at gas prices of 10-20 GWei for a reasonable amount of time waiting (<5 minutes). Even then, there will be times where you'll pay >20 GWei and it will take 10+ minutes, or you'll spend 5 GWei and it will be mined in 30 seconds. It all depends on the load. You can use metadata on new blocks added to the blockchain to determine the load and average gas prices (if you need to do this dynamically in code), or you can just look at https://ethgasstation.info/.