I have this line of code where it registers a transaction in blockchain. And I wanted to know how I can make the nonce different between the transactions?
txhash = careercertificate
.createCertificate(id, fecha, nombre, rutalumno, instituto, rutinstituto, titulo, totalhash, fechatitulacion, registronumber, codigoV)
.send().getTransactionHash();
Best Answer
Under the hood, the Web3J SmartContract Java Wrapper calculates the nonce by getting the number of transactions
getTransactionCount
for the account (credential
).If for some reasons, you want to manually set the nonce (because you're sending multiple parallel transactions I guess), it is not an easy task and you gonna have to replicate what web3j is doing behind the scene.
i. Some constants
ii. Connect to the node and initiate web3j
iii. Read the private key
iv. Calculate the transaction data
A transaction is constituted of several information like
from
account,to
account,value
,gasPrice
,gasLimit
,nonce
anddata
.data
in the case of a smart contact represents the encoding of the function, the parameter types and the parameters.You can calculate
data
like this:v. Choose a nonce
Based on whatever you want, select a nonce (/!\ must be greater than the nonce of the last transaction on this account)
vi. Prepare the transaction
vii. Send the transaction
You can find the code here: