Updated on 2026-08-14
This commit is contained in:
parent
134f518bfb
commit
a943d462b7
5 changed files with 566 additions and 497 deletions
|
|
@ -0,0 +1,139 @@
|
|||
package com.tangem.data.network.task.loaded_wallet;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
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.LastSignStorage;
|
||||
import com.tangem.presentation.fragment.LoadedWallet;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.math.BigInteger;
|
||||
import java.util.List;
|
||||
|
||||
public class ETHRequestTask extends InfuraTask {
|
||||
private WeakReference<LoadedWallet> reference;
|
||||
|
||||
public ETHRequestTask(LoadedWallet context, Blockchain blockchain) {
|
||||
super(blockchain);
|
||||
reference = new WeakReference<>(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(List<InfuraRequest> requests) {
|
||||
super.onPostExecute(requests);
|
||||
LoadedWallet loadedWallet = reference.get();
|
||||
|
||||
for (InfuraRequest request : requests) {
|
||||
try {
|
||||
if (request.error == null) {
|
||||
|
||||
if (request.isMethod(InfuraRequest.METHOD_ETH_GetBalance)) {
|
||||
try {
|
||||
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();
|
||||
|
||||
loadedWallet.mCard.setBalanceConfirmed(balance);
|
||||
loadedWallet.mCard.setBalanceUnconfirmed(0L);
|
||||
if (loadedWallet.mCard.getBlockchain() != Blockchain.Token)
|
||||
loadedWallet.mCard.setDecimalBalance(l.toString(10));
|
||||
loadedWallet.mCard.setDecimalBalanceAlter(l.toString(10));
|
||||
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
loadedWallet.errorOnUpdate(e.toString());
|
||||
}
|
||||
} else if (request.isMethod(InfuraRequest.METHOD_ETH_Call)) {
|
||||
try {
|
||||
String balanceCap = request.getResultString();
|
||||
balanceCap = balanceCap.substring(2);
|
||||
BigInteger l = new BigInteger(balanceCap, 16);
|
||||
Long balance = l.longValue();
|
||||
|
||||
// Log.i(TAG, " dvddvdv BigInteger.ZERO");
|
||||
|
||||
if (l.compareTo(BigInteger.ZERO) == 0) {
|
||||
|
||||
// Log.i(TAG, "BigInteger.ZERO");
|
||||
|
||||
loadedWallet.mCard.setBlockchainID(Blockchain.Ethereum.getID());
|
||||
loadedWallet.mCard.addTokenToBlockchainName();
|
||||
loadedWallet.mSwipeRefreshLayout.setRefreshing(false);
|
||||
loadedWallet.onRefresh();
|
||||
return;
|
||||
}
|
||||
|
||||
loadedWallet.mCard.setBalanceConfirmed(balance);
|
||||
loadedWallet.mCard.setBalanceUnconfirmed(0L);
|
||||
loadedWallet.mCard.setDecimalBalance(l.toString(10));
|
||||
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
loadedWallet.errorOnUpdate(e.toString());
|
||||
}
|
||||
} else if (request.isMethod(InfuraRequest.METHOD_ETH_GetOutTransactionCount)) {
|
||||
try {
|
||||
String nonce = request.getResultString();
|
||||
nonce = nonce.substring(2);
|
||||
BigInteger count = new BigInteger(nonce, 16);
|
||||
|
||||
loadedWallet.mCard.SetConfirmTXCount(count);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else if (request.isMethod(InfuraRequest.METHOD_ETH_SendRawTransaction)) {
|
||||
try {
|
||||
String hashTX = "";
|
||||
|
||||
try {
|
||||
String tmp = request.getResultString();
|
||||
hashTX = tmp;
|
||||
} catch (JSONException e) {
|
||||
JSONObject msg = request.getAnswer();
|
||||
JSONObject err = msg.getJSONObject("error");
|
||||
hashTX = err.getString("message");
|
||||
LastSignStorage.setLastMessage(loadedWallet.mCard.getWallet(), hashTX);
|
||||
loadedWallet.errorOnUpdate("Failed to send transaction. Try again");
|
||||
return;
|
||||
}
|
||||
|
||||
if (hashTX.startsWith("0x") || hashTX.startsWith("0X")) {
|
||||
hashTX = hashTX.substring(2);
|
||||
}
|
||||
BigInteger bigInt = new BigInteger(hashTX, 16); //TODO: очень плохой способ
|
||||
LastSignStorage.setTxWasSend(loadedWallet.mCard.getWallet());
|
||||
LastSignStorage.setLastMessage(loadedWallet.mCard.getWallet(), "");
|
||||
Log.e("TX_RESULT", hashTX);
|
||||
|
||||
|
||||
BigInteger nonce = loadedWallet.mCard.GetConfirmTXCount();
|
||||
nonce.add(BigInteger.valueOf(1));
|
||||
loadedWallet.mCard.SetConfirmTXCount(nonce);
|
||||
Log.e("TX_RESULT", hashTX);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
loadedWallet.errorOnUpdate("Failed to send transaction. Try again");
|
||||
}
|
||||
}
|
||||
loadedWallet.updateViews();
|
||||
} else {
|
||||
loadedWallet.errorOnUpdate(request.error);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
loadedWallet.errorOnUpdate(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
if (loadedWallet.updateTasks.size() == 0)
|
||||
loadedWallet.mSwipeRefreshLayout.setRefreshing(false);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
package com.tangem.data.network.task.loaded_wallet;
|
||||
|
||||
import com.tangem.data.network.request.ExchangeRequest;
|
||||
import com.tangem.data.network.task.ExchangeTask;
|
||||
import com.tangem.presentation.fragment.LoadedWallet;
|
||||
|
||||
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<LoadedWallet> reference;
|
||||
|
||||
public RateInfoTask(LoadedWallet context) {
|
||||
reference = new WeakReference<>(context);
|
||||
}
|
||||
|
||||
protected void onPostExecute(List<ExchangeRequest> requests) {
|
||||
super.onPostExecute(requests);
|
||||
LoadedWallet loadedWallet = 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);
|
||||
loadedWallet.mCard.setRate(rate);
|
||||
loadedWallet.updateViews();
|
||||
stop = true;
|
||||
}
|
||||
|
||||
if (currency.equals(request.currencyAlter)) {
|
||||
String usd = obj.getString("price_usd");
|
||||
|
||||
Float rate = Float.valueOf(usd);
|
||||
loadedWallet.mCard.setRateAlter(rate);
|
||||
loadedWallet.updateViews();
|
||||
stopAlter = true;
|
||||
}
|
||||
|
||||
if (stop && stopAlter) {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,301 @@
|
|||
package com.tangem.data.network.task.loaded_wallet;
|
||||
|
||||
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.BitcoinException;
|
||||
import com.tangem.domain.wallet.CoinEngine;
|
||||
import com.tangem.domain.wallet.CoinEngineFactory;
|
||||
import com.tangem.domain.wallet.LastSignStorage;
|
||||
import com.tangem.domain.wallet.SharedData;
|
||||
import com.tangem.domain.wallet.TangemCard;
|
||||
import com.tangem.presentation.fragment.LoadedWallet;
|
||||
import com.tangem.util.BTCUtils;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class UpdateWalletInfoTask extends ElectrumTask {
|
||||
private WeakReference<LoadedWallet> reference;
|
||||
|
||||
public UpdateWalletInfoTask(LoadedWallet context, String host, int port) {
|
||||
super(host, port);
|
||||
reference = new WeakReference<>(context);
|
||||
}
|
||||
|
||||
|
||||
public UpdateWalletInfoTask(LoadedWallet 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();
|
||||
LoadedWallet loadedWallet = reference.get();
|
||||
|
||||
loadedWallet.updateTasks.remove(this);
|
||||
if (loadedWallet.updateTasks.size() == 0) loadedWallet.mSwipeRefreshLayout.setRefreshing(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(List<ElectrumRequest> requests) {
|
||||
super.onPostExecute(requests);
|
||||
LoadedWallet loadedWallet = reference.get();
|
||||
|
||||
Log.i("RequestWalletInfoTask", "onPostExecute[" + String.valueOf(loadedWallet.updateTasks.size()) + "]");
|
||||
loadedWallet.updateTasks.remove(this);
|
||||
|
||||
CoinEngine engine = CoinEngineFactory.Create(loadedWallet.mCard.getBlockchain());
|
||||
|
||||
for (ElectrumRequest request : requests) {
|
||||
try {
|
||||
if (request.error == null) {
|
||||
if (request.isMethod(ElectrumRequest.METHOD_GetBalance)) {
|
||||
try {
|
||||
String mWalletAddress = request.getParams().getString(0);
|
||||
Long confBalance = request.getResult().getLong("confirmed");
|
||||
Long unconf = request.getResult().getLong("unconfirmed");
|
||||
if (sharedCounter != null) {
|
||||
int counter = sharedCounter.requestCounter.incrementAndGet();
|
||||
if (counter != 1) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
loadedWallet.mCard.setBalanceConfirmed(confBalance);
|
||||
loadedWallet.mCard.setBalanceUnconfirmed(unconf);
|
||||
loadedWallet.mCard.setDecimalBalance(String.valueOf(confBalance));
|
||||
loadedWallet.mCard.setValidationNodeDescription(getValidationNodeDescription());
|
||||
} catch (JSONException e) {
|
||||
if (sharedCounter != null) {
|
||||
int errCounter = sharedCounter.errorRequest.incrementAndGet();
|
||||
if (errCounter >= sharedCounter.allRequest) {
|
||||
e.printStackTrace();
|
||||
loadedWallet.errorOnUpdate(e.toString());
|
||||
engine.SwitchNode(loadedWallet.mCard);
|
||||
}
|
||||
} else {
|
||||
e.printStackTrace();
|
||||
loadedWallet.errorOnUpdate(e.toString());
|
||||
engine.SwitchNode(loadedWallet.mCard);
|
||||
}
|
||||
}
|
||||
} else if (request.isMethod(ElectrumRequest.METHOD_SendTransaction)) {
|
||||
try {
|
||||
String hashTX = request.getResultString();
|
||||
|
||||
try {
|
||||
LastSignStorage.setLastMessage(loadedWallet.mCard.getWallet(), hashTX);
|
||||
if (hashTX.startsWith("0x") || hashTX.startsWith("0X")) {
|
||||
hashTX = hashTX.substring(2);
|
||||
}
|
||||
BigInteger bigInt = new BigInteger(hashTX, 16); //TODO: очень плохой способ
|
||||
LastSignStorage.setTxWasSend(loadedWallet.mCard.getWallet());
|
||||
LastSignStorage.setLastMessage(loadedWallet.mCard.getWallet(), "");
|
||||
Log.e("TX_RESULT", hashTX);
|
||||
|
||||
} catch (Exception e) {
|
||||
engine.SwitchNode(loadedWallet.mCard);
|
||||
loadedWallet.errorOnUpdate("Failed to send transaction. Try again.");
|
||||
}
|
||||
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
loadedWallet.errorOnUpdate("Failed to send transaction. Try again.");
|
||||
engine.SwitchNode(loadedWallet.mCard);
|
||||
}
|
||||
} else if (request.isMethod(ElectrumRequest.METHOD_ListUnspent)) {
|
||||
try {
|
||||
String mWalletAddress = request.getParams().getString(0);
|
||||
|
||||
JSONArray jsUnspentArray = request.getResultArray();
|
||||
try {
|
||||
loadedWallet.mCard.getUnspentTransactions().clear();
|
||||
for (int i = 0; i < jsUnspentArray.length(); i++) {
|
||||
JSONObject jsUnspent = jsUnspentArray.getJSONObject(i);
|
||||
TangemCard.UnspentTransaction trUnspent = new TangemCard.UnspentTransaction();
|
||||
trUnspent.txID = jsUnspent.getString("tx_hash");
|
||||
trUnspent.Amount = jsUnspent.getInt("value");
|
||||
trUnspent.Height = jsUnspent.getInt("height");
|
||||
loadedWallet.mCard.getUnspentTransactions().add(trUnspent);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
loadedWallet.errorOnUpdate(e.toString());
|
||||
engine.SwitchNode(loadedWallet.mCard);
|
||||
}
|
||||
|
||||
for (int i = 0; i < jsUnspentArray.length(); i++) {
|
||||
JSONObject jsUnspent = jsUnspentArray.getJSONObject(i);
|
||||
Integer height = jsUnspent.getInt("height");
|
||||
String hash = jsUnspent.getString("tx_hash");
|
||||
if (height != -1) {
|
||||
String nodeAddress = engine.GetNextNode(loadedWallet.mCard);
|
||||
int nodePort = engine.GetNextNodePort(loadedWallet.mCard);
|
||||
UpdateWalletInfoTask updateWalletInfoTask = new UpdateWalletInfoTask(loadedWallet, nodeAddress, nodePort);
|
||||
|
||||
loadedWallet.updateTasks.add(updateWalletInfoTask);
|
||||
|
||||
updateWalletInfoTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, ElectrumRequest.GetHeader(mWalletAddress, String.valueOf(height)),
|
||||
ElectrumRequest.GetTransaction(mWalletAddress, hash));
|
||||
}
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
loadedWallet.errorOnUpdate(e.toString());
|
||||
engine.SwitchNode(loadedWallet.mCard);
|
||||
}
|
||||
} else if (request.isMethod(ElectrumRequest.METHOD_GetHistory)) {
|
||||
try {
|
||||
String mWalletAddress = request.getParams().getString(0);
|
||||
|
||||
JSONArray jsHistoryArray = request.getResultArray();
|
||||
try {
|
||||
loadedWallet.mCard.getHistoryTransactions().clear();
|
||||
for (int i = 0; i < jsHistoryArray.length(); i++) {
|
||||
JSONObject jsUnspent = jsHistoryArray.getJSONObject(i);
|
||||
TangemCard.HistoryTransaction trHistory = new TangemCard.HistoryTransaction();
|
||||
trHistory.txID = jsUnspent.getString("tx_hash");
|
||||
trHistory.Height = jsUnspent.getInt("height");
|
||||
loadedWallet.mCard.getHistoryTransactions().add(trHistory);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
loadedWallet.errorOnUpdate(e.toString());
|
||||
engine.SwitchNode(loadedWallet.mCard);
|
||||
}
|
||||
|
||||
for (int i = 0; i < jsHistoryArray.length(); i++) {
|
||||
JSONObject jsUnspent = jsHistoryArray.getJSONObject(i);
|
||||
Integer height = jsUnspent.getInt("height");
|
||||
String hash = jsUnspent.getString("tx_hash");
|
||||
if (height != -1) {
|
||||
|
||||
String nodeAddress = engine.GetNode(loadedWallet.mCard);
|
||||
int nodePort = engine.GetNodePort(loadedWallet.mCard);
|
||||
UpdateWalletInfoTask updateWalletInfoTask = new UpdateWalletInfoTask(loadedWallet, nodeAddress, nodePort);
|
||||
loadedWallet.updateTasks.add(updateWalletInfoTask);
|
||||
|
||||
updateWalletInfoTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, ElectrumRequest.GetHeader(mWalletAddress, String.valueOf(height)),
|
||||
ElectrumRequest.GetTransaction(mWalletAddress, hash));
|
||||
}
|
||||
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
loadedWallet.errorOnUpdate(e.toString());
|
||||
engine.SwitchNode(loadedWallet.mCard);
|
||||
}
|
||||
} else if (request.isMethod(ElectrumRequest.METHOD_GetHeader)) {
|
||||
try {
|
||||
JSONObject jsHeader = request.getResult();
|
||||
try {
|
||||
loadedWallet.mCard.getHaedersInfo();
|
||||
loadedWallet.mCard.UpdateHeaderInfo(new TangemCard.HeaderInfo(
|
||||
jsHeader.getInt("block_height"),
|
||||
jsHeader.getInt("timestamp")));
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
loadedWallet.errorOnUpdate(e.toString());
|
||||
engine.SwitchNode(loadedWallet.mCard);
|
||||
|
||||
}
|
||||
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
loadedWallet.errorOnUpdate(e.toString());
|
||||
engine.SwitchNode(loadedWallet.mCard);
|
||||
|
||||
}
|
||||
} else if (request.isMethod(ElectrumRequest.METHOD_GetTransaction)) {
|
||||
try {
|
||||
|
||||
String txHash = request.TxHash;
|
||||
String raw = request.getResultString();
|
||||
|
||||
List<TangemCard.UnspentTransaction> listTx = loadedWallet.mCard.getUnspentTransactions();
|
||||
for (TangemCard.UnspentTransaction tx : listTx) {
|
||||
if (tx.txID.equals(txHash)) {
|
||||
tx.Raw = raw;
|
||||
}
|
||||
}
|
||||
|
||||
List<TangemCard.HistoryTransaction> listHTx = loadedWallet.mCard.getHistoryTransactions();
|
||||
for (TangemCard.HistoryTransaction tx : listHTx) {
|
||||
if (tx.txID.equals(txHash)) {
|
||||
tx.Raw = raw;
|
||||
try {
|
||||
ArrayList<byte[]> prevHashes = BTCUtils.getPrevTX(raw);
|
||||
|
||||
boolean isOur = false;
|
||||
for (byte[] hash : prevHashes) {
|
||||
String checkID = BTCUtils.toHex(hash);
|
||||
for (TangemCard.HistoryTransaction txForCheck : listHTx) {
|
||||
if (txForCheck.txID == checkID) {
|
||||
isOur = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tx.isInput = !isOur;
|
||||
} catch (BitcoinException e) {
|
||||
e.printStackTrace();
|
||||
loadedWallet.errorOnUpdate(e.toString());
|
||||
}
|
||||
Log.e("TX", raw);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
loadedWallet.errorOnUpdate(e.toString());
|
||||
engine.SwitchNode(loadedWallet.mCard);
|
||||
}
|
||||
}
|
||||
loadedWallet.updateViews();
|
||||
} else {
|
||||
if (sharedCounter != null) {
|
||||
int errCounter = sharedCounter.errorRequest.incrementAndGet();
|
||||
if (errCounter >= sharedCounter.allRequest) {
|
||||
loadedWallet.errorOnUpdate(request.error);
|
||||
engine.SwitchNode(loadedWallet.mCard);
|
||||
|
||||
}
|
||||
} else {
|
||||
loadedWallet.errorOnUpdate(request.error);
|
||||
engine.SwitchNode(loadedWallet.mCard);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
if (sharedCounter != null) {
|
||||
int errCounter = sharedCounter.errorRequest.incrementAndGet();
|
||||
if (errCounter >= sharedCounter.allRequest) {
|
||||
e.printStackTrace();
|
||||
loadedWallet.errorOnUpdate(e.toString());
|
||||
}
|
||||
} else {
|
||||
e.printStackTrace();
|
||||
loadedWallet.errorOnUpdate(e.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (loadedWallet.updateTasks.size() == 0) loadedWallet.mSwipeRefreshLayout.setRefreshing(false);
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue