Updated on 2026-08-14

This commit is contained in:
Tangem 2018-11-01 11:23:59 +03:00
parent 240b513303
commit 47d731ffe2
9 changed files with 87 additions and 46 deletions

View file

@ -5,7 +5,11 @@ import android.text.InputFilter;
import com.tangem.domain.cardReader.CardProtocol;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
@ -42,6 +46,11 @@ public abstract class CoinEngine {
this.currency=currency;
}
public InternalAmount(BigInteger amount, String currency) {
super(new BigDecimal(amount).unscaledValue(), new BigDecimal(amount).scale());
this.currency=currency;
}
public boolean notZero()
{
return compareTo(BigDecimal.ZERO)>0;
@ -54,6 +63,32 @@ public abstract class CoinEngine {
public String getCurrency() {
return currency;
}
public String toValueString(int decimals) {
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
symbols.setDecimalSeparator('.');
DecimalFormat df = new DecimalFormat();
df.setDecimalFormatSymbols(symbols);
df.setMaximumFractionDigits(decimals);
df.setMinimumFractionDigits(0);
df.setGroupingUsed(false);
BigDecimal bd=new BigDecimal(unscaledValue(), scale());
bd.setScale(decimals, ROUND_DOWN);
return df.format(bd);
}
public String toValueString() {
return toValueString(scale());
}
@Override
public String toString() {
return super.toString();
}
}
public static class Amount extends BigDecimal {
@ -94,6 +129,10 @@ public abstract class CoinEngine {
}
public String toDescriptionString(int decimals) {
return toValueString(decimals)+ " " + currency;
}
public String toValueString(int decimals) {
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
symbols.setDecimalSeparator('.');
DecimalFormat df = new DecimalFormat();
@ -106,11 +145,11 @@ public abstract class CoinEngine {
BigDecimal bd=new BigDecimal(unscaledValue(), scale());
bd.setScale(decimals, ROUND_DOWN);
return df.format(bd)+ " " + currency;
return df.format(bd);
}
public String toEditString() {
return super.toString();
public String toValueString() {
return toValueString(scale());
}
public String toEquivalentString(double rateValue) {

View file

@ -31,12 +31,12 @@ public class EthData extends CoinData {
public void setUnconfirmedTXCount(BigInteger count) {
countUnconfirmedTX = count;
}
public String getAmountInGwei(String amount) {
BigInteger d = new BigInteger(amount, 10);
BigInteger m = d.divide(BigInteger.valueOf(1000000000L));
return m.toString(10);
}
//
// public String getAmountInGwei(String amount) {
// BigInteger d = new BigInteger(amount, 10);
// BigInteger m = d.divide(BigInteger.valueOf(1000000000L));
// return m.toString(10);
// }
@Override
public void clearInfo() {

View file

@ -75,7 +75,7 @@ public class TokenEngine extends CoinEngine {
return " " + convertToAmount(coinData.getBalanceInInternalUnits()).toDescriptionString(getTokenDecimals()) + " <br><small><small> + " + convertToAmount(coinData.getBalanceAlterInInternalUnits()).toDescriptionString(getEthDecimals()) + " for gas</small></small>";
} catch (Exception e) {
e.printStackTrace();
return null;
return "";
}
} else {
return "";

View file

@ -40,14 +40,13 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
private var serverApiHelper: ServerApiHelper = ServerApiHelper()
private var serverApiHelperElectrum: ServerApiHelperElectrum = ServerApiHelperElectrum()
//private var card: TangemCard? = null
private lateinit var ctx: TangemContext
private var feeRequestSuccess = false
private var balanceRequestSuccess = false
private var minFee: String? = null
private var maxFee: String? = null
private var normalFee: String? = null
private var minFee: CoinEngine.Amount? = null
private var maxFee: CoinEngine.Amount? = null
private var normalFee: CoinEngine.Amount? = null
private var isIncludeFee: Boolean = true
private var minFeeInInternalUnits: CoinEngine.InternalAmount? = CoinEngine.InternalAmount(0, "")
private var requestPIN2Count = 0
@ -69,8 +68,8 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
//
val engine = CoinEngineFactory.create(ctx)
val html = Html.fromHtml(engine!!.balanceHTML)
tvBalance.text = html
val html = Html.fromHtml(engine!!.balanceHTML)
tvBalance.text = html
isIncludeFee = intent.getBooleanExtra(SignPaymentActivity.EXTRA_FEE_INCLUDED, true)
@ -82,7 +81,9 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
tvIncFee.visibility = View.INVISIBLE
else
tvIncFee.visibility = View.VISIBLE
etAmount.setText(intent.getStringExtra(SignPaymentActivity.EXTRA_AMOUNT))
val amount=CoinEngine.Amount(intent.getStringExtra(SignPaymentActivity.EXTRA_AMOUNT), intent.getStringExtra(SignPaymentActivity.EXTRA_AMOUNT_CURRENCY))
etAmount.setText(amount.toValueString())
tvCurrency.text = engine.balanceCurrency
tvCurrency2.text = engine.feeCurrency
tvCardID.text = ctx.card!!.cidDescription
@ -188,8 +189,6 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
val intent = Intent(baseContext, PinRequestActivity::class.java)
intent.putExtra("mode", PinRequestActivity.Mode.RequestPIN2.toString())
ctx.saveToIntent(intent)
// intent.putExtra(TangemCard.EXTRA_UID, card!!.uid)
// intent.putExtra(TangemCard.EXTRA_CARD, card!!.asBundle)
intent.putExtra(SignPaymentActivity.EXTRA_FEE_INCLUDED, isIncludeFee)
startActivityForResult(intent, REQUEST_CODE_REQUEST_PIN2)
}
@ -201,7 +200,7 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
try {
etFee.setText(getString(R.string.empty))
val engine = CoinEngineFactory.create(ctx)
val balance= engine.convertToAmount(CoinEngine.InternalAmount(electrumRequest.result.getLong("confirmed") + electrumRequest.result.getLong("unconfirmed"), "Satoshi"))
val balance = engine.convertToAmount(CoinEngine.InternalAmount(electrumRequest.result.getLong("confirmed") + electrumRequest.result.getLong("unconfirmed"), "Satoshi"))
val amount = CoinEngine.Amount(etAmount.text.toString(), ctx.blockchain.currency)
if (balance < amount) {
etFee.error = getString(R.string.not_enough_funds)
@ -240,20 +239,21 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
val l = BigInteger(gasPrice, 16).divide(BigInteger.valueOf(1000000000L)).multiply(BigInteger.valueOf(1000000000L))
val m = if (ctx.blockchain == Blockchain.Token) BigInteger.valueOf(60000) else BigInteger.valueOf(21000)
val minFeeInGwei = (ctx.coinData!! as EthData).getAmountInGwei(l.multiply(m).toString())
val normalFeeInGwei = (ctx.coinData!! as EthData).getAmountInGwei(l.multiply(BigInteger.valueOf(12)).divide(BigInteger.valueOf(10)).multiply(m).toString())
val maxFeeInGwei = (ctx.coinData!! as EthData).getAmountInGwei(l.multiply(BigInteger.valueOf(15)).divide(BigInteger.valueOf(10)).multiply(m).toString())
val weiMinFee = CoinEngine.InternalAmount(l.multiply(m), "wei")
val weiNormalFee = CoinEngine.InternalAmount(weiMinFee.multiply(BigDecimal.valueOf(12)).divide(BigDecimal.valueOf(10)), "wei")
val weiMaxFee = CoinEngine.InternalAmount(weiMinFee.multiply(BigDecimal.valueOf(15)).divide(BigDecimal.valueOf(10)), "wei")
minFee = minFeeInGwei
normalFee = normalFeeInGwei
maxFee = maxFeeInGwei
etFee.setText(normalFeeInGwei.replace(',', '.'))
minFee = engine.convertToAmount(weiMinFee)
normalFee = engine.convertToAmount(weiNormalFee)
maxFee = engine.convertToAmount(weiMaxFee)
doSetFee(rgFee.checkedRadioButtonId)
//etFee.setText(weiNormalFee.toValueString())
etFee.error = null
btnSend.visibility = View.VISIBLE
feeRequestSuccess = true
balanceRequestSuccess = true
dtVerified = Date()
minFeeInInternalUnits = CoinEngine.InternalAmount(normalFeeInGwei,"Gwei")
minFeeInInternalUnits = weiNormalFee // TODO why not weiMinFee ???
}
}
}
@ -291,25 +291,23 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
df.maximumFractionDigits = 7
df.minimumFractionDigits = 3
df.isGroupingUsed = false
val strFee = df.format(fee)
when (blockCount) {
ServerApiHelper.ESTIMATE_FEE_MINIMAL -> {
minFee = strFee
//TODO - we must know currency of fee
minFeeInInternalUnits = CoinEngine.InternalAmount(strFee, tvCurrency2.text.toString())
minFee = CoinEngine.Amount(fee, engine.feeCurrency)
minFeeInInternalUnits = engine.convertToInternalAmount(minFee)
}
ServerApiHelper.ESTIMATE_FEE_NORMAL -> {
normalFee = strFee
normalFee = CoinEngine.Amount(fee, engine.feeCurrency)
doSetFee(rgFee.checkedRadioButtonId)
}
ServerApiHelper.ESTIMATE_FEE_PRIORITY -> {
maxFee = strFee
maxFee = CoinEngine.Amount(fee, engine.feeCurrency)
}
}
doSetFee(rgFee.checkedRadioButtonId)
etFee.error = null
feeRequestSuccess = true

View file

@ -8,7 +8,6 @@ import android.nfc.NfcAdapter
import android.nfc.Tag
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.text.Html
import android.view.View
import com.tangem.data.network.Cryptonit_OtherAPI
import com.tangem.domain.cardReader.NfcManager
@ -57,7 +56,7 @@ class PrepareCryptonitOtherAPIWithdrawalActivity : AppCompatActivity(), NfcAdapt
tvCurrency.text = engine.balanceCurrency
etAmount.setText(engine.convertToAmount(engine.convertToInternalAmount(ctx.card!!.denomination)).toEditString())
etAmount.setText(engine.convertToAmount(engine.convertToInternalAmount(ctx.card!!.denomination)).toValueString())
etAmount.filters=engine.amountInputFilters
// set listeners

View file

@ -7,7 +7,6 @@ import android.nfc.NfcAdapter
import android.nfc.Tag
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.text.Html
import android.text.InputFilter
import android.view.View
import android.view.inputmethod.EditorInfo
@ -59,7 +58,7 @@ class PrepareCryptonitWithdrawalActivity : AppCompatActivity(), NfcAdapter.Reade
tvCurrency.text = engine.balanceCurrency
tvFeeCurrency.text = engine.feeCurrency
etAmount.setText(engine.convertToAmount(engine.convertToInternalAmount(ctx.card!!.denomination)).toEditString())
etAmount.setText(engine.convertToAmount(engine.convertToInternalAmount(ctx.card!!.denomination)).toValueString())
etAmount.filters=engine.amountInputFilters
etAmount.setOnEditorActionListener { lv, actionId, event ->

View file

@ -11,7 +11,6 @@ import android.nfc.NfcAdapter
import android.nfc.Tag
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.text.Html
import android.view.View
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
@ -63,7 +62,7 @@ class PrepareKrakenWithdrawalActivity : AppCompatActivity(), NfcAdapter.ReaderCa
tvCurrency.text = engine.balanceCurrency
etAmount.setText(engine.convertToAmount(engine.convertToInternalAmount(ctx.card!!.denomination)).toEditString())
etAmount.setText(engine.convertToAmount(engine.convertToInternalAmount(ctx.card!!.denomination)).toValueString())
etAmount.filters=engine.amountInputFilters
etAmount.setOnEditorActionListener { lv, actionId, event ->

View file

@ -41,7 +41,7 @@ class PreparePaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
nfcManager = NfcManager(this, this)
ctx= TangemContext.loadFromBundle(this, intent.extras)
ctx = TangemContext.loadFromBundle(this, intent.extras)
tvCardID.text = ctx.card!!.cidDescription
val engine = CoinEngineFactory.create(ctx)
@ -60,7 +60,7 @@ class PreparePaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
etAmount.isEnabled = false
tvCurrency.text = engine.balance.currency
etAmount.setText(engine.balance.toEditString())
etAmount.setText(engine.balance.toValueString())
// limit number of symbols after comma
etAmount.filters = engine.amountInputFilters
@ -81,11 +81,13 @@ class PreparePaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
val engine1 = CoinEngineFactory.create(ctx)
val strAmount: String = etAmount.text.toString().replace(",", ".")
val amount=engine1.convertToAmount(etAmount.text.toString(), tvCurrency.text.toString())
val amount = engine1.convertToAmount(etAmount.text.toString(), tvCurrency.text.toString())
try {
if (!engine.checkNewTransactionAmount(amount))
etAmount.error = getString(R.string.not_enough_funds_on_your_card)
else
etAmount.error = null
} catch (e: Exception) {
etAmount.error = getString(R.string.unknown_amount_format)
}
@ -98,6 +100,8 @@ class PreparePaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
if (!checkAddress) {
etWallet.error = getString(R.string.incorrect_destination_wallet_address)
return@setOnClickListener
} else {
etWallet.error = null
}
if (etWallet.text.toString() == ctx.card!!.wallet) {
@ -111,6 +115,9 @@ class PreparePaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
// etAmount.error = getString(R.string.not_enough_funds_on_your_card)
// return@setOnClickListener
// }
if (!etAmount.error.isNullOrEmpty() || !etWallet.error.isNullOrEmpty()) {
return@setOnClickListener
}
val intent = Intent(baseContext, ConfirmPaymentActivity::class.java)
ctx.saveToIntent(intent)

View file

@ -344,7 +344,7 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific
ctx.card!!.addTokenToBlockchainName()
//TODO check
//ctx.blockchain=Blockchain.Ethereum
//ctx.blockchain=lBlockchain.Ethereum
requestCounter--
if (requestCounter == 0) srl!!.isRefreshing = false
@ -427,7 +427,7 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific
localStorage.applySubstitution(ctx.card!!)
if (ctx.card!!.blockchain == Blockchain.Token || ctx.card!!.blockchain == Blockchain.Ethereum) {
ctx.card!!.setBlockchainIDFromCard(Blockchain.Ethereum.id)
ctx.blockchain=Blockchain.Ethereum
//ctx.blockchain=Blockchain.Ethereum
//engine=engine!!.swithToOtherEngine(Blockchain.Ethereum)
}
refresh()