Skip to content

Commit fe444f4

Browse files
committed
[2.0.0-SNAPSHOT]
GasEstimate added to GasTrackerAPI#estimate
1 parent b9a8dda commit fe444f4

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
lines changed

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

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

33
import io.goodforgod.api.etherscan.error.EtherScanException;
4+
import io.goodforgod.api.etherscan.model.GasEstimate;
45
import io.goodforgod.api.etherscan.model.GasOracle;
6+
import io.goodforgod.api.etherscan.model.Wei;
57
import org.jetbrains.annotations.NotNull;
68

79
/**
@@ -14,7 +16,16 @@
1416
public interface GasTrackerAPI {
1517

1618
/**
17-
* GasOracle details
19+
* Returns the estimated time, in seconds, for a transaction to be confirmed on the blockchain.
20+
*
21+
* @return fast, suggested gas price
22+
* @throws EtherScanException parent exception class
23+
*/
24+
@NotNull
25+
GasEstimate estimate(@NotNull Wei wei) throws EtherScanException;
26+
27+
/**
28+
* Returns the current Safe, Proposed and Fast gas prices.
1829
*
1930
* @return fast, suggested gas price
2031
* @throws EtherScanException parent exception class

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

+16
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import io.goodforgod.api.etherscan.error.EtherScanResponseException;
55
import io.goodforgod.api.etherscan.executor.EthHttpClient;
66
import io.goodforgod.api.etherscan.manager.RequestQueueManager;
7+
import io.goodforgod.api.etherscan.model.GasEstimate;
78
import io.goodforgod.api.etherscan.model.GasOracle;
9+
import io.goodforgod.api.etherscan.model.Wei;
10+
import io.goodforgod.api.etherscan.model.response.GasEstimateResponseTO;
811
import io.goodforgod.api.etherscan.model.response.GasOracleResponseTO;
912
import org.jetbrains.annotations.NotNull;
1013

@@ -18,13 +21,26 @@
1821
final class GasTrackerAPIProvider extends BasicProvider implements GasTrackerAPI {
1922

2023
private static final String ACT_GAS_ORACLE_PARAM = ACT_PREFIX + "gasoracle";
24+
private static final String ACT_GAS_ESTIMATE_PARAM = ACT_PREFIX + "gasestimate";
25+
26+
private static final String GASPRICE_PARAM = "&gasprice=";
2127

2228
GasTrackerAPIProvider(RequestQueueManager queue,
2329
String baseUrl,
2430
EthHttpClient ethHttpClient) {
2531
super(queue, "gastracker", baseUrl, ethHttpClient);
2632
}
2733

34+
@Override
35+
public @NotNull GasEstimate estimate(@NotNull Wei wei) throws EtherScanException {
36+
final String urlParams = ACT_GAS_ESTIMATE_PARAM + GASPRICE_PARAM + wei.getValue().toString();
37+
final GasEstimateResponseTO response = getRequest(urlParams, GasEstimateResponseTO.class);
38+
if (response.getStatus() != 1)
39+
throw new EtherScanResponseException(response);
40+
41+
return new GasEstimate(Long.parseLong(response.getResult()));
42+
}
43+
2844
@NotNull
2945
@Override
3046
public GasOracle oracle() throws EtherScanException {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package io.goodforgod.api.etherscan.model;
2+
3+
import java.time.Duration;
4+
import java.util.Objects;
5+
6+
/**
7+
* @author GoodforGod
8+
* @since 14.05.2023
9+
*/
10+
public class GasEstimate {
11+
12+
private final Duration duration;
13+
14+
public GasEstimate(long durationInSeconds) {
15+
this.duration = Duration.ofSeconds(durationInSeconds);
16+
}
17+
18+
public GasEstimate(Duration duration) {
19+
this.duration = duration;
20+
}
21+
22+
public Duration getDuration() {
23+
return duration;
24+
}
25+
26+
@Override
27+
public boolean equals(Object o) {
28+
if (this == o)
29+
return true;
30+
if (!(o instanceof GasEstimate))
31+
return false;
32+
GasEstimate that = (GasEstimate) o;
33+
return Objects.equals(duration, that.duration);
34+
}
35+
36+
@Override
37+
public int hashCode() {
38+
return Objects.hash(duration);
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return "GasEstimate{" +
44+
"duration=" + duration +
45+
'}';
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.goodforgod.api.etherscan.model.response;
2+
3+
/**
4+
* @author Abhay Gupta
5+
* @since 14.11.2022
6+
*/
7+
public class GasEstimateResponseTO extends BaseResponseTO {
8+
9+
private String result;
10+
11+
public String getResult() {
12+
return result;
13+
}
14+
}

0 commit comments

Comments
 (0)