From f47fe3b993142cbed6a2b260db8724eebff2f2dd Mon Sep 17 00:00:00 2001 From: Tangem Date: Mon, 3 Sep 2018 01:20:58 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../com/tangem/data/network/InfuraApi.java | 14 ++++++ .../java/com/tangem/data/network/Server.java | 12 +++++ .../tangem/data/network/ServerApiHelper.java | 50 +++++++++++++++++-- .../com/tangem/data/network/ServerURL.java | 1 + .../network/model/InfuraEthGasPriceBody.java | 12 +++++ .../model/InfuraEthGasPriceResponse.kt | 14 ++++++ .../data/network/request/InfuraRequest.java | 28 +++++------ .../presentation/fragment/LoadedWallet.kt | 2 + 8 files changed, 114 insertions(+), 19 deletions(-) create mode 100644 app/src/main/java/com/tangem/data/network/InfuraApi.java create mode 100644 app/src/main/java/com/tangem/data/network/model/InfuraEthGasPriceBody.java create mode 100644 app/src/main/java/com/tangem/data/network/model/InfuraEthGasPriceResponse.kt diff --git a/app/src/main/java/com/tangem/data/network/InfuraApi.java b/app/src/main/java/com/tangem/data/network/InfuraApi.java new file mode 100644 index 0000000000..477ce87ba1 --- /dev/null +++ b/app/src/main/java/com/tangem/data/network/InfuraApi.java @@ -0,0 +1,14 @@ +package com.tangem.data.network; + +import com.tangem.data.network.model.InfuraEthGasPriceBody; +import com.tangem.data.network.model.InfuraEthGasPriceResponse; + +import retrofit2.Call; +import retrofit2.http.Body; +import retrofit2.http.Header; +import retrofit2.http.POST; + +public interface InfuraApi { + @POST(Server.ApiInfura.Method.MAIN) + Call ethGasPrice(@Header("Content-Type") String contentType, @Body InfuraEthGasPriceBody body); +} \ No newline at end of file diff --git a/app/src/main/java/com/tangem/data/network/Server.java b/app/src/main/java/com/tangem/data/network/Server.java index 685540401c..ff6ad5298f 100644 --- a/app/src/main/java/com/tangem/data/network/Server.java +++ b/app/src/main/java/com/tangem/data/network/Server.java @@ -25,4 +25,16 @@ public class Server { } } + /** + * https://infura.io/ + */ + public static class ApiInfura { + public static final String URL_INFURA = ServerURL.API_INFURA; + + public static class Method { + public static final String MAIN = URL_INFURA + "AfWg0tmYEX5Kukn2UkKV"; + } + + } + } \ 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 993ccbeda6..115fc5b460 100644 --- a/app/src/main/java/com/tangem/data/network/ServerApiHelper.java +++ b/app/src/main/java/com/tangem/data/network/ServerApiHelper.java @@ -11,6 +11,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.RateInfoResponse; import com.tangem.data.network.request.ElectrumRequest; import com.tangem.domain.BitcoinNode; @@ -48,6 +50,49 @@ import retrofit2.converter.gson.GsonConverterFactory; public class ServerApiHelper { private static String TAG = ServerApiHelper.class.getSimpleName(); + /** + * HTTP + * Infura + */ + private InfuraListener infuraListener; + + public interface InfuraListener { + void onInfura(CardVerifyResponse cardVerifyResponse); + } + + public void setInfura(InfuraListener listener) { + infuraListener = listener; + } + + public void infura(String method, int id) { + Retrofit retrofit = new Retrofit.Builder() + .baseUrl(Server.ApiInfura.URL_INFURA) + .addConverterFactory(GsonConverterFactory.create()) + .build(); + + InfuraApi infuraApi = retrofit.create(InfuraApi.class); + + InfuraEthGasPriceBody infuraEthGasPriceBody = new InfuraEthGasPriceBody(method, id); + + Call call = infuraApi.ethGasPrice("application/json", infuraEthGasPriceBody); + + call.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.code() == 200) { + Log.i(TAG, "infura onResponse " + response.code()); + Log.i(TAG, "infura onResponse " + response.body().getResult()); + } else + Log.e(TAG, "infura onResponse " + response.code()); + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + Log.e(TAG, "infura onFailure " + t.getMessage()); + } + }); + } + /** * HTTP * Card verify @@ -83,13 +128,12 @@ public class ServerApiHelper { CardVerifyResponse cardVerifyResponse = response.body(); cardVerifyListener.onCardVerify(cardVerifyResponse); Log.i(TAG, "cardVerify onResponse " + response.code()); - } else { + } else Log.e(TAG, "cardVerify onResponse " + response.code()); - } } @Override - public void onFailure(Call call, Throwable t) { + public void onFailure(@NonNull Call call, @NonNull Throwable t) { Log.e(TAG, "cardVerify onFailure " + t.getMessage()); } }); diff --git a/app/src/main/java/com/tangem/data/network/ServerURL.java b/app/src/main/java/com/tangem/data/network/ServerURL.java index b6394e5abc..9cb0569b6c 100644 --- a/app/src/main/java/com/tangem/data/network/ServerURL.java +++ b/app/src/main/java/com/tangem/data/network/ServerURL.java @@ -3,4 +3,5 @@ package com.tangem.data.network; public class ServerURL { public static final String API_TANGEM = "https://verify.tangem.com/"; public static final String API_COINMARKET = "https://api.coinmarketcap.com/"; + public static final String API_INFURA = "https://mainnet.infura.io/"; } \ 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 new file mode 100644 index 0000000000..fa60a8ace5 --- /dev/null +++ b/app/src/main/java/com/tangem/data/network/model/InfuraEthGasPriceBody.java @@ -0,0 +1,12 @@ +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/InfuraEthGasPriceResponse.kt new file mode 100644 index 0000000000..ea9d7c11a2 --- /dev/null +++ b/app/src/main/java/com/tangem/data/network/model/InfuraEthGasPriceResponse.kt @@ -0,0 +1,14 @@ +package com.tangem.data.network.model + +import com.google.gson.annotations.SerializedName + +data class InfuraEthGasPriceResponse( + @SerializedName("jsonrpc") + var jsonrpc: String = "", + + @SerializedName("id") + var id: Int? = null, + + @SerializedName("result") + var result: String = "" +) \ No newline at end of file diff --git a/app/src/main/java/com/tangem/data/network/request/InfuraRequest.java b/app/src/main/java/com/tangem/data/network/request/InfuraRequest.java index 4b9516d765..053b02118e 100644 --- a/app/src/main/java/com/tangem/data/network/request/InfuraRequest.java +++ b/app/src/main/java/com/tangem/data/network/request/InfuraRequest.java @@ -14,8 +14,8 @@ public class InfuraRequest { public static final String METHOD_ETH_GetBalance = "eth_getBalance"; public static final String METHOD_ETH_GetOutTransactionCount = "eth_getTransactionCount"; public static final String METHOD_ETH_GetGasPrice = "eth_gasPrice"; - public static final String METHOD_ETH_SendRawTransaction = "eth_sendRawTransaction"; - public static final String METHOD_ETH_Call = "eth_call"; + public static final String METHOD_ETH_SendRawTransaction = "eth_sendRawTransaction"; + public static final String METHOD_ETH_Call = "eth_call"; public JSONObject jsRequestData; public String answerData; @@ -29,8 +29,7 @@ public class InfuraRequest { blockchain = value; } - public Blockchain getBlockchain() - { + public Blockchain getBlockchain() { return blockchain; } @@ -86,7 +85,7 @@ public class InfuraRequest { public static InfuraRequest GetBalance(String wallet) { InfuraRequest request = new InfuraRequest(); try { - request.WalletAddress=wallet; + request.WalletAddress = wallet; request.jsRequestData = new JSONObject("{ \"method\":\"" + METHOD_ETH_GetBalance + "\", \"params\":[\"" + wallet + "\", \"latest\"] }"); } catch (JSONException e) { e.printStackTrace(); @@ -95,15 +94,14 @@ public class InfuraRequest { return request; } - public static InfuraRequest GetTokenBalance(String wallet, String contract, int dec) - { + public static InfuraRequest GetTokenBalance(String wallet, String contract, int dec) { InfuraRequest request = new InfuraRequest(); try { - request.WalletAddress=wallet; + request.WalletAddress = wallet; request.Dec = dec; String address = wallet.substring(2); String dataValue = String.format("{\"data\": \"0x70a08231000000000000000000000000%s\", \"to\": \"%s\"}", address, contract); - request.jsRequestData = new JSONObject("{ \"method\":\"" + METHOD_ETH_Call + "\", \"params\":[" +dataValue+", \"latest\"] }"); + request.jsRequestData = new JSONObject("{ \"method\":\"" + METHOD_ETH_Call + "\", \"params\":[" + dataValue + ", \"latest\"] }"); } catch (JSONException e) { e.printStackTrace(); request.error = e.toString(); @@ -114,7 +112,7 @@ public class InfuraRequest { public static InfuraRequest SendTransaction(String wallet, String tx) { InfuraRequest request = new InfuraRequest(); try { - request.WalletAddress=wallet; + request.WalletAddress = wallet; request.jsRequestData = new JSONObject("{ \"method\":\"" + METHOD_ETH_SendRawTransaction + "\", \"params\":[\"" + tx + "\"] }"); } catch (JSONException e) { e.printStackTrace(); @@ -124,11 +122,10 @@ public class InfuraRequest { } - public static InfuraRequest GetOutTransactionCount(String wallet) { InfuraRequest request = new InfuraRequest(); try { - request.WalletAddress=wallet; + request.WalletAddress = wallet; request.jsRequestData = new JSONObject("{ \"method\":\"" + METHOD_ETH_GetOutTransactionCount + "\", \"params\":[\"" + wallet + "\", \"latest\"] }"); } catch (JSONException e) { e.printStackTrace(); @@ -140,7 +137,7 @@ public class InfuraRequest { public static InfuraRequest GetPendingTransactionCount(String wallet) { InfuraRequest request = new InfuraRequest(); try { - request.WalletAddress=wallet; + request.WalletAddress = wallet; request.jsRequestData = new JSONObject("{ \"method\":\"" + METHOD_ETH_GetOutTransactionCount + "\", \"params\":[\"" + wallet + "\", \"pending\"] }"); } catch (JSONException e) { e.printStackTrace(); @@ -152,7 +149,7 @@ public class InfuraRequest { public static InfuraRequest GetGasPrise(String wallet) { InfuraRequest request = new InfuraRequest(); try { - request.WalletAddress=wallet; + request.WalletAddress = wallet; request.jsRequestData = new JSONObject("{ \"method\":\"" + METHOD_ETH_GetGasPrice + "\", \"params\":[] }"); } catch (JSONException e) { e.printStackTrace(); @@ -165,7 +162,7 @@ public class InfuraRequest { public static InfuraRequest SendTransactionCount(String wallet, String TX) { InfuraRequest request = new InfuraRequest(); try { - request.WalletAddress=wallet; + request.WalletAddress = wallet; request.jsRequestData = new JSONObject("{ \"method\":\"" + METHOD_ETH_SendRawTransaction + "\", \"params\":[\"" + TX + "\"] }"); } catch (JSONException e) { e.printStackTrace(); @@ -175,7 +172,6 @@ public class InfuraRequest { } - //METHOD_ETH_GetOutTransactionCount 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 0e640cd14d..e510e0600a 100644 --- a/app/src/main/java/com/tangem/presentation/fragment/LoadedWallet.kt +++ b/app/src/main/java/com/tangem/presentation/fragment/LoadedWallet.kt @@ -173,6 +173,8 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific } } + serverApiHelper!!.infura("eth_gasPrice", 67) + // request card verify listener serverApiHelper!!.setCardVerify { card!!.isOnlineVerified = it.results!![0].passed