diff --git a/app/src/main/java/com/tangem/domain/wallet/CoinEngine.java b/app/src/main/java/com/tangem/domain/wallet/CoinEngine.java index 229593fe4c..f6a104c7ca 100644 --- a/app/src/main/java/com/tangem/domain/wallet/CoinEngine.java +++ b/app/src/main/java/com/tangem/domain/wallet/CoinEngine.java @@ -27,36 +27,35 @@ public abstract class CoinEngine { public InternalAmount() { super(0); - currency=""; + currency = ""; } public InternalAmount(String amountString, String currency) { - super(amountString.replace(',','.')); - this.currency=currency; + super(amountString.replace(',', '.')); + this.currency = currency; } public InternalAmount(long amount, String currency) { super(amount); - this.currency=currency; + this.currency = currency; } public InternalAmount(BigDecimal amount, String currency) { super(amount.unscaledValue(), amount.scale()); - this.currency=currency; + this.currency = currency; } public InternalAmount(BigInteger amount, String currency) { super(new BigDecimal(amount).unscaledValue(), new BigDecimal(amount).scale()); - this.currency=currency; + this.currency = currency; } - public boolean notZero() - { - return compareTo(BigDecimal.ZERO)>0; + public boolean notZero() { + return compareTo(BigDecimal.ZERO) > 0; } public boolean isZero() { - return compareTo(BigDecimal.ZERO)==0; + return compareTo(BigDecimal.ZERO) == 0; } public String getCurrency() { @@ -74,7 +73,7 @@ public abstract class CoinEngine { df.setGroupingUsed(false); - BigDecimal bd=new BigDecimal(unscaledValue(), scale()); + BigDecimal bd = new BigDecimal(unscaledValue(), scale()); bd.setScale(decimals, ROUND_DOWN); return df.format(bd); } @@ -95,11 +94,11 @@ public abstract class CoinEngine { public Amount() { super(0); - currency=""; + currency = ""; } public Amount(String amountString, String currency) { - super(amountString.replace(',','.')); + super(amountString.replace(',', '.')); this.currency = currency; } @@ -122,13 +121,12 @@ public abstract class CoinEngine { return super.toString() + " " + currency; } - public boolean notZero() - { - return compareTo(BigDecimal.ZERO)>0; + public boolean notZero() { + return compareTo(BigDecimal.ZERO) > 0; } public String toDescriptionString(int decimals) { - return toValueString(decimals)+ " " + currency; + return toValueString(decimals) + " " + currency; } public String toValueString(int decimals) { @@ -142,7 +140,7 @@ public abstract class CoinEngine { df.setGroupingUsed(false); - BigDecimal bd=new BigDecimal(unscaledValue(), scale()); + BigDecimal bd = new BigDecimal(unscaledValue(), scale()); bd.setScale(decimals, ROUND_DOWN); return df.format(bd); } @@ -163,7 +161,7 @@ public abstract class CoinEngine { } public boolean isZero() { - return compareTo(BigDecimal.ZERO)==0; + return compareTo(BigDecimal.ZERO) == 0; } } @@ -222,9 +220,11 @@ public abstract class CoinEngine { public abstract String calculateAddress(byte[] pkUncompressed) throws NoSuchProviderException, NoSuchAlgorithmException; public abstract Amount convertToAmount(InternalAmount internalAmount) throws Exception; + public abstract Amount convertToAmount(String strAmount, String currency); public abstract InternalAmount convertToInternalAmount(Amount amount) throws Exception; + public abstract InternalAmount convertToInternalAmount(byte[] bytes) throws Exception; public abstract byte[] convertToByteArray(InternalAmount internalAmount) throws Exception; @@ -237,39 +237,66 @@ public abstract class CoinEngine { try { String wallet = calculateAddress(ctx.getCard().getWalletPublicKey()); ctx.getCoinData().setWallet(wallet); - } - catch (Exception e) - { + } catch (Exception e) { ctx.getCoinData().setWallet("ERROR"); throw new CardProtocol.TangemException("Can't define wallet address"); } } + /** + * Create instance of {@link SignTask.PaymentToSign} used for transaction signing and sending + * + * Transaction processing sequence: + * 1. User enter transaction attributes + * 2. Application create instance of {@link SignTask.PaymentToSign} by call {@see constructPayment} + * 3. Application set notification when transaction were prepared {@see setOnNeedSendPayment} and start {@link SignTask} + * 4. User tap card and card sign transaction + * 5. Application receive {@link CoinEngine.OnNeedSendPayment} notification with prepared raw transaction + * 6. Application show user information that transaction ready for sending and start sending procedure by call {@see requestSendTransaction} + * 7. Application receive notification of sending result through {@link CoinEngine.BlockchainRequestsCallbacks} and show result to user + * + * @param amountValue - amount of desired transaction + * @param feeValue - fee amount of desired transaction + * @param IncFee - true if fee amount is included in amountValue (amountValue is total amount of transaction) + * @param targetAddress - target address of transaction + * @return instance of {@link SignTask.PaymentToSign} + * @throws Exception if something goes wrong + */ public abstract SignTask.PaymentToSign constructPayment(Amount amountValue, Amount feeValue, boolean IncFee, String targetAddress) throws Exception; - public interface OnNeedSendPayment - { + /** + * Interface used to notify main application when new transaction is prepared to send + */ + public interface OnNeedSendPayment { void onPaymentPrepared(byte[] txForSend); } + protected OnNeedSendPayment onNeedSendPayment; + + /** + * Set notification callback when new transaction is prepared to send + */ public void setOnNeedSendPayment(OnNeedSendPayment onNeedSendPayment) { this.onNeedSendPayment = onNeedSendPayment; } protected void notifyOnNeedSendPayment(byte[] txForSend) throws Exception { - if(onNeedSendPayment==null) + if (onNeedSendPayment == null) throw new Exception("Payment signed but no callback defined to send!"); onNeedSendPayment.onPaymentPrepared(txForSend); } - public interface BlockchainRequestsCallbacks - { + /** + * Interface used to notify/querying application during processing sequence of request to blockchain nodes/servers + */ + public interface BlockchainRequestsCallbacks { /** * Notification that the all requests in sequence completed * Call after a last request completed * If occurred error return in ctx.error + * * @param success -* */ void onComplete(Boolean success); @@ -283,10 +310,18 @@ public abstract class CoinEngine { /** * Return flag that allow to add new or re-requests in the sequence * Call between requests or when request fail and before re-request + * * @return true if not need terminate (e.g. activity is online) */ boolean allowAdvance(); } + + /** + * Start sequence of request to blockchain nodes needed to get balance and other information (for example unspent transaction) needed to + * show current state of wallet and prepare new withdrawal transaction + * @param blockchainRequestsCallbacks - notifications + * @throws Exception + */ public abstract void requestBalanceAndUnspentTransactions(BlockchainRequestsCallbacks blockchainRequestsCallbacks) throws Exception; public abstract void requestFee(BlockchainRequestsCallbacks blockchainRequestsCallbacks, String targetAddress, Amount amount) throws Exception;