交易随机数

优质
小牛编辑
119浏览
2023-12-01

交易随机数nonce

nonce是一个不断增长的数值,用来唯一地标识交易。一个nonce只能使用一次,直到交易被挖掘完成,可以以相同的随机数发送交易的多个版本,但是一旦其中一个被挖掘完成,其他后续提交的都将被拒绝。

可以通过eth_getTransactionCount方法获得下一个可用的nonce

EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
             address, DefaultBlockParameterName.LATEST).sendAsync().get();

     BigInteger nonce = ethGetTransactionCount.getTransactionCount();

然后可以使用nonce创建你的交易对象:

RawTransaction rawTransaction  = RawTransaction.createEtherTransaction(
             nonce, <gas price>, <gas limit>, <toAddress>, <value>);