Skip to content

Commit

Permalink
Merge pull request #14532 from brave/pr14472_android_solana_tx_list_1…
Browse files Browse the repository at this point in the history
….43.x

Shows all values for Solana transactions in transactions list on Android and opens a correct block explorer for solana transactions (uplift to 1.43.x)
  • Loading branch information
srirambv authored Aug 8, 2022
2 parents d8911ae + 8dcfb51 commit 2099a64
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.chromium.brave_wallet.mojom.AssetRatioService;
import org.chromium.brave_wallet.mojom.BlockchainRegistry;
import org.chromium.brave_wallet.mojom.BraveWalletService;
import org.chromium.brave_wallet.mojom.CoinType;
import org.chromium.brave_wallet.mojom.EthTxManagerProxy;
import org.chromium.brave_wallet.mojom.JsonRpcService;
import org.chromium.brave_wallet.mojom.KeyringInfo;
Expand Down Expand Up @@ -1015,7 +1016,8 @@ public void openBraveWallet(boolean fromDapp) {
}

public void viewOnBlockExplorer(String address) {
Utils.openAddress("/address/" + address, mJsonRpcService, this);
// TODO(sergz): We will need to correct that while doing Solana DApps
Utils.openAddress("/address/" + address, mJsonRpcService, this, CoinType.ETH);
}

// should only be called if the wallet is setup and unlocked
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public void onAssetClick(BlockchainToken asset) {

@Override
public void onTransactionClick(TransactionInfo txInfo) {
Utils.openTransaction(txInfo, mJsonRpcService, this, mName);
Utils.openTransaction(txInfo, mJsonRpcService, this, mName, mCoinType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ public void onAccountClick(WalletListItemModel walletListItemModel) {

@Override
public void onTransactionClick(TransactionInfo txInfo) {
Utils.openTransaction(txInfo, mJsonRpcService, this, accountInfos);
Utils.openTransaction(txInfo, mJsonRpcService, this, accountInfos, mCoinType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ private void fillAddSuggestTokenRequest(boolean init) {
mTokenAddress.setOnClickListener(v -> {
Activity activity = getActivity();
if (activity instanceof BraveWalletBaseActivity) {
// TODO(sergz): We will need to correct that while doing Solana DApps
Utils.openAddress(
"/token/" + mCurrentAddSuggestTokenRequest.token.contractAddress,
getJsonRpcService(), (BraveWalletBaseActivity) activity);
getJsonRpcService(), (BraveWalletBaseActivity) activity,
CoinType.ETH);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,8 @@ public Pair<String, String> makeTxListItemTitles(Context context) {

String action = "";
String detailInfo = "";
String actionFiatValue = String.format(Locale.getDefault(), "%.2f", this.fiatValue);
if (this.type == TransactionType.ERC20_TRANSFER) {
String actionFiatValue = String.format(Locale.getDefault(), "%.2f", this.fiatValue);
action = String.format(context.getResources().getString(R.string.wallet_tx_info_sent),
this.senderLabel, this.formatValueToDisplay(), this.symbol, actionFiatValue,
strDate);
Expand All @@ -568,7 +568,8 @@ public Pair<String, String> makeTxListItemTitles(Context context) {
+ "0x Exchange Proxy";
} else {
action = String.format(context.getResources().getString(R.string.wallet_tx_info_sent),
this.senderLabel, this.formatValueToDisplay(), this.symbol, "0.00", strDate);
this.senderLabel, this.formatValueToDisplay(), this.symbol, actionFiatValue,
strDate);
detailInfo = this.senderLabel + " -> " + this.recipientLabel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ public static AccountInfo findAccount(AccountInfo[] accounts, String address) {
}

public static void openTransaction(TransactionInfo txInfo, JsonRpcService jsonRpcService,
AppCompatActivity activity, String accountName) {
AppCompatActivity activity, String accountName, int coinType) {
assert txInfo != null;
if (txInfo.txStatus == TransactionStatus.UNAPPROVED) {
if (activity instanceof ApprovedTxObserver) {
Expand All @@ -1094,39 +1094,40 @@ public static void openTransaction(TransactionInfo txInfo, JsonRpcService jsonRp
if (txInfo.txHash == null || txInfo.txHash.isEmpty()) {
return;
}
openAddress("/tx/" + txInfo.txHash, jsonRpcService, activity);
openAddress("/tx/" + txInfo.txHash, jsonRpcService, activity, coinType);
}
}

public static void openAddress(
String toAppend, JsonRpcService jsonRpcService, AppCompatActivity activity) {
public static void openAddress(String toAppend, JsonRpcService jsonRpcService,
AppCompatActivity activity, int coinType) {
assert jsonRpcService != null;
jsonRpcService.getChainId(CoinType.ETH, chainId -> {
jsonRpcService.getAllNetworks(CoinType.ETH, networks -> {
for (NetworkInfo network : networks) {
if (!chainId.equals(network.chainId)) {
continue;
}
String blockExplorerUrl = Arrays.toString(network.blockExplorerUrls);
if (blockExplorerUrl.length() > 2) {
blockExplorerUrl =
blockExplorerUrl.substring(1, blockExplorerUrl.length() - 1)
+ toAppend;
TabUtils.openUrlInNewTab(false, blockExplorerUrl);
TabUtils.bringChromeTabbedActivityToTheTop(activity);
break;
}
jsonRpcService.getNetwork(coinType, network -> {
String blockExplorerUrl = Arrays.toString(network.blockExplorerUrls);
if (blockExplorerUrl.length() > 2) {
blockExplorerUrl = blockExplorerUrl.substring(1, blockExplorerUrl.length() - 1);
}
if (coinType == CoinType.ETH) {
blockExplorerUrl += toAppend;
} else if (coinType == CoinType.SOL) {
int iPos = blockExplorerUrl.indexOf("?cluster=");
if (iPos != -1) {
blockExplorerUrl = blockExplorerUrl.substring(0, iPos - 1) + toAppend
+ blockExplorerUrl.substring(iPos);
} else {
blockExplorerUrl += toAppend;
}
});
}
TabUtils.openUrlInNewTab(false, blockExplorerUrl);
TabUtils.bringChromeTabbedActivityToTheTop(activity);
});
}

public static void openTransaction(TransactionInfo txInfo, JsonRpcService jsonRpcService,
AppCompatActivity activity, AccountInfo[] accountInfos) {
AppCompatActivity activity, AccountInfo[] accountInfos, int coinType) {
assert txInfo != null;
AccountInfo account = findAccount(accountInfos, txInfo.fromAddress);
openTransaction(txInfo, jsonRpcService, activity,
account != null ? account.name : stripAccountAddress(txInfo.fromAddress));
account != null ? account.name : stripAccountAddress(txInfo.fromAddress), coinType);
}

public static void setUpTransactionList(BraveWalletBaseActivity activity,
Expand Down Expand Up @@ -1154,21 +1155,45 @@ public static void setUpTransactionList(BraveWalletBaseActivity activity,
pendingTxHelper.fetchTransactions(() -> {
HashMap<String, TransactionInfo[]> pendingTxInfos =
pendingTxHelper.getTransactions();
workWithTransactions(activity, selectedNetwork, pendingTxInfos, accounts,
walletListItemModel, assetPrices, fullTokenList, nativeAssetsBalances,
blockchainTokensBalances, rvTransactions, callback, walletTxCoinAdapter);
SolanaTransactionsGasHelper solanaTransactionsGasHelper =
new SolanaTransactionsGasHelper(
activity, getTransactionArray(pendingTxInfos));
solanaTransactionsGasHelper.maybeGetSolanaGasEstimations(() -> {
workWithTransactions(activity, selectedNetwork, pendingTxInfos, accounts,
walletListItemModel, assetPrices, fullTokenList, nativeAssetsBalances,
blockchainTokensBalances, rvTransactions, callback, walletTxCoinAdapter,
solanaTransactionsGasHelper.getPerTxFee());
});
});
});
}

private static TransactionInfo[] getTransactionArray(
HashMap<String, TransactionInfo[]> txInfos) {
TransactionInfo[] result = new TransactionInfo[0];
for (String key : txInfos.keySet()) {
TransactionInfo[] txs = txInfos.get(key);
result = concatWithArrayCopy(result, txs);
}

return result;
}

private static <T> T[] concatWithArrayCopy(T[] array1, T[] array2) {
T[] result = Arrays.copyOf(array1, array1.length + array2.length);
System.arraycopy(array2, 0, result, array1.length, array2.length);

return result;
}

private static void workWithTransactions(BraveWalletBaseActivity activity,
NetworkInfo selectedNetwork, HashMap<String, TransactionInfo[]> pendingTxInfos,
AccountInfo[] accounts, WalletListItemModel walletListItemModel,
HashMap<String, Double> assetPrices, BlockchainToken[] fullTokenList,
HashMap<String, Double> nativeAssetsBalances,
HashMap<String, HashMap<String, Double>> blockchainTokensBalances,
RecyclerView rvTransactions, OnWalletListItemClick callback,
WalletCoinAdapter walletTxCoinAdapter) {
WalletCoinAdapter walletTxCoinAdapter, HashMap<String, Long> perTxSolanaFee) {
walletTxCoinAdapter.setWalletCoinAdapterType(
WalletCoinAdapter.AdapterType.VISIBLE_ASSETS_LIST);
List<WalletListItemModel> walletListItemModelList = new ArrayList<>();
Expand All @@ -1178,8 +1203,12 @@ private static void workWithTransactions(BraveWalletBaseActivity activity,
for (String accountName : pendingTxInfos.keySet()) {
TransactionInfo[] txInfos = pendingTxInfos.get(accountName);
for (TransactionInfo txInfo : txInfos) {
long solanaEstimatedTxFee = 0;
if (perTxSolanaFee.get(txInfo.id) != null) {
solanaEstimatedTxFee = perTxSolanaFee.get(txInfo.id);
}
ParsedTransaction parsedTx = ParsedTransaction.parseTransaction(txInfo,
selectedNetwork, accounts, assetPrices, 0, fullTokenList,
selectedNetwork, accounts, assetPrices, solanaEstimatedTxFee, fullTokenList,
nativeAssetsBalances, blockchainTokensBalances);
WalletListItemModel itemModel =
makeWalletItem((Context) activity, txInfo, selectedNetwork, parsedTx);
Expand Down

0 comments on commit 2099a64

Please sign in to comment.