Skip to content

Commit b05bd8a

Browse files
committed
[2.0.0]
Tx refactored and simplified and common parts moved to BlockTx Comparable for multiple models added GasOracle simplified and reinforced Price reinforced GasEstimate.java removed as useless
1 parent d1ec9e5 commit b05bd8a

23 files changed

+368
-415
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +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;
54
import io.goodforgod.api.etherscan.model.GasOracle;
65
import io.goodforgod.api.etherscan.model.Wei;
6+
import java.time.Duration;
77
import org.jetbrains.annotations.NotNull;
88

99
/**
@@ -16,13 +16,13 @@
1616
public interface GasTrackerAPI {
1717

1818
/**
19-
* Returns the estimated time, in seconds, for a transaction to be confirmed on the blockchain.
19+
* Returns the estimated time for a transaction to be confirmed on the blockchain.
2020
*
21-
* @return fast, suggested gas price
21+
* @return estimated time
2222
* @throws EtherScanException parent exception class
2323
*/
2424
@NotNull
25-
GasEstimate estimate(@NotNull Wei wei) throws EtherScanException;
25+
Duration estimate(@NotNull Wei wei) throws EtherScanException;
2626

2727
/**
2828
* Returns the current Safe, Proposed and Fast gas prices.

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import io.goodforgod.api.etherscan.error.EtherScanResponseException;
55
import io.goodforgod.api.etherscan.http.EthHttpClient;
66
import io.goodforgod.api.etherscan.manager.RequestQueueManager;
7-
import io.goodforgod.api.etherscan.model.GasEstimate;
87
import io.goodforgod.api.etherscan.model.GasOracle;
98
import io.goodforgod.api.etherscan.model.Wei;
109
import io.goodforgod.api.etherscan.model.response.GasEstimateResponseTO;
1110
import io.goodforgod.api.etherscan.model.response.GasOracleResponseTO;
11+
import java.time.Duration;
1212
import org.jetbrains.annotations.NotNull;
1313

1414
/**
@@ -33,13 +33,13 @@ final class GasTrackerAPIProvider extends BasicProvider implements GasTrackerAPI
3333
}
3434

3535
@Override
36-
public @NotNull GasEstimate estimate(@NotNull Wei wei) throws EtherScanException {
36+
public @NotNull Duration estimate(@NotNull Wei wei) throws EtherScanException {
3737
final String urlParams = ACT_GAS_ESTIMATE_PARAM + GASPRICE_PARAM + wei.asWei().toString();
3838
final GasEstimateResponseTO response = getRequest(urlParams, GasEstimateResponseTO.class);
3939
if (response.getStatus() != 1)
4040
throw new EtherScanResponseException(response);
4141

42-
return new GasEstimate(Long.parseLong(response.getResult()));
42+
return Duration.ofSeconds(Long.parseLong(response.getResult()));
4343
}
4444

4545
@NotNull

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

+4-13
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
import java.time.LocalDateTime;
77
import java.time.ZoneOffset;
88
import java.util.Objects;
9+
import org.jetbrains.annotations.NotNull;
910

1011
/**
1112
* @author GoodforGod
1213
* @since 28.10.2018
1314
*/
14-
abstract class BaseTx {
15+
abstract class BaseTx implements Comparable<BaseTx> {
1516

1617
long blockNumber;
1718
String timeStamp;
@@ -84,17 +85,7 @@ public int hashCode() {
8485
}
8586

8687
@Override
87-
public String toString() {
88-
return "BaseTx{" +
89-
"blockNumber=" + blockNumber +
90-
", timeStamp='" + timeStamp + '\'' +
91-
", hash='" + hash + '\'' +
92-
", from='" + from + '\'' +
93-
", to='" + to + '\'' +
94-
", contractAddress='" + contractAddress + '\'' +
95-
", input='" + input + '\'' +
96-
", gas=" + gas +
97-
", gasUsed=" + gasUsed +
98-
'}';
88+
public int compareTo(@NotNull BaseTx o) {
89+
return Long.compare(blockNumber, o.blockNumber);
9990
}
10091
}

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
import java.time.LocalDateTime;
77
import java.time.ZoneOffset;
88
import java.util.Objects;
9+
import org.jetbrains.annotations.NotNull;
910

1011
/**
1112
* @author GoodforGod
1213
* @since 28.10.2018
1314
*/
14-
public class Block {
15+
public class Block implements Comparable<Block> {
1516

1617
long blockNumber;
1718
BigInteger blockReward;
@@ -58,10 +59,14 @@ public String toString() {
5859
"blockNumber=" + blockNumber +
5960
", blockReward=" + blockReward +
6061
", timeStamp='" + timeStamp + '\'' +
61-
", _timeStamp=" + _timeStamp +
6262
'}';
6363
}
6464

65+
@Override
66+
public int compareTo(@NotNull Block o) {
67+
return Long.compare(blockNumber, o.blockNumber);
68+
}
69+
6570
public static BlockBuilder builder() {
6671
return new BlockBuilder();
6772
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package io.goodforgod.api.etherscan.model;
2+
3+
import java.math.BigInteger;
4+
import java.util.Objects;
5+
import org.jetbrains.annotations.NotNull;
6+
7+
/**
8+
* @author Anton Kurako (GoodforGod)
9+
* @since 15.05.2023
10+
*/
11+
abstract class BlockTx extends BaseTx {
12+
13+
long nonce;
14+
String blockHash;
15+
long transactionIndex;
16+
long confirmations;
17+
BigInteger gasPrice;
18+
BigInteger cumulativeGasUsed;
19+
20+
public long getNonce() {
21+
return nonce;
22+
}
23+
24+
public String getBlockHash() {
25+
return blockHash;
26+
}
27+
28+
public long getTransactionIndex() {
29+
return transactionIndex;
30+
}
31+
32+
public Wei getGasPrice() {
33+
return Wei.ofWei(gasPrice);
34+
}
35+
36+
public Wei getGasUsedCumulative() {
37+
return Wei.ofWei(cumulativeGasUsed);
38+
}
39+
40+
public long getConfirmations() {
41+
return confirmations;
42+
}
43+
44+
@Override
45+
public boolean equals(Object o) {
46+
if (this == o)
47+
return true;
48+
if (!(o instanceof BlockTx))
49+
return false;
50+
if (!super.equals(o))
51+
return false;
52+
BlockTx blockTx = (BlockTx) o;
53+
return nonce == blockTx.nonce && transactionIndex == blockTx.transactionIndex
54+
&& Objects.equals(blockHash, blockTx.blockHash);
55+
}
56+
57+
@Override
58+
public int hashCode() {
59+
return Objects.hash(super.hashCode(), nonce, blockHash, transactionIndex);
60+
}
61+
62+
@Override
63+
public int compareTo(@NotNull BaseTx o) {
64+
final int superCompare = super.compareTo(o);
65+
if (superCompare == 0) {
66+
if (o instanceof Tx) {
67+
return Long.compare(transactionIndex, ((Tx) o).getTransactionIndex());
68+
} else if (o instanceof TxErc20) {
69+
return Long.compare(transactionIndex, ((TxErc20) o).getTransactionIndex());
70+
} else if (o instanceof TxErc721) {
71+
return Long.compare(transactionIndex, ((TxErc721) o).getTransactionIndex());
72+
} else if (o instanceof TxErc1155) {
73+
return Long.compare(transactionIndex, ((TxErc1155) o).getTransactionIndex());
74+
}
75+
}
76+
77+
return superCompare;
78+
}
79+
}

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

-45
This file was deleted.

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

+21-19
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
public class GasOracle {
1515

1616
private Long LastBlock;
17-
private Integer SafeGasPrice;
18-
private Integer ProposeGasPrice;
19-
private Integer FastGasPrice;
20-
private Double suggestBaseFee;
17+
private BigInteger SafeGasPrice;
18+
private BigInteger ProposeGasPrice;
19+
private BigInteger FastGasPrice;
20+
private BigDecimal suggestBaseFee;
2121
private String gasUsedRatio;
2222

2323
protected GasOracle() {}
@@ -27,18 +27,18 @@ public Long getLastBlock() {
2727
}
2828

2929
public Wei getSafeGasPriceInWei() {
30-
return Wei.ofWei(BigInteger.valueOf(SafeGasPrice).multiply(BigInteger.TEN.pow(9)));
30+
return Wei.ofGwei(SafeGasPrice);
3131
}
3232

3333
public Wei getProposeGasPriceInWei() {
34-
return Wei.ofWei(BigInteger.valueOf(ProposeGasPrice).multiply(BigInteger.TEN.pow(9)));
34+
return Wei.ofGwei(ProposeGasPrice);
3535
}
3636

3737
public Wei getFastGasPriceInWei() {
38-
return Wei.ofWei(BigInteger.valueOf(FastGasPrice).multiply(BigInteger.TEN.pow(9)));
38+
return Wei.ofGwei(FastGasPrice);
3939
}
4040

41-
public Double getSuggestBaseFee() {
41+
public BigDecimal getSuggestBaseFee() {
4242
return suggestBaseFee;
4343
}
4444

@@ -52,12 +52,14 @@ public List<BigDecimal> getGasUsedRatio() {
5252
public boolean equals(Object o) {
5353
if (this == o)
5454
return true;
55-
if (o == null || getClass() != o.getClass())
55+
if (!(o instanceof GasOracle))
5656
return false;
5757
GasOracle gasOracle = (GasOracle) o;
58-
return LastBlock.equals(gasOracle.LastBlock) && SafeGasPrice.equals(gasOracle.SafeGasPrice)
59-
&& ProposeGasPrice.equals(gasOracle.ProposeGasPrice) && FastGasPrice.equals(gasOracle.FastGasPrice)
60-
&& suggestBaseFee.equals(gasOracle.suggestBaseFee) && gasUsedRatio.equals(gasOracle.gasUsedRatio);
58+
return Objects.equals(LastBlock, gasOracle.LastBlock) && Objects.equals(SafeGasPrice, gasOracle.SafeGasPrice)
59+
&& Objects.equals(ProposeGasPrice, gasOracle.ProposeGasPrice)
60+
&& Objects.equals(FastGasPrice, gasOracle.FastGasPrice)
61+
&& Objects.equals(suggestBaseFee, gasOracle.suggestBaseFee)
62+
&& Objects.equals(gasUsedRatio, gasOracle.gasUsedRatio);
6163
}
6264

6365
@Override
@@ -73,7 +75,7 @@ public String toString() {
7375
", ProposeGasPrice=" + ProposeGasPrice +
7476
", FastGasPrice=" + FastGasPrice +
7577
", suggestBaseFee=" + suggestBaseFee +
76-
", gasUsedRatio='" + gasUsedRatio + '\'' +
78+
", gasUsedRatio=" + gasUsedRatio +
7779
'}';
7880
}
7981

@@ -87,7 +89,7 @@ public static final class GasOracleBuilder {
8789
private Wei safeGasPrice;
8890
private Wei proposeGasPrice;
8991
private Wei fastGasPrice;
90-
private Double suggestBaseFee;
92+
private BigDecimal suggestBaseFee;
9193
private List<BigDecimal> gasUsedRatio;
9294

9395
private GasOracleBuilder() {}
@@ -112,7 +114,7 @@ public GasOracleBuilder withFastGasPrice(Wei fastGasPrice) {
112114
return this;
113115
}
114116

115-
public GasOracleBuilder withSuggestBaseFee(Double suggestBaseFee) {
117+
public GasOracleBuilder withSuggestBaseFee(BigDecimal suggestBaseFee) {
116118
this.suggestBaseFee = suggestBaseFee;
117119
return this;
118120
}
@@ -127,18 +129,18 @@ public GasOracle build() {
127129
gasOracle.LastBlock = this.lastBlock;
128130
gasOracle.suggestBaseFee = this.suggestBaseFee;
129131
if (this.proposeGasPrice != null) {
130-
gasOracle.ProposeGasPrice = this.proposeGasPrice.asGwei().intValue();
132+
gasOracle.ProposeGasPrice = this.proposeGasPrice.asGwei().toBigInteger();
131133
}
132134
if (this.safeGasPrice != null) {
133-
gasOracle.SafeGasPrice = this.safeGasPrice.asGwei().intValue();
135+
gasOracle.SafeGasPrice = this.safeGasPrice.asGwei().toBigInteger();
134136
}
135137
if (this.fastGasPrice != null) {
136-
gasOracle.FastGasPrice = this.fastGasPrice.asGwei().intValue();
138+
gasOracle.FastGasPrice = this.fastGasPrice.asGwei().toBigInteger();
137139
}
138140
if (this.gasUsedRatio != null) {
139141
gasOracle.gasUsedRatio = this.gasUsedRatio.stream()
140142
.map(BigDecimal::toString)
141-
.collect(Collectors.joining(", "));
143+
.collect(Collectors.joining(","));
142144
}
143145
return gasOracle;
144146
}

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

-6
Original file line numberDiff line numberDiff line change
@@ -139,21 +139,15 @@ public int hashCode() {
139139
public String toString() {
140140
return "Log{" +
141141
"blockNumber='" + blockNumber + '\'' +
142-
", _blockNumber=" + _blockNumber +
143142
", address='" + address + '\'' +
144143
", transactionHash='" + transactionHash + '\'' +
145144
", transactionIndex='" + transactionIndex + '\'' +
146-
", _transactionIndex=" + _transactionIndex +
147145
", timeStamp='" + timeStamp + '\'' +
148-
", _timeStamp=" + _timeStamp +
149146
", data='" + data + '\'' +
150147
", gasPrice='" + gasPrice + '\'' +
151-
", _gasPrice=" + _gasPrice +
152148
", gasUsed='" + gasUsed + '\'' +
153-
", _gasUsed=" + _gasUsed +
154149
", topics=" + topics +
155150
", logIndex='" + logIndex + '\'' +
156-
", _logIndex=" + _logIndex +
157151
'}';
158152
}
159153

0 commit comments

Comments
 (0)