Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit555f7d4

Browse files
committed
Proxy api improvements & fixes
Http get empty response handleHttp post implementationNew models introduced
1 parentd533851 commit555f7d4

File tree

12 files changed

+310
-46
lines changed

12 files changed

+310
-46
lines changed

‎src/main/java/io/api/etherscan/App.java‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
packageio.api.etherscan;
22

33
importio.api.etherscan.core.impl.EtherScanApi;
4-
importio.api.etherscan.model.Tx;
5-
importio.api.etherscan.model.TxInternal;
6-
importio.api.etherscan.model.TxToken;
7-
importio.api.etherscan.model.UncleBlock;
4+
importio.api.etherscan.model.*;
85

96
importjava.util.List;
107
importjava.util.Optional;
@@ -15,6 +12,7 @@
1512
publicclassApp {
1613
publicstaticvoidmain(String[]args) {
1714
EtherScanApiapi =newEtherScanApi(args[0]);
15+
Balancebalance =api.account().balance("0x8d4426f94e42f721C7116E81d6688cd935cB3b4F");
1816
List<Tx>txs =api.account().txs("0x8d4426f94e42f721C7116E81d6688cd935cB3b4F");
1917
List<TxToken>txTokens =api.account().txsToken("0xf261B3A60Ef40eE0B369B0705c1a2c58B02799DF");
2018
List<TxInternal>txInternals =api.account().txsInternal("0x2c1ba59d6f58433fb1eaee7d20b26ed83bda51a3");

‎src/main/java/io/api/etherscan/core/IProxyApi.java‎

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
importio.api.etherscan.error.ApiException;
44
importio.api.etherscan.model.proxy.BlockProxy;
5+
importio.api.etherscan.model.proxy.TxInfoProxy;
56
importio.api.etherscan.model.proxy.TxProxy;
67
importorg.jetbrains.annotations.NotNull;
78

@@ -17,49 +18,89 @@
1718
*/
1819
publicinterfaceIProxyApi {
1920

20-
/** Returns the number of most recent block */
21+
/**
22+
* Returns the number of most recent block
23+
* eth_blockNumber
24+
*/
2125
longblockNoLast();
2226

23-
/** Returns information about a block by block number */
27+
/**
28+
* Returns information about a block by block number
29+
* eth_getBlockByNumber
30+
*/
2431
@NotNullOptional<BlockProxy>block(longblockNo)throwsApiException;
2532

26-
/** Returns information about a uncle by block number */
33+
/**
34+
* Returns information about a uncle by block number
35+
* eth_getUncleByBlockNumberAndIndex
36+
*/
2737
@NotNullOptional<BlockProxy>blockUncle(longblockNo,longindex)throwsApiException;
2838

29-
/** Returns the information about a transaction requested by transaction hash */
39+
/**
40+
* Returns the information about a transaction requested by transaction hash
41+
* eth_getTransactionByHash
42+
*/
3043
@NotNullOptional<TxProxy>tx(Stringtxhash)throwsApiException;
3144

32-
/** Returns information about a transaction by block number and transaction index position */
45+
/**
46+
* Returns information about a transaction by block number and transaction index position
47+
* eth_getTransactionByBlockNumberAndIndex
48+
*/
3349
@NotNullOptional<TxProxy>tx(longblockNo,longindex)throwsApiException;
3450

35-
/** Returns the number of transactions in a block from a block matching the given block number */
51+
/**
52+
* Returns the number of transactions in a block from a block matching the given block number
53+
* eth_getBlockTransactionCountByNumber
54+
*/
3655
inttxCount(longblockNo)throwsApiException;
3756

38-
/** Returns the number of transactions sent from an address */
57+
/**
58+
* Returns the number of transactions sent from an address
59+
* eth_getTransactionCount
60+
*/
3961
inttxSendCount(Stringaddress)throwsApiException;
4062

41-
//TODO implement
42-
/** Creates new message call transaction or a contract creation for signed transactions */
43-
booleantxSendRaw(StringhexEncodedTx)throwsApiException;
63+
/**
64+
* Creates new message call transaction or a contract creation for signed transactions
65+
* eth_sendRawTransaction
66+
*/
67+
@NotNullOptional<String>txSendRaw(StringhexEncodedTx)throwsApiException;
68+
69+
/**
70+
* Returns the receipt of a transaction by transaction hash
71+
* eth_getTransactionReceipt
72+
*/
73+
@NotNullOptional<TxInfoProxy>txReceipt(Stringtxhash);
4474

45-
/** Executes a new message call immediately without creating a transaction on the block chain */
46-
@NotNullStringcall(Stringaddress,Stringdata)throwsApiException;
75+
/**
76+
* Executes a new message call immediately without creating a transaction on the block chain
77+
* eth_call
78+
*/
79+
@NotNullOptional<String>call(Stringaddress,Stringdata)throwsApiException;
4780

48-
/** Returns code at a given address */
49-
@NotNullStringcode(Stringaddress)throwsApiException;
81+
/**
82+
* Returns code at a given address
83+
* eth_getCode
84+
*/
85+
@NotNullOptional<String>code(Stringaddress)throwsApiException;
5086

5187
/**
5288
* (**experimental)
5389
* Returns the value from a storage position at a given address
90+
* eth_getStorageAt
5491
*/
55-
@NotNullStringstorageAt(Stringaddress,longposition)throwsApiException;
92+
@NotNullOptional<String>storageAt(Stringaddress,longposition)throwsApiException;
5693

57-
/** Returns the current price per gas in wei */
94+
/**
95+
* Returns the current price per gas in wei
96+
* eth_gasPrice
97+
*/
5898
@NotNullBigIntegergasPrice()throwsApiException;
5999

60100
/**
61101
* Makes a call or transaction, which won't be added to the blockchain and returns the used gas,
62102
* which can be used for estimating the used gas
103+
* eth_estimateGas
63104
*/
64105
@NotNullBigIntegergasEstimated()throwsApiException;
65106
@NotNullBigIntegergasEstimated(StringhexData)throwsApiException;

‎src/main/java/io/api/etherscan/core/impl/BasicProvider.java‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
packageio.api.etherscan.core.impl;
22

33
importcom.google.gson.Gson;
4+
importio.api.etherscan.error.NoResponseException;
45
importio.api.etherscan.error.ParseException;
56
importio.api.etherscan.executor.IHttpExecutor;
67
importio.api.etherscan.manager.IQueueManager;
8+
importio.api.etherscan.util.BasicUtils;
79

810
/**
911
* Base provider for API Implementations
1012
*
11-
* @see EtherScanApi
12-
*
1313
* @author GoodforGod
14+
* @see EtherScanApi
1415
* @since 28.10.2018
1516
*/
1617
abstractclassBasicProvider {
@@ -24,6 +25,7 @@ abstract class BasicProvider {
2425
privatefinalStringbaseUrl;
2526
privatefinalIHttpExecutorexecutor;
2627
privatefinalIQueueManagerqueue;
28+
privatefinalGsongson;
2729

2830
BasicProvider(finalIQueueManagerqueue,
2931
finalStringmodule,
@@ -33,11 +35,12 @@ abstract class BasicProvider {
3335
this.module ="&module=" +module;
3436
this.baseUrl =baseUrl;
3537
this.executor =executor;
38+
this.gson =newGson();
3639
}
3740

3841
private <T>Tconvert(finalStringjson,finalClass<T>tClass) {
3942
try {
40-
returnnewGson().fromJson(json,tClass);
43+
returngson.fromJson(json,tClass);
4144
}catch (Exceptione) {
4245
thrownewParseException(e.getMessage(),e.getCause());
4346
}
@@ -46,10 +49,24 @@ private <T> T convert(final String json, final Class<T> tClass) {
4649
privateStringgetRequest(finalStringurlParameters) {
4750
queue.takeTurn();
4851
finalStringurl =baseUrl +module +urlParameters;
49-
returnexecutor.get(url);
52+
finalStringresult =executor.get(url);
53+
if (BasicUtils.isEmpty(result))
54+
thrownewNoResponseException("Server returned null value for GET request at URL - " +url);
55+
56+
returnresult;
57+
}
58+
59+
privateStringpostRequest(finalStringurlParameters,finalStringdataToPost) {
60+
queue.takeTurn();
61+
finalStringurl =baseUrl +module +urlParameters;
62+
returnexecutor.post(url,dataToPost);
5063
}
5164

5265
<T>TgetRequest(finalStringurlParameters,finalClass<T>tClass) {
5366
returnconvert(getRequest(urlParameters),tClass);
5467
}
68+
69+
<T>TpostRequest(finalStringurlParameters,finalStringdataToPost,finalClass<T>tClass) {
70+
returnconvert(postRequest(urlParameters,dataToPost),tClass);
71+
}
5572
}

‎src/main/java/io/api/etherscan/core/impl/BlockApiProvider.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class BlockApiProvider extends BasicProvider implements IBlockApi {
3535
publicOptional<UncleBlock>uncles(longblockNumber) {
3636
finalStringurlParam =ACT_BLOCK_PARAM +BLOCKNO_PARAM +blockNumber;
3737
finalUncleBlockResponseTOresponse =getRequest(urlParam,UncleBlockResponseTO.class);
38+
3839
BasicUtils.validateTxResponse(response);
3940

4041
return (response.getResult() ==null ||response.getResult().isEmpty())

‎src/main/java/io/api/etherscan/core/impl/ProxyApiProvider.java‎

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
importio.api.etherscan.core.IProxyApi;
44
importio.api.etherscan.error.ApiException;
5+
importio.api.etherscan.error.EtherScanException;
56
importio.api.etherscan.error.InvalidDataHexException;
67
importio.api.etherscan.executor.IHttpExecutor;
78
importio.api.etherscan.manager.IQueueManager;
89
importio.api.etherscan.model.proxy.BlockProxy;
10+
importio.api.etherscan.model.proxy.TxInfoProxy;
911
importio.api.etherscan.model.proxy.TxProxy;
1012
importio.api.etherscan.model.proxy.utility.BlockProxyTO;
1113
importio.api.etherscan.model.proxy.utility.StringProxyTO;
14+
importio.api.etherscan.model.proxy.utility.TxInfoProxyTO;
1215
importio.api.etherscan.model.proxy.utility.TxProxyTO;
1316
importio.api.etherscan.util.BasicUtils;
1417
importorg.jetbrains.annotations.NotNull;
@@ -112,7 +115,7 @@ public Optional<TxProxy> tx(final long blockNo, final long index) throws ApiExce
112115
@Override
113116
publicinttxCount(finallongblockNo)throwsApiException {
114117
finallongcompensatedBlockNo =BasicUtils.compensateMinBlock(blockNo);
115-
finalStringurlParams =ACT_TX_COUNT_PARAM +TAG_PARAM +compensatedBlockNo;
118+
finalStringurlParams =ACT_BLOCKTX_COUNT_PARAM +TAG_PARAM +compensatedBlockNo;
116119
finalStringProxyTOresponse =getRequest(urlParams,StringProxyTO.class);
117120
returnInteger.valueOf(response.getResult());
118121
}
@@ -121,52 +124,73 @@ public int txCount(final long blockNo) throws ApiException {
121124
publicinttxSendCount(finalStringaddress)throwsApiException {
122125
BasicUtils.validateAddress(address);
123126

124-
finalStringurlParams =ACT_TX_BY_HASH_PARAM +ADDRESS_PARAM +address +TAG_LAST_PARAM;
127+
finalStringurlParams =ACT_TX_COUNT_PARAM +ADDRESS_PARAM +address +TAG_LAST_PARAM;
125128
finalStringProxyTOresponse =getRequest(urlParams,StringProxyTO.class);
126129
return (int)BasicUtils.parseHex(response.getResult());
127130
}
128131

129-
//TODO need postRequest executor implementation
130132
@Override
131-
publicbooleantxSendRaw(finalStringhexEncodedTx)throwsApiException {
132-
returnfalse;
133+
@NotNull
134+
publicOptional<String>txSendRaw(finalStringhexEncodedTx)throwsApiException {
135+
if(BasicUtils.isNotHex(hexEncodedTx))
136+
thrownewInvalidDataHexException("Data is not encoded in hex format - " +hexEncodedTx);
137+
138+
finalStringurlParams =ACT_SEND_RAW_TX_PARAM +HEX_PARAM +hexEncodedTx;
139+
finalStringProxyTOresponse =getRequest(urlParams,StringProxyTO.class);
140+
if(response.getError() !=null)
141+
thrownewEtherScanException("Error occurred with code " +response.getError().getCode()
142+
+" with message " +response.getError().getMessage());
143+
144+
return (BasicUtils.isEmpty(response.getResult()))
145+
?Optional.empty()
146+
:Optional.of(response.getResult());
147+
}
148+
149+
@NotNull
150+
@Override
151+
publicOptional<TxInfoProxy>txReceipt(finalStringtxhash) {
152+
BasicUtils.validateTxHash(txhash);
153+
154+
finalStringurlParams =ACT_TX_RECEIPT_PARAM +TXHASH_PARAM +txhash;
155+
finalTxInfoProxyTOresponse =getRequest(urlParams,TxInfoProxyTO.class);
156+
returnOptional.ofNullable(response.getResult());
133157
}
134158

135159
@NotNull
136160
@Override
137-
publicStringcall(finalStringaddress,finalStringdata)throwsApiException {
161+
publicOptional<String>call(finalStringaddress,finalStringdata)throwsApiException {
138162
BasicUtils.validateAddress(address);
139163

140164
finalStringurlParams =ACT_CALL_PARAM +TO_PARAM +address +DATA_PARAM +data +TAG_LAST_PARAM;
141165
finalStringProxyTOresponse =getRequest(urlParams,StringProxyTO.class);
142166
return (BasicUtils.isEmpty(response.getResult()))
143-
?""
144-
:response.getResult();
167+
?Optional.empty()
168+
:Optional.of(response.getResult());
145169
}
146170

147171
@NotNull
148172
@Override
149-
publicStringcode(finalStringaddress)throwsApiException {
173+
publicOptional<String>code(finalStringaddress)throwsApiException {
150174
BasicUtils.validateAddress(address);
151175

152176
finalStringurlParams =ACT_CODE_PARAM +ADDRESS_PARAM +address +TAG_LAST_PARAM;
153177
finalStringProxyTOresponse =getRequest(urlParams,StringProxyTO.class);
154178
return (BasicUtils.isEmpty(response.getResult()))
155-
?""
156-
:response.getResult();
179+
?Optional.empty()
180+
:Optional.of(response.getResult());
157181
}
158182

159183
@NotNull
160184
@Override
161-
publicStringstorageAt(finalStringaddress,finallongposition)throwsApiException {
185+
publicOptional<String>storageAt(finalStringaddress,finallongposition)throwsApiException {
162186
BasicUtils.validateAddress(address);
163187
finallongcompPosition =BasicUtils.compensateMinBlock(position);
164188

165189
finalStringurlParams =ACT_STORAGEAT_PARAM +ADDRESS_PARAM +address +POSITION_PARAM +compPosition +TAG_LAST_PARAM;
166190
finalStringProxyTOresponse =getRequest(urlParams,StringProxyTO.class);
167191
return (BasicUtils.isEmpty(response.getResult()))
168-
?""
169-
:response.getResult();
192+
?Optional.empty()
193+
:Optional.of(response.getResult());
170194
}
171195

172196
@NotNull
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
packageio.api.etherscan.error;
2+
3+
/**
4+
* ! NO DESCRIPTION !
5+
*
6+
* @author GoodforGod
7+
* @since 03.11.2018
8+
*/
9+
publicclassNoResponseExceptionextendsApiException {
10+
11+
publicNoResponseException(Stringmessage) {
12+
super(message);
13+
}
14+
}

‎src/main/java/io/api/etherscan/executor/IHttpExecutor.java‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
packageio.api.etherscan.executor;
22

33
/**
4-
*! NO DESCRIPTION !
4+
*Http Client interface
55
*
66
* @author GoodforGod
77
* @since 31.10.2018
@@ -15,4 +15,13 @@ public interface IHttpExecutor {
1515
* @return result as string
1616
*/
1717
Stringget(Stringurl);
18+
19+
/**
20+
* Performs a Http POST request
21+
*
22+
* @param url as string
23+
* @param data to post
24+
* @return result as string
25+
*/
26+
Stringpost(Stringurl,Stringdata);
1827
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp