【acuibc】获得日志和遍历区块获得交易日志的代码(备忘)
获得日志
LogFilter filter = new LogFilter();
// filter.setAddress(Arrays.asList(address));
filter.setFromEpoch(Epoch.numberOf(2025454));
filter.setToEpoch(Epoch.numberOf(2025658));
// filter.setLimit(20000l);
Request<List<Log>, Log.Response> req = cfx.getLogs(filter);
List<Log> logs = req.sendAndGet();
System.out.println("logs.size=" + logs.size());
for(Log log : logs) {
System.out.println("address=" + log.getAddress() + ", epochNumber=" + log.getEpochNumber().orElse(BigInteger.ZERO));
if(StringUtils.startsWithIgnoreCase(log.getAddress(), "0x1")) {
System.out.println("address=" + log.getAddress());
System.out.println("data=" + log.getData());
List<String> topics = log.getTopics();
for(String topic : topics) {
System.out.println("topic=" + topic);
}
System.out.println("transactionHash=" + log.getTransactionHash());
System.out.println("————————————————————————————————————————————————————");
}
}
遍历区块获得交易日志
Request<BigInteger, BigIntResponse> req = cfx.getEpochNumber(Epoch.latestMined());
BigInteger end = req.sendAndGet();
getTransactionsByAddr(address, 2025456, 2025656);
private void getTransactionsByAddr(String address, long startBlockNumber, long endBlockNumber) {
long ts = System.currentTimeMillis();
CFXBlockChain bc = Lookup.getDefault().lookup(CFXBlockChain.class);
Cfx cfx = bc.getCfx();
for(long i=startBlockNumber; i<=endBlockNumber; i++) {
Request<Optional<Block>, Block.Response> req = cfx.getBlockByEpoch(Epoch.numberOf(i));
Optional<Block> block = req.sendAndGet(3, 100000);
List<Transaction> transactions = block.get().getTransactions();
for(Transaction trans : transactions) {
// System.out.println(trans.getFrom());
if(StringUtils.equalsAnyIgnoreCase(address, trans.getFrom(), trans.getTo().get())) {
System.out.println("tx hash=" + trans.getHash());
System.out.println("nonce =" + trans.getNonce());
System.out.println("blockHash =" + trans.getBlockHash());
System.out.println("transactionIndex=" + trans.getTransactionIndex().get());
System.out.println("from =" + trans.getFrom());
System.out.println("to =" + trans.getTo().get());
System.out.println("value =" + trans.getValue());
System.out.println("time =" + trans.getContractCreated().orElse("无"));
System.out.println("gasPrice =" + trans.getGasPrice());
System.out.println("gas =" + trans.getGas());
System.out.println("epoch height =" + trans.getEpochHeight());
System.out.println("————————————————————————————————————————————————————");
}
}
}
System.out.println("ts=" + (System.currentTimeMillis() - ts));
}