Updated on 2026-08-14

This commit is contained in:
Tangem 2018-07-10 13:39:36 +03:00
parent a943d462b7
commit 0500d454fb
7 changed files with 475 additions and 429 deletions

View file

@ -10,7 +10,6 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class InfuraRequest {
public static final String METHOD_ETH_GetBalance = "eth_getBalance";
public static final String METHOD_ETH_GetOutTransactionCount = "eth_getTransactionCount";

View file

@ -0,0 +1,102 @@
package com.tangem.data.network.task.main;
import com.tangem.data.network.request.InfuraRequest;
import com.tangem.data.network.task.InfuraTask;
import com.tangem.domain.wallet.Blockchain;
import com.tangem.domain.wallet.TangemCard;
import com.tangem.presentation.fragment.Main;
import org.json.JSONException;
import java.lang.ref.WeakReference;
import java.math.BigInteger;
import java.util.List;
public class ETHRequestTask extends InfuraTask {
private WeakReference<Main> reference;
public ETHRequestTask(Main context, Blockchain blockchain) {
super(blockchain);
reference = new WeakReference<>(context);
}
private void FinishWithError(String wallet, String message) {
Main main = reference.get();
main.mCardListAdapter.UpdateWalletError(wallet, "Cannot obtain data from blockchain");
}
@Override
protected void onPostExecute(List<InfuraRequest> requests) {
super.onPostExecute(requests);
Main main = reference.get();
for (InfuraRequest request : requests) {
try {
if (request.error == null) {
if (request.isMethod(InfuraRequest.METHOD_ETH_GetBalance)) {
try {
String mWalletAddress = request.getParams().getString(0);
String balanceCap = request.getResultString();
balanceCap = balanceCap.substring(2);
BigInteger l = new BigInteger(balanceCap, 16);
BigInteger d = l.divide(new BigInteger("1000000000000000000", 10));
Long balance = d.longValue();
if (request.getBlockchain() != Blockchain.Token)
main.mCardListAdapter.UpdateWalletBalance(mWalletAddress, balance, l.toString(10), getValidationNodeDescription());
main.mCardListAdapter.UpdateWalletBalanceOnlyAlter(mWalletAddress, l.toString(10));
} catch (JSONException e) {
e.printStackTrace();
FinishWithError(request.WalletAddress, e.toString());
}
} else if (request.isMethod(InfuraRequest.METHOD_ETH_Call)) {
try {
String mWalletAddress = request.WalletAddress;
String balanceCap = request.getResultString();
balanceCap = balanceCap.substring(2);
BigInteger l = new BigInteger(balanceCap, 16);
Long balance = l.longValue();
if (l.compareTo(BigInteger.ZERO) == 0) {
main.mCardListAdapter.UpdateWalletBlockchain(mWalletAddress, Blockchain.Ethereum);
main.mCardListAdapter.AddWalletBlockchainNameToken(mWalletAddress);
TangemCard card = main.mCardListAdapter.getCardByWallet(request.WalletAddress);
if (card != null) {
main.refreshCard(card);
}
return;
}
main.mCardListAdapter.UpdateWalletBalance(mWalletAddress, balance, l.toString(10), getValidationNodeDescription());
} catch (JSONException e) {
e.printStackTrace();
FinishWithError(request.WalletAddress, e.toString());
} catch (Exception e) {
e.printStackTrace();
FinishWithError(request.WalletAddress, e.toString());
}
} else if (request.isMethod(InfuraRequest.METHOD_ETH_GetOutTransactionCount)) {
try {
String mWalletAddress = request.getParams().getString(0);
String nonce = request.getResultString();
nonce = nonce.substring(2);
BigInteger count = new BigInteger(nonce, 16);
main.mCardListAdapter.UpdateWalletCoutConfirmTx(mWalletAddress, count);
} catch (JSONException e) {
e.printStackTrace();
FinishWithError(request.WalletAddress, e.toString());
}
}
} else {
FinishWithError(request.WalletAddress, request.error);
}
} catch (JSONException e) {
e.printStackTrace();
FinishWithError(request.WalletAddress, e.toString());
}
}
}
}

View file

@ -0,0 +1,65 @@
package com.tangem.data.network.task.main;
import com.tangem.data.network.request.ExchangeRequest;
import com.tangem.data.network.task.ExchangeTask;
import com.tangem.presentation.fragment.Main;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.lang.ref.WeakReference;
import java.util.List;
public class RateInfoTask extends ExchangeTask {
private WeakReference<Main> reference;
public RateInfoTask(Main context) {
reference = new WeakReference<>(context);
}
protected void onPostExecute(List<ExchangeRequest> requests) {
super.onPostExecute(requests);
Main main = reference.get();
for (ExchangeRequest request : requests) {
if (request.error == null) {
try {
JSONArray arr = request.getAnswerList();
for (int i = 0; i < arr.length(); ++i) {
JSONObject obj = arr.getJSONObject(i);
String currency = obj.getString("id");
boolean stop = false;
boolean stopAlter = false;
if (currency.equals(request.currency)) {
String usd = obj.getString("price_usd");
Float rate = Float.valueOf(usd);
main.mCardListAdapter.UpdateRate(request.WalletAddress, rate);
stop = true;
}
if (currency.equals(request.currencyAlter)) {
String usd = obj.getString("price_usd");
Float rate = Float.valueOf(usd);
main.mCardListAdapter.UpdateRateAlter(request.WalletAddress, rate);
stopAlter = true;
}
if (stop && stopAlter) {
break;
}
}
//mCardListAdapter.UpdateWalletBalance(mWalletAddress, balance, l.toString(10));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}

View file

@ -0,0 +1,196 @@
package com.tangem.data.network.task.main;
import android.os.AsyncTask;
import android.util.Log;
import com.tangem.data.network.request.ElectrumRequest;
import com.tangem.data.network.task.ElectrumTask;
import com.tangem.domain.wallet.Blockchain;
import com.tangem.domain.wallet.CoinEngine;
import com.tangem.domain.wallet.CoinEngineFactory;
import com.tangem.domain.wallet.SharedData;
import com.tangem.presentation.fragment.Main;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.lang.ref.WeakReference;
import java.util.List;
public class RequestWalletInfoTask extends ElectrumTask {
private WeakReference<Main> reference;
public RequestWalletInfoTask(Main context, String host, int port) {
super(host, port);
reference = new WeakReference<>(context);
}
public RequestWalletInfoTask(Main context, String host, int port, SharedData sharedData) {
super(host, port, sharedData);
reference = new WeakReference<>(context);
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
@Override
protected void onCancelled() {
super.onCancelled();
Main main = reference.get();
main.requestTasks.remove(this);
}
private void FinishWithError(String wallet, String message) {
Main main = reference.get();
main.mCardListAdapter.UpdateWalletError(wallet, "Cannot obtain data from blockchain");
}
@Override
protected void onPostExecute(List<ElectrumRequest> requests) {
super.onPostExecute(requests);
Main main = reference.get();
main.requestTasks.remove(this);
// Log.i("RequestWalletInfoTask", "onPostExecute[" + String.valueOf(requests.size()) + "]");
CoinEngine engine = CoinEngineFactory.Create(Blockchain.Bitcoin);
for (ElectrumRequest request : requests) {
try {
if (request.error == null) {
if (request.isMethod(ElectrumRequest.METHOD_GetBalance)) {
try {
Long conf = request.getResult().getLong("confirmed");
Long unconf = request.getResult().getLong("unconfirmed");
if (sharedCounter != null) {
int counter = sharedCounter.requestCounter.incrementAndGet();
if (counter != 1) {
continue;
}
}
String mWalletAddress = request.getParams().getString(0);
main.mCardListAdapter.UpdateWalletBalance(mWalletAddress, conf, unconf, getValidationNodeDescription());
} catch (JSONException e) {
if (sharedCounter != null) {
int errCounter = sharedCounter.errorRequest.incrementAndGet();
if (errCounter == sharedCounter.allRequest) {
e.printStackTrace();
FinishWithError(request.WalletAddress, e.toString());
engine.SwitchNode(null);
}
} else {
e.printStackTrace();
FinishWithError(request.WalletAddress, e.toString());
engine.SwitchNode(null);
}
}
} else if (request.isMethod(ElectrumRequest.METHOD_ListUnspent)) {
try {
String mWalletAddress = request.getParams().getString(0);
main.mCardListAdapter.UpdateWalletUnspent(mWalletAddress, request.getResultArray());
JSONArray unspentList = request.getResultArray();
for (int i = 0; i < unspentList.length(); i++) {
JSONObject jsUnspent = unspentList.getJSONObject(i);
Integer height = jsUnspent.getInt("height");
String hash = jsUnspent.getString("tx_hash");
if (height != -1) {
RequestWalletInfoTask task = new RequestWalletInfoTask(main, request.Host, request.Port);
main.requestTasks.add(task);
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, ElectrumRequest.GetHeader(mWalletAddress, String.valueOf(height)),
ElectrumRequest.GetTransaction(mWalletAddress, hash));
}
}
} catch (JSONException e) {
e.printStackTrace();
FinishWithError(request.WalletAddress, e.toString());
engine.SwitchNode(null);
}
} else if (request.isMethod(ElectrumRequest.METHOD_GetHistory)) {
try {
String mWalletAddress = request.getParams().getString(0);
main.mCardListAdapter.UpdateWalletHistory(mWalletAddress, request.getResultArray());
JSONArray historyList = request.getResultArray();
for (int i = 0; i < historyList.length(); i++) {
JSONObject jsUnspent = historyList.getJSONObject(i);
Integer height = jsUnspent.getInt("height");
String hash = jsUnspent.getString("tx_hash");
if (height != -1) {
RequestWalletInfoTask task = new RequestWalletInfoTask(main, request.Host, request.Port);
main.requestTasks.add(task);
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, ElectrumRequest.GetHeader(mWalletAddress, String.valueOf(height)),
ElectrumRequest.GetTransaction(mWalletAddress, hash));
}
}
} catch (JSONException e) {
e.printStackTrace();
FinishWithError(request.WalletAddress, e.toString());
engine.SwitchNode(null);
}
} else if (request.isMethod(ElectrumRequest.METHOD_GetHeader)) {
try {
String mWalletAddress = request.WalletAddress;
main.mCardListAdapter.UpdateWalletHeader(mWalletAddress, request.getResult());
} catch (JSONException e) {
e.printStackTrace();
FinishWithError(request.WalletAddress, e.toString());
engine.SwitchNode(null);
}
} else if (request.isMethod(ElectrumRequest.METHOD_GetTransaction)) {
try {
Log.e("MainActivityFragment_TX", request.TxHash);
String mWalletAddress = request.WalletAddress;
String tx = request.TxHash;
String raw = request.getResultString();
Log.e("MainActivityFragment_R", raw);
main.mCardListAdapter.UpdateTransaction(mWalletAddress, tx, raw);
} catch (JSONException e) {
e.printStackTrace();
main.mCardListAdapter.UpdateWalletError(request.WalletAddress, "Cannot obtain data from blockchain");
FinishWithError(request.WalletAddress, e.toString());
engine.SwitchNode(null);
}
}
} else {
if (sharedCounter != null) {
int errCounter = sharedCounter.errorRequest.incrementAndGet();
if (errCounter >= sharedCounter.allRequest) {
FinishWithError(request.WalletAddress, request.error);
engine.SwitchNode(null);
}
} else {
FinishWithError(request.WalletAddress, request.error);
engine.SwitchNode(null);
}
}
} catch (JSONException e) {
if (sharedCounter != null) {
int errCounter = sharedCounter.errorRequest.incrementAndGet();
if (errCounter >= sharedCounter.allRequest) {
e.printStackTrace();
main.mCardListAdapter.UpdateWalletError(request.WalletAddress, "Cannot obtain data from blockchain");
engine.SwitchNode(null);
}
} else {
e.printStackTrace();
main.mCardListAdapter.UpdateWalletError(request.WalletAddress, "Cannot obtain data from blockchain");
engine.SwitchNode(null);
}
}
}
}
}