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

Commitd8a7865

Browse files
committed
Proxy test implemented
Basic utils fixesProxy provider fixes
1 parent4ee5873 commitd8a7865

File tree

11 files changed

+422
-14
lines changed

11 files changed

+422
-14
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class ProxyApiProvider extends BasicProvider implements IProxyApi {
4545
privatestaticfinalStringACT_ESTIMATEGAS_PARAM =ACT_PREFIX +"eth_estimateGas";
4646

4747
privatestaticfinalStringBOOLEAN_PARAM ="&boolean=true";
48-
privatestaticfinalStringTAG_LAST_PARAM ="&tag=lastest";
48+
privatestaticfinalStringTAG_LAST_PARAM ="&tag=latest";
4949
privatestaticfinalStringPOSITION_PARAM ="&position=";
5050
privatestaticfinalStringADDRESS_PARAM ="&address=";
5151
privatestaticfinalStringTXHASH_PARAM ="&txhash=";
@@ -67,7 +67,7 @@ public long blockNoLast() throws ApiException {
6767
finalStringProxyTOresponse =getRequest(ACT_BLOCKNO_PARAM,StringProxyTO.class);
6868
return (BasicUtils.isEmpty(response.getResult()))
6969
? -1
70-
:BasicUtils.parseHex(response.getResult());
70+
:BasicUtils.parseHex(response.getResult()).longValue();
7171
}
7272

7373
@NotNull
@@ -105,19 +105,19 @@ public Optional<TxProxy> tx(final String txhash) throws ApiException {
105105
@Override
106106
publicOptional<TxProxy>tx(finallongblockNo,finallongindex)throwsApiException {
107107
finallongcompBlockNo =BasicUtils.compensateMinBlock(blockNo);
108-
finallongcompIndex =BasicUtils.compensateMinBlock(index);
108+
finallongcompIndex = (index <1) ?1 :index;
109109

110-
finalStringurlParams =ACT_TX_BY_BLOCKNOINDEX_PARAM +TAG_PARAM +compBlockNo +INDEX_PARAM +compIndex;
110+
finalStringurlParams =ACT_TX_BY_BLOCKNOINDEX_PARAM +TAG_PARAM +compBlockNo +INDEX_PARAM +"0x" +Long.toHexString(compIndex);
111111
finalTxProxyTOresponse =getRequest(urlParams,TxProxyTO.class);
112112
returnOptional.ofNullable(response.getResult());
113113
}
114114

115115
@Override
116116
publicinttxCount(finallongblockNo)throwsApiException {
117117
finallongcompensatedBlockNo =BasicUtils.compensateMinBlock(blockNo);
118-
finalStringurlParams =ACT_BLOCKTX_COUNT_PARAM +TAG_PARAM +compensatedBlockNo;
118+
finalStringurlParams =ACT_BLOCKTX_COUNT_PARAM +TAG_PARAM +"0x" +Long.toHexString(compensatedBlockNo);
119119
finalStringProxyTOresponse =getRequest(urlParams,StringProxyTO.class);
120-
returnInteger.valueOf(response.getResult());
120+
returnBasicUtils.parseHex(response.getResult()).intValue();
121121
}
122122

123123
@Override
@@ -126,7 +126,7 @@ public int txSendCount(final String address) throws ApiException {
126126

127127
finalStringurlParams =ACT_TX_COUNT_PARAM +ADDRESS_PARAM +address +TAG_LAST_PARAM;
128128
finalStringProxyTOresponse =getRequest(urlParams,StringProxyTO.class);
129-
return(int)BasicUtils.parseHex(response.getResult());
129+
returnBasicUtils.parseHex(response.getResult()).intValue();
130130
}
131131

132132
@Override
@@ -160,6 +160,8 @@ public Optional<TxInfoProxy> txReceipt(final String txhash) throws ApiException
160160
@Override
161161
publicOptional<String>call(finalStringaddress,finalStringdata)throwsApiException {
162162
BasicUtils.validateAddress(address);
163+
if(BasicUtils.isNotHex(data))
164+
thrownewInvalidDataHexException("Data is not hex encoded.");
163165

164166
finalStringurlParams =ACT_CALL_PARAM +TO_PARAM +address +DATA_PARAM +data +TAG_LAST_PARAM;
165167
finalStringProxyTOresponse =getRequest(urlParams,StringProxyTO.class);
@@ -199,7 +201,7 @@ public BigInteger gasPrice() throws ApiException {
199201
finalStringProxyTOresponse =getRequest(ACT_GASPRICE_PARAM,StringProxyTO.class);
200202
return (BasicUtils.isEmpty(response.getResult()))
201203
?BigInteger.valueOf(-1)
202-
:BigInteger.valueOf(BasicUtils.parseHex(response.getResult()));
204+
:BasicUtils.parseHex(response.getResult());
203205
}
204206

205207
@NotNull
@@ -211,13 +213,13 @@ public BigInteger gasEstimated() throws ApiException {
211213
@NotNull
212214
@Override
213215
publicBigIntegergasEstimated(finalStringhexData)throwsApiException {
214-
if(BasicUtils.isNotHex(hexData))
216+
if(!BasicUtils.isEmpty(hexData) &&BasicUtils.isNotHex(hexData))
215217
thrownewInvalidDataHexException("Data is not in hex format.");
216218

217219
finalStringurlParams =ACT_ESTIMATEGAS_PARAM +DATA_PARAM +hexData +GAS_PARAM +"2000000000000000";
218220
finalStringProxyTOresponse =getRequest(urlParams,StringProxyTO.class);
219221
return (BasicUtils.isEmpty(response.getResult()))
220222
?BigInteger.valueOf(-1)
221-
:BigInteger.valueOf(BasicUtils.parseHex(response.getResult()));
223+
:BasicUtils.parseHex(response.getResult());
222224
}
223225
}

‎src/main/java/io/api/etherscan/util/BasicUtils.java‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
importio.api.etherscan.model.utility.BlockParam;
88
importorg.jetbrains.annotations.NotNull;
99

10+
importjava.math.BigInteger;
1011
importjava.util.*;
1112
importjava.util.regex.Pattern;
1213

@@ -69,18 +70,25 @@ public static boolean isNotAddress(String value) {
6970
}
7071

7172
publicstaticbooleanisNotTxHash(Stringvalue) {
72-
returnisEmpty(value) || !TXHASH_PATTERN.matcher(value).matches();
73+
returnisEmpty(value) || !TXHASH_PATTERN.matcher(value).find();
7374
}
7475

7576
publicstaticbooleanisNotHex(Stringvalue) {
7677
returnisEmpty(value) || !HEX_PATTERN.matcher(value).matches();
7778
}
7879

79-
publicstaticlongparseHex(Stringhex) {
80+
publicstaticBigIntegerparseHex(Stringhex) {
8081
try {
81-
returnLong.valueOf(hex,16);
82+
if(BasicUtils.isEmpty(hex))
83+
returnBigInteger.valueOf(0);
84+
85+
finalStringformatted = (hex.length() >2 &&hex.charAt(0) =='0' &&hex.charAt(1) =='x')
86+
?hex.substring(2,hex.length())
87+
:hex;
88+
89+
returnnewBigInteger(formatted,16);
8290
}catch (NumberFormatExceptione) {
83-
return-1;
91+
returnBigInteger.valueOf(-1L);
8492
}
8593
}
8694

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
packageio.api.etherscan.proxy;
2+
3+
importio.api.etherscan.core.impl.EtherScanApi;
4+
importio.api.etherscan.model.proxy.BlockProxy;
5+
importorg.junit.Assert;
6+
importorg.junit.Test;
7+
8+
importjava.util.Optional;
9+
10+
/**
11+
* ! NO DESCRIPTION !
12+
*
13+
* @author GoodforGod
14+
* @since 03.11.2018
15+
*/
16+
publicclassProxyBlockApiTestextendsAssert {
17+
18+
privatefinalEtherScanApiapi =newEtherScanApi();
19+
20+
@Test
21+
publicvoidcorrect() {
22+
Optional<BlockProxy>block =api.proxy().block(5120);
23+
assertTrue(block.isPresent());
24+
assertNotNull(block.get().getHash());
25+
}
26+
27+
@Test
28+
publicvoidcorrectParamWithEmptyExpectedResult() {
29+
Optional<BlockProxy>block =api.proxy().block(99999999999L);
30+
assertFalse(block.isPresent());
31+
}
32+
33+
@Test
34+
publicvoidcorrectParamNegativeNo() {
35+
Optional<BlockProxy>block =api.proxy().block(-1);
36+
assertTrue(block.isPresent());
37+
assertNotNull(block.get().getHash());
38+
}
39+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
packageio.api.etherscan.proxy;
2+
3+
importio.api.etherscan.core.impl.EtherScanApi;
4+
importio.api.etherscan.error.InvalidAddressException;
5+
importio.api.etherscan.error.InvalidDataHexException;
6+
importio.api.etherscan.util.BasicUtils;
7+
importorg.junit.Assert;
8+
importorg.junit.Test;
9+
10+
importjava.util.Optional;
11+
12+
/**
13+
* ! NO DESCRIPTION !
14+
*
15+
* @author GoodforGod
16+
* @since 03.11.2018
17+
*/
18+
publicclassProxyCallApiTestextendsAssert {
19+
20+
privatefinalEtherScanApiapi =newEtherScanApi();
21+
22+
@Test
23+
publicvoidcorrect() {
24+
Optional<String>call =api.proxy().call("0xAEEF46DB4855E25702F8237E8f403FddcaF931C0",
25+
"0x70a08231000000000000000000000000e16359506c028e51f16be38986ec5746251e9724");
26+
assertTrue(call.isPresent());
27+
assertFalse(BasicUtils.isNotHex(call.get()));
28+
}
29+
30+
@Test(expected =InvalidAddressException.class)
31+
publicvoidinvalidParamWithError() {
32+
Optional<String>call =api.proxy().call("0xEEF46DB4855E25702F8237E8f403FddcaF931C0",
33+
"0x70a08231000000000000000000000000e16359506c028e51f16be38986ec5746251e9724");
34+
}
35+
36+
@Test(expected =InvalidDataHexException.class)
37+
publicvoidinvalidParamNotHex() {
38+
Optional<String>call =api.proxy().call("0xAEEF46DB4855E25702F8237E8f403FddcaF931C0",
39+
"7-0a08231000000000000000000000000e16359506c028e51f16be38986ec5746251e9724");
40+
}
41+
42+
@Test
43+
publicvoidcorrectParamWithEmptyExpectedResult() {
44+
Optional<String>call =api.proxy().call("0xAEEF16DB4855E25702F8237E8f403FddcaF931C0",
45+
"0x70a08231000000000000000000000000e16359506c028e51f16be38986ec5746251e9724");
46+
assertTrue(call.isPresent());
47+
assertFalse(BasicUtils.isNotHex(call.get()));
48+
}
49+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
packageio.api.etherscan.proxy;
2+
3+
importio.api.etherscan.core.impl.EtherScanApi;
4+
importio.api.etherscan.error.InvalidAddressException;
5+
importio.api.etherscan.util.BasicUtils;
6+
importorg.junit.Assert;
7+
importorg.junit.Test;
8+
9+
importjava.util.Optional;
10+
11+
/**
12+
* ! NO DESCRIPTION !
13+
*
14+
* @author GoodforGod
15+
* @since 03.11.2018
16+
*/
17+
publicclassProxyCodeApiTestextendsAssert{
18+
19+
privatefinalEtherScanApiapi =newEtherScanApi();
20+
21+
@Test
22+
publicvoidcorrect() {
23+
Optional<String>call =api.proxy().code("0xf75e354c5edc8efed9b59ee9f67a80845ade7d0c");
24+
assertTrue(call.isPresent());
25+
assertFalse(BasicUtils.isNotHex(call.get()));
26+
}
27+
28+
@Test(expected =InvalidAddressException.class)
29+
publicvoidinvalidParamWithError() {
30+
Optional<String>call =api.proxy().code("0f75e354c5edc8efed9b59ee9f67a80845ade7d0c");
31+
}
32+
33+
@Test
34+
publicvoidcorrectParamWithEmptyExpectedResult() {
35+
Optional<String>call =api.proxy().code("0xf15e354c5edc8efed9b59ee9f67a80845ade7d0c");
36+
assertTrue(call.isPresent());
37+
assertFalse(BasicUtils.isNotHex(call.get()));
38+
}
39+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
packageio.api.etherscan.proxy;
2+
3+
importio.api.etherscan.core.impl.EtherScanApi;
4+
importio.api.etherscan.error.InvalidDataHexException;
5+
importorg.junit.Assert;
6+
importorg.junit.Test;
7+
8+
importjava.math.BigInteger;
9+
10+
/**
11+
* ! NO DESCRIPTION !
12+
*
13+
* @author GoodforGod
14+
* @since 03.11.2018
15+
*/
16+
publicclassProxyGasApiTestextendsAssert{
17+
18+
privatefinalEtherScanApiapi =newEtherScanApi();
19+
20+
@Test
21+
publicvoidcorrectPrice() {
22+
BigIntegerprice =api.proxy().gasPrice();
23+
assertNotNull(price);
24+
assertNotEquals(0,price.intValue());
25+
}
26+
27+
@Test
28+
publicvoidcorrectEstimated() {
29+
BigIntegerprice =api.proxy().gasEstimated();
30+
assertNotNull(price);
31+
assertNotEquals(0,price.intValue());
32+
}
33+
34+
@Test
35+
publicvoidcorrectEstimatedWithData() {
36+
StringdataCustom ="606060405260728060106000396000f360606040526000606060405260728060106000396000f360606040526000";
37+
BigIntegerprice =api.proxy().gasEstimated();
38+
BigIntegerpriceCustom =api.proxy().gasEstimated(dataCustom);
39+
assertNotNull(price);
40+
assertNotNull(priceCustom);
41+
assertNotEquals(price,priceCustom);
42+
}
43+
44+
@Test(expected =InvalidDataHexException.class)
45+
publicvoidinvalidParamWithError() {
46+
StringdataCustom ="280&60106000396000f360606040526000";
47+
BigIntegerpriceCustom =api.proxy().gasEstimated(dataCustom);
48+
}
49+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
packageio.api.etherscan.proxy;
2+
3+
importio.api.etherscan.core.impl.EtherScanApi;
4+
importio.api.etherscan.error.InvalidAddressException;
5+
importio.api.etherscan.util.BasicUtils;
6+
importorg.junit.Assert;
7+
importorg.junit.Test;
8+
9+
importjava.util.Optional;
10+
11+
/**
12+
* ! NO DESCRIPTION !
13+
*
14+
* @author GoodforGod
15+
* @since 03.11.2018
16+
*/
17+
publicclassProxyStorageApiTestextendsAssert{
18+
19+
privatefinalEtherScanApiapi =newEtherScanApi();
20+
21+
@Test
22+
publicvoidcorrect() {
23+
Optional<String>call =api.proxy().storageAt("0x6e03d9cce9d60f3e9f2597e13cd4c54c55330cfd",0);
24+
assertTrue(call.isPresent());
25+
assertFalse(BasicUtils.isNotHex(call.get()));
26+
}
27+
28+
@Test(expected =InvalidAddressException.class)
29+
publicvoidinvalidParamWithError() {
30+
Optional<String>call =api.proxy().storageAt("0xe03d9cce9d60f3e9f2597e13cd4c54c55330cfd",0);
31+
}
32+
33+
@Test
34+
publicvoidcorrectParamWithEmptyExpectedResult() {
35+
Optional<String>call =api.proxy().storageAt("0x6e03d9cce9d60f3e9f2597e13cd4c54c55330cfd",100);
36+
assertTrue(call.isPresent());
37+
assertFalse(BasicUtils.isNotHex(call.get()));
38+
}
39+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp