Git Product home page Git Product logo

java-etherscan-api's Introduction

Java EtherScan API

Minimum required Java version Maven Central Java CI Coverage Maintainability Rating Lines of Code

Etherscan.io Java API implementation.

Library supports EtherScan API for all available Ethereum Networks for etherscan.io

Dependency 🚀

Gradle

implementation "com.github.goodforgod:java-etherscan-api:2.1.0"

Maven

<dependency>
    <groupId>com.github.goodforgod</groupId>
    <artifactId>java-etherscan-api</artifactId>
    <version>2.1.0</version>
</dependency>

Content

MainNet and TestNets

API support all Ethereum default networks:

EtherScanAPI api = EtherScanAPI.builder().build();
EtherScanAPI apiGoerli = EtherScanAPI.builder().withNetwork(EthNetworks.GORLI).build();
EtherScanAPI apiSepolia = EtherScanAPI.builder().withNetwork(EthNetworks.SEPOLIA).build();

Custom Network

In case you want to use API for other EtherScan compatible network, you can easily provide custom network with domain api URI.

EtherScanAPI api = EtherScanAPI.builder()
        .withNetwork(() -> URI.create("https://api-my-custom.etherscan.io/api"))
        .build();

Custom HttpClient

In case you need to set custom timeout, custom headers or better implementation for HttpClient, just implement EthHttpClient by your self or initialize it with your values.

Supplier<EthHttpClient> ethHttpClientSupplier = () -> new UrlEthHttpClient(Duration.ofMillis(300), Duration.ofMillis(300));
EtherScanAPI api = EtherScanAPI.builder()
    .withHttpClient(supplier)
    .build();

API Examples

You can read about all API methods on Etherscan

Library support all available EtherScan API.

You can use library with or without API key (Check API request\sec restrictions when used without API key).

Library will automatically limit requests up to 1 requests in 5 seconds when used without key and up to 5 requests in 1 seconds when used with API KEY (free plan).

EtherScanAPI.builder()
        .withApiKey(ApiRunner.API_KEY)
        .build();

Below are examples for each API category.

Account API

Get Ether Balance for a single Address

EtherScanAPI api = EtherScanAPI.builder().build();
Balance balance = api.account().balance("0x8d4426f94e42f721C7116E81d6688cd935cB3b4F");

Block API

Get uncles block for block height

EtherScanAPI api = EtherScanAPI.builder().build();
Optional<UncleBlock> uncles = api.block().uncles(200000);

Contract API

Request contract ABI from verified codes

EtherScanAPI api = EtherScanAPI.builder().build();
Abi abi = api.contract().contractAbi("0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413");

Logs API

Get event logs for single topic

EtherScanAPI api = EtherScanAPI.builder().build();
LogQuery query = LogQuery.builder("0x33990122638b9132ca29c723bdf037f1a891a70c")
           .withTopic("0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545")
           .build();
List<Log> logs = api.logs().logs(query);

Get event logs for 3 topics with respectful operations

EtherScanAPI api = EtherScanAPI.builder().build();
LogQuery query = LogQuery.builder("0x33990122638b9132ca29c723bdf037f1a891a70c")
        .withBlockFrom(379224)
        .withBlockTo(400000)
        .withTopic("0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545",
        "0x72657075746174696f6e00000000000000000000000000000000000000000000",
        "0x72657075746174696f6e00000000000000000000000000000000000000000000")
        .setOpTopic0_1(LogOp.AND)
        .setOpTopic0_2(null)
        .setOpTopic1_2(LogOp.AND)
        .build();
 
List<Log> logs = api.logs().logs(query);

Proxy API

Get tx details with proxy endpoint

EtherScanAPI api = EtherScanAPI.builder().build();
Optional<TxProxy> tx = api.proxy().tx("0x1e2910a262b1008d0616a0beb24c1a491d78771baa54a33e66065e03b1f46bc1");

Get block info with proxy endpoint

EtherScanAPI api = EtherScanAPI.builder().build();
Optional<BlockProxy> block = api.proxy().block(15215);

Stats API

Statistic about last price

EtherScanAPI api = EtherScanAPI.builder().build();
Price price = api.stats().priceLast();

Transaction API

Request receipt status for tx

EtherScanAPI api = EtherScanAPI.builder().build();
Optional<Boolean> status = api.txs().receiptStatus("0x513c1ba0bebf66436b5fed86ab668452b7805593c05073eb2d51d3a52f480a76");

Token API

You can read about token API here

Token API methods migrated to Account & Stats respectfully.

License

This project licensed under the MIT - see the LICENSE file for details.

java-etherscan-api's People

Contributors

abhaygupta1999 avatar blackmorse avatar dependabot[bot] avatar goodforgod avatar guggio avatar isnow avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

java-etherscan-api's Issues

io.api.etherscan.error.ConnectionException: Server error: Bad Gateway

Exception in thread "main" io.api.etherscan.error.ConnectionException: Server error: Bad Gateway
	at io.api.etherscan.executor.impl.HttpExecutor.get(HttpExecutor.java:102)
	at io.api.etherscan.core.impl.BasicProvider.getRequest(BasicProvider.java:73)
	at io.api.etherscan.core.impl.BasicProvider.getRequest(BasicProvider.java:87)
	at io.api.etherscan.core.impl.AccountApiProvider.balance(AccountApiProvider.java:63)
	at info.www.Main3.main(Main3.java:40)
Caused by: io.api.etherscan.error.ConnectionException: Server error: Bad Gateway
	at io.api.etherscan.executor.impl.HttpExecutor.get(HttpExecutor.java:93)
Caused by: io.api.etherscan.error.ConnectionException: Server error: Bad Gateway
Main 40
  for(int i = 0; i < 10000; i++){
...
                Balance balance = api.account().balance(address);
...
}

[Need Help] EtherScan Logs API condition

I'm trying to use the Logs API to query the following condition: topic0 and (topic1 or topic2), but I can't seem to figure it out.

E.g. this query should not return tx 0x74086ddc2f74d8bab13bdeaf1b4a98ceac3d042ec4901fdafe9c585b88369b14, since its topic0 is not 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef

https://api.etherscan.io/api?module=logs&action=getLogs
&fromBlock=13938782
&toBlock=13938782
&topic0=0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef
&topic1=0x000000000000000000000000864ba7f98750ff42cbf07e1f47fd8636d251dfe0
&topic2=0x000000000000000000000000864ba7f98750ff42cbf07e1f47fd8636d251dfe0
&topic1_2_opr=or

Any suggestion is really appreciated.

Mvp777

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like
A clear and concise description of what you want to happen.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

Refactore structure to be extendable

Refactor project structure to be easily extendable, so you can easily create new implementations for same API like for Polygon API and other Etherscan compatible sites

I want to use http proxy

Is your feature request related to a problem? Please describe.
I want to use http proxy

Describe the solution you'd like
I want to add a proxy parameter to io.goodforgod.api.etherscan.http.impl.UrlEthHttpClient to set the http proxy.

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 1080));
UrlEthHttpClient .proxy = proxy;

Describe alternatives you've considered
make a copy https://github.com/GoodforGod/java-etherscan-api/blob/master/src/main/java/io/goodforgod/api/etherscan/http/impl/UrlEthHttpClient.java

Additional context
Add any other context or scre
20230621144948
enshots about the feature request here.

Suggest adding custom features to Gson for LocalDateTime and LocalDate

Since use com.google.gson.Gson to parse the json of Etherscan's api response.
However the error will appear:
io.api.etherscan.error.ParseException: Unable to make field private final java.time.LocalDate java.time.LocalDateTime.date accessible: module java.base does not "opens java.time" to unnamed module @38d8f54a

The reason is at:
io.api.etherscan.core.impl.BasicProvider

Line 47:
this.gson = new Gson();

I change the below code to fix this issuse:

this.gson = new GsonBuilder()
                .registerTypeAdapter(LocalDateTime.class, (JsonSerializer<LocalDateTime>) (src, typeOfSrc, context) -> new JsonPrimitive(src.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))))
                .registerTypeAdapter(LocalDate.class, (JsonSerializer<LocalDate>) (src, typeOfSrc, context) -> new JsonPrimitive(src.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))))
                .registerTypeAdapter(LocalDateTime.class, (JsonDeserializer<LocalDateTime>) (json, type, jsonDeserializationContext) -> {
                    String datetime = json.getAsJsonPrimitive().getAsString();
                    return LocalDateTime.parse(datetime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                }).registerTypeAdapter(LocalDate.class, (JsonDeserializer<LocalDate>) (json, type, jsonDeserializationContext) -> {
                    String datetime = json.getAsJsonPrimitive().getAsString();
                    return LocalDate.parse(datetime, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                }).create();

Maybe the best way is either update the source code or supply a custom features.

Documentation is out of date.

Describe the bug
There are a lot of places with the following sample code which can not compile.
EtherScanAPI api = EtherScanAPI.build();

Expected behavior
EtherScanAPI api = EtherScanAPI.builder().build();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.