Skip to content

Commit 420c68f

Browse files
committed
[2.0.0-SNAPSHOT]
GasTrackerAPI refactored to new API EthHttpClient package refactoring
1 parent 7436509 commit 420c68f

14 files changed

+113
-104
lines changed

src/main/java/io/goodforgod/api/etherscan/AccountAPI.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,21 @@ public interface AccountAPI {
112112
/**
113113
* All ERC-20 token txs for given address and contract address
114114
*
115-
* @param address get txs for
115+
* @param address get txs for
116116
* @param contractAddress contract address to get txs for
117-
* @param startBlock tx from this blockNumber
118-
* @param endBlock tx to this blockNumber
117+
* @param startBlock tx from this blockNumber
118+
* @param endBlock tx to this blockNumber
119119
* @return txs for address
120-
* @throws ApiException parent exception class
120+
* @throws EtherScanException parent exception class
121121
*/
122122
@NotNull
123-
List<TxToken> txsToken(String address, String contractAddress, long startBlock, long endBlock) throws ApiException;
123+
List<TxERC20> txsERC20(String address, String contractAddress, long startBlock, long endBlock) throws EtherScanException;
124124

125125
@NotNull
126-
List<TxToken> txsToken(String address, String contractAddress, long startBlock) throws ApiException;
126+
List<TxERC20> txsERC20(String address, String contractAddress, long startBlock) throws EtherScanException;
127127

128128
@NotNull
129-
List<TxToken> txsToken(String address, String contractAddress) throws ApiException;
129+
List<TxERC20> txsERC20(String address, String contractAddress) throws EtherScanException;
130130

131131
/**
132132
* All ERC-721 (NFT) token txs for given address

src/main/java/io/goodforgod/api/etherscan/AccountAPIProvider.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -234,19 +234,20 @@ public List<TxERC20> txsERC20(String address, long startBlock, long endBlock) th
234234

235235
@NotNull
236236
@Override
237-
public List<TxToken> txsToken(final String address, final String contractAddress) throws ApiException {
238-
return txsToken(address, contractAddress, MIN_START_BLOCK);
237+
public List<TxERC20> txsERC20(String address, String contractAddress) throws EtherScanException {
238+
return txsERC20(address, contractAddress, MIN_START_BLOCK);
239239
}
240240

241241
@NotNull
242242
@Override
243-
public List<TxToken> txsToken(final String address, final String contractAddress, final long startBlock) throws ApiException {
244-
return txsToken(address, contractAddress, startBlock, MAX_END_BLOCK);
243+
public List<TxERC20> txsERC20(String address, String contractAddress, long startBlock) throws EtherScanException {
244+
return txsERC20(address, contractAddress, startBlock, MAX_END_BLOCK);
245245
}
246246

247247
@NotNull
248248
@Override
249-
public List<TxToken> txsToken(final String address, final String contractAddress, final long startBlock, final long endBlock) throws ApiException {
249+
public List<TxERC20> txsERC20(String address, String contractAddress, long startBlock, long endBlock)
250+
throws EtherScanException {
250251
BasicUtils.validateAddress(address);
251252
final BlockParam blocks = BasicUtils.compensateBlocks(startBlock, endBlock);
252253

@@ -255,7 +256,7 @@ public List<TxToken> txsToken(final String address, final String contractAddress
255256
final String urlParams = ACT_TX_TOKEN_ACTION + offsetParam + ADDRESS_PARAM + address
256257
+ CONTRACT_PARAM + contractAddress + blockParam + SORT_ASC_PARAM;
257258

258-
return getRequestUsingOffset(urlParams, TxTokenResponseTO.class);
259+
return getRequestUsingOffset(urlParams, TxERC20ResponseTO.class);
259260
}
260261

261262
@NotNull

src/main/java/io/goodforgod/api/etherscan/BasicProvider.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ abstract class BasicProvider {
3232
private final RequestQueueManager queue;
3333
private final Gson gson;
3434

35-
BasicProvider(RequestQueueManager queue,
35+
BasicProvider(RequestQueueManager requestQueueManager,
3636
String module,
3737
String baseUrl,
38-
EthHttpClient executor) {
39-
this.queue = queue;
38+
EthHttpClient ethHttpClient) {
39+
this.queue = requestQueueManager;
4040
this.module = "&module=" + module;
4141
this.baseUrl = baseUrl;
42-
this.executor = executor;
42+
this.executor = ethHttpClient;
4343
this.gson = new GsonConfiguration().builder().create();
4444
}
4545

src/main/java/io/goodforgod/api/etherscan/EtherScanAPI.java

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public interface EtherScanAPI extends AutoCloseable {
3434
@NotNull
3535
StatisticAPI stats();
3636

37+
@NotNull
38+
GasTrackerAPI gasTracker();
39+
40+
@NotNull
3741
static Builder builder() {
3842
return new EthScanAPIBuilder();
3943
}

src/main/java/io/goodforgod/api/etherscan/EtherScanAPIProvider.java

+15-8
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,25 @@ final class EtherScanAPIProvider implements EtherScanAPI {
2121
private final ProxyAPI proxy;
2222
private final StatisticAPI stats;
2323
private final TransactionAPI txs;
24+
private final GasTrackerAPI gasTracker;
2425

2526
EtherScanAPIProvider(String apiKey,
2627
EthNetwork network,
2728
Supplier<EthHttpClient> executorSupplier,
2829
RequestQueueManager queue) {
2930
// EtherScan 1request\5sec limit support by queue manager
30-
final EthHttpClient executor = executorSupplier.get();
31+
final EthHttpClient ethHttpClient = executorSupplier.get();
3132
final String baseUrl = network.domain() + "?apikey=" + apiKey;
3233

3334
this.requestQueueManager = queue;
34-
this.account = new AccountAPIProvider(queue, baseUrl, executor);
35-
this.block = new BlockAPIProvider(queue, baseUrl, executor);
36-
this.contract = new ContractAPIProvider(queue, baseUrl, executor);
37-
this.logs = new LogsAPIProvider(queue, baseUrl, executor);
38-
this.proxy = new ProxyAPIProvider(queue, baseUrl, executor);
39-
this.stats = new StatisticAPIProvider(queue, baseUrl, executor);
40-
this.txs = new TransactionAPIProvider(queue, baseUrl, executor);
35+
this.account = new AccountAPIProvider(queue, baseUrl, ethHttpClient);
36+
this.block = new BlockAPIProvider(queue, baseUrl, ethHttpClient);
37+
this.contract = new ContractAPIProvider(queue, baseUrl, ethHttpClient);
38+
this.logs = new LogsAPIProvider(queue, baseUrl, ethHttpClient);
39+
this.proxy = new ProxyAPIProvider(queue, baseUrl, ethHttpClient);
40+
this.stats = new StatisticAPIProvider(queue, baseUrl, ethHttpClient);
41+
this.txs = new TransactionAPIProvider(queue, baseUrl, ethHttpClient);
42+
this.gasTracker = new GasTrackerAPIProvider(queue, baseUrl, ethHttpClient);
4143
}
4244

4345
@NotNull
@@ -82,6 +84,11 @@ public StatisticAPI stats() {
8284
return stats;
8385
}
8486

87+
@Override
88+
public @NotNull GasTrackerAPI gasTracker() {
89+
return gasTracker;
90+
}
91+
8592
@Override
8693
public void close() throws Exception {
8794
requestQueueManager.close();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.goodforgod.api.etherscan;
2+
3+
import io.goodforgod.api.etherscan.error.EtherScanException;
4+
import io.goodforgod.api.etherscan.model.GasOracle;
5+
import org.jetbrains.annotations.NotNull;
6+
7+
/**
8+
* EtherScan - API Descriptions
9+
* <a href="https://docs.etherscan.io/api-endpoints/gas-tracker">...</a>
10+
*
11+
* @author Abhay Gupta
12+
* @since 14.11.2022
13+
*/
14+
public interface GasTrackerAPI {
15+
16+
/**
17+
* GasOracle details
18+
*
19+
* @return fast, suggested gas price
20+
* @throws EtherScanException parent exception class
21+
*/
22+
@NotNull
23+
GasOracle oracle() throws EtherScanException;
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.goodforgod.api.etherscan;
2+
3+
import io.goodforgod.api.etherscan.error.EtherScanException;
4+
import io.goodforgod.api.etherscan.error.EtherScanResponseException;
5+
import io.goodforgod.api.etherscan.executor.EthHttpClient;
6+
import io.goodforgod.api.etherscan.manager.RequestQueueManager;
7+
import io.goodforgod.api.etherscan.model.GasOracle;
8+
import io.goodforgod.api.etherscan.model.response.GasOracleResponseTO;
9+
import org.jetbrains.annotations.NotNull;
10+
11+
/**
12+
* GasTracker API Implementation
13+
*
14+
* @see GasTrackerAPI
15+
* @author Abhay Gupta
16+
* @since 14.11.2022
17+
*/
18+
final class GasTrackerAPIProvider extends BasicProvider implements GasTrackerAPI {
19+
20+
private static final String ACT_GAS_ORACLE_PARAM = ACT_PREFIX + "gasoracle";
21+
22+
GasTrackerAPIProvider(RequestQueueManager queue,
23+
String baseUrl,
24+
EthHttpClient ethHttpClient) {
25+
super(queue, "gastracker", baseUrl, ethHttpClient);
26+
}
27+
28+
@NotNull
29+
@Override
30+
public GasOracle oracle() throws EtherScanException {
31+
final GasOracleResponseTO response = getRequest(ACT_GAS_ORACLE_PARAM, GasOracleResponseTO.class);
32+
if (response.getStatus() != 1)
33+
throw new EtherScanResponseException(response);
34+
35+
return response.getResult();
36+
}
37+
}

src/main/java/io/goodforgod/api/etherscan/GasTrackerApiProvider.java

-39
This file was deleted.

src/main/java/io/goodforgod/api/etherscan/IGasTrackerApi.java

-23
This file was deleted.

src/main/java/io/goodforgod/api/etherscan/model/BlockUncle.java

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.goodforgod.api.etherscan.model;
22

33
import io.goodforgod.api.etherscan.util.BasicUtils;
4-
54
import java.math.BigInteger;
65
import java.util.List;
76

src/main/java/io/api/etherscan/model/GasOracle.java renamed to src/main/java/io/goodforgod/api/etherscan/model/GasOracle.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
package io.api.etherscan.model;
1+
package io.goodforgod.api.etherscan.model;
22

33
import java.math.BigInteger;
44
import java.util.Objects;
55

66
/**
7-
* ! NO DESCRIPTION !
8-
*
97
* @author Abhay Gupta
108
* @since 14.11.2022
119
*/
1210
public class GasOracle {
11+
1312
private Long LastBlock;
1413
private Integer SafeGasPrice;
1514
private Integer ProposeGasPrice;
@@ -43,10 +42,14 @@ public String getGasUsedRatio() {
4342

4443
@Override
4544
public boolean equals(Object o) {
46-
if (this == o) return true;
47-
if (o == null || getClass() != o.getClass()) return false;
45+
if (this == o)
46+
return true;
47+
if (o == null || getClass() != o.getClass())
48+
return false;
4849
GasOracle gasOracle = (GasOracle) o;
49-
return LastBlock.equals(gasOracle.LastBlock) && SafeGasPrice.equals(gasOracle.SafeGasPrice) && ProposeGasPrice.equals(gasOracle.ProposeGasPrice) && FastGasPrice.equals(gasOracle.FastGasPrice) && suggestBaseFee.equals(gasOracle.suggestBaseFee) && gasUsedRatio.equals(gasOracle.gasUsedRatio);
50+
return LastBlock.equals(gasOracle.LastBlock) && SafeGasPrice.equals(gasOracle.SafeGasPrice)
51+
&& ProposeGasPrice.equals(gasOracle.ProposeGasPrice) && FastGasPrice.equals(gasOracle.FastGasPrice)
52+
&& suggestBaseFee.equals(gasOracle.suggestBaseFee) && gasUsedRatio.equals(gasOracle.gasUsedRatio);
5053
}
5154

5255
@Override

src/main/java/io/goodforgod/api/etherscan/model/response/GasOracleResponseTO.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
package io.api.etherscan.model.utility;
1+
package io.goodforgod.api.etherscan.model.response;
22

3-
import io.api.etherscan.model.GasOracle;
3+
import io.goodforgod.api.etherscan.model.GasOracle;
44

55
/**
6-
* ! NO DESCRIPTION !
7-
*
86
* @author Abhay Gupta
97
* @since 14.11.2022
108
*/

src/test/java/io/goodforgod/api/etherscan/account/AccountTxRc721TokenTest.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
import io.goodforgod.api.etherscan.ApiRunner;
44
import io.goodforgod.api.etherscan.error.EtherScanInvalidAddressException;
5-
import io.goodforgod.api.etherscan.model.TxERC20;
6-
import java.util.List;
7-
85
import io.goodforgod.api.etherscan.model.TxERC721;
6+
import java.util.List;
97
import org.junit.jupiter.api.Test;
108

119
/**

src/test/java/io/goodforgod/api/etherscan/manager/SemaphoreRequestQueueManagerTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
import io.goodforgod.api.etherscan.ApiRunner;
44
import io.goodforgod.api.etherscan.manager.impl.FakeRequestQueueManager;
55
import io.goodforgod.api.etherscan.manager.impl.SemaphoreRequestQueueManager;
6+
import java.time.Duration;
67
import org.junit.jupiter.api.Test;
78
import org.junit.jupiter.api.Timeout;
89

9-
import java.time.Duration;
10-
1110
/**
1211
* @author GoodforGod
1312
* @since 03.11.2018
@@ -38,7 +37,8 @@ void queueManager() {
3837
@Test
3938
@Timeout(4500)
4039
void queueManagerWithDelay() {
41-
RequestQueueManager requestQueueManager = new SemaphoreRequestQueueManager(1, Duration.ofSeconds(2), Duration.ofSeconds(2));
40+
RequestQueueManager requestQueueManager = new SemaphoreRequestQueueManager(1, Duration.ofSeconds(2),
41+
Duration.ofSeconds(2));
4242
requestQueueManager.takeTurn();
4343
requestQueueManager.takeTurn();
4444
assertNotNull(requestQueueManager);

0 commit comments

Comments
 (0)