Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitec0cf80

Browse files
committed
Higher-level API to read well-known ETH events as polymorphic classes
1 parent235be0d commitec0cf80

File tree

11 files changed

+250
-0
lines changed

11 files changed

+250
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
packageio.api.etherscan.core;
2+
3+
importio.api.etherscan.error.ApiException;
4+
importio.api.etherscan.model.Log;
5+
importio.api.etherscan.model.event.IEvent;
6+
importio.api.etherscan.model.query.impl.LogQuery;
7+
importorg.jetbrains.annotations.NotNull;
8+
9+
importjava.util.List;
10+
11+
/**
12+
* EtherScan - API Descriptions
13+
* https://etherscan.io/apis#logs
14+
*/
15+
publicinterfaceIEventsApi {
16+
17+
/**
18+
* This is a high-level alternative to the ILogsApi and an alternative to the native eth_getLogs
19+
* Read at EtherScan API description for full info!
20+
* @param query build log query
21+
* @return logs according to query
22+
* @throws ApiException parent exception class
23+
*
24+
* @see io.api.etherscan.model.query.impl.LogQueryBuilder
25+
*/
26+
@NotNull
27+
List<IEvent>events(LogQueryquery)throwsApiException;
28+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
packageio.api.etherscan.core.impl;
2+
3+
importio.api.etherscan.core.IEventsApi;
4+
importio.api.etherscan.core.ILogsApi;
5+
importio.api.etherscan.error.ApiException;
6+
importio.api.etherscan.executor.IHttpExecutor;
7+
importio.api.etherscan.manager.IQueueManager;
8+
importio.api.etherscan.model.Log;
9+
importio.api.etherscan.model.event.IEvent;
10+
importio.api.etherscan.model.event.impl.Event;
11+
importio.api.etherscan.model.query.impl.LogQuery;
12+
importio.api.etherscan.model.utility.LogResponseTO;
13+
importio.api.etherscan.util.BasicUtils;
14+
importorg.jetbrains.annotations.NotNull;
15+
16+
importjava.util.Collections;
17+
importjava.util.List;
18+
importjava.util.stream.Collectors;
19+
20+
/**
21+
* Logs API Implementation
22+
*
23+
* @see IEventsApi
24+
*
25+
*/
26+
publicclassEventsApiProviderextendsBasicProviderimplementsIEventsApi {
27+
28+
privatestaticfinalStringACT_LOGS_PARAM =ACT_PREFIX +"getLogs";
29+
30+
EventsApiProvider(finalIQueueManagerqueue,
31+
finalStringbaseUrl,
32+
finalIHttpExecutorexecutor) {
33+
super(queue,"logs",baseUrl,executor);
34+
}
35+
36+
@NotNull
37+
@Override
38+
publicList<IEvent>events(finalLogQueryquery)throwsApiException {
39+
finalStringurlParams =ACT_LOGS_PARAM +query.getParams();
40+
finalLogResponseTOresponse =getRequest(urlParams,LogResponseTO.class);
41+
BasicUtils.validateTxResponse(response);
42+
43+
if (BasicUtils.isEmpty(response.getResult())) {
44+
returnCollections.emptyList();
45+
};
46+
returnresponse
47+
.getResult()
48+
.stream()
49+
.map((log) -> {
50+
StringeventTypeHash =log.getTopics().get(0);
51+
returnIEvent.createEvent(eventTypeHash,log);
52+
})
53+
.collect(Collectors.toList());
54+
}
55+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
packageio.api.etherscan.error;
2+
3+
publicclassEventModelExceptionextendsApiException {
4+
publicEventModelException(Stringmessage) {
5+
super(message);
6+
}
7+
8+
publicEventModelException(Stringmessage,Throwablecause) {
9+
super(message,cause);
10+
}
11+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
packageio.api.etherscan.model.event;
2+
3+
importio.api.etherscan.error.ApiException;
4+
importio.api.etherscan.error.EventModelException;
5+
importio.api.etherscan.model.Log;
6+
7+
importjava.util.HashMap;
8+
importjava.util.Map;
9+
10+
publicinterfaceIEvent {
11+
staticfinalMap<String,Class<?>>subTypes =newHashMap<>();
12+
13+
voidsetLog(Loglog);
14+
15+
staticvoidregisterEventType(StringtypeHash,Class<?>clazz) {
16+
subTypes.put(typeHash,clazz);
17+
}
18+
19+
staticIEventcreateEvent(StringtypeHash,Loglog) {
20+
if (null ==typeHash) {
21+
thrownewEventModelException("Event type hash cannot be null");
22+
}
23+
Class<?>clazz =subTypes.get(typeHash);
24+
try {
25+
IEventevt = (IEvent)clazz.newInstance();
26+
evt.setLog(log);
27+
returnevt;
28+
}catch (InstantiationException |IllegalAccessExceptionex) {
29+
thrownewApiException("Client-side error instantiating Event object",ex);
30+
}
31+
}
32+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
packageio.api.etherscan.model.event.impl;
2+
3+
importio.api.etherscan.model.event.IEvent;
4+
5+
publicclassApprovalEventextendsEvent {
6+
staticfinalStringeventTypeHash ="0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925";
7+
static {
8+
IEvent.registerEventType(ApprovalEvent.eventTypeHash,ApprovalEvent.class);
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
packageio.api.etherscan.model.event.impl;
2+
3+
importio.api.etherscan.model.event.IEvent;
4+
5+
publicclassDepositEventextendsEvent {
6+
staticfinalStringeventTypeHash ="0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c";
7+
static {
8+
IEvent.registerEventType(DepositEvent.eventTypeHash,DepositEvent.class);
9+
}
10+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
packageio.api.etherscan.model.event.impl;
2+
3+
importio.api.etherscan.error.ApiException;
4+
importio.api.etherscan.error.EventModelException;
5+
importio.api.etherscan.model.Log;
6+
importio.api.etherscan.model.event.IEvent;
7+
8+
importjava.util.HashMap;
9+
importjava.util.Map;
10+
11+
/**
12+
* Base class for a higher-level API on top of {@link Log}. Each Event class has an identifying hash
13+
*/
14+
publicclassEventimplementsIEvent {
15+
16+
staticStringeventTypeHash;
17+
18+
privateLoglog;
19+
20+
Stringaddress;
21+
22+
publicstaticStringgetEventTypeHash() {
23+
returneventTypeHash;
24+
}
25+
26+
publicLoggetLog() {
27+
returnlog;
28+
}
29+
30+
publicStringgetAddress() {
31+
returnaddress;
32+
}
33+
34+
publicvoidsetLog(Loglog) {
35+
this.log =log;
36+
}
37+
38+
publicvoidsetAddress(Stringaddress) {
39+
this.address =address;
40+
}
41+
42+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
packageio.api.etherscan.model.event.impl;
2+
3+
importio.api.etherscan.model.event.IEvent;
4+
5+
publicclassMintEventextendsEvent {
6+
staticfinalStringeventTypeHash ="0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f";
7+
static {
8+
IEvent.registerEventType(MintEvent.eventTypeHash,MintEvent.class);
9+
}
10+
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
packageio.api.etherscan.model.event.impl;
2+
3+
importio.api.etherscan.model.event.IEvent;
4+
5+
publicclassSyncEventextendsEvent {
6+
staticfinalStringeventTypeHash ="0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1";
7+
static {
8+
IEvent.registerEventType(SyncEvent.eventTypeHash,SyncEvent.class);
9+
}
10+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
packageio.api.etherscan.model.event.impl;
2+
3+
importio.api.etherscan.model.event.IEvent;
4+
5+
publicclassTransferErc20EventextendsEvent {
6+
staticfinalStringeventTypeHash ="0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef";
7+
static {
8+
IEvent.registerEventType(TransferErc20Event.eventTypeHash,TransferErc20Event.class);
9+
}
10+
11+
StringfromAddress;
12+
13+
StringtoAddress;
14+
15+
publicStringgetFromAddress() {
16+
returnfromAddress;
17+
}
18+
19+
publicvoidsetFromAddress(StringfromAddress) {
20+
this.fromAddress =fromAddress;
21+
}
22+
23+
publicStringgetToAddress() {
24+
returntoAddress;
25+
}
26+
27+
publicvoidsetToAddress(StringtoAddress) {
28+
this.toAddress =toAddress;
29+
}
30+
31+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp