Skip to content

Commit 9e4d21a

Browse files
authored
Merge pull request GoodforGod#23 from abhaygupta1999/master
added support for txsToken with contract address too
2 parents a039cff + c462175 commit 9e4d21a

File tree

7 files changed

+201
-0
lines changed

7 files changed

+201
-0
lines changed

src/main/java/io/api/etherscan/core/IAccountApi.java

+19
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,25 @@ public interface IAccountApi {
109109
@NotNull
110110
List<TxToken> txsToken(String address) throws ApiException;
111111

112+
/**
113+
* All ERC-20 token txs for given address and contract address
114+
*
115+
* @param address get txs for
116+
* @param contractAddress contract address to get txs for
117+
* @param startBlock tx from this blockNumber
118+
* @param endBlock tx to this blockNumber
119+
* @return txs for address
120+
* @throws ApiException parent exception class
121+
*/
122+
@NotNull
123+
List<TxToken> txsToken(String address, String contractAddress, long startBlock, long endBlock) throws ApiException;
124+
125+
@NotNull
126+
List<TxToken> txsToken(String address, String contractAddress, long startBlock) throws ApiException;
127+
128+
@NotNull
129+
List<TxToken> txsToken(String address, String contractAddress) throws ApiException;
130+
112131
/**
113132
* All ERC-721 (NFT) token txs for given address
114133
*
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+
}

src/main/java/io/api/etherscan/core/impl/AccountApiProvider.java

+26
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,32 @@ public List<TxToken> txsToken(final String address, final long startBlock, final
229229
return getRequestUsingOffset(urlParams, TxTokenResponseTO.class);
230230
}
231231

232+
@NotNull
233+
@Override
234+
public List<TxToken> txsToken(final String address, final String contractAddress) throws ApiException {
235+
return txsToken(address, contractAddress, MIN_START_BLOCK);
236+
}
237+
238+
@NotNull
239+
@Override
240+
public List<TxToken> txsToken(final String address, final String contractAddress, final long startBlock) throws ApiException {
241+
return txsToken(address, contractAddress, startBlock, MAX_END_BLOCK);
242+
}
243+
244+
@NotNull
245+
@Override
246+
public List<TxToken> txsToken(final String address, final String contractAddress, final long startBlock, final long endBlock) throws ApiException {
247+
BasicUtils.validateAddress(address);
248+
final BlockParam blocks = BasicUtils.compensateBlocks(startBlock, endBlock);
249+
250+
final String offsetParam = PAGE_PARAM + "%s" + OFFSET_PARAM + OFFSET_MAX;
251+
final String blockParam = START_BLOCK_PARAM + blocks.start() + END_BLOCK_PARAM + blocks.end();
252+
final String urlParams = ACT_TX_TOKEN_ACTION + offsetParam + ADDRESS_PARAM + address
253+
+ CONTRACT_PARAM + contractAddress + blockParam + SORT_ASC_PARAM;
254+
255+
return getRequestUsingOffset(urlParams, TxTokenResponseTO.class);
256+
}
257+
232258
@NotNull
233259
@Override
234260
public List<TxToken> txsNftToken(String address) throws ApiException {

src/main/java/io/api/etherscan/core/impl/EtherScanApi.java

+8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class EtherScanApi implements AutoCloseable {
3333
private final IProxyApi proxy;
3434
private final IStatisticApi stats;
3535
private final ITransactionApi txs;
36+
private final IGasTrackerApi gastracker;
3637

3738
public EtherScanApi() {
3839
this(DEFAULT_KEY, EthNetwork.MAINNET);
@@ -88,6 +89,7 @@ public EtherScanApi(final String apiKey,
8889
? "com"
8990
: "io";
9091
final String baseUrl = "https://" + network.getDomain() + ".etherscan." + ending + "/api" + "?apikey=" + apiKey;
92+
final String mainnetBaseUrl = "https://" + EthNetwork.MAINNET.getDomain() + ".etherscan." + ending + "/api" + "?apikey=" + apiKey;
9193

9294
this.queueManager = queue;
9395
this.account = new AccountApiProvider(queue, baseUrl, executor);
@@ -97,6 +99,7 @@ public EtherScanApi(final String apiKey,
9799
this.proxy = new ProxyApiProvider(queue, baseUrl, executor);
98100
this.stats = new StatisticApiProvider(queue, baseUrl, executor);
99101
this.txs = new TransactionApiProvider(queue, baseUrl, executor);
102+
this.gastracker = new GasTrackerApiProvider(queue, mainnetBaseUrl, executor);
100103
}
101104

102105
@NotNull
@@ -134,6 +137,11 @@ public IStatisticApi stats() {
134137
return stats;
135138
}
136139

140+
@NotNull
141+
public IGasTrackerApi gastracker() {
142+
return gastracker;
143+
}
144+
137145
@Override
138146
public void close() throws Exception {
139147
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+
}
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)