Updated on 2026-08-14

This commit is contained in:
Tangem 2018-09-07 14:29:22 +03:00
commit 12583818ec
13 changed files with 125 additions and 84 deletions

View file

@ -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<InfuraEthGasPriceResponse> ethGasPrice(@Body InfuraEthGasPriceBody body);
Call<InfuraResponse> infura(@Body InfuraBody body);
}

View file

@ -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;
@ -110,19 +110,24 @@ public class ServerApiHelper {
/**
* HTTP
* InfuraEthGasPrice
* Infura
* <p>
* 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())
@ -130,23 +135,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<InfuraEthGasPriceResponse> call = infuraApi.ethGasPrice(infuraEthGasPriceBody);
case INFURA_ETH_GAS_PRICE:
infuraBody = new InfuraBody(method, id);
break;
call.enqueue(new Callback<InfuraEthGasPriceResponse>() {
default:
infuraBody = new InfuraBody();
}
Call<InfuraResponse> call = infuraApi.infura(infuraBody);
call.enqueue(new Callback<InfuraResponse>() {
@Override
public void onResponse(@NonNull Call<InfuraEthGasPriceResponse> call, @NonNull Response<InfuraEthGasPriceResponse> response) {
public void onResponse(@NonNull Call<InfuraResponse> call, @NonNull Response<InfuraResponse> 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<InfuraEthGasPriceResponse> call, @NonNull Throwable t) {
Log.e(TAG, "infuraEthGasPrice onFailure " + t.getMessage());
public void onFailure(@NonNull Call<InfuraResponse> call, @NonNull Throwable t) {
Log.e(TAG, "infura " + method + " onFailure " + t.getMessage());
}
});
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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 = "",

View file

@ -1514,17 +1514,19 @@ public class TangemCard {
case "0004":
case "0006":
case "0010":
return R.drawable.card_btc001;
return R.drawable.card_btc_001;
case "0005":
case "0007":
case "0011":
return R.drawable.card_btc005;
return R.drawable.card_btc_005;
case "0012":
return R.drawable.card_seed;
case "0013":
return R.drawable.card_btc_hk_s;
case "0014":
return R.drawable.card_btc_0014;
case "0015":
return R.drawable.card_btc000;
return R.drawable.card_btc_000;
case "0016":
return R.drawable.card_eth000;
case "0017":

View file

@ -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

View file

@ -40,7 +40,9 @@ 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.*
import kotlin.concurrent.fixedRateTimer
class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notifications, SharedPreferences.OnSharedPreferenceChangeListener {
companion object {
@ -73,7 +75,7 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific
private val inactiveColor: ColorStateList by lazy { resources.getColorStateList(R.color.primary) }
private val activeColor: ColorStateList by lazy { resources.getColorStateList(R.color.colorAccent) }
private val timerRepeatRefresh = Timer()
private var timerRepeatRefresh: Timer? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -237,6 +239,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() {
@ -253,27 +267,11 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific
nfcManager!!.onResume()
}
private fun repeatRefresh() {
val getBalance = object : TimerTask() {
override fun run() {
activity!!.runOnUiThread {
refresh()
// Log.i("efgrgegsdfgsd", "repeatRefresh")
}
}
}
timerRepeatRefresh.schedule(getBalance, 0, 5000)
}
override fun onPause() {
super.onPause()
nfcManager!!.onPause()
try {
timerRepeatRefresh.cancel()
} catch (e: IllegalStateException) {
e.printStackTrace()
}
if (timerRepeatRefresh != null)
timerRepeatRefresh!!.cancel()
}
override fun onStop() {
@ -421,8 +419,15 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific
if (resultCode == Activity.RESULT_OK) {
// srlLoadedWallet!!.postDelayed({ refresh() }, 10000)
// srlLoadedWallet!!.isRefreshing = true
timerRepeatRefresh = fixedRateTimer(name = "refresh", initialDelay = 2000, period = 5000) {
activity!!.runOnUiThread {
refresh()
Log.i("sfsf", "refresh")
}
}
card!!.clearInfo()
repeatRefresh()
// updateViews()
}
@ -613,9 +618,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 +660,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 +698,10 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific
if (needResendTX)
sendTransaction(LastSignStorage.getTxForSend(card!!.wallet))
// requestInfura(ServerApiHelper.INFURA_ETH_GET_BALANCE)
}
fun prepareResultIntent(): Intent {