From 52e80e9842fce0439d00f86062d77443d8b7922b Mon Sep 17 00:00:00 2001 From: Samuel Tessian Date: Mon, 10 Jul 2023 22:37:57 +1000 Subject: [PATCH] Refactoring to make covalent provider generic --- .../CovalentBalanceItemJson.java | 2 +- .../CovalentBalancesDataJson.java | 2 +- .../CovalentBalancesJson.java | 2 +- .../CovalentBalancesRequest.java | 12 ++--- .../balance/covalent/CovalentProvider.java | 44 +++++++++++++++++++ .../balance/ethereum/EthProvider.java | 32 ++------------ 6 files changed, 58 insertions(+), 36 deletions(-) rename src/main/java/com/allmycoins/balance/{ethereum => covalent}/CovalentBalanceItemJson.java (92%) rename src/main/java/com/allmycoins/balance/{ethereum => covalent}/CovalentBalancesDataJson.java (86%) rename src/main/java/com/allmycoins/balance/{ethereum => covalent}/CovalentBalancesJson.java (86%) rename src/main/java/com/allmycoins/balance/{ethereum => covalent}/CovalentBalancesRequest.java (64%) create mode 100644 src/main/java/com/allmycoins/balance/covalent/CovalentProvider.java diff --git a/src/main/java/com/allmycoins/balance/ethereum/CovalentBalanceItemJson.java b/src/main/java/com/allmycoins/balance/covalent/CovalentBalanceItemJson.java similarity index 92% rename from src/main/java/com/allmycoins/balance/ethereum/CovalentBalanceItemJson.java rename to src/main/java/com/allmycoins/balance/covalent/CovalentBalanceItemJson.java index b36e78d..34138c3 100644 --- a/src/main/java/com/allmycoins/balance/ethereum/CovalentBalanceItemJson.java +++ b/src/main/java/com/allmycoins/balance/covalent/CovalentBalanceItemJson.java @@ -1,4 +1,4 @@ -package com.allmycoins.balance.ethereum; +package com.allmycoins.balance.covalent; import java.math.BigDecimal; diff --git a/src/main/java/com/allmycoins/balance/ethereum/CovalentBalancesDataJson.java b/src/main/java/com/allmycoins/balance/covalent/CovalentBalancesDataJson.java similarity index 86% rename from src/main/java/com/allmycoins/balance/ethereum/CovalentBalancesDataJson.java rename to src/main/java/com/allmycoins/balance/covalent/CovalentBalancesDataJson.java index 4ffe7d3..4004774 100644 --- a/src/main/java/com/allmycoins/balance/ethereum/CovalentBalancesDataJson.java +++ b/src/main/java/com/allmycoins/balance/covalent/CovalentBalancesDataJson.java @@ -1,4 +1,4 @@ -package com.allmycoins.balance.ethereum; +package com.allmycoins.balance.covalent; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; diff --git a/src/main/java/com/allmycoins/balance/ethereum/CovalentBalancesJson.java b/src/main/java/com/allmycoins/balance/covalent/CovalentBalancesJson.java similarity index 86% rename from src/main/java/com/allmycoins/balance/ethereum/CovalentBalancesJson.java rename to src/main/java/com/allmycoins/balance/covalent/CovalentBalancesJson.java index 95a7d34..bbac2fe 100644 --- a/src/main/java/com/allmycoins/balance/ethereum/CovalentBalancesJson.java +++ b/src/main/java/com/allmycoins/balance/covalent/CovalentBalancesJson.java @@ -1,4 +1,4 @@ -package com.allmycoins.balance.ethereum; +package com.allmycoins.balance.covalent; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; diff --git a/src/main/java/com/allmycoins/balance/ethereum/CovalentBalancesRequest.java b/src/main/java/com/allmycoins/balance/covalent/CovalentBalancesRequest.java similarity index 64% rename from src/main/java/com/allmycoins/balance/ethereum/CovalentBalancesRequest.java rename to src/main/java/com/allmycoins/balance/covalent/CovalentBalancesRequest.java index f5c7ddd..53474e6 100644 --- a/src/main/java/com/allmycoins/balance/ethereum/CovalentBalancesRequest.java +++ b/src/main/java/com/allmycoins/balance/covalent/CovalentBalancesRequest.java @@ -1,4 +1,4 @@ -package com.allmycoins.balance.ethereum; +package com.allmycoins.balance.covalent; import java.util.Map; @@ -6,10 +6,12 @@ public final class CovalentBalancesRequest implements GetRequest { - private final String ethAddress; + private final String network; + private final String publicAddress; - public CovalentBalancesRequest(String pEthAddress) { - ethAddress = pEthAddress; + public CovalentBalancesRequest(final String pNetwork, final String pPublicAddress) { + network = pNetwork; + publicAddress = pPublicAddress; } @Override @@ -19,7 +21,7 @@ public String baseUrl() { @Override public String endPoint() { - return "/v1/eth-mainnet/address/" + ethAddress + "/balances_v2/"; + return "/v1/" + network + "/address/" + publicAddress + "/balances_v2/"; } @Override diff --git a/src/main/java/com/allmycoins/balance/covalent/CovalentProvider.java b/src/main/java/com/allmycoins/balance/covalent/CovalentProvider.java new file mode 100644 index 0000000..2e1be5e --- /dev/null +++ b/src/main/java/com/allmycoins/balance/covalent/CovalentProvider.java @@ -0,0 +1,44 @@ +package com.allmycoins.balance.covalent; + +import java.util.List; +import java.util.concurrent.Future; +import java.util.stream.Collectors; + +import com.allmycoins.balance.PublicAddressBalanceProvider; +import com.allmycoins.json.BalanceJson; +import com.allmycoins.utils.BigDecimalUtils; +import com.allmycoins.utils.FutureUtils; +import com.allmycoins.utils.RequestUtils; + +public class CovalentProvider implements PublicAddressBalanceProvider { + + private String balanceSource; + private String network; + private String configKey; + + public CovalentProvider(final String pBalanceSource, final String pNetwork, final String pConfigKey) { + balanceSource = pBalanceSource; + network = pNetwork; + configKey = pConfigKey; + } + + @Override + public final List balance(String publicAddress) { + Future futureAmberdataEthTokensJson = RequestUtils + .sendRequestFuture(new CovalentBalancesRequest(network, publicAddress)); + + CovalentBalancesJson covalentBalancesJson = FutureUtils.futureResult(futureAmberdataEthTokensJson); + + List list = List.of(covalentBalancesJson.getData().getItems()); + + return list.stream() + .map(i -> new BalanceJson(i.getContract_ticker_symbol(), + BigDecimalUtils.decimal(i.getBalance(), i.getContract_decimals()), balanceSource)) + .collect(Collectors.toList()); + } + + @Override + public final String privateConfigKey() { + return configKey; + } +} diff --git a/src/main/java/com/allmycoins/balance/ethereum/EthProvider.java b/src/main/java/com/allmycoins/balance/ethereum/EthProvider.java index d7543f9..1af3182 100644 --- a/src/main/java/com/allmycoins/balance/ethereum/EthProvider.java +++ b/src/main/java/com/allmycoins/balance/ethereum/EthProvider.java @@ -1,34 +1,10 @@ package com.allmycoins.balance.ethereum; -import java.util.List; -import java.util.concurrent.Future; -import java.util.stream.Collectors; +import com.allmycoins.balance.covalent.CovalentProvider; -import com.allmycoins.balance.PublicAddressBalanceProvider; -import com.allmycoins.json.BalanceJson; -import com.allmycoins.utils.BigDecimalUtils; -import com.allmycoins.utils.FutureUtils; -import com.allmycoins.utils.RequestUtils; +public final class EthProvider extends CovalentProvider { -public final class EthProvider implements PublicAddressBalanceProvider { - - @Override - public List balance(String publicAddress) { - Future futureAmberdataEthTokensJson = RequestUtils - .sendRequestFuture(new CovalentBalancesRequest(publicAddress)); - - CovalentBalancesJson covalentBalancesJson = FutureUtils.futureResult(futureAmberdataEthTokensJson); - - List list = List.of(covalentBalancesJson.getData().getItems()); - - return list.stream() - .map(i -> new BalanceJson(i.getContract_ticker_symbol(), - BigDecimalUtils.decimal(i.getBalance(), i.getContract_decimals()), "ETH wallet")) - .collect(Collectors.toList()); - } - - @Override - public String privateConfigKey() { - return "ETH_ADDRESS"; + public EthProvider() { + super("ETH wallet", "eth-mainnet", "ETH_ADDRESS"); } }