Skip to content

added support for txsToken with contract address too #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 13, 2023
Merged
19 changes: 19 additions & 0 deletions src/main/java/io/api/etherscan/core/IAccountApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@ public interface IAccountApi {
@NotNull
List<TxToken> txsToken(String address) throws ApiException;

/**
* All ERC-20 token txs for given address and contract address
*
* @param address get txs for
* @param contractAddress contract address to get txs for
* @param startBlock tx from this blockNumber
* @param endBlock tx to this blockNumber
* @return txs for address
* @throws ApiException parent exception class
*/
@NotNull
List<TxToken> txsToken(String address, String contractAddress, long startBlock, long endBlock) throws ApiException;

@NotNull
List<TxToken> txsToken(String address, String contractAddress, long startBlock) throws ApiException;

@NotNull
List<TxToken> txsToken(String address, String contractAddress) throws ApiException;

/**
* All ERC-721 (NFT) token txs for given address
*
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/io/api/etherscan/core/IGasTrackerApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.api.etherscan.core;

import io.api.etherscan.error.ApiException;
import io.api.etherscan.model.GasOracle;
import org.jetbrains.annotations.NotNull;

/**
* EtherScan - API Descriptions https://docs.etherscan.io/api-endpoints/gas-tracker
*
* @author Abhay Gupta
* @since 14.11.2022
*/
public interface IGasTrackerApi {

/**
* GasOracle details
*
* @return fast, suggested gas price
* @throws ApiException parent exception class
*/
@NotNull
GasOracle gasoracle() throws ApiException;
}
26 changes: 26 additions & 0 deletions src/main/java/io/api/etherscan/core/impl/AccountApiProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,32 @@ public List<TxToken> txsToken(final String address, final long startBlock, final
return getRequestUsingOffset(urlParams, TxTokenResponseTO.class);
}

@NotNull
@Override
public List<TxToken> txsToken(final String address, final String contractAddress) throws ApiException {
return txsToken(address, contractAddress, MIN_START_BLOCK);
}

@NotNull
@Override
public List<TxToken> txsToken(final String address, final String contractAddress, final long startBlock) throws ApiException {
return txsToken(address, contractAddress, startBlock, MAX_END_BLOCK);
}

@NotNull
@Override
public List<TxToken> txsToken(final String address, final String contractAddress, final long startBlock, final long endBlock) throws ApiException {
BasicUtils.validateAddress(address);
final BlockParam blocks = BasicUtils.compensateBlocks(startBlock, endBlock);

final String offsetParam = PAGE_PARAM + "%s" + OFFSET_PARAM + OFFSET_MAX;
final String blockParam = START_BLOCK_PARAM + blocks.start() + END_BLOCK_PARAM + blocks.end();
final String urlParams = ACT_TX_TOKEN_ACTION + offsetParam + ADDRESS_PARAM + address
+ CONTRACT_PARAM + contractAddress + blockParam + SORT_ASC_PARAM;

return getRequestUsingOffset(urlParams, TxTokenResponseTO.class);
}

@NotNull
@Override
public List<TxToken> txsNftToken(String address) throws ApiException {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/io/api/etherscan/core/impl/EtherScanApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class EtherScanApi implements AutoCloseable {
private final IProxyApi proxy;
private final IStatisticApi stats;
private final ITransactionApi txs;
private final IGasTrackerApi gastracker;

public EtherScanApi() {
this(DEFAULT_KEY, EthNetwork.MAINNET);
Expand Down Expand Up @@ -87,6 +88,7 @@ public EtherScanApi(final String apiKey,

final String ending = EthNetwork.TOBALABA.equals(network) ? "com" : "io";
final String baseUrl = "https://" + network.getDomain() + ".etherscan." + ending + "/api" + "?apikey=" + apiKey;
final String mainnetBaseUrl = "https://" + EthNetwork.MAINNET.getDomain() + ".etherscan." + ending + "/api" + "?apikey=" + apiKey;

this.queueManager = queue;
this.account = new AccountApiProvider(queue, baseUrl, executor);
Expand All @@ -96,6 +98,7 @@ public EtherScanApi(final String apiKey,
this.proxy = new ProxyApiProvider(queue, baseUrl, executor);
this.stats = new StatisticApiProvider(queue, baseUrl, executor);
this.txs = new TransactionApiProvider(queue, baseUrl, executor);
this.gastracker = new GasTrackerApiProvider(queue, mainnetBaseUrl, executor);
}

@NotNull
Expand Down Expand Up @@ -133,6 +136,11 @@ public IStatisticApi stats() {
return stats;
}

@NotNull
public IGasTrackerApi gastracker() {
return gastracker;
}

@Override
public void close() throws Exception {
queueManager.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.api.etherscan.core.impl;

import io.api.etherscan.core.IGasTrackerApi;
import io.api.etherscan.error.ApiException;
import io.api.etherscan.error.EtherScanException;
import io.api.etherscan.executor.IHttpExecutor;
import io.api.etherscan.manager.IQueueManager;
import io.api.etherscan.model.GasOracle;
import io.api.etherscan.model.utility.GasOracleResponseTO;
import org.jetbrains.annotations.NotNull;

/**
* GasTracker API Implementation
*
* @see IGasTrackerApi
*
* @author Abhay Gupta
* @since 14.11.2022
*/
public class GasTrackerApiProvider extends BasicProvider implements IGasTrackerApi {

private static final String ACT_GAS_ORACLE_PARAM = ACT_PREFIX + "gasoracle";

GasTrackerApiProvider(final IQueueManager queue,
final String baseUrl,
final IHttpExecutor executor) {
super(queue, "gastracker", baseUrl, executor);
}

@NotNull
@Override
public GasOracle gasoracle() throws ApiException {
final GasOracleResponseTO response = getRequest(ACT_GAS_ORACLE_PARAM, GasOracleResponseTO.class);
if (response.getStatus() != 1)
throw new EtherScanException(response);

return response.getResult();
}
}
68 changes: 68 additions & 0 deletions src/main/java/io/api/etherscan/model/GasOracle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package io.api.etherscan.model;

import java.math.BigInteger;
import java.util.Objects;

/**
* ! NO DESCRIPTION !
*
* @author Abhay Gupta
* @since 14.11.2022
*/
public class GasOracle {
private Long LastBlock;
private Integer SafeGasPrice;
private Integer ProposeGasPrice;
private Integer FastGasPrice;
private Double suggestBaseFee;
private String gasUsedRatio;

public Long getLastBlock() {
return LastBlock;
}

public BigInteger getSafeGasPriceInWei() {
return BigInteger.valueOf(SafeGasPrice).multiply(BigInteger.TEN.pow(9));
}

public BigInteger getProposeGasPriceInWei() {
return BigInteger.valueOf(ProposeGasPrice).multiply(BigInteger.TEN.pow(9));
}

public BigInteger getFastGasPriceInWei() {
return BigInteger.valueOf(FastGasPrice).multiply(BigInteger.TEN.pow(9));
}

public Double getSuggestBaseFee() {
return suggestBaseFee;
}

public String getGasUsedRatio() {
return gasUsedRatio;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GasOracle gasOracle = (GasOracle) o;
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);
}

@Override
public int hashCode() {
return Objects.hash(LastBlock, SafeGasPrice, ProposeGasPrice, FastGasPrice, suggestBaseFee, gasUsedRatio);
}

@Override
public String toString() {
return "GasOracle{" +
"LastBlock=" + LastBlock +
", SafeGasPrice=" + SafeGasPrice +
", ProposeGasPrice=" + ProposeGasPrice +
", FastGasPrice=" + FastGasPrice +
", suggestBaseFee=" + suggestBaseFee +
", gasUsedRatio='" + gasUsedRatio + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.api.etherscan.model.utility;

import io.api.etherscan.model.GasOracle;

/**
* ! NO DESCRIPTION !
*
* @author Abhay Gupta
* @since 14.11.2022
*/
public class GasOracleResponseTO extends BaseResponseTO {

private GasOracle result;

public GasOracle getResult() {
return result;
}
}