diff --git a/README.md b/README.md index 733a2ed..545c0e7 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ # java-etherscan-api +[![GitHub Action](https://github.com/goodforgod/java-etherscan-api/workflows/Java%20CI/badge.svg)](https://github.com/GoodforGod/dummymaker/actions?query=workflow%3A%22Java+CI%22) +[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=GoodforGod_java-etherscan-api&metric=coverage)](https://sonarcloud.io/dashboard?id=GoodforGod_dummymaker) +[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=GoodforGod_java-etherscan-api&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=GoodforGod_dummymaker) +[![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=GoodforGod_java-etherscan-api&metric=ncloc)](https://sonarcloud.io/dashboard?id=GoodforGod_dummymaker) [![Jitpack](https://jitpack.io/v/iSnow/java-etherscan-api.svg)](https://jitpack.io/#GoodforGod/java-etherscan-api) [Etherscan](https://etherscan.io/apis) Java API implementation. @@ -163,6 +167,8 @@ Token API methods migrated to [Account](#account-api) & [Stats](#stats-api) resp ## Version History +**1.1.0** - Improved error handling, QueueManager improved, Gradle 6.7 instead of Maven, GitHub CI, Sonarcloud analyzer, dependencies updated. + **1.0.2** - Minor http client improvements. **1.0.1** - Gorli & TOBALABA networks support. @@ -171,4 +177,4 @@ Token API methods migrated to [Account](#account-api) & [Stats](#stats-api) resp ## License -This project is licensed under the MIT - see the [LICENSE](LICENSE) file for details. +This project licensed under the MIT - see the [LICENSE](LICENSE) file for details. diff --git a/gradle.properties b/gradle.properties index 0c89028..1fa1ad4 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ groupId=com.github.goodforgod artifactId=java-etherscan-api -artifactVersion=1.1.0-SNAPSHOT +artifactVersion=1.1.0 buildNumber=1 diff --git a/src/main/java/io/api/etherscan/core/impl/BasicProvider.java b/src/main/java/io/api/etherscan/core/impl/BasicProvider.java index cf16337..b89447a 100644 --- a/src/main/java/io/api/etherscan/core/impl/BasicProvider.java +++ b/src/main/java/io/api/etherscan/core/impl/BasicProvider.java @@ -46,10 +46,8 @@ abstract class BasicProvider { T convert(final String json, final Class tClass) { try { final T t = gson.fromJson(json, tClass); - if (t instanceof StringResponseTO) { - if (((StringResponseTO) t).getResult().startsWith("Max rate limit reached")) { - throw new RateLimitException(((StringResponseTO) t).getResult()); - } + if (t instanceof StringResponseTO && ((StringResponseTO) t).getResult().startsWith("Max rate limit reached")) { + throw new RateLimitException(((StringResponseTO) t).getResult()); } return t; diff --git a/src/main/java/io/api/etherscan/manager/impl/FakeQueueManager.java b/src/main/java/io/api/etherscan/manager/impl/FakeQueueManager.java index d8bc048..e797f8b 100644 --- a/src/main/java/io/api/etherscan/manager/impl/FakeQueueManager.java +++ b/src/main/java/io/api/etherscan/manager/impl/FakeQueueManager.java @@ -11,5 +11,8 @@ public class FakeQueueManager implements IQueueManager { @Override - public void takeTurn() {} + public void takeTurn() { + // no limit or await provided for fake impl so rate limit exception will be + // thrown if too many calls + } } diff --git a/src/main/java/io/api/etherscan/model/Balance.java b/src/main/java/io/api/etherscan/model/Balance.java index 5529a90..cbd8502 100644 --- a/src/main/java/io/api/etherscan/model/Balance.java +++ b/src/main/java/io/api/etherscan/model/Balance.java @@ -3,6 +3,7 @@ import io.api.etherscan.model.utility.BalanceTO; import java.math.BigInteger; +import java.util.Objects; /** * ! NO DESCRIPTION ! @@ -63,7 +64,7 @@ public boolean equals(Object o) { if (!balance.equals(balance1.balance)) return false; - return address != null ? address.equals(balance1.address) : balance1.address == null; + return Objects.equals(address, balance1.address); } @Override diff --git a/src/main/java/io/api/etherscan/model/BaseTx.java b/src/main/java/io/api/etherscan/model/BaseTx.java index 5aea827..a219e57 100644 --- a/src/main/java/io/api/etherscan/model/BaseTx.java +++ b/src/main/java/io/api/etherscan/model/BaseTx.java @@ -5,6 +5,7 @@ import java.math.BigInteger; import java.time.LocalDateTime; import java.time.ZoneOffset; +import java.util.Objects; /** * ! NO DESCRIPTION ! @@ -33,7 +34,7 @@ public long getBlockNumber() { public LocalDateTime getTimeStamp() { if (_timeStamp == null && !BasicUtils.isEmpty(timeStamp)) - _timeStamp = LocalDateTime.ofEpochSecond(Long.valueOf(timeStamp), 0, ZoneOffset.UTC); + _timeStamp = LocalDateTime.ofEpochSecond(Long.parseLong(timeStamp), 0, ZoneOffset.UTC); return _timeStamp; } @@ -81,15 +82,15 @@ public boolean equals(Object o) { if (blockNumber != baseTx.blockNumber) return false; - if (timeStamp != null ? !timeStamp.equals(baseTx.timeStamp) : baseTx.timeStamp != null) + if (!Objects.equals(timeStamp, baseTx.timeStamp)) return false; - if (hash != null ? !hash.equals(baseTx.hash) : baseTx.hash != null) + if (!Objects.equals(hash, baseTx.hash)) return false; - if (from != null ? !from.equals(baseTx.from) : baseTx.from != null) + if (!Objects.equals(from, baseTx.from)) return false; - if (to != null ? !to.equals(baseTx.to) : baseTx.to != null) + if (!Objects.equals(to, baseTx.to)) return false; - return value != null ? value.equals(baseTx.value) : baseTx.value == null; + return Objects.equals(value, baseTx.value); } @Override diff --git a/src/main/java/io/api/etherscan/model/Block.java b/src/main/java/io/api/etherscan/model/Block.java index 2e9b96b..d328841 100644 --- a/src/main/java/io/api/etherscan/model/Block.java +++ b/src/main/java/io/api/etherscan/model/Block.java @@ -26,7 +26,7 @@ public long getBlockNumber() { public LocalDateTime getTimeStamp() { if (_timeStamp == null && !BasicUtils.isEmpty(timeStamp)) - _timeStamp = LocalDateTime.ofEpochSecond(Long.valueOf(timeStamp), 0, ZoneOffset.UTC); + _timeStamp = LocalDateTime.ofEpochSecond(Long.parseLong(timeStamp), 0, ZoneOffset.UTC); return _timeStamp; } diff --git a/src/main/java/io/api/etherscan/model/Log.java b/src/main/java/io/api/etherscan/model/Log.java index 2a7cc09..4649d6b 100644 --- a/src/main/java/io/api/etherscan/model/Log.java +++ b/src/main/java/io/api/etherscan/model/Log.java @@ -6,6 +6,7 @@ import java.time.LocalDateTime; import java.time.ZoneOffset; import java.util.List; +import java.util.Objects; /** * ! NO DESCRIPTION ! @@ -124,15 +125,15 @@ public boolean equals(Object o) { Log log = (Log) o; - if (blockNumber != null ? !blockNumber.equals(log.blockNumber) : log.blockNumber != null) + if (!Objects.equals(blockNumber, log.blockNumber)) return false; - if (address != null ? !address.equals(log.address) : log.address != null) + if (!Objects.equals(address, log.address)) return false; - if (transactionHash != null ? !transactionHash.equals(log.transactionHash) : log.transactionHash != null) + if (!Objects.equals(transactionHash, log.transactionHash)) return false; - if (timeStamp != null ? !timeStamp.equals(log.timeStamp) : log.timeStamp != null) + if (!Objects.equals(timeStamp, log.timeStamp)) return false; - return logIndex != null ? logIndex.equals(log.logIndex) : log.logIndex == null; + return Objects.equals(logIndex, log.logIndex); } @Override diff --git a/src/main/java/io/api/etherscan/model/Price.java b/src/main/java/io/api/etherscan/model/Price.java index 9a37592..d2c6d1c 100644 --- a/src/main/java/io/api/etherscan/model/Price.java +++ b/src/main/java/io/api/etherscan/model/Price.java @@ -28,13 +28,13 @@ public double inBtc() { public LocalDateTime usdTimestamp() { if (_ethusd_timestamp == null) - _ethusd_timestamp = LocalDateTime.ofEpochSecond(Long.valueOf(ethusd_timestamp), 0, ZoneOffset.UTC); + _ethusd_timestamp = LocalDateTime.ofEpochSecond(Long.parseLong(ethusd_timestamp), 0, ZoneOffset.UTC); return _ethusd_timestamp; } public LocalDateTime btcTimestamp() { if (_ethbtc_timestamp == null) - _ethbtc_timestamp = LocalDateTime.ofEpochSecond(Long.valueOf(ethbtc_timestamp), 0, ZoneOffset.UTC); + _ethbtc_timestamp = LocalDateTime.ofEpochSecond(Long.parseLong(ethbtc_timestamp), 0, ZoneOffset.UTC); return _ethbtc_timestamp; } diff --git a/src/main/java/io/api/etherscan/model/Status.java b/src/main/java/io/api/etherscan/model/Status.java index 4a1fe18..9683bde 100644 --- a/src/main/java/io/api/etherscan/model/Status.java +++ b/src/main/java/io/api/etherscan/model/Status.java @@ -1,5 +1,7 @@ package io.api.etherscan.model; +import java.util.Objects; + /** * Contract Execution Status * @@ -33,7 +35,7 @@ public boolean equals(Object o) { if (isError != status.isError) return false; - return errDescription != null ? errDescription.equals(status.errDescription) : status.errDescription == null; + return Objects.equals(errDescription, status.errDescription); } @Override diff --git a/src/main/java/io/api/etherscan/model/TokenBalance.java b/src/main/java/io/api/etherscan/model/TokenBalance.java index e198079..d057992 100644 --- a/src/main/java/io/api/etherscan/model/TokenBalance.java +++ b/src/main/java/io/api/etherscan/model/TokenBalance.java @@ -1,6 +1,7 @@ package io.api.etherscan.model; import java.math.BigInteger; +import java.util.Objects; /** * ! NO DESCRIPTION ! @@ -31,8 +32,7 @@ public boolean equals(Object o) { return false; TokenBalance that = (TokenBalance) o; - - return tokenContract != null ? tokenContract.equals(that.tokenContract) : that.tokenContract == null; + return Objects.equals(tokenContract, that.tokenContract); } @Override diff --git a/src/main/java/io/api/etherscan/model/Tx.java b/src/main/java/io/api/etherscan/model/Tx.java index 6fce75b..4136d23 100644 --- a/src/main/java/io/api/etherscan/model/Tx.java +++ b/src/main/java/io/api/etherscan/model/Tx.java @@ -3,6 +3,7 @@ import io.api.etherscan.util.BasicUtils; import java.math.BigInteger; +import java.util.Objects; /** * ! NO DESCRIPTION ! @@ -70,9 +71,9 @@ public boolean equals(Object o) { return false; if (transactionIndex != tx.transactionIndex) return false; - if (blockHash != null ? !blockHash.equals(tx.blockHash) : tx.blockHash != null) + if (!Objects.equals(blockHash, tx.blockHash)) return false; - return isError != null ? isError.equals(tx.isError) : tx.isError == null; + return Objects.equals(isError, tx.isError); } @Override diff --git a/src/main/java/io/api/etherscan/model/TxInternal.java b/src/main/java/io/api/etherscan/model/TxInternal.java index e7b75d9..22c5104 100644 --- a/src/main/java/io/api/etherscan/model/TxInternal.java +++ b/src/main/java/io/api/etherscan/model/TxInternal.java @@ -1,5 +1,7 @@ package io.api.etherscan.model; +import java.util.Objects; + /** * ! NO DESCRIPTION ! * @@ -44,7 +46,7 @@ public boolean equals(Object o) { if (traceId != that.traceId) return false; - return errCode != null ? errCode.equals(that.errCode) : that.errCode == null; + return Objects.equals(errCode, that.errCode); } @Override diff --git a/src/main/java/io/api/etherscan/model/Wei.java b/src/main/java/io/api/etherscan/model/Wei.java index 35f90d0..eddf8d2 100644 --- a/src/main/java/io/api/etherscan/model/Wei.java +++ b/src/main/java/io/api/etherscan/model/Wei.java @@ -1,6 +1,7 @@ package io.api.etherscan.model; import java.math.BigInteger; +import java.util.Objects; /** * ! NO DESCRIPTION ! @@ -46,8 +47,7 @@ public boolean equals(Object o) { return false; Wei wei = (Wei) o; - - return result != null ? result.equals(wei.result) : wei.result == null; + return Objects.equals(result, wei.result); } @Override diff --git a/src/main/java/io/api/etherscan/model/utility/BaseResponseTO.java b/src/main/java/io/api/etherscan/model/utility/BaseResponseTO.java index 144deef..d3653e2 100644 --- a/src/main/java/io/api/etherscan/model/utility/BaseResponseTO.java +++ b/src/main/java/io/api/etherscan/model/utility/BaseResponseTO.java @@ -14,7 +14,7 @@ public abstract class BaseResponseTO { private String message; public int getStatus() { - return (BasicUtils.isEmpty(status)) ? -1 : Integer.valueOf(status); + return BasicUtils.isEmpty(status) ? -1 : Integer.parseInt(status); } public String getMessage() { diff --git a/src/main/java/io/api/etherscan/model/utility/BlockParam.java b/src/main/java/io/api/etherscan/model/utility/BlockParam.java index cbc5a3e..0f027ec 100644 --- a/src/main/java/io/api/etherscan/model/utility/BlockParam.java +++ b/src/main/java/io/api/etherscan/model/utility/BlockParam.java @@ -8,8 +8,8 @@ */ public class BlockParam { - private long startBlock; - private long endBlock; + private final long startBlock; + private final long endBlock; public BlockParam(long startBlock, long endBlock) { this.startBlock = startBlock; diff --git a/src/main/java/io/api/etherscan/util/BasicUtils.java b/src/main/java/io/api/etherscan/util/BasicUtils.java index 0d2a654..96b855d 100644 --- a/src/main/java/io/api/etherscan/util/BasicUtils.java +++ b/src/main/java/io/api/etherscan/util/BasicUtils.java @@ -26,6 +26,8 @@ public class BasicUtils { private static final Pattern TXHASH_PATTERN = Pattern.compile("0x[a-zA-Z0-9]{64}"); private static final Pattern HEX_PATTERN = Pattern.compile("[a-zA-Z0-9]+"); + private BasicUtils() {} + public static boolean isEmpty(String value) { return value == null || value.isEmpty(); } @@ -42,14 +44,8 @@ public static BlockParam compensateBlocks(long startBlock, long endBlock) { long startCompensated = compensateMinBlock(startBlock); long endCompensated = compensateMaxBlock(endBlock); - final long startFinal = (startCompensated > endCompensated) - ? endCompensated - : startCompensated; - - final long endFinal = (startCompensated > endCompensated) - ? startCompensated - : endCompensated; - + final long startFinal = Math.min(startCompensated, endCompensated); + final long endFinal = Math.max(startCompensated, endCompensated); return new BlockParam(startFinal, endFinal); } diff --git a/src/test/java/io/api/etherscan/statistic/StatisticPriceApiTest.java b/src/test/java/io/api/etherscan/statistic/StatisticPriceApiTest.java index 3245b17..e29a6b1 100644 --- a/src/test/java/io/api/etherscan/statistic/StatisticPriceApiTest.java +++ b/src/test/java/io/api/etherscan/statistic/StatisticPriceApiTest.java @@ -18,8 +18,8 @@ public void correct() { assertNotNull(price); assertNotNull(price.btcTimestamp()); assertNotNull(price.usdTimestamp()); - assertNotEquals(0, price.inBtc()); - assertNotEquals(0, price.inUsd()); + assertNotEquals(0.0, price.inBtc()); + assertNotEquals(0.0, price.inUsd()); assertNotNull(price.toString()); Price empty = new Price(); diff --git a/src/test/java/io/api/manager/QueueManagerTest.java b/src/test/java/io/api/manager/QueueManagerTest.java index acc7b43..74e674c 100644 --- a/src/test/java/io/api/manager/QueueManagerTest.java +++ b/src/test/java/io/api/manager/QueueManagerTest.java @@ -23,6 +23,7 @@ public void fakeManager() { fakeManager.takeTurn(); fakeManager.takeTurn(); fakeManager.takeTurn(); + assertNotNull(fakeManager); } @Test(timeout = 3500) @@ -30,6 +31,7 @@ public void queueManager() { IQueueManager queueManager = new QueueManager(1, 3); queueManager.takeTurn(); queueManager.takeTurn(); + assertNotNull(queueManager); } @Test(timeout = 4500) @@ -37,6 +39,7 @@ public void queueManagerWithDelay() { IQueueManager queueManager = new QueueManager(1, 2, 2); queueManager.takeTurn(); queueManager.takeTurn(); + assertNotNull(queueManager); } @Test