Skip to content

Commit

Permalink
Refactoring to make covalent provider generic
Browse files Browse the repository at this point in the history
  • Loading branch information
RedRaton committed Jul 10, 2023
1 parent 875bd7a commit 52e80e9
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.allmycoins.balance.ethereum;
package com.allmycoins.balance.covalent;

import java.math.BigDecimal;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.allmycoins.balance.ethereum;
package com.allmycoins.balance.covalent;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.allmycoins.balance.ethereum;
package com.allmycoins.balance.covalent;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.allmycoins.balance.ethereum;
package com.allmycoins.balance.covalent;

import java.util.Map;

import com.allmycoins.request.GetRequest;

public final class CovalentBalancesRequest implements GetRequest<CovalentBalancesJson> {

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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<BalanceJson> balance(String publicAddress) {
Future<CovalentBalancesJson> futureAmberdataEthTokensJson = RequestUtils
.sendRequestFuture(new CovalentBalancesRequest(network, publicAddress));

CovalentBalancesJson covalentBalancesJson = FutureUtils.futureResult(futureAmberdataEthTokensJson);

List<CovalentBalanceItemJson> 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;
}
}
32 changes: 4 additions & 28 deletions src/main/java/com/allmycoins/balance/ethereum/EthProvider.java
Original file line number Diff line number Diff line change
@@ -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<BalanceJson> balance(String publicAddress) {
Future<CovalentBalancesJson> futureAmberdataEthTokensJson = RequestUtils
.sendRequestFuture(new CovalentBalancesRequest(publicAddress));

CovalentBalancesJson covalentBalancesJson = FutureUtils.futureResult(futureAmberdataEthTokensJson);

List<CovalentBalanceItemJson> 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");
}
}

0 comments on commit 52e80e9

Please sign in to comment.