diff --git a/app/build.gradle b/app/build.gradle index 5f5c366855..0c79645200 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -15,7 +15,7 @@ android { applicationId "com.tangem.wallet" minSdkVersion 21 targetSdkVersion 27 - versionCode 74 + versionCode 75 versionName "0.88.1." + generateVersionName() testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } diff --git a/app/src/main/java/com/tangem/data/network/ServerApiHelper.java b/app/src/main/java/com/tangem/data/network/ServerApiHelper.java index 3b4fede76d..fc0ef81924 100644 --- a/app/src/main/java/com/tangem/data/network/ServerApiHelper.java +++ b/app/src/main/java/com/tangem/data/network/ServerApiHelper.java @@ -123,13 +123,16 @@ public class ServerApiHelper { public static final String INFURA_ETH_CALL = "eth_call"; public static final String INFURA_ETH_SEND_RAW_TRANSACTION = "eth_sendRawTransaction"; public static final String INFURA_ETH_GAS_PRICE = "eth_gasPrice"; + private InfuraBodyListener infuraBodyListener; public interface InfuraBodyListener { - void onInfura(String method, InfuraResponse infuraResponse); + void onInfuraSuccess(String method, InfuraResponse infuraResponse); + + void onInfuraFail(String method, String message); } - public void setInfura(InfuraBodyListener listener) { + public void setInfuraResponse(InfuraBodyListener listener) { infuraBodyListener = listener; } @@ -166,19 +169,21 @@ public class ServerApiHelper { } Call call = infuraApi.infura(infuraBody); - call.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.code() == 200) { - infuraBodyListener.onInfura(method, response.body()); + infuraBodyListener.onInfuraSuccess(method, response.body()); Log.i(TAG, "infura " + method + " onResponse " + response.code()); - } else + } else { + infuraBodyListener.onInfuraFail(method, String.valueOf(response.code())); Log.e(TAG, "infura " + method + " onResponse " + response.code()); + } } @Override public void onFailure(@NonNull Call call, @NonNull Throwable t) { + infuraBodyListener.onInfuraFail(method, String.valueOf(t.getMessage())); Log.e(TAG, "infura " + method + " onFailure " + t.getMessage()); } }); diff --git a/app/src/main/java/com/tangem/presentation/activity/ConfirmPaymentActivity.kt b/app/src/main/java/com/tangem/presentation/activity/ConfirmPaymentActivity.kt index 65e86b1d9d..0916f33e64 100644 --- a/app/src/main/java/com/tangem/presentation/activity/ConfirmPaymentActivity.kt +++ b/app/src/main/java/com/tangem/presentation/activity/ConfirmPaymentActivity.kt @@ -15,6 +15,7 @@ import android.view.KeyEvent import android.view.View import android.widget.* import com.tangem.data.network.ServerApiHelper +import com.tangem.data.network.model.InfuraResponse import com.tangem.data.network.request.ElectrumRequest import com.tangem.data.network.request.FeeRequest import com.tangem.data.network.task.confirm_payment.ConnectFeeTask @@ -223,33 +224,7 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback { startActivityForResult(intent, REQUEST_CODE_REQUEST_PIN2) } - // request eth gas price listener - serverApiHelper!!.setInfura { method, infuraResponse -> - if (method == ServerApiHelper.INFURA_ETH_GAS_PRICE) { - var gasPrice = infuraResponse.result - gasPrice = gasPrice.substring(2) - var l = BigInteger(gasPrice, 16) - val m = if (card!!.blockchain == Blockchain.Token) BigInteger.valueOf(60000) else BigInteger.valueOf(21000) - l = l.multiply(m) - val minFeeInGwei = card!!.getAmountInGwei(l.toString()) - val normalFeeInGwei = card!!.getAmountInGwei(l.multiply(BigInteger.valueOf(12)).divide(BigInteger.valueOf(10)).toString()) - val maxFeeInGwei = card!!.getAmountInGwei(l.multiply(BigInteger.valueOf(15)).divide(BigInteger.valueOf(10)).toString()) - - minFee = minFeeInGwei - normalFee = normalFeeInGwei - maxFee = maxFeeInGwei - etFee!!.setText(normalFeeInGwei) - etFee!!.error = null - btnSend!!.visibility = View.VISIBLE - feeRequestSuccess = true - balanceRequestSuccess = true - dtVerified = Date() - minFeeInInternalUnits = card!!.internalUnitsFromString(normalFeeInGwei) - -// Log.i("eth_gas_price", gasPrice) - } - } // request estimate fee listener serverApiHelper!!.setEstimateFee { blockCount, estimateFeeResponse -> @@ -267,6 +242,56 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback { } } } + + // request eth gasPrice listener + val infuraBodyListener: ServerApiHelper.InfuraBodyListener = object : ServerApiHelper.InfuraBodyListener { + override fun onInfuraSuccess(method: String, infuraResponse: InfuraResponse) { + when (method) { + ServerApiHelper.INFURA_ETH_GAS_PRICE -> { + var gasPrice = infuraResponse.result + gasPrice = gasPrice.substring(2) + var l = BigInteger(gasPrice, 16) + + val m = if (card!!.blockchain == Blockchain.Token) BigInteger.valueOf(60000) else BigInteger.valueOf(21000) + l = l.multiply(m) + val minFeeInGwei = card!!.getAmountInGwei(l.toString()) + val normalFeeInGwei = card!!.getAmountInGwei(l.multiply(BigInteger.valueOf(12)).divide(BigInteger.valueOf(10)).toString()) + val maxFeeInGwei = card!!.getAmountInGwei(l.multiply(BigInteger.valueOf(15)).divide(BigInteger.valueOf(10)).toString()) + + minFee = minFeeInGwei + normalFee = normalFeeInGwei + maxFee = maxFeeInGwei + etFee!!.setText(normalFeeInGwei) + etFee!!.error = null + btnSend!!.visibility = View.VISIBLE + feeRequestSuccess = true + balanceRequestSuccess = true + dtVerified = Date() + minFeeInInternalUnits = card!!.internalUnitsFromString(normalFeeInGwei) + + Log.i("eth_gas_price", gasPrice) + } + + else -> { + + } + } + } + + override fun onInfuraFail(method: String, message: String) { + when (method) { + ServerApiHelper.INFURA_ETH_GAS_PRICE -> { + + } + + else -> { + + } + } + } + } + + serverApiHelper!!.setInfuraResponse(infuraBodyListener) } private fun requestInfura(method: String) { diff --git a/app/src/main/java/com/tangem/presentation/activity/SendTransactionActivity.kt b/app/src/main/java/com/tangem/presentation/activity/SendTransactionActivity.kt index 00d5c54613..039624962b 100644 --- a/app/src/main/java/com/tangem/presentation/activity/SendTransactionActivity.kt +++ b/app/src/main/java/com/tangem/presentation/activity/SendTransactionActivity.kt @@ -3,9 +3,12 @@ package com.tangem.presentation.activity import android.content.Intent import android.os.Bundle import android.support.v7.app.AppCompatActivity +import android.util.Log import android.view.KeyEvent import android.widget.ProgressBar import android.widget.Toast +import com.tangem.data.network.ServerApiHelper +import com.tangem.data.network.model.InfuraResponse import com.tangem.data.network.request.ElectrumRequest import com.tangem.data.network.request.InfuraRequest @@ -13,9 +16,11 @@ import com.tangem.data.network.task.send_transaction.ConnectTask import com.tangem.data.network.task.send_transaction.ETHRequestTask import com.tangem.domain.wallet.Blockchain import com.tangem.domain.wallet.CoinEngineFactory +import com.tangem.domain.wallet.LastSignStorage import com.tangem.domain.wallet.TangemCard -import com.tangem.presentation.fragment.LoadedWallet import com.tangem.wallet.R +import org.json.JSONException +import java.math.BigInteger class SendTransactionActivity : AppCompatActivity() { @@ -25,9 +30,9 @@ class SendTransactionActivity : AppCompatActivity() { const val EXTRA_TX: String = "TX" } + private var serverApiHelper: ServerApiHelper? = null var card: TangemCard? = null private var tx: String? = null - private var progressBar: ProgressBar? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -35,7 +40,7 @@ class SendTransactionActivity : AppCompatActivity() { MainActivity.commonInit(applicationContext) - progressBar = findViewById(R.id.progressBar) + serverApiHelper = ServerApiHelper() val intent = intent card = TangemCard(getIntent().getStringExtra(EXTRA_UID)) @@ -44,11 +49,14 @@ class SendTransactionActivity : AppCompatActivity() { val engine = CoinEngineFactory.create(card!!.blockchain) if (card!!.blockchain == Blockchain.Ethereum || card!!.blockchain == Blockchain.EthereumTestNet || card!!.blockchain == Blockchain.Token) { - val task = ETHRequestTask(this@SendTransactionActivity, card!!.blockchain) - val req = InfuraRequest.SendTransaction(card!!.wallet, tx) - req.id = 67 - req.setBlockchain(card!!.blockchain) - task.execute(req) +// val task = ETHRequestTask(this@SendTransactionActivity, card!!.blockchain) +// val req = InfuraRequest.SendTransaction(card!!.wallet, tx) +// req.id = 67 +// req.setBlockchain(card!!.blockchain) +// task.execute(req) + + requestInfura(ServerApiHelper.INFURA_ETH_SEND_RAW_TRANSACTION, "") + } else if (card!!.blockchain == Blockchain.Bitcoin || card!!.blockchain == Blockchain.BitcoinTestNet) { val nodeAddress = engine!!.getNode(card) val nodePort = engine.getNodePort(card) @@ -60,6 +68,66 @@ class SendTransactionActivity : AppCompatActivity() { val connectTask = ConnectTask(this@SendTransactionActivity, nodeAddress, nodePort, 3) connectTask.execute(ElectrumRequest.broadcast(card!!.wallet, tx)) } + + // request eth sendRawTransaction + val infuraBodyListener: ServerApiHelper.InfuraBodyListener = object : ServerApiHelper.InfuraBodyListener { + override fun onInfuraSuccess(method: String, infuraResponse: InfuraResponse) { + when (method) { + ServerApiHelper.INFURA_ETH_SEND_RAW_TRANSACTION -> { + try { + var hashTX: String + try { + val tmp = infuraResponse.result + hashTX = tmp + } catch (e: JSONException) { +// val msg = request.getAnswer() +// val err = msg!!.getJSONObject("error") +// hashTX = err.getString("message") +// LastSignStorage.setLastMessage(card!!.wallet, hashTX) + return@onInfuraSuccess + } + + if (hashTX.startsWith("0x") || hashTX.startsWith("0X")) { + hashTX = hashTX.substring(2) + } + val bigInt = BigInteger(hashTX, 16) //TODO: очень плохой способ + LastSignStorage.setTxWasSend(card!!.wallet) + LastSignStorage.setLastMessage(card!!.wallet, "") + Log.e("TX_RESULT", hashTX) + + + val nonce = card!!.GetConfirmTXCount() + nonce.add(BigInteger.valueOf(1)) + card!!.setConfirmTXCount(nonce) + Log.e("TX_RESULT", hashTX) + + finishWithSuccess() + + } catch (e: Exception) { + e.printStackTrace() + } + } + + else -> { + + } + } + } + + override fun onInfuraFail(method: String, message: String) { + when (method) { + ServerApiHelper.INFURA_ETH_SEND_RAW_TRANSACTION -> { + finishWithError(message) + } + + else -> { + + } + } + } + } + + serverApiHelper!!.setInfuraResponse(infuraBodyListener) } override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean { @@ -72,6 +140,10 @@ class SendTransactionActivity : AppCompatActivity() { return super.onKeyDown(keyCode, event) } + private fun requestInfura(method: String, contract: String) { + serverApiHelper!!.infura(method, 67, card!!.wallet, contract, tx) + } + fun finishWithError(message: String) { val intent = Intent() intent.putExtra("message", String.format(getString(R.string.try_again_failed_to_send_transaction), message)) diff --git a/app/src/main/java/com/tangem/presentation/fragment/LoadedWallet.kt b/app/src/main/java/com/tangem/presentation/fragment/LoadedWallet.kt index 0dee0bbd92..11a17fe48a 100644 --- a/app/src/main/java/com/tangem/presentation/fragment/LoadedWallet.kt +++ b/app/src/main/java/com/tangem/presentation/fragment/LoadedWallet.kt @@ -23,6 +23,7 @@ import android.view.ViewGroup import android.widget.Toast import com.google.zxing.WriterException import com.tangem.data.network.ServerApiHelper +import com.tangem.data.network.model.InfuraResponse import com.tangem.data.network.request.ElectrumRequest import com.tangem.data.network.request.InfuraRequest import com.tangem.data.network.task.loaded_wallet.ETHRequestTask @@ -117,15 +118,6 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific btnExtract.backgroundTintList = inactiveColor -// updateViews() - -// if (!card!!.hasBalanceInfo()) { -// srlLoadedWallet!!.isRefreshing = true -// srlLoadedWallet!!.postDelayed({ refresh() }, 1000) -// } - -// repeatRefresh() - refresh() startVerify(lastTag) @@ -241,86 +233,102 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific // Log.i(TAG, "setRateInfoData $rate") } - // request eth get balance, eth get transaction count listener - serverApiHelper!!.setInfura { method, infuraResponse -> - if (method == ServerApiHelper.INFURA_ETH_GET_BALANCE) { - var balanceCap = infuraResponse.result - balanceCap = balanceCap.substring(2) - val l = BigInteger(balanceCap, 16) - val d = l.divide(BigInteger("1000000000000000000", 10)) - val balance = d.toLong() + // request eth get balance, eth get transaction count, eth call, eth sendRawTransaction listener + val infuraBodyListener: ServerApiHelper.InfuraBodyListener = object : ServerApiHelper.InfuraBodyListener { + override fun onInfuraSuccess(method: String, infuraResponse: InfuraResponse) { + when (method) { + ServerApiHelper.INFURA_ETH_GET_BALANCE -> { + var balanceCap = infuraResponse.result + balanceCap = balanceCap.substring(2) + val l = BigInteger(balanceCap, 16) + val d = l.divide(BigInteger("1000000000000000000", 10)) + val balance = d.toLong() - card!!.setBalanceConfirmed(balance) - card!!.balanceUnconfirmed = 0L - if (card!!.blockchain != Blockchain.Token) - card!!.decimalBalance = l.toString(10) - card!!.decimalBalanceAlter = l.toString(10) + card!!.setBalanceConfirmed(balance) + card!!.balanceUnconfirmed = 0L + if (card!!.blockchain != Blockchain.Token) + card!!.decimalBalance = l.toString(10) + card!!.decimalBalanceAlter = l.toString(10) -// Log.i("eth_get_balance", balanceCap) - } + Log.i("$TAG eth_get_balance", balanceCap) + } - if (method == ServerApiHelper.INFURA_ETH_GET_TRANSACTION_COUNT) { - var nonce = infuraResponse.result - nonce = nonce.substring(2) - val count = BigInteger(nonce, 16) - card!!.setConfirmTXCount(count) + ServerApiHelper.INFURA_ETH_GET_TRANSACTION_COUNT -> { + var nonce = infuraResponse.result + nonce = nonce.substring(2) + val count = BigInteger(nonce, 16) + card!!.setConfirmTXCount(count) -// Log.i("eth_getTransactionCount", nonce) - } + Log.i("$TAG eth_getTransCount", nonce) + } - if (method == ServerApiHelper.INFURA_ETH_CALL) { - var balanceCap = infuraResponse.result - balanceCap = balanceCap.substring(2) - val l = BigInteger(balanceCap, 16) - val balance = l.toLong() - if (l.compareTo(BigInteger.ZERO) == 0) { - card!!.blockchainID = Blockchain.Ethereum.id - card!!.addTokenToBlockchainName() - srlLoadedWallet!!.isRefreshing = false -// refresh() - return@setInfura - } - card!!.setBalanceConfirmed(balance) - card!!.balanceUnconfirmed = 0L - card!!.decimalBalance = l.toString(10) + ServerApiHelper.INFURA_ETH_CALL -> { + var balanceCap = infuraResponse.result + balanceCap = balanceCap.substring(2) + val l = BigInteger(balanceCap, 16) + val balance = l.toLong() + if (l.compareTo(BigInteger.ZERO) == 0) { + card!!.blockchainID = Blockchain.Ethereum.id + card!!.addTokenToBlockchainName() + srlLoadedWallet!!.isRefreshing = false +// refresh() + return@onInfuraSuccess + } + card!!.setBalanceConfirmed(balance) + card!!.balanceUnconfirmed = 0L + card!!.decimalBalance = l.toString(10) -// Log.i("eth_call", balanceCap) - } + Log.i("$TAG eth_call", balanceCap) + } - if (method == ServerApiHelper.INFURA_ETH_SEND_RAW_TRANSACTION) { -// try { -// var hashTX: String -// try { -// val tmp = request.getResultString() -// hashTX = tmp -// } catch (e: JSONException) { + ServerApiHelper.INFURA_ETH_SEND_RAW_TRANSACTION -> { + try { + var hashTX: String + try { + val tmp = infuraResponse.result + hashTX = tmp + } catch (e: JSONException) { // val msg = request.getAnswer() // val err = msg!!.getJSONObject("error") // hashTX = err.getString("message") // LastSignStorage.setLastMessage(card!!.wallet, hashTX) -// return@setInfura -// } -// -// if (hashTX.startsWith("0x") || hashTX.startsWith("0X")) { -// hashTX = hashTX.substring(2) -// } -// LastSignStorage.setTxWasSend(card!!.wallet) -// LastSignStorage.setLastMessage(card!!.wallet, "") -// Log.e("TX_RESULT", hashTX) -// -// -// val nonce = card!!.GetConfirmTXCount() -// nonce.add(BigInteger.valueOf(1)) -// card!!.setConfirmTXCount(nonce) -// Log.e("TX_RESULT", hashTX) -// -// } catch (e: Exception) { -// e.printStackTrace() -// } + return@onInfuraSuccess + } + + if (hashTX.startsWith("0x") || hashTX.startsWith("0X")) { + hashTX = hashTX.substring(2) + } + val bigInt = BigInteger(hashTX, 16) //TODO: очень плохой способ + LastSignStorage.setTxWasSend(card!!.wallet) + LastSignStorage.setLastMessage(card!!.wallet, "") + + Log.e("$TAG TX_RESULT", hashTX) + + val nonce = card!!.GetConfirmTXCount() + nonce.add(BigInteger.valueOf(1)) + card!!.setConfirmTXCount(nonce) + + Log.e("$TAG TX_RESULT", hashTX) + + } catch (e: Exception) { + e.printStackTrace() + } + } + + else -> { + + } + } + + updateViews() } - updateViews() + override fun onInfuraFail(method: String, message: String) { + + } } + + serverApiHelper!!.setInfuraResponse(infuraBodyListener) } private fun requestInfura(method: String, contract: String) { @@ -861,13 +869,13 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific private fun sendTransaction(tx: String) { val engine = CoinEngineFactory.create(card!!.blockchain) if (card!!.blockchain == Blockchain.Ethereum || card!!.blockchain == Blockchain.EthereumTestNet || card!!.blockchain == Blockchain.Token) { - val task = ETHRequestTask(this@LoadedWallet, card!!.blockchain) - val req = InfuraRequest.SendTransaction(card!!.wallet, tx) - req.id = 67 - req.setBlockchain(card!!.blockchain) - task.execute(req) +// val task = ETHRequestTask(this@LoadedWallet, card!!.blockchain) +// val req = InfuraRequest.SendTransaction(card!!.wallet, tx) +// req.id = 67 +// req.setBlockchain(card!!.blockchain) +// task.execute(req) -// requestInfura(ServerApiHelper.INFURA_ETH_SEND_RAW_TRANSACTION, "") + requestInfura(ServerApiHelper.INFURA_ETH_SEND_RAW_TRANSACTION, "") } else if (card!!.blockchain == Blockchain.Bitcoin || card!!.blockchain == Blockchain.BitcoinTestNet) { val nodeAddress = engine!!.getNode(card)