Skip to content

Commit 948a6f3

Browse files
committed
[2.0.0-SNAPSHOT]
README.md updated Balance & TokenBalance constructor improved
1 parent 1beaafd commit 948a6f3

File tree

7 files changed

+73
-75
lines changed

7 files changed

+73
-75
lines changed

README.md

+55-56
Original file line numberDiff line numberDiff line change
@@ -43,141 +43,140 @@ implementation "com.github.goodforgod:java-etherscan-api:2.0.0-SNAPSHOT"
4343

4444
## Mainnet and Testnets
4545

46-
API support Ethereum: *[MAINNET](https://etherscan.io),
47-
[ROPSTEN](https://ropsten.etherscan.io),
48-
[KOVAN](https://kovan.etherscan.io),
49-
[RINKEBY](https://rinkeby.etherscan.io),
50-
[GORLI](https://goerli.etherscan.io),
51-
[TOBALABA](https://tobalaba.etherscan.com)* networks.
46+
API support Ethereum [default networks](https://docs.etherscan.io/getting-started/endpoint-urls):
47+
- [Mainnet](https://api.etherscan.io/)
48+
- [Goerli](https://api-goerli.etherscan.io/)
49+
- [Sepolia](https://api-sepolia.etherscan.io/)
50+
5251
```java
53-
EtherScanApi api = new EtherScanApi(EthNetwork.MAINNET); // Default
54-
EtherScanApi apiRinkeby = new EtherScanApi(EthNetwork.RINKEBY);
55-
EtherScanApi apiRopsten = new EtherScanApi(EthNetwork.ROPSTEN);
56-
EtherScanApi apiKovan = new EtherScanApi("YourApiKey", EthNetwork.KOVAN);
52+
EtherScanAPI api = EtherScanAPI.build();
53+
EtherScanAPI apiGoerli = EtherScanAPI.builder().withNetwork(EthNetworks.GORLI).build();
54+
EtherScanAPI apiSepolia = EtherScanAPI.builder().withNetwork(EthNetworks.SEPOLIA).build();
55+
```
56+
57+
### Custom Network
58+
59+
In case you want to use API for other EtherScan compatible network, you can easily provide custom network with domain api URI.
60+
61+
```java
62+
EtherScanAPI api = EtherScanAPI.builder()
63+
.withNetwork(() -> URI.create("https://api-my-custom.etherscan.io/api"))
64+
.build();
5765
```
5866

5967
## Custom HttpClient
6068

6169
In case you need to set custom timeout, custom headers or better implementation for HttpClient,
62-
just implement **IHttpExecutor** by your self or initialize it with your values.
70+
just implement **EthHttpClient** by your self or initialize it with your values.
6371

6472
```java
65-
int connectionTimeout = 10000;
66-
int readTimeout = 7000;
67-
68-
Supplier<IHttpExecutor> supplier = () -> new HttpExecutor(connectionTimeout);
69-
Supplier<IHttpExecutor> supplierFull = () -> new HttpExecutor(connectionTimeout, readTimeout);
70-
71-
EtherScanApi api = new EtherScanApi(EthNetwork.RINKEBY, supplier);
72-
EtherScanApi apiWithKey = new EtherScanApi("YourApiKey", EthNetwork.MAINNET, supplierFull);
73+
Supplier<EthHttpClient> ethHttpClientSupplier = () -> new UrlEthHttpClient(Duration.ofMillis(300), Duration.ofMillis(300));
74+
EtherScanAPI api = EtherScanAPI.builder()
75+
.withHttpClient(supplier)
76+
.build();
7377
```
7478

7579
## API Examples
7680

77-
You can read about all API methods on [Etherscan](https://etherscan.io/apis)
81+
You can read about all API methods on [Etherscan](https://docs.etherscan.io/api-endpoints/accounts)
7882

7983
*Library support all available EtherScan API.*
8084

81-
You can use library *with or without* API key *([Check API request\sec restrictions when used without API key](https://ethereum.stackexchange.com/questions/34190/does-etherscan-require-the-use-of-an-api-key))*.
85+
You can use library *with or without* API key *([Check API request\sec restrictions when used without API key](https://docs.etherscan.io/getting-started/viewing-api-usage-statistics))*.
8286

83-
Library will automatically limit requests up to **5 req/sec** when used *without* key.
87+
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).
8488
```java
85-
EtherScanApi api = new EtherScanApi();
86-
EtherScanApi api = new EtherScanApi("YourApiKey");
89+
EtherScanAPI.builder()
90+
.withApiKey(ApiRunner.API_KEY)
91+
.build();
8792
```
8893

8994
Below are examples for each API category.
9095

91-
### Account Api
96+
### Account API
9297

9398
**Get Ether Balance for a single Address**
94-
9599
```java
96-
EtherScanApi api = new EtherScanApi();
100+
EtherScanAPI api = EtherScanAPI.build();
97101
Balance balance = api.account().balance("0x8d4426f94e42f721C7116E81d6688cd935cB3b4F");
98102
```
99103

100-
### Block Api
104+
### Block API
101105

102106
**Get uncles block for block height**
103-
104107
```java
105-
EtherScanApi api = new EtherScanApi();
108+
EtherScanAPI api = EtherScanAPI.build();
106109
Optional<UncleBlock> uncles = api.block().uncles(200000);
107110
```
108111

109-
### Contract Api
112+
### Contract API
110113
**Request contract ABI from [verified codes](https://etherscan.io/contractsVerified)**
111114
```java
112-
EtherScanApi api = new EtherScanApi();
115+
EtherScanAPI api = EtherScanAPI.build();
113116
Abi abi = api.contract().contractAbi("0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413");
114117
```
115118

116-
### Logs Api
119+
### Logs API
117120

118121
**Get event logs for single topic**
119-
120122
```java
121-
EtherScanApi api = new EtherScanApi();
123+
EtherScanAPI api = EtherScanAPI.build();
122124
LogQuery query = LogQueryBuilder.with("0x33990122638b9132ca29c723bdf037f1a891a70c")
123125
.topic("0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545")
124126
.build();
125127
List<Log> logs = api.logs().logs(query);
126128
```
127129

128130
**Get event logs for 3 topics with respectful operations**
129-
130131
```java
131-
EtherScanApi api = new EtherScanApi();
132-
LogQuery query = LogQueryBuilder.with("0x33990122638b9132ca29c723bdf037f1a891a70c", 379224, 400000)
133-
.topic("0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545",
134-
"0x72657075746174696f6e00000000000000000000000000000000000000000000",
135-
"0x72657075746174696f6e00000000000000000000000000000000000000000000")
132+
EtherScanAPI api = EtherScanAPI.build();
133+
LogQuery query = LogQuery.builder("0x33990122638b9132ca29c723bdf037f1a891a70c")
134+
.withBlockFrom(379224)
135+
.withBlockTo(400000)
136+
.withTopic("0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545",
137+
"0x72657075746174696f6e00000000000000000000000000000000000000000000",
138+
"0x72657075746174696f6e00000000000000000000000000000000000000000000")
136139
.setOpTopic0_1(LogOp.AND)
137-
.setOpTopic0_2(LogOp.OR)
140+
.setOpTopic0_2(null)
138141
.setOpTopic1_2(LogOp.AND)
139142
.build();
140143

141144
List<Log> logs = api.logs().logs(query);
142145
```
143146

144-
### Proxy Api
145-
146-
**Get tx detailds with proxy endpoint**
147+
### Proxy API
147148

149+
**Get tx details with proxy endpoint**
148150
```java
149-
EtherScanApi api = new EtherScanApi(EthNetwork.MAINNET);
151+
EtherScanAPI api = EtherScanAPI.build();
150152
Optional<TxProxy> tx = api.proxy().tx("0x1e2910a262b1008d0616a0beb24c1a491d78771baa54a33e66065e03b1f46bc1");
151153
```
152154

153155
**Get block info with proxy endpoint**
154-
155156
```java
156-
EtherScanApi api = new EtherScanApi(EthNetwork.MAINNET);
157+
EtherScanAPI api = EtherScanAPI.build();
157158
Optional<BlockProxy> block = api.proxy().block(15215);
158159
```
159160

160-
### Stats Api
161+
### Stats API
161162

162163
**Statistic about last price**
163-
164164
```java
165-
EtherScanApi api = new EtherScanApi();
165+
EtherScanAPI api = EtherScanAPI.build();
166166
Price price = api.stats().lastPrice();
167167
```
168168

169-
### Transaction Api
169+
### Transaction API
170170

171171
**Request receipt status for tx**
172-
173172
```java
174-
EtherScanApi api = new EtherScanApi();
173+
EtherScanAPI api = EtherScanAPI.build();
175174
Optional<Boolean> status = api.txs().receiptStatus("0x513c1ba0bebf66436b5fed86ab668452b7805593c05073eb2d51d3a52f480a76");
176175
```
177176

178-
### Token Api
177+
### Token API
179178

180-
You can read about token API [here](https://etherscan.io/apis#tokens)
179+
You can read about token API [here](https://docs.etherscan.io/api-endpoints/tokens)
181180

182181
Token API methods migrated to [Account](#account-api) & [Stats](#stats-api) respectfully.
183182

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public Balance balance(String address) throws EtherScanException {
6464
if (response.getStatus() != 1)
6565
throw new EtherScanResponseException(response);
6666

67-
return new Balance(address, new BigInteger(response.getResult()));
67+
return new Balance(address, new Wei(new BigInteger(response.getResult())));
6868
}
6969

7070
@NotNull
@@ -78,7 +78,7 @@ public TokenBalance balance(String address, String contract) throws EtherScanExc
7878
if (response.getStatus() != 1)
7979
throw new EtherScanResponseException(response);
8080

81-
return new TokenBalance(address, new BigInteger(response.getResult()), contract);
81+
return new TokenBalance(address, new Wei(new BigInteger(response.getResult())), contract);
8282
}
8383

8484
@NotNull
@@ -101,7 +101,7 @@ public List<Balance> balances(List<String> addresses) throws EtherScanException
101101

102102
if (!BasicUtils.isEmpty(response.getResult()))
103103
balances.addAll(response.getResult().stream()
104-
.map(r -> new Balance(r.getAccount(), new BigInteger(r.getBalance())))
104+
.map(r -> new Balance(r.getAccount(), new Wei(new BigInteger(r.getBalance()))))
105105
.collect(Collectors.toList()));
106106
}
107107

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

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public interface EtherScanAPI extends AutoCloseable {
3737
@NotNull
3838
GasTrackerAPI gasTracker();
3939

40+
@NotNull
41+
static EtherScanAPI build() {
42+
return builder().build();
43+
}
44+
4045
@NotNull
4146
static Builder builder() {
4247
return new EthScanAPIBuilder();

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.goodforgod.api.etherscan.model;
22

3-
import java.math.BigInteger;
43
import java.util.Objects;
54

65
/**
@@ -13,9 +12,9 @@ public class Balance {
1312
private final Wei balance;
1413
private final String address;
1514

16-
public Balance(String address, BigInteger balance) {
15+
public Balance(String address, Wei balance) {
1716
this.address = address;
18-
this.balance = new Wei(balance);
17+
this.balance = balance;
1918
}
2019

2120
// <editor-fold desc="Getters">

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.goodforgod.api.etherscan.model;
22

3-
import java.math.BigInteger;
43
import java.util.Objects;
54

65
/**
@@ -11,7 +10,7 @@ public class TokenBalance extends Balance {
1110

1211
private final String tokenContract;
1312

14-
public TokenBalance(String address, BigInteger balance, String tokenContract) {
13+
public TokenBalance(String address, Wei balance, String tokenContract) {
1514
super(address, balance);
1615
this.tokenContract = tokenContract;
1716
}

src/test/java/io/goodforgod/api/etherscan/account/AccountBalanceTests.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ class AccountBalanceTests extends ApiRunner {
1818
void correct() {
1919
Balance balance = api.account().balance("0x8d4426f94e42f721C7116E81d6688cd935cB3b4F");
2020
assertNotNull(balance);
21-
assertNotNull(balance.getWei());
22-
assertNotNull(balance.getMwei());
23-
assertNotNull(balance.getKwei());
24-
assertNotNull(balance.getGwei());
25-
assertNotNull(balance.getEther());
21+
assertNotNull(balance.getBalanceInWei());
2622
assertNotNull(balance.getAddress());
2723
assertNotNull(balance.toString());
2824
}
@@ -37,8 +33,8 @@ void invalidParamWithError() {
3733
void correctParamWithEmptyExpectedResult() {
3834
Balance balance = api.account().balance("0x1d4426f94e42f721C7116E81d6688cd935cB3b4F");
3935
assertNotNull(balance);
40-
assertNotNull(balance.getWei());
36+
assertNotNull(balance.getBalanceInWei());
4137
assertNotNull(balance.getAddress());
42-
assertEquals(0, balance.getWei().intValue());
38+
assertEquals(0, balance.getBalanceInWei().asWei().intValue());
4339
}
4440
}

src/test/java/io/goodforgod/api/etherscan/account/AccountTokenBalanceTests.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ void correct() {
1919
TokenBalance balance = api.account().balance("0x5d807e7F124EC2103a59c5249187f772c0b8D6b2",
2020
"0x5EaC95ad5b287cF44E058dCf694419333b796123");
2121
assertNotNull(balance);
22-
assertNotNull(balance.getWei());
22+
assertNotNull(balance.getBalanceInWei());
2323
assertNotNull(balance.getAddress());
2424
assertNotNull(balance.getContract());
2525
assertNotNull(balance.toString());
2626

27-
TokenBalance balance2 = new TokenBalance("125161", balance.getWei(), balance.getContract());
27+
TokenBalance balance2 = new TokenBalance("125161", balance.getBalanceInWei(), balance.getContract());
2828
assertNotEquals(balance, balance2);
2929
assertNotEquals(balance.hashCode(), balance2.hashCode());
3030
}
@@ -48,9 +48,9 @@ void correctParamWithEmptyExpectedResult() {
4848
TokenBalance balance = api.account().balance("0x1d807e7F124EC2103a59c5249187f772c0b8D6b2",
4949
"0x5EaC95ad5b287cF44E058dCf694419333b796123");
5050
assertNotNull(balance);
51-
assertNotNull(balance.getWei());
51+
assertNotNull(balance.getBalanceInWei());
5252
assertNotNull(balance.getAddress());
5353
assertNotNull(balance.getContract());
54-
assertEquals(0, balance.getWei().intValue());
54+
assertEquals(0, balance.getBalanceInWei().asWei().intValue());
5555
}
5656
}

0 commit comments

Comments
 (0)