Skip to content

Commit 0480bf3

Browse files
committed
Add support for PolygonScan
1 parent cafcdff commit 0480bf3

File tree

7 files changed

+373
-116
lines changed

7 files changed

+373
-116
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package io.api.etherscan.core.impl;
2+
3+
import io.api.etherscan.core.*;
4+
import io.api.etherscan.error.ApiException;
5+
import io.api.etherscan.error.ApiKeyException;
6+
import io.api.etherscan.executor.IHttpExecutor;
7+
import io.api.etherscan.executor.impl.HttpExecutor;
8+
import io.api.etherscan.manager.IQueueManager;
9+
import io.api.etherscan.manager.impl.FakeQueueManager;
10+
import io.api.etherscan.manager.impl.QueueManager;
11+
import io.api.etherscan.model.EthNetwork;
12+
import io.api.etherscan.model.network.Network;
13+
import io.api.etherscan.util.BasicUtils;
14+
import org.jetbrains.annotations.NotNull;
15+
16+
import java.util.function.Supplier;
17+
18+
public abstract class BaseApi implements AutoCloseable {
19+
20+
private static final Supplier<IHttpExecutor> DEFAULT_SUPPLIER = HttpExecutor::new;
21+
22+
public static final String DEFAULT_KEY = "YourApiKeyToken";
23+
24+
private final IQueueManager queueManager;
25+
private final IAccountApi account;
26+
private final IBlockApi block;
27+
private final IContractApi contract;
28+
private final ILogsApi logs;
29+
private final IProxyApi proxy;
30+
private final IStatisticApi stats;
31+
private final ITransactionApi txs;
32+
33+
public BaseApi() {
34+
this(DEFAULT_KEY, EthNetwork.MAINNET);
35+
}
36+
37+
public BaseApi(final Network network) {
38+
this(DEFAULT_KEY, network);
39+
}
40+
41+
public BaseApi(final String apiKey) {
42+
this(apiKey, EthNetwork.MAINNET);
43+
}
44+
45+
public BaseApi(final Network network,
46+
final Supplier<IHttpExecutor> executorSupplier) {
47+
this(DEFAULT_KEY, network, executorSupplier);
48+
}
49+
50+
public BaseApi(final String apiKey,
51+
final Network network,
52+
final IQueueManager queue) {
53+
this(apiKey, network, DEFAULT_SUPPLIER, queue);
54+
}
55+
56+
public BaseApi(final String apiKey,
57+
final Network network) {
58+
this(apiKey, network, DEFAULT_SUPPLIER);
59+
}
60+
61+
public BaseApi(final String apiKey,
62+
final Network network,
63+
final Supplier<IHttpExecutor> executorSupplier) {
64+
this(apiKey, network, executorSupplier,
65+
DEFAULT_KEY.equals(apiKey)
66+
? QueueManager.DEFAULT_KEY_QUEUE
67+
: new FakeQueueManager());
68+
}
69+
70+
public BaseApi(final String apiKey,
71+
final Network network,
72+
final Supplier<IHttpExecutor> executorSupplier,
73+
final IQueueManager queue) {
74+
if (BasicUtils.isBlank(apiKey))
75+
throw new ApiKeyException("API key can not be null or empty");
76+
77+
if (network == null)
78+
throw new ApiException("Ethereum Network is set to NULL value");
79+
80+
// EtherScan 1request\5sec limit support by queue manager
81+
final IHttpExecutor executor = executorSupplier.get();
82+
83+
final String baseUrl = network.getUrl() + apiKey;
84+
85+
this.queueManager = queue;
86+
this.account = new AccountApiProvider(queue, baseUrl, executor);
87+
this.block = new BlockApiProvider(queue, baseUrl, executor);
88+
this.contract = new ContractApiProvider(queue, baseUrl, executor);
89+
this.logs = new LogsApiProvider(queue, baseUrl, executor);
90+
this.proxy = new ProxyApiProvider(queue, baseUrl, executor);
91+
this.stats = new StatisticApiProvider(queue, baseUrl, executor);
92+
this.txs = new TransactionApiProvider(queue, baseUrl, executor);
93+
}
94+
95+
@NotNull
96+
public IAccountApi account() {
97+
return account;
98+
}
99+
100+
@NotNull
101+
public IContractApi contract() {
102+
return contract;
103+
}
104+
105+
@NotNull
106+
public ITransactionApi txs() {
107+
return txs;
108+
}
109+
110+
@NotNull
111+
public IBlockApi block() {
112+
return block;
113+
}
114+
115+
@NotNull
116+
public ILogsApi logs() {
117+
return logs;
118+
}
119+
120+
@NotNull
121+
public IProxyApi proxy() {
122+
return proxy;
123+
}
124+
125+
@NotNull
126+
public IStatisticApi stats() {
127+
return stats;
128+
}
129+
130+
@Override
131+
public void close() throws Exception {
132+
queueManager.close();
133+
}
134+
135+
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
package io.api.etherscan.core.impl;
22

3-
import io.api.etherscan.core.*;
4-
import io.api.etherscan.error.ApiException;
5-
import io.api.etherscan.error.ApiKeyException;
63
import io.api.etherscan.executor.IHttpExecutor;
7-
import io.api.etherscan.executor.impl.HttpExecutor;
84
import io.api.etherscan.manager.IQueueManager;
9-
import io.api.etherscan.manager.impl.FakeQueueManager;
10-
import io.api.etherscan.manager.impl.QueueManager;
115
import io.api.etherscan.model.EthNetwork;
12-
import io.api.etherscan.util.BasicUtils;
13-
import org.jetbrains.annotations.NotNull;
146

157
import java.util.function.Supplier;
168

@@ -20,121 +12,37 @@
2012
* @author GoodforGod
2113
* @since 28.10.2018
2214
*/
23-
public class EtherScanApi implements AutoCloseable {
24-
25-
private static final Supplier<IHttpExecutor> DEFAULT_SUPPLIER = HttpExecutor::new;
26-
27-
public static final String DEFAULT_KEY = "YourApiKeyToken";
28-
29-
private final IQueueManager queueManager;
30-
private final IAccountApi account;
31-
private final IBlockApi block;
32-
private final IContractApi contract;
33-
private final ILogsApi logs;
34-
private final IProxyApi proxy;
35-
private final IStatisticApi stats;
36-
private final ITransactionApi txs;
15+
public class EtherScanApi extends BaseApi {
3716

3817
public EtherScanApi() {
39-
this(DEFAULT_KEY, EthNetwork.MAINNET);
40-
}
41-
42-
public EtherScanApi(final EthNetwork network) {
43-
this(DEFAULT_KEY, network);
44-
}
45-
46-
public EtherScanApi(final String apiKey) {
47-
this(apiKey, EthNetwork.MAINNET);
48-
}
49-
50-
public EtherScanApi(final EthNetwork network,
51-
final Supplier<IHttpExecutor> executorSupplier) {
52-
this(DEFAULT_KEY, network, executorSupplier);
53-
}
54-
55-
public EtherScanApi(final String apiKey,
56-
final EthNetwork network,
57-
final IQueueManager queue) {
58-
this(apiKey, network, DEFAULT_SUPPLIER, queue);
59-
}
60-
61-
public EtherScanApi(final String apiKey,
62-
final EthNetwork network) {
63-
this(apiKey, network, DEFAULT_SUPPLIER);
64-
}
65-
66-
public EtherScanApi(final String apiKey,
67-
final EthNetwork network,
68-
final Supplier<IHttpExecutor> executorSupplier) {
69-
this(apiKey, network, executorSupplier,
70-
DEFAULT_KEY.equals(apiKey)
71-
? QueueManager.DEFAULT_KEY_QUEUE
72-
: new FakeQueueManager());
73-
}
74-
75-
public EtherScanApi(final String apiKey,
76-
final EthNetwork network,
77-
final Supplier<IHttpExecutor> executorSupplier,
78-
final IQueueManager queue) {
79-
if (BasicUtils.isBlank(apiKey))
80-
throw new ApiKeyException("API key can not be null or empty");
81-
82-
if (network == null)
83-
throw new ApiException("Ethereum Network is set to NULL value");
84-
85-
// EtherScan 1request\5sec limit support by queue manager
86-
final IHttpExecutor executor = executorSupplier.get();
87-
88-
final String ending = EthNetwork.TOBALABA.equals(network) ? "com" : "io";
89-
final String baseUrl = "https://" + network.getDomain() + ".etherscan." + ending + "/api" + "?apikey=" + apiKey;
90-
91-
this.queueManager = queue;
92-
this.account = new AccountApiProvider(queue, baseUrl, executor);
93-
this.block = new BlockApiProvider(queue, baseUrl, executor);
94-
this.contract = new ContractApiProvider(queue, baseUrl, executor);
95-
this.logs = new LogsApiProvider(queue, baseUrl, executor);
96-
this.proxy = new ProxyApiProvider(queue, baseUrl, executor);
97-
this.stats = new StatisticApiProvider(queue, baseUrl, executor);
98-
this.txs = new TransactionApiProvider(queue, baseUrl, executor);
99-
}
100-
101-
@NotNull
102-
public IAccountApi account() {
103-
return account;
18+
super();
10419
}
10520

106-
@NotNull
107-
public IContractApi contract() {
108-
return contract;
21+
public EtherScanApi(EthNetwork network) {
22+
super(network);
10923
}
11024

111-
@NotNull
112-
public ITransactionApi txs() {
113-
return txs;
25+
public EtherScanApi(String apiKey) {
26+
super(apiKey);
11427
}
11528

116-
@NotNull
117-
public IBlockApi block() {
118-
return block;
29+
public EtherScanApi(EthNetwork network, Supplier<IHttpExecutor> executorSupplier) {
30+
super(network, executorSupplier);
11931
}
12032

121-
@NotNull
122-
public ILogsApi logs() {
123-
return logs;
33+
public EtherScanApi(String apiKey, EthNetwork network, IQueueManager queue) {
34+
super(apiKey, network, queue);
12435
}
12536

126-
@NotNull
127-
public IProxyApi proxy() {
128-
return proxy;
37+
public EtherScanApi(String apiKey, EthNetwork network) {
38+
super(apiKey, network);
12939
}
13040

131-
@NotNull
132-
public IStatisticApi stats() {
133-
return stats;
41+
public EtherScanApi(String apiKey, EthNetwork network, Supplier<IHttpExecutor> executorSupplier) {
42+
super(apiKey, network, executorSupplier);
13443
}
13544

136-
@Override
137-
public void close() throws Exception {
138-
queueManager.close();
45+
public EtherScanApi(String apiKey, EthNetwork network, Supplier<IHttpExecutor> executorSupplier, IQueueManager queue) {
46+
super(apiKey, network, executorSupplier, queue);
13947
}
14048
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.api.etherscan.core.impl;
2+
3+
import io.api.etherscan.executor.IHttpExecutor;
4+
import io.api.etherscan.manager.IQueueManager;
5+
import io.api.etherscan.model.network.PolygonNetwork;
6+
7+
import java.util.function.Supplier;
8+
9+
public class PolygonScanApi extends BaseApi {
10+
11+
public PolygonScanApi() {
12+
super(PolygonNetwork.MAINNET);
13+
}
14+
15+
public PolygonScanApi(PolygonNetwork network) {
16+
super(network);
17+
}
18+
19+
public PolygonScanApi(String apiKey) {
20+
super(apiKey, PolygonNetwork.MAINNET);
21+
}
22+
23+
public PolygonScanApi(PolygonNetwork network, Supplier<IHttpExecutor> executorSupplier) {
24+
super(network, executorSupplier);
25+
}
26+
27+
public PolygonScanApi(String apiKey, PolygonNetwork network, IQueueManager queue) {
28+
super(apiKey, network, queue);
29+
}
30+
31+
public PolygonScanApi(String apiKey, PolygonNetwork network) {
32+
super(apiKey, network);
33+
}
34+
35+
public PolygonScanApi(String apiKey, PolygonNetwork network, Supplier<IHttpExecutor> executorSupplier) {
36+
super(apiKey, network, executorSupplier);
37+
}
38+
39+
public PolygonScanApi(String apiKey, PolygonNetwork network, Supplier<IHttpExecutor> executorSupplier, IQueueManager queue) {
40+
super(apiKey, network, executorSupplier, queue);
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,42 @@
11
package io.api.etherscan.model;
22

3+
import io.api.etherscan.model.network.Network;
4+
35
/**
46
* ! NO DESCRIPTION !
57
*
68
* @author GoodforGod
79
* @since 28.10.2018
810
*/
9-
public enum EthNetwork {
11+
public enum EthNetwork implements Network {
1012

11-
MAINNET("api"),
12-
ROPSTEN("api-ropsten"),
13-
KOVAN("api-kovan"),
14-
TOBALABA("api-tobalaba"),
15-
GORLI("api-goerli"),
16-
RINKEBY("api-rinkeby");
13+
MAINNET("api", "io"),
14+
ROPSTEN("api-ropsten", "io"),
15+
KOVAN("api-kovan", "io"),
16+
TOBALABA("api-tobalaba", "com"),
17+
GORLI("api-goerli", "io"),
18+
RINKEBY("api-rinkeby", "io");
1719

1820
private final String domain;
21+
private final String ending;
1922

20-
EthNetwork(String domain) {
23+
EthNetwork(String domain, String ending) {
2124
this.domain = domain;
25+
this.ending = ending;
2226
}
2327

28+
@Override
2429
public String getDomain() {
2530
return domain;
2631
}
32+
33+
@Override
34+
public String getExplorerName() {
35+
return "etherscan";
36+
}
37+
38+
@Override
39+
public String getEnding() {
40+
return ending;
41+
}
2742
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.api.etherscan.model.network;
2+
3+
public interface Network {
4+
5+
String getDomain();
6+
7+
String getExplorerName();
8+
9+
String getEnding();
10+
11+
default String getUrl() {
12+
return "https://" + getDomain() + "." + getExplorerName() + "." + getEnding() + "/api?apikey=";
13+
}
14+
}

0 commit comments

Comments
 (0)