How to get a transaction record within a day? I am new to Stellar so how do I do this in Java? Is there any way?
2 Answers
This working, untested Java code will print all transaction hashes from the preceding 24 hours:
long ONE_DAY_IN_SECONDS = 60 * 60 * 24;
DateTimeFormatter dateParser = DateTimeFormatter
.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(ZoneId.of("UTC"));
long now = ZonedDateTime.now().toEpochSecond();
Server server = new Server("https://horizon-testnet.stellar.org");
TransactionsRequestBuilder txnRequests = server.transactions().limit(200)
.order(RequestBuilder.Order.DESC);
List<TransactionResponse> responses = new ArrayList<>();
Page<TransactionResponse> page = txnRequests.execute();
boolean limitHit = false;
while (!limitHit) {
page.getRecords().removeIf(txn -> {
ZonedDateTime txnDateTime =
ZonedDateTime.parse(txn.getCreatedAt(), dateParser);
return now - txnDateTime.toEpochSecond() > ONE_DAY_IN_SECONDS;
});
if (page.getRecords().isEmpty()) {
limitHit = true;
} else {
responses.addAll(page.getRecords());
String pagingToken = responses.get(responses.size() - 1).getPagingToken();
page = server.transactions().limit(200).order(RequestBuilder.Order.DESC)
.cursor(pagingToken).execute();
}
}
responses.forEach(txn -> {
System.out.println("txn = " + txn.getHash());
});
-
-
1@srbyk1990 Yes there is. Just add
.forAccount(KeyPair)
onto the transaction lookup chain. e.g.server.transactions().forAccount(myAccn).limit(200)...
– SynessoCommented Apr 24, 2018 at 22:33
If you want just today's tx, just curl the Horizon "transaction" endpoint is ok. Example with testnet: https://horizon-testnet.stellar.org/transactions?order=desc
Crawl the "next" page recursively to check if there are still tx created today.
.
By default, only 10 tx are listed in every page. You may increase it, for example, to 150: https://horizon-testnet.stellar.org/transactions?limit=150&order=desc (Note: largest possible return limit is 200)
The "cursor" parameter can be helpful, but it is DB stuff and unlikely you will use it. If you wish to find all tx of a certain day, you need to build your own Horizon and query the DB yourself. There is a transaction_history table containing tx timestamps.
Note: Timestamps are of UTC.
Better have quick check at Laboratory to see if this suits what you want..