Updated on 2026-08-14

This commit is contained in:
Tangem 2018-12-26 18:08:15 +03:00
parent de62d5cd86
commit f6186a521d
10 changed files with 175 additions and 1 deletions

View file

@ -0,0 +1,30 @@
package com.tangem.data.network;
import com.tangem.data.network.model.InsightBody;
import com.tangem.data.network.model.InsightResponse;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.Headers;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.Query;
public interface InsightApi {
@GET(ServerApiInsight.INSIGHT_ADDRESS)
Call<InsightResponse> insightAddress(@Path("address") String address);
@GET(ServerApiInsight.INSIGHT_UNSPENT_OUTPUTS)
Call<InsightResponse> insightUnspent(@Path("address") String address);
@GET(ServerApiInsight.INSIGHT_TRANSACTION)
Call<InsightResponse> insightTransaction(@Path("transaction") String transaction);
@GET(ServerApiInsight.INSIGHT_FEE)
Call<InsightResponse> insightFee(@Query("nbBlocks") int nbBlocks);
@Headers("Content-Type: application/json")
@POST(ServerApiInsight.INSIGHT_SEND)
Call<InsightResponse> insightSend(@Body InsightBody body );
}

View file

@ -44,5 +44,4 @@ public class Server {
static final String N_6 = URL_ESTIMATEFEE + "n/6";
}
}
}

View file

@ -0,0 +1,102 @@
package com.tangem.data.network;
import android.support.annotation.NonNull;
import android.util.Log;
import com.tangem.App;
import com.tangem.data.network.model.InsightBody;
import com.tangem.data.network.model.InsightResponse;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class ServerApiInsight {
private static String TAG = ServerApiInsight.class.getSimpleName();
public static final String INSIGHT_ADDRESS = "/addr/{address}";
public static final String INSIGHT_UNSPENT_OUTPUTS = "/addr/{address}/utxo";
public static final String INSIGHT_TRANSACTION = "/tx/{transaction}";
public static final String INSIGHT_FEE = "/utils/estimatefee";
public static final String INSIGHT_SEND = "/tx/send";
private int requestsCount=0;
public boolean isRequestsSequenceCompleted() {
Log.i(TAG, String.format("isRequestsSequenceCompleted: %s (%d requests left)", String.valueOf(requestsCount <= 0), requestsCount));
return requestsCount <= 0;
}
private InsightBodyListener insightBodyListener;
public interface InsightBodyListener {
void onSuccess(String method, InsightResponse insightResponse);
void onFail(String method, String message);
}
public void setInsightResponse(InsightBodyListener listener) {
insightBodyListener = listener;
}
public void insight(String method, String wallet, String tx, int nbBlocks) {
requestsCount++;
String insightURL = "http://130.185.109.17:3001/insight-api"; //TODO: make random selection
Retrofit retrofitInsight = new Retrofit.Builder() //TODO: check
.baseUrl(insightURL)
.addConverterFactory(GsonConverterFactory.create())
.build();
// InsightApi insightApi = App.getNetworkComponent().getRetrofitInsight(insightURL).create(InsightApi.class);
InsightApi insightApi = retrofitInsight.create(InsightApi.class);
Call<InsightResponse> call;
switch (method) {
case INSIGHT_ADDRESS:
call = insightApi.insightAddress(wallet);
break;
case INSIGHT_UNSPENT_OUTPUTS:
call = insightApi.insightUnspent(wallet);
break;
case INSIGHT_TRANSACTION:
call = insightApi.insightTransaction(tx);
break;
case INSIGHT_FEE:
call = insightApi.insightFee(nbBlocks);
break;
case INSIGHT_SEND:
call = insightApi.insightSend(new InsightBody(tx));
break;
default:
call = insightApi.insightAddress(wallet);
break;
}
call.enqueue(new Callback<InsightResponse>() {
@Override
public void onResponse(@NonNull Call<InsightResponse> call, @NonNull Response<InsightResponse> response) {
if (response.code() == 200) {
requestsCount--;
insightBodyListener.onSuccess(method, response.body());
Log.i(TAG, "insight " + method + " onResponse " + response.code());
} else {
insightBodyListener.onFail(method, String.valueOf(response.code()));
Log.e(TAG, "insight " + method + " onResponse " + response.code());
}
}
@Override
public void onFailure(@NonNull Call<InsightResponse> call, @NonNull Throwable t) {
insightBodyListener.onFail(method, String.valueOf(t.getMessage()));
Log.e(TAG, "insight " + method + " onFailure " + t.getMessage());
}
});
}
}

View file

@ -0,0 +1,9 @@
package com.tangem.data.network.model;
public class InsightBody {
private String rawtx;
public InsightBody(String rawtx){
this.rawtx = rawtx;
}
}

View file

@ -0,0 +1,17 @@
package com.tangem.data.network.model
import com.google.gson.annotations.SerializedName
data class InsightResponse(
@SerializedName("jsonrpc")
var jsonrpc: String = "",
@SerializedName("id")
var id: Int? = null,
@SerializedName("result")
var result: String = "",
@SerializedName("error")
var error: String = ""
)

View file

@ -26,6 +26,9 @@ public interface NetworkComponent {
@Named(Server.ApiUpdateVersion.URL_UPDATE_VERSION)
Retrofit getRetrofitGithubusercontent();
// @Named("Insight") //TODO:check
// Retrofit getRetrofitInsight(String insightURL);
@Named("socket")
Socket getSocket();

View file

@ -31,6 +31,16 @@ class NetworkModule {
.build();
}
// //@Singleton // TODO:check
// @Provides
// @Named("Insight")
// Retrofit provideRetrofitInsight(String insightURL) {
// return new Retrofit.Builder()
// .baseUrl(insightURL)
// .addConverterFactory(GsonConverterFactory.create())
// .build();
// }
@Singleton
@Provides
@Named(Server.ApiEstimatefee.URL_ESTIMATEFEE)