From 344444264509a00b2e8975d7c1419dc40e969c1e Mon Sep 17 00:00:00 2001 From: Tangem Date: Thu, 6 Sep 2018 18:34:50 +0300 Subject: [PATCH] Updated on 2026-08-14 --- app/build.gradle | 2 +- .../com/tangem/data/network/InfuraApi.java | 7 ++- .../tangem/data/network/ServerApiHelper.java | 53 ++++++++++++------- .../tangem/data/network/model/InfuraBody.java | 21 ++++++++ .../network/model/InfuraEthGasPriceBody.java | 12 ----- ...hGasPriceResponse.kt => InfuraResponse.kt} | 2 +- .../activity/ConfirmPaymentActivity.kt | 44 +++++++-------- .../presentation/fragment/LoadedWallet.kt | 26 +++++++-- 8 files changed, 107 insertions(+), 60 deletions(-) create mode 100644 app/src/main/java/com/tangem/data/network/model/InfuraBody.java delete mode 100644 app/src/main/java/com/tangem/data/network/model/InfuraEthGasPriceBody.java rename app/src/main/java/com/tangem/data/network/model/{InfuraEthGasPriceResponse.kt => InfuraResponse.kt} (88%) diff --git a/app/build.gradle b/app/build.gradle index 8b9e416b5c..bd7ec6e06e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -47,7 +47,7 @@ dependencies { implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support:design:27.1.1' implementation 'com.android.support:support-compat:27.1.1' - implementation 'com.android.support.constraint:constraint-layout:1.1.2' + implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.android.support:cardview-v7:27.1.1' implementation 'com.android.support:support-v4:27.1.1' implementation 'com.google.zxing:core:3.3.2' diff --git a/app/src/main/java/com/tangem/data/network/InfuraApi.java b/app/src/main/java/com/tangem/data/network/InfuraApi.java index 7cba8d3ff4..2ad1802e25 100644 --- a/app/src/main/java/com/tangem/data/network/InfuraApi.java +++ b/app/src/main/java/com/tangem/data/network/InfuraApi.java @@ -1,16 +1,15 @@ package com.tangem.data.network; -import com.tangem.data.network.model.InfuraEthGasPriceBody; -import com.tangem.data.network.model.InfuraEthGasPriceResponse; +import com.tangem.data.network.model.InfuraBody; +import com.tangem.data.network.model.InfuraResponse; import retrofit2.Call; import retrofit2.http.Body; -import retrofit2.http.Header; import retrofit2.http.Headers; import retrofit2.http.POST; public interface InfuraApi { @Headers("Content-Type: application/json") @POST(Server.ApiInfura.Method.MAIN) - Call ethGasPrice(@Body InfuraEthGasPriceBody body); + Call infura(@Body InfuraBody body); } \ No newline at end of file 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 773fe789fc..2524307b69 100644 --- a/app/src/main/java/com/tangem/data/network/ServerApiHelper.java +++ b/app/src/main/java/com/tangem/data/network/ServerApiHelper.java @@ -8,8 +8,8 @@ import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; import com.tangem.data.network.model.CardVerify; import com.tangem.data.network.model.CardVerifyBody; import com.tangem.data.network.model.CardVerifyResponse; -import com.tangem.data.network.model.InfuraEthGasPriceBody; -import com.tangem.data.network.model.InfuraEthGasPriceResponse; +import com.tangem.data.network.model.InfuraBody; +import com.tangem.data.network.model.InfuraResponse; import com.tangem.data.network.model.RateInfoResponse; import com.tangem.data.network.request.ElectrumRequest; import com.tangem.domain.BitcoinNode; @@ -106,19 +106,24 @@ public class ServerApiHelper { /** * HTTP - * InfuraEthGasPrice + * Infura + *

+ * eth_getBalance + * eth_gasPrice */ - private InfuraEthGasPriceBodyListener infuraEthGasPriceBodyListener; + public static final String INFURA_ETH_GET_BALANCE = "eth_getBalance"; + public static final String INFURA_ETH_GAS_PRICE = "eth_gasPrice"; + private InfuraBodyListener infuraBodyListener; - public interface InfuraEthGasPriceBodyListener { - void onInfuraEthGasPrice(InfuraEthGasPriceResponse cardVerifyResponse); + public interface InfuraBodyListener { + void onInfura(String method, InfuraResponse infuraResponse); } - public void setInfuraEthGasPrice(InfuraEthGasPriceBodyListener listener) { - infuraEthGasPriceBodyListener = listener; + public void setInfura(InfuraBodyListener listener) { + infuraBodyListener = listener; } - public void infuraEthGasPrice(String method, int id) { + public void infura(String method, int id, String wallet) { Retrofit retrofit = new Retrofit.Builder() .baseUrl(Server.ApiInfura.URL_INFURA) .addConverterFactory(GsonConverterFactory.create()) @@ -126,23 +131,35 @@ public class ServerApiHelper { InfuraApi infuraApi = retrofit.create(InfuraApi.class); - InfuraEthGasPriceBody infuraEthGasPriceBody = new InfuraEthGasPriceBody(method, id); + InfuraBody infuraBody; + switch (method) { + case INFURA_ETH_GET_BALANCE: + infuraBody = new InfuraBody(method, new String[]{wallet, "latest"}, id); + break; - Call call = infuraApi.ethGasPrice(infuraEthGasPriceBody); + case INFURA_ETH_GAS_PRICE: + infuraBody = new InfuraBody(method, id); + break; - call.enqueue(new Callback() { + default: + infuraBody = new InfuraBody(); + } + + Call call = infuraApi.infura(infuraBody); + + call.enqueue(new Callback() { @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { + public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.code() == 200) { - infuraEthGasPriceBodyListener.onInfuraEthGasPrice(response.body()); - Log.i(TAG, "infuraEthGasPrice onResponse " + response.code()); + infuraBodyListener.onInfura(method, response.body()); + Log.i(TAG, "infura " + method + " onResponse " + response.code()); } else - Log.e(TAG, "infuraEthGasPrice onResponse " + response.code()); + Log.e(TAG, "infura " + method + " onResponse " + response.code()); } @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - Log.e(TAG, "infuraEthGasPrice onFailure " + t.getMessage()); + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + Log.e(TAG, "infura " + method + " onFailure " + t.getMessage()); } }); } diff --git a/app/src/main/java/com/tangem/data/network/model/InfuraBody.java b/app/src/main/java/com/tangem/data/network/model/InfuraBody.java new file mode 100644 index 0000000000..6fcabf6992 --- /dev/null +++ b/app/src/main/java/com/tangem/data/network/model/InfuraBody.java @@ -0,0 +1,21 @@ +package com.tangem.data.network.model; + +public class InfuraBody { + private String method; + private String[] params; + private int id; + + public InfuraBody() { + } + + public InfuraBody(String method, int id) { + this.method = method; + this.id = id; + } + + public InfuraBody(String method, String[] params, int id) { + this.method = method; + this.params = params; + this.id = id; + } +} \ No newline at end of file diff --git a/app/src/main/java/com/tangem/data/network/model/InfuraEthGasPriceBody.java b/app/src/main/java/com/tangem/data/network/model/InfuraEthGasPriceBody.java deleted file mode 100644 index fa60a8ace5..0000000000 --- a/app/src/main/java/com/tangem/data/network/model/InfuraEthGasPriceBody.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.tangem.data.network.model; - -public class InfuraEthGasPriceBody { - private String method; - private String[] params; - private int id; - - public InfuraEthGasPriceBody(String method, int id) { - this.method = method; - this.id = id; - } -} \ No newline at end of file diff --git a/app/src/main/java/com/tangem/data/network/model/InfuraEthGasPriceResponse.kt b/app/src/main/java/com/tangem/data/network/model/InfuraResponse.kt similarity index 88% rename from app/src/main/java/com/tangem/data/network/model/InfuraEthGasPriceResponse.kt rename to app/src/main/java/com/tangem/data/network/model/InfuraResponse.kt index ea9d7c11a2..dd6618ab3c 100644 --- a/app/src/main/java/com/tangem/data/network/model/InfuraEthGasPriceResponse.kt +++ b/app/src/main/java/com/tangem/data/network/model/InfuraResponse.kt @@ -2,7 +2,7 @@ package com.tangem.data.network.model import com.google.gson.annotations.SerializedName -data class InfuraEthGasPriceResponse( +data class InfuraResponse( @SerializedName("jsonrpc") var jsonrpc: String = "", 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 7aee4cb2c4..be954ee7f1 100644 --- a/app/src/main/java/com/tangem/presentation/activity/ConfirmPaymentActivity.kt +++ b/app/src/main/java/com/tangem/presentation/activity/ConfirmPaymentActivity.kt @@ -105,7 +105,7 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback { if (card!!.blockchain == Blockchain.Ethereum || card!!.blockchain == Blockchain.EthereumTestNet || card!!.blockchain == Blockchain.Token) { rgFee!!.isEnabled = false - serverApiHelper!!.infuraEthGasPrice("eth_gasPrice", 67) + serverApiHelper!!.infura(ServerApiHelper.INFURA_ETH_GAS_PRICE, 67, card!!.wallet) } else { rgFee!!.isEnabled = true @@ -223,29 +223,31 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback { } // request eth gas price listener - serverApiHelper!!.setInfuraEthGasPrice { - var gasPrice = it.result - gasPrice = gasPrice.substring(2) - var l = BigInteger(gasPrice, 16) + 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()) + 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) + 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", feeInGwei) +// Log.i("eth_gas_price", gasPrice) + } } // request estimate fee listener 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 abf2c374d6..5f195fffe5 100644 --- a/app/src/main/java/com/tangem/presentation/fragment/LoadedWallet.kt +++ b/app/src/main/java/com/tangem/presentation/fragment/LoadedWallet.kt @@ -40,6 +40,7 @@ import com.tangem.util.UtilHelper import com.tangem.wallet.BuildConfig import com.tangem.wallet.R import kotlinx.android.synthetic.main.fr_loaded_wallet.* +import java.math.BigInteger import java.util.* class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notifications, SharedPreferences.OnSharedPreferenceChangeListener { @@ -237,6 +238,18 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific // Log.i(TAG, "setRateInfoData $rate") } + + // request eth get balance listener + serverApiHelper!!.setInfura { method, infuraResponse -> + if (method == ServerApiHelper.INFURA_ETH_GET_BALANCE) { + +// Log.i("eth_get_balance", gasPrice) + } + } + } + + private fun requestInfura(method: String) { + serverApiHelper!!.infura(method, 67, card!!.wallet) } private fun requestCardVerify() { @@ -613,9 +626,9 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific val nodeAddress = engine!!.getNextNode(card) val nodePort = engine.getNextNodePort(card) - Log.i(TAG, nodeAddress) - Log.i(TAG, nodePort.toString()) - Log.i(TAG, i.toString()) +// Log.i(TAG, nodeAddress) +// Log.i(TAG, nodePort.toString()) +// Log.i(TAG, i.toString()) // check balance val connectTaskEx = UpdateWalletInfoTask(this@LoadedWallet, nodeAddress, nodePort, data) @@ -655,6 +668,9 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific // Ethereum else if (card!!.blockchain == Blockchain.Ethereum || card!!.blockchain == Blockchain.EthereumTestNet) { + +// requestInfura(ServerApiHelper.INFURA_ETH_GET_BALANCE) + val updateETH = ETHRequestTask(this@LoadedWallet, card!!.blockchain) val reqETH = InfuraRequest.GetBalance(card!!.wallet) reqETH.id = 67 @@ -690,6 +706,10 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific if (needResendTX) sendTransaction(LastSignStorage.getTxForSend(card!!.wallet)) + +// requestInfura(ServerApiHelper.INFURA_ETH_GET_BALANCE) + + } fun prepareResultIntent(): Intent {