Skip to content

Commit 9fb7d91

Browse files
gas tracker API implementation
1 parent 1559a3f commit 9fb7d91

File tree

5 files changed

+155
-0
lines changed

5 files changed

+155
-0
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.api.etherscan.core;
2+
3+
import io.api.etherscan.error.ApiException;
4+
import io.api.etherscan.model.GasOracle;
5+
import org.jetbrains.annotations.NotNull;
6+
7+
/**
8+
* EtherScan - API Descriptions https://docs.etherscan.io/api-endpoints/gas-tracker
9+
*
10+
* @author Abhay Gupta
11+
* @since 14.11.2022
12+
*/
13+
public interface IGasTrackerApi {
14+
15+
/**
16+
* GasOracle details
17+
*
18+
* @return fast, suggested gas price
19+
* @throws ApiException parent exception class
20+
*/
21+
@NotNull
22+
GasOracle gasoracle() throws ApiException;
23+
}

Diff for: src/main/java/io/api/etherscan/core/impl/EtherScanApi.java

+7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class EtherScanApi implements AutoCloseable {
3434
private final IProxyApi proxy;
3535
private final IStatisticApi stats;
3636
private final ITransactionApi txs;
37+
private final IGasTrackerApi gastracker;
3738

3839
public EtherScanApi() {
3940
this(DEFAULT_KEY, EthNetwork.MAINNET);
@@ -96,6 +97,7 @@ public EtherScanApi(final String apiKey,
9697
this.proxy = new ProxyApiProvider(queue, baseUrl, executor);
9798
this.stats = new StatisticApiProvider(queue, baseUrl, executor);
9899
this.txs = new TransactionApiProvider(queue, baseUrl, executor);
100+
this.gastracker = new GasTrackerApiProvider(queue, baseUrl, executor);
99101
}
100102

101103
@NotNull
@@ -133,6 +135,11 @@ public IStatisticApi stats() {
133135
return stats;
134136
}
135137

138+
@NotNull
139+
public IGasTrackerApi gastracker() {
140+
return gastracker;
141+
}
142+
136143
@Override
137144
public void close() throws Exception {
138145
queueManager.close();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.api.etherscan.core.impl;
2+
3+
import io.api.etherscan.core.IGasTrackerApi;
4+
import io.api.etherscan.error.ApiException;
5+
import io.api.etherscan.error.EtherScanException;
6+
import io.api.etherscan.executor.IHttpExecutor;
7+
import io.api.etherscan.manager.IQueueManager;
8+
import io.api.etherscan.model.GasOracle;
9+
import io.api.etherscan.model.utility.GasOracleResponseTO;
10+
import org.jetbrains.annotations.NotNull;
11+
12+
/**
13+
* GasTracker API Implementation
14+
*
15+
* @see IGasTrackerApi
16+
*
17+
* @author Abhay Gupta
18+
* @since 14.11.2022
19+
*/
20+
public class GasTrackerApiProvider extends BasicProvider implements IGasTrackerApi {
21+
22+
private static final String ACT_GAS_ORACLE_PARAM = ACT_PREFIX + "gasoracle";
23+
24+
GasTrackerApiProvider(final IQueueManager queue,
25+
final String baseUrl,
26+
final IHttpExecutor executor) {
27+
super(queue, "gastracker", baseUrl, executor);
28+
}
29+
30+
@NotNull
31+
@Override
32+
public GasOracle gasoracle() throws ApiException {
33+
final GasOracleResponseTO response = getRequest(ACT_GAS_ORACLE_PARAM, GasOracleResponseTO.class);
34+
if (response.getStatus() != 1)
35+
throw new EtherScanException(response);
36+
37+
return response.getResult();
38+
}
39+
}

Diff for: src/main/java/io/api/etherscan/model/GasOracle.java

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package io.api.etherscan.model;
2+
3+
import java.math.BigInteger;
4+
import java.util.Objects;
5+
6+
/**
7+
* ! NO DESCRIPTION !
8+
*
9+
* @author Abhay Gupta
10+
* @since 14.11.2022
11+
*/
12+
public class GasOracle {
13+
private Long LastBlock;
14+
private Integer SafeGasPrice;
15+
private Integer ProposeGasPrice;
16+
private Integer FastGasPrice;
17+
private Double suggestBaseFee;
18+
private String gasUsedRatio;
19+
20+
public Long getLastBlock() {
21+
return LastBlock;
22+
}
23+
24+
public BigInteger getSafeGasPriceInWei() {
25+
return BigInteger.valueOf(SafeGasPrice).multiply(BigInteger.TEN.pow(9));
26+
}
27+
28+
public BigInteger getProposeGasPriceInWei() {
29+
return BigInteger.valueOf(ProposeGasPrice).multiply(BigInteger.TEN.pow(9));
30+
}
31+
32+
public BigInteger getFastGasPriceInWei() {
33+
return BigInteger.valueOf(FastGasPrice).multiply(BigInteger.TEN.pow(9));
34+
}
35+
36+
public Double getSuggestBaseFee() {
37+
return suggestBaseFee;
38+
}
39+
40+
public String getGasUsedRatio() {
41+
return gasUsedRatio;
42+
}
43+
44+
@Override
45+
public boolean equals(Object o) {
46+
if (this == o) return true;
47+
if (o == null || getClass() != o.getClass()) return false;
48+
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+
}
51+
52+
@Override
53+
public int hashCode() {
54+
return Objects.hash(LastBlock, SafeGasPrice, ProposeGasPrice, FastGasPrice, suggestBaseFee, gasUsedRatio);
55+
}
56+
57+
@Override
58+
public String toString() {
59+
return "GasOracle{" +
60+
"LastBlock=" + LastBlock +
61+
", SafeGasPrice=" + SafeGasPrice +
62+
", ProposeGasPrice=" + ProposeGasPrice +
63+
", FastGasPrice=" + FastGasPrice +
64+
", suggestBaseFee=" + suggestBaseFee +
65+
", gasUsedRatio='" + gasUsedRatio + '\'' +
66+
'}';
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.api.etherscan.model.utility;
2+
3+
import io.api.etherscan.model.GasOracle;
4+
5+
/**
6+
* ! NO DESCRIPTION !
7+
*
8+
* @author Abhay Gupta
9+
* @since 14.11.2022
10+
*/
11+
public class GasOracleResponseTO extends BaseResponseTO {
12+
13+
private GasOracle result;
14+
15+
public GasOracle getResult() {
16+
return result;
17+
}
18+
}

0 commit comments

Comments
 (0)