Skip to content

Commit d8a7865

Browse files
committed
Proxy test implemented
Basic utils fixes Proxy provider fixes
1 parent 4ee5873 commit d8a7865

11 files changed

+422
-14
lines changed

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

+12-10
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class ProxyApiProvider extends BasicProvider implements IProxyApi {
4545
private static final String ACT_ESTIMATEGAS_PARAM = ACT_PREFIX + "eth_estimateGas";
4646

4747
private static final String BOOLEAN_PARAM = "&boolean=true";
48-
private static final String TAG_LAST_PARAM = "&tag=lastest";
48+
private static final String TAG_LAST_PARAM = "&tag=latest";
4949
private static final String POSITION_PARAM = "&position=";
5050
private static final String ADDRESS_PARAM = "&address=";
5151
private static final String TXHASH_PARAM = "&txhash=";
@@ -67,7 +67,7 @@ public long blockNoLast() throws ApiException {
6767
final StringProxyTO response = 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
public Optional<TxProxy> tx(final long blockNo, final long index) throws ApiException {
107107
final long compBlockNo = BasicUtils.compensateMinBlock(blockNo);
108-
final long compIndex = BasicUtils.compensateMinBlock(index);
108+
final long compIndex = (index < 1) ? 1 : index;
109109

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

115115
@Override
116116
public int txCount(final long blockNo) throws ApiException {
117117
final long compensatedBlockNo = BasicUtils.compensateMinBlock(blockNo);
118-
final String urlParams = ACT_BLOCKTX_COUNT_PARAM + TAG_PARAM + compensatedBlockNo;
118+
final String urlParams = ACT_BLOCKTX_COUNT_PARAM + TAG_PARAM + "0x" + Long.toHexString(compensatedBlockNo);
119119
final StringProxyTO response = getRequest(urlParams, StringProxyTO.class);
120-
return Integer.valueOf(response.getResult());
120+
return BasicUtils.parseHex(response.getResult()).intValue();
121121
}
122122

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

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

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

164166
final String urlParams = ACT_CALL_PARAM + TO_PARAM + address + DATA_PARAM + data + TAG_LAST_PARAM;
165167
final StringProxyTO response = getRequest(urlParams, StringProxyTO.class);
@@ -199,7 +201,7 @@ public BigInteger gasPrice() throws ApiException {
199201
final StringProxyTO response = 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
public BigInteger gasEstimated(final String hexData) throws ApiException {
214-
if(BasicUtils.isNotHex(hexData))
216+
if(!BasicUtils.isEmpty(hexData) && BasicUtils.isNotHex(hexData))
215217
throw new InvalidDataHexException("Data is not in hex format.");
216218

217219
final String urlParams = ACT_ESTIMATEGAS_PARAM + DATA_PARAM + hexData + GAS_PARAM + "2000000000000000";
218220
final StringProxyTO response = 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

+12-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.api.etherscan.model.utility.BlockParam;
88
import org.jetbrains.annotations.NotNull;
99

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

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

7172
public static boolean isNotTxHash(String value) {
72-
return isEmpty(value) || !TXHASH_PATTERN.matcher(value).matches();
73+
return isEmpty(value) || !TXHASH_PATTERN.matcher(value).find();
7374
}
7475

7576
public static boolean isNotHex(String value) {
7677
return isEmpty(value) || !HEX_PATTERN.matcher(value).matches();
7778
}
7879

79-
public static long parseHex(String hex) {
80+
public static BigInteger parseHex(String hex) {
8081
try {
81-
return Long.valueOf(hex, 16);
82+
if(BasicUtils.isEmpty(hex))
83+
return BigInteger.valueOf(0);
84+
85+
final String formatted = (hex.length() > 2 && hex.charAt(0) == '0' && hex.charAt(1) == 'x')
86+
? hex.substring(2, hex.length())
87+
: hex;
88+
89+
return new BigInteger(formatted, 16);
8290
} catch (NumberFormatException e) {
83-
return -1;
91+
return BigInteger.valueOf(-1L);
8492
}
8593
}
8694

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.api.etherscan.proxy;
2+
3+
import io.api.etherscan.core.impl.EtherScanApi;
4+
import io.api.etherscan.model.proxy.BlockProxy;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
import java.util.Optional;
9+
10+
/**
11+
* ! NO DESCRIPTION !
12+
*
13+
* @author GoodforGod
14+
* @since 03.11.2018
15+
*/
16+
public class ProxyBlockApiTest extends Assert {
17+
18+
private final EtherScanApi api = new EtherScanApi();
19+
20+
@Test
21+
public void correct() {
22+
Optional<BlockProxy> block = api.proxy().block(5120);
23+
assertTrue(block.isPresent());
24+
assertNotNull(block.get().getHash());
25+
}
26+
27+
@Test
28+
public void correctParamWithEmptyExpectedResult() {
29+
Optional<BlockProxy> block = api.proxy().block(99999999999L);
30+
assertFalse(block.isPresent());
31+
}
32+
33+
@Test
34+
public void correctParamNegativeNo() {
35+
Optional<BlockProxy> block = api.proxy().block(-1);
36+
assertTrue(block.isPresent());
37+
assertNotNull(block.get().getHash());
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.api.etherscan.proxy;
2+
3+
import io.api.etherscan.core.impl.EtherScanApi;
4+
import io.api.etherscan.error.InvalidAddressException;
5+
import io.api.etherscan.error.InvalidDataHexException;
6+
import io.api.etherscan.util.BasicUtils;
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
import java.util.Optional;
11+
12+
/**
13+
* ! NO DESCRIPTION !
14+
*
15+
* @author GoodforGod
16+
* @since 03.11.2018
17+
*/
18+
public class ProxyCallApiTest extends Assert {
19+
20+
private final EtherScanApi api = new EtherScanApi();
21+
22+
@Test
23+
public void correct() {
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+
public void invalidParamWithError() {
32+
Optional<String> call = api.proxy().call("0xEEF46DB4855E25702F8237E8f403FddcaF931C0",
33+
"0x70a08231000000000000000000000000e16359506c028e51f16be38986ec5746251e9724");
34+
}
35+
36+
@Test(expected = InvalidDataHexException.class)
37+
public void invalidParamNotHex() {
38+
Optional<String> call = api.proxy().call("0xAEEF46DB4855E25702F8237E8f403FddcaF931C0",
39+
"7-0a08231000000000000000000000000e16359506c028e51f16be38986ec5746251e9724");
40+
}
41+
42+
@Test
43+
public void correctParamWithEmptyExpectedResult() {
44+
Optional<String> call = api.proxy().call("0xAEEF16DB4855E25702F8237E8f403FddcaF931C0",
45+
"0x70a08231000000000000000000000000e16359506c028e51f16be38986ec5746251e9724");
46+
assertTrue(call.isPresent());
47+
assertFalse(BasicUtils.isNotHex(call.get()));
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.api.etherscan.proxy;
2+
3+
import io.api.etherscan.core.impl.EtherScanApi;
4+
import io.api.etherscan.error.InvalidAddressException;
5+
import io.api.etherscan.util.BasicUtils;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
import java.util.Optional;
10+
11+
/**
12+
* ! NO DESCRIPTION !
13+
*
14+
* @author GoodforGod
15+
* @since 03.11.2018
16+
*/
17+
public class ProxyCodeApiTest extends Assert{
18+
19+
private final EtherScanApi api = new EtherScanApi();
20+
21+
@Test
22+
public void correct() {
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+
public void invalidParamWithError() {
30+
Optional<String> call = api.proxy().code("0f75e354c5edc8efed9b59ee9f67a80845ade7d0c");
31+
}
32+
33+
@Test
34+
public void correctParamWithEmptyExpectedResult() {
35+
Optional<String> call = api.proxy().code("0xf15e354c5edc8efed9b59ee9f67a80845ade7d0c");
36+
assertTrue(call.isPresent());
37+
assertFalse(BasicUtils.isNotHex(call.get()));
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.api.etherscan.proxy;
2+
3+
import io.api.etherscan.core.impl.EtherScanApi;
4+
import io.api.etherscan.error.InvalidDataHexException;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
import java.math.BigInteger;
9+
10+
/**
11+
* ! NO DESCRIPTION !
12+
*
13+
* @author GoodforGod
14+
* @since 03.11.2018
15+
*/
16+
public class ProxyGasApiTest extends Assert{
17+
18+
private final EtherScanApi api = new EtherScanApi();
19+
20+
@Test
21+
public void correctPrice() {
22+
BigInteger price = api.proxy().gasPrice();
23+
assertNotNull(price);
24+
assertNotEquals(0, price.intValue());
25+
}
26+
27+
@Test
28+
public void correctEstimated() {
29+
BigInteger price = api.proxy().gasEstimated();
30+
assertNotNull(price);
31+
assertNotEquals(0, price.intValue());
32+
}
33+
34+
@Test
35+
public void correctEstimatedWithData() {
36+
String dataCustom = "606060405260728060106000396000f360606040526000606060405260728060106000396000f360606040526000";
37+
BigInteger price = api.proxy().gasEstimated();
38+
BigInteger priceCustom = api.proxy().gasEstimated(dataCustom);
39+
assertNotNull(price);
40+
assertNotNull(priceCustom);
41+
assertNotEquals(price, priceCustom);
42+
}
43+
44+
@Test(expected = InvalidDataHexException.class)
45+
public void invalidParamWithError() {
46+
String dataCustom = "280&60106000396000f360606040526000";
47+
BigInteger priceCustom = api.proxy().gasEstimated(dataCustom);
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.api.etherscan.proxy;
2+
3+
import io.api.etherscan.core.impl.EtherScanApi;
4+
import io.api.etherscan.error.InvalidAddressException;
5+
import io.api.etherscan.util.BasicUtils;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
import java.util.Optional;
10+
11+
/**
12+
* ! NO DESCRIPTION !
13+
*
14+
* @author GoodforGod
15+
* @since 03.11.2018
16+
*/
17+
public class ProxyStorageApiTest extends Assert{
18+
19+
private final EtherScanApi api = new EtherScanApi();
20+
21+
@Test
22+
public void correct() {
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+
public void invalidParamWithError() {
30+
Optional<String> call = api.proxy().storageAt("0xe03d9cce9d60f3e9f2597e13cd4c54c55330cfd", 0);
31+
}
32+
33+
@Test
34+
public void correctParamWithEmptyExpectedResult() {
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)