Skip to content

Commit

Permalink
Update CryptX wallet to process USDT in TRON network (#883)
Browse files Browse the repository at this point in the history
* Add support for USDT on TR20 network for CryptX wallet

* Add TRX support to CryptX wallet

* Make CryptX wallet implement IQueryableWallet
  • Loading branch information
sandgub authored Feb 14, 2024
1 parent 6e54774 commit 6342b94
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class Converters {
public static final BigDecimal USDTTRON = BigDecimal.TEN.pow(6);
public static final BigDecimal XRP = BigDecimal.TEN.pow(6);
public static final BigDecimal VERUM = BigDecimal.TEN.pow(8);
public static final BigDecimal TRX = BigDecimal.TEN.pow(6);

public static final BigDecimal TBCH = BigDecimal.TEN.pow(8);
public static final BigDecimal TBTC = BigDecimal.TEN.pow(8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ public Set<String> getCryptoCurrencies() {
coins.add(CryptoCurrency.LTC.getCode());
coins.add(CryptoCurrency.BCH.getCode());
coins.add(CryptoCurrency.ETH.getCode());
coins.add(CryptoCurrency.TRX.getCode());
coins.add(CryptoCurrency.USDT.getCode());
coins.add(CryptoCurrency.BTBS.getCode());
coins.add(CryptoCurrency.GQ.getCode());
coins.add(CryptoCurrency.USDTTRON.getCode());
coins.add(CryptoCurrency.TBTC.getCode());
coins.add(CryptoCurrency.TLTC.getCode());
coins.add(CryptoCurrency.TBCH.getCode());
Expand Down Expand Up @@ -159,6 +159,9 @@ public String getAPICryptocurrency(String cryptoCurrency) {
if (cryptoCurrency.equalsIgnoreCase(CryptoCurrency.USDT.getCode())) {
return CryptoCurrency.ETH.getCode();
}
if (cryptoCurrency.equalsIgnoreCase(CryptoCurrency.USDTTRON.getCode())) {
return CryptoCurrency.TRX.getCode();
}
return cryptoCurrency;
}

Expand All @@ -177,12 +180,16 @@ private BigInteger toMinorUnit(String cryptoCurrency, BigDecimal amount) {
case TETH:
case ETH:
return amount.multiply(Converters.ETH).toBigInteger();
case TRX:
return amount.multiply(Converters.TRX).toBigInteger();
case BTBS:
return amount.multiply(Converters.BTBS).toBigInteger();
case GQ:
return amount.multiply(Converters.GQ).toBigInteger();
case USDT:
return amount.multiply(Converters.USDT).toBigInteger();
case USDTTRON:
return amount.multiply(Converters.USDTTRON).toBigInteger();
default:
return amount.toBigInteger();
}
Expand All @@ -205,7 +212,7 @@ private String sendCryptXTransaction(String cryptoCurrency, CryptXSendTransactio
return null;
}

private BigDecimal toMajorUnit(String cryptoCurrency, String amount) {
protected BigDecimal toMajorUnit(String cryptoCurrency, String amount) {
BigInteger bigIntegerAmount = new BigInteger(amount);
switch (CryptoCurrency.valueOfCode(cryptoCurrency)) {
case TBTC:
Expand All @@ -221,6 +228,8 @@ private BigDecimal toMajorUnit(String cryptoCurrency, String amount) {
case GQ:
return new BigDecimal(bigIntegerAmount).movePointLeft(18);
case USDT:
case USDTTRON:
case TRX:
return new BigDecimal(bigIntegerAmount).movePointLeft(6);
default:
throw new IllegalArgumentException("Unsupported crypto currency");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

import com.generalbytes.batm.common.currencies.CryptoCurrency;
import com.generalbytes.batm.server.extensions.IGeneratesNewDepositCryptoAddress;
import com.generalbytes.batm.server.extensions.IQueryableWallet;
import com.generalbytes.batm.server.extensions.extra.bitcoin.wallets.cryptx.v2.dto.CryptXCreateAddressRequest;
import com.generalbytes.batm.server.extensions.extra.bitcoin.wallets.cryptx.v2.dto.CryptXException;
import com.generalbytes.batm.server.extensions.extra.bitcoin.wallets.cryptx.v2.dto.CryptXReceivedAmount;
import com.generalbytes.batm.server.extensions.payment.ReceivedAmount;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import si.mazi.rescu.HttpStatusIOException;

import java.math.BigInteger;
import java.util.Map;

public class CryptXWithUniqueAddresses extends CryptXWallet implements IGeneratesNewDepositCryptoAddress {
public class CryptXWithUniqueAddresses extends CryptXWallet implements IGeneratesNewDepositCryptoAddress, IQueryableWallet {

private static final Logger log = LoggerFactory.getLogger(CryptXWithUniqueAddresses.class);

Expand Down Expand Up @@ -49,4 +53,29 @@ public String generateNewDepositCryptoAddress(String cryptoCurrency, String labe
return null;
}

@Override
public ReceivedAmount getReceivedAmount(String address, String cryptoCurrency) {
if (!getCryptoCurrencies().contains(cryptoCurrency)) {
log.warn("{} not supported", cryptoCurrency);
return ReceivedAmount.ZERO;
}
cryptoCurrency = cryptoCurrency.toLowerCase();

try {
CryptXReceivedAmount cryptXReceivedAmount = api.getReceivedAmount(cryptoCurrency, this.walletId, address);
if (cryptXReceivedAmount.getAmount().compareTo(BigInteger.ZERO) > 0) {
return new ReceivedAmount(toMajorUnit(cryptoCurrency, cryptXReceivedAmount.getAmount().toString()), 999);
}
return new ReceivedAmount(toMajorUnit(cryptoCurrency, cryptXReceivedAmount.getAmount().toString()), 0);
} catch (HttpStatusIOException hse) {
log.debug("get received amount error: {}", hse.getHttpBody());
} catch (CryptXException e) {
log.debug("get received amount error: {}", e.getErrorMessage());
} catch (Exception e) {
log.error("get received amount error", e);
}

return ReceivedAmount.ZERO;
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.generalbytes.batm.server.extensions.extra.bitcoin.wallets.cryptx.v2;

import com.generalbytes.batm.server.extensions.extra.bitcoin.wallets.cryptx.v2.dto.Balance;
import com.generalbytes.batm.server.extensions.extra.bitcoin.wallets.cryptx.v2.dto.CryptXCreateAddressRequest;
import com.generalbytes.batm.server.extensions.extra.bitcoin.wallets.cryptx.v2.dto.CryptXException;
import com.generalbytes.batm.server.extensions.extra.bitcoin.wallets.cryptx.v2.dto.CryptXSendTransactionRequest;
import com.generalbytes.batm.server.extensions.extra.bitcoin.wallets.cryptx.v2.dto.*;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
Expand Down Expand Up @@ -53,4 +50,12 @@ Balance getWalletBalance(
@QueryParam("allTokens") Boolean allTokens
) throws IOException;

@GET
@Path("{coin}/wallet/{walletId}/{address}/amount")
CryptXReceivedAmount getReceivedAmount(
@PathParam("coin") String coin,
@PathParam("walletId") String walletId,
@PathParam("address") String address
) throws IOException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.generalbytes.batm.server.extensions.extra.bitcoin.wallets.cryptx.v2.dto;

import java.math.BigInteger;

public class CryptXReceivedAmount {

private BigInteger amount;

public BigInteger getAmount() {
return amount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@
<cryptocurrency>BTC</cryptocurrency>
<cryptocurrency>LTC</cryptocurrency>
<cryptocurrency>ETH</cryptocurrency>
<cryptocurrency>TRX</cryptocurrency>
<cryptocurrency>USDT</cryptocurrency>
<cryptocurrency>USDTTRON</cryptocurrency>
<help>
*host: https://api.walletpro.cryptal.com/ (required) -
port: (not required) -
Expand All @@ -106,7 +108,9 @@
<cryptocurrency>BTC</cryptocurrency>
<cryptocurrency>LTC</cryptocurrency>
<cryptocurrency>ETH</cryptocurrency>
<cryptocurrency>TRX</cryptocurrency>
<cryptocurrency>USDT</cryptocurrency>
<cryptocurrency>USDTTRON</cryptocurrency>
<help>
*host: https://api.walletpro.cryptal.com/ (required) -
port: (not required) -
Expand Down

0 comments on commit 6342b94

Please sign in to comment.