Skip to content

Commit 1416a23

Browse files
committed
[2.0.0-SNAPSHOT]
RequestQueueManager static consts -> static method to produce uniq request queue managers Tests manager provision fixed
1 parent 6d19b73 commit 1416a23

11 files changed

+69
-60
lines changed

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ public TokenBalance balance(String address, String contract) throws EtherScanExc
8484
@NotNull
8585
@Override
8686
public List<Balance> balances(List<String> addresses) throws EtherScanException {
87-
if (BasicUtils.isEmpty(addresses))
87+
if (BasicUtils.isEmpty(addresses)) {
8888
return Collections.emptyList();
89+
}
8990

9091
BasicUtils.validateAddresses(addresses);
9192

@@ -96,13 +97,15 @@ public List<Balance> balances(List<String> addresses) throws EtherScanException
9697
for (final List<String> batch : addressesAsBatches) {
9798
final String urlParams = ACT_BALANCE_MULTI_ACTION + TAG_LATEST_PARAM + ADDRESS_PARAM + toAddressParam(batch);
9899
final BalanceResponseTO response = getRequest(urlParams, BalanceResponseTO.class);
99-
if (response.getStatus() != 1)
100+
if (response.getStatus() != 1) {
100101
throw new EtherScanResponseException(response);
102+
}
101103

102-
if (!BasicUtils.isEmpty(response.getResult()))
104+
if (!BasicUtils.isEmpty(response.getResult())) {
103105
balances.addAll(response.getResult().stream()
104106
.map(r -> new Balance(r.getAccount(), Wei.ofWei(new BigInteger(r.getBalance()))))
105107
.collect(Collectors.toList()));
108+
}
106109
}
107110

108111
return balances;

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

+12-5
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ final class EthScanAPIBuilder implements EtherScanAPI.Builder {
2727
private final Gson gson = new GsonConfiguration().builder().create();
2828

2929
private String apiKey = DEFAULT_KEY;
30+
private RequestQueueManager queueManager;
3031
private EthNetwork ethNetwork = EthNetworks.MAINNET;
31-
private RequestQueueManager queueManager = RequestQueueManager.ANONYMOUS;
3232
private Supplier<EthHttpClient> ethHttpClientSupplier = DEFAULT_SUPPLIER;
3333
private Supplier<Converter> converterSupplier = () -> new Converter() {
3434

@@ -49,9 +49,6 @@ public EtherScanAPI.Builder withApiKey(@NotNull String apiKey) {
4949
throw new EtherScanKeyException("API key can not be null or empty");
5050

5151
this.apiKey = apiKey;
52-
if (!DEFAULT_KEY.equals(apiKey)) {
53-
queueManager = RequestQueueManager.UNLIMITED;
54-
}
5552
return this;
5653
}
5754

@@ -92,6 +89,16 @@ public EtherScanAPI.Builder withConverter(@NotNull Supplier<Converter> converter
9289

9390
@Override
9491
public @NotNull EtherScanAPI build() {
95-
return new EtherScanAPIProvider(apiKey, ethNetwork, queueManager, ethHttpClientSupplier.get(), converterSupplier.get());
92+
RequestQueueManager requestQueueManager;
93+
if (queueManager != null) {
94+
requestQueueManager = queueManager;
95+
} else if (DEFAULT_KEY.equals(apiKey)) {
96+
requestQueueManager = RequestQueueManager.anonymous();
97+
} else {
98+
requestQueueManager = RequestQueueManager.planFree();
99+
}
100+
101+
return new EtherScanAPIProvider(apiKey, ethNetwork, requestQueueManager, ethHttpClientSupplier.get(),
102+
converterSupplier.get());
96103
}
97104
}

src/main/java/io/goodforgod/api/etherscan/manager/RequestQueueManager.java

+21-6
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,33 @@ public interface RequestQueueManager extends AutoCloseable {
1616
/**
1717
* Is used by default when no API KEY is provided
1818
*/
19-
RequestQueueManager ANONYMOUS = new SemaphoreRequestQueueManager(1, Duration.ofMillis(5015L));
19+
static RequestQueueManager anonymous() {
20+
return new SemaphoreRequestQueueManager(1, Duration.ofMillis(5005L));
21+
}
2022

2123
/**
2224
* Is available for all registered free API KEYs
2325
* <a href="https://docs.etherscan.io/getting-started/viewing-api-usage-statistics">Free API KEY</a>
2426
*/
25-
RequestQueueManager FREE_PLAN = new SemaphoreRequestQueueManager(5, Duration.ofMillis(1015L));
26-
RequestQueueManager STANDARD_PLAN = new SemaphoreRequestQueueManager(10, Duration.ofMillis(1015L));
27-
RequestQueueManager ADVANCED_PLAN = new SemaphoreRequestQueueManager(20, Duration.ofMillis(1015L));
28-
RequestQueueManager PROFESSIONAL_PLAN = new SemaphoreRequestQueueManager(30, Duration.ofMillis(1015L));
27+
static RequestQueueManager planFree() {
28+
return new SemaphoreRequestQueueManager(5, Duration.ofMillis(1005L));
29+
}
2930

30-
RequestQueueManager UNLIMITED = new FakeRequestQueueManager();
31+
static RequestQueueManager planStandard() {
32+
return new SemaphoreRequestQueueManager(10, Duration.ofMillis(1005L));
33+
}
34+
35+
static RequestQueueManager planAdvanced() {
36+
return new SemaphoreRequestQueueManager(20, Duration.ofMillis(1005L));
37+
}
38+
39+
static RequestQueueManager planProfessional() {
40+
return new SemaphoreRequestQueueManager(30, Duration.ofMillis(1005L));
41+
}
42+
43+
static RequestQueueManager unlimited() {
44+
return new FakeRequestQueueManager();
45+
}
3146

3247
/**
3348
* Waits in queue for chance to take turn

src/test/java/io/goodforgod/api/etherscan/ApiRunner.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public class ApiRunner extends Assertions {
2020
.orElse(DEFAULT_KEY);
2121

2222
final RequestQueueManager queueManager = (DEFAULT_KEY.equals(API_KEY))
23-
? RequestQueueManager.ANONYMOUS
24-
: RequestQueueManager.FREE_PLAN;
23+
? RequestQueueManager.anonymous()
24+
: RequestQueueManager.planFree();
2525

2626
API = EtherScanAPI.builder()
2727
.withApiKey(ApiRunner.API_KEY)
@@ -30,10 +30,6 @@ public class ApiRunner extends Assertions {
3030
.build();
3131
}
3232

33-
public static String getApiKey() {
34-
return API_KEY;
35-
}
36-
3733
public static EtherScanAPI getApi() {
3834
return API;
3935
}

src/test/java/io/goodforgod/api/etherscan/EtherScanAPITests.java

-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ void timeout() throws InterruptedException {
6868
TimeUnit.SECONDS.sleep(5);
6969
Supplier<EthHttpClient> supplier = () -> new UrlEthHttpClient(Duration.ofMillis(300), Duration.ofMillis(300));
7070
EtherScanAPI api = EtherScanAPI.builder()
71-
.withApiKey(getApiKey())
7271
.withNetwork(() -> URI.create("https://api-unknown.etherscan.io/api"))
7372
.withHttpClient(supplier)
7473
.build();

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void correct() {
1818
assertNotNull(txs);
1919
assertEquals(3, txs.size());
2020
assertTxs(txs);
21-
assertNotEquals(0, txs.get(0).getGasPrice());
21+
assertNotEquals(0, txs.get(0).getGasPrice().asWei().intValue());
2222
assertNotEquals(-1, txs.get(0).getNonce());
2323

2424
assertNotNull(txs.get(0).toString());
@@ -71,7 +71,7 @@ private void assertTxs(List<TxErc20> txs) {
7171
assertNotNull(tx.getTokenDecimal());
7272
assertNotEquals(-1, (tx.getConfirmations()));
7373
assertNotNull(tx.getGasUsed());
74-
assertNotEquals(-1, tx.getCumulativeGasUsed());
74+
assertNotEquals(-1, tx.getGasUsedCumulative());
7575
assertNotEquals(-1, tx.getTransactionIndex());
7676
}
7777
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private void asserTx(TxErc1155 tx) {
7575
assertNotNull(tx.getTokenValue());
7676
assertNotEquals(-1, (tx.getConfirmations()));
7777
assertNotNull(tx.getGasUsed());
78-
assertNotEquals(-1, tx.getCumulativeGasUsed());
78+
assertNotEquals(-1, tx.getGasUsedCumulative());
7979
assertNotEquals(-1, tx.getTransactionIndex());
8080
}
8181
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private void assertTxs(List<TxErc721> txs) {
7373
assertNotNull(tx.getTokenDecimal());
7474
assertNotEquals(-1, (tx.getConfirmations()));
7575
assertNotNull(tx.getGasUsed());
76-
assertNotEquals(-1, tx.getCumulativeGasUsed());
76+
assertNotEquals(-1, tx.getGasUsedCumulative());
7777
assertNotEquals(-1, tx.getTransactionIndex());
7878
}
7979
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void correct() {
2424
assertNotNull(txs.get(0).getTo());
2525
assertNotNull(txs.get(0).getBlockHash());
2626
assertNotNull(txs.get(0).getGas());
27-
assertNotNull(txs.get(0).getCumulativeGasUsed());
27+
assertNotNull(txs.get(0).getGasUsedCumulative());
2828
assertNotNull(txs.get(0).getGasPrice());
2929
assertNotNull(txs.get(0).getValue());
3030
assertNotNull(txs.get(0).getContractAddress());
@@ -75,7 +75,7 @@ private void assertTxs(List<Tx> txs) {
7575
assertNotEquals(-1, (tx.getNonce()));
7676
assertNotEquals(0, (tx.getTransactionIndex()));
7777
assertNotEquals(0, tx.getConfirmations());
78-
assertNotNull(tx.getTxreceipt_status());
78+
assertNotNull(tx.getTxReceiptStatus());
7979
}
8080
}
8181
}

src/test/java/io/goodforgod/api/etherscan/model/ModelBuilderTests.java

+19-19
Original file line numberDiff line numberDiff line change
@@ -132,20 +132,20 @@ void txBuilder() {
132132
.withBlockNumber(1L)
133133
.withConfirmations(1L)
134134
.withContractAddress("1")
135-
.withCumulativeGasUsed(BigInteger.ONE)
136135
.withFrom("1")
137136
.withTo("1")
138-
.withGas(BigInteger.ONE)
139-
.withGasPrice(BigInteger.ONE)
140-
.withGasUsed(BigInteger.ONE)
137+
.withCumulativeGasUsed(Wei.ofWei(BigInteger.ONE))
138+
.withGas(Wei.ofWei(BigInteger.ONE))
139+
.withGasPrice(Wei.ofWei(BigInteger.ONE))
140+
.withGasUsed(Wei.ofWei(BigInteger.ONE))
141141
.withHash("1")
142142
.withInput("1")
143143
.withIsError("1")
144144
.withNonce(1L)
145145
.withTimeStamp(timestamp)
146146
.withValue(BigInteger.ONE)
147147
.withTransactionIndex(1)
148-
.withTxreceiptStatus("1")
148+
.withTxReceiptStatus("1")
149149
.build();
150150

151151
assertNotNull(value);
@@ -162,12 +162,12 @@ void txErc20Builder() {
162162
.withBlockNumber(1L)
163163
.withConfirmations(1L)
164164
.withContractAddress("1")
165-
.withCumulativeGasUsed(BigInteger.ONE)
166165
.withFrom("1")
167166
.withTo("1")
168-
.withGas(BigInteger.ONE)
169-
.withGasPrice(BigInteger.ONE)
170-
.withGasUsed(BigInteger.ONE)
167+
.withCumulativeGasUsed(Wei.ofWei(BigInteger.ONE))
168+
.withGas(Wei.ofWei(BigInteger.ONE))
169+
.withGasPrice(Wei.ofWei(BigInteger.ONE))
170+
.withGasUsed(Wei.ofWei(BigInteger.ONE))
171171
.withHash("1")
172172
.withInput("1")
173173
.withTokenName("1")
@@ -192,12 +192,12 @@ void txErc721Builder() {
192192
.withBlockNumber(1L)
193193
.withConfirmations(1L)
194194
.withContractAddress("1")
195-
.withCumulativeGasUsed(BigInteger.ONE)
196195
.withFrom("1")
197196
.withTo("1")
198-
.withGas(BigInteger.ONE)
199-
.withGasPrice(BigInteger.ONE)
200-
.withGasUsed(BigInteger.ONE)
197+
.withCumulativeGasUsed(Wei.ofWei(BigInteger.ONE))
198+
.withGas(Wei.ofWei(BigInteger.ONE))
199+
.withGasPrice(Wei.ofWei(BigInteger.ONE))
200+
.withGasUsed(Wei.ofWei(BigInteger.ONE))
201201
.withHash("1")
202202
.withInput("1")
203203
.withTokenName("1")
@@ -222,12 +222,12 @@ void txErc1155Builder() {
222222
.withBlockNumber(1L)
223223
.withConfirmations(1L)
224224
.withContractAddress("1")
225-
.withCumulativeGasUsed(BigInteger.ONE)
226225
.withFrom("1")
227226
.withTo("1")
228-
.withGas(BigInteger.ONE)
229-
.withGasPrice(BigInteger.ONE)
230-
.withGasUsed(BigInteger.ONE)
227+
.withCumulativeGasUsed(Wei.ofWei(BigInteger.ONE))
228+
.withGas(Wei.ofWei(BigInteger.ONE))
229+
.withGasPrice(Wei.ofWei(BigInteger.ONE))
230+
.withGasUsed(Wei.ofWei(BigInteger.ONE))
231231
.withHash("1")
232232
.withInput("1")
233233
.withTokenName("1")
@@ -253,8 +253,8 @@ void txInternalBuilder() {
253253
.withFrom("1")
254254
.withTo("1")
255255
.withValue(BigInteger.ONE)
256-
.withGas(BigInteger.ONE)
257-
.withGasUsed(BigInteger.ONE)
256+
.withGas(Wei.ofWei(BigInteger.ONE))
257+
.withGasUsed(Wei.ofWei(BigInteger.ONE))
258258
.withHash("1")
259259
.withInput("1")
260260
.withTimeStamp(timestamp)

src/test/java/io/goodforgod/api/etherscan/proxy/ProxyBlockApiTests.java

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

33
import io.goodforgod.api.etherscan.ApiRunner;
4-
import io.goodforgod.api.etherscan.EthNetworks;
5-
import io.goodforgod.api.etherscan.EtherScanAPI;
6-
import io.goodforgod.api.etherscan.manager.RequestQueueManager;
74
import io.goodforgod.api.etherscan.model.proxy.BlockProxy;
85
import java.util.Optional;
96
import org.junit.jupiter.api.Test;
@@ -14,17 +11,9 @@
1411
*/
1512
class ProxyBlockApiTests extends ApiRunner {
1613

17-
private final EtherScanAPI api;
18-
19-
ProxyBlockApiTests() {
20-
final RequestQueueManager queueManager = RequestQueueManager.ANONYMOUS;
21-
this.api = EtherScanAPI.builder().withApiKey(getApiKey()).withNetwork(EthNetworks.MAINNET).withQueue(queueManager)
22-
.build();
23-
}
24-
2514
@Test
2615
void correct() {
27-
Optional<BlockProxy> block = api.proxy().block(5120);
16+
Optional<BlockProxy> block = getApi().proxy().block(5120);
2817
assertTrue(block.isPresent());
2918
BlockProxy proxy = block.get();
3019
assertNotNull(proxy.getHash());
@@ -57,13 +46,13 @@ void correct() {
5746

5847
@Test
5948
void correctParamWithEmptyExpectedResult() {
60-
Optional<BlockProxy> block = api.proxy().block(99999999999L);
49+
Optional<BlockProxy> block = getApi().proxy().block(99999999999L);
6150
assertFalse(block.isPresent());
6251
}
6352

6453
@Test
6554
void correctParamNegativeNo() {
66-
Optional<BlockProxy> block = api.proxy().block(-1);
55+
Optional<BlockProxy> block = getApi().proxy().block(-1);
6756
assertTrue(block.isPresent());
6857
assertNotNull(block.get().getHash());
6958
}

0 commit comments

Comments
 (0)