From 2b04de06616272489851a7a6c312dcc8427e5da7 Mon Sep 17 00:00:00 2001 From: Tangem Date: Mon, 20 Apr 2020 16:12:36 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../java/com/tangem/wallet/ltc/LtcEngine.java | 74 +++++++++++++++++-- 1 file changed, 66 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/tangem/wallet/ltc/LtcEngine.java b/app/src/main/java/com/tangem/wallet/ltc/LtcEngine.java index 3e0520bdf2..9312a17a87 100644 --- a/app/src/main/java/com/tangem/wallet/ltc/LtcEngine.java +++ b/app/src/main/java/com/tangem/wallet/ltc/LtcEngine.java @@ -23,6 +23,7 @@ import com.tangem.wallet.BTCUtils; import com.tangem.wallet.BalanceValidator; import com.tangem.wallet.Base58; import com.tangem.wallet.CoinData; +import com.tangem.wallet.CoinEngine; import com.tangem.wallet.R; import com.tangem.wallet.TangemContext; import com.tangem.wallet.Transaction; @@ -621,12 +622,74 @@ public class LtcEngine extends BtcEngine { } } - private final static BigDecimal relayFee = new BigDecimal(0.00001).setScale(8, RoundingMode.DOWN); + private final static Amount relayFee = new Amount(new BigDecimal(0.00001).setScale(8, RoundingMode.DOWN), "LTC"); + + private void forceRelayFeeMinimum(Amount fee) { + if (fee.compareTo(relayFee) < 0) { + fee = relayFee; + } + } @Override public void requestFee(BlockchainRequestsCallbacks blockchainRequestsCallbacks, String targetAddress, Amount amount) throws Exception { - coinData.minFee = coinData.normalFee = coinData.maxFee = new Amount(relayFee, "LTC"); - blockchainRequestsCallbacks.onComplete(true); + final int calcSize = calculateEstimatedTransactionSize(targetAddress, amount.toValueString()); + Log.e(TAG, String.format("Estimated tx size %d", calcSize)); + coinData.minFee = null; + coinData.maxFee = null; + coinData.normalFee = null; + + final ServerApiBlockcypher serverApiBlockcypher = new ServerApiBlockcypher(); + + ServerApiBlockcypher.ResponseListener blockcypherListener = new ServerApiBlockcypher.ResponseListener() { + @Override + public void onSuccess(String method, BlockcypherResponse blockcypherResponse) { + Log.e(TAG, "Wrong response type for requestFee"); + ctx.setError("Wrong response type for requestFee"); + blockchainRequestsCallbacks.onComplete(false); + } + + public void onSuccess(String method, BlockcypherFee blockcypherFee) { + Log.i(TAG, "onSuccess: " + method); + try { + BigDecimal minByteFee = new BigDecimal(blockcypherFee.getLow_fee_per_kb()).divide(BigDecimal.valueOf(1024)); + BigDecimal normalByteFee = new BigDecimal(blockcypherFee.getMedium_fee_per_kb()).divide(BigDecimal.valueOf(1024)); + BigDecimal maxByteFee = new BigDecimal(blockcypherFee.getHigh_fee_per_kb()).divide(BigDecimal.valueOf(1024)); + + CoinEngine.InternalAmount minIntAmount = new CoinEngine.InternalAmount(minByteFee.multiply(BigDecimal.valueOf(calcSize)), "satoshi"); + CoinEngine.InternalAmount normalIntAmount = new CoinEngine.InternalAmount(normalByteFee.multiply(BigDecimal.valueOf(calcSize)), "satoshi"); + CoinEngine.InternalAmount maxIntAmount = new CoinEngine.InternalAmount(maxByteFee.multiply(BigDecimal.valueOf(calcSize)), "satoshi"); + + coinData.minFee = convertToAmount(minIntAmount); + coinData.normalFee = convertToAmount(normalIntAmount); + coinData.maxFee = convertToAmount(maxIntAmount); + + forceRelayFeeMinimum(coinData.minFee); + forceRelayFeeMinimum(coinData.normalFee); + forceRelayFeeMinimum(coinData.maxFee); + + } catch (Exception e) { + e.printStackTrace(); + Log.e(TAG, "FAIL BLOCKCYPHER_FEE Exception"); + } + + if (serverApiBlockcypher.isRequestsSequenceCompleted()) { + blockchainRequestsCallbacks.onComplete(!ctx.hasError()); + } else { + blockchainRequestsCallbacks.onProgress(); + } + } + + @Override + public void onFail(String method, String message) { + Log.i(TAG, "onFail: " + method + " " + message); + ctx.setError(message); + blockchainRequestsCallbacks.onComplete(false); + } + }; + + serverApiBlockcypher.setResponseListener(blockcypherListener); + + serverApiBlockcypher.requestData(ctx.getBlockchain().getID(), ServerApiBlockcypher.BLOCKCYPHER_FEE, "", ""); } @Override @@ -675,9 +738,4 @@ public class LtcEngine extends BtcEngine { serverApiBlockcypher.requestData(ctx.getBlockchain().getID(), ServerApiBlockcypher.BLOCKCYPHER_SEND, "", txStr); } - - @Override - public boolean allowSelectFeeLevel() { - return false; - } } \ No newline at end of file