From 8dcfb51ae7a771d801f6006de46c7afadda7e86d Mon Sep 17 00:00:00 2001 From: brave-builds Date: Mon, 8 Aug 2022 15:32:19 +0000 Subject: [PATCH] Uplift of #14472 (squashed) to beta --- .../chrome/browser/app/BraveActivity.java | 4 +- .../activities/AccountDetailActivity.java | 2 +- .../activities/AssetDetailActivity.java | 2 +- .../fragments/dapps/AddTokenFragment.java | 4 +- .../crypto_wallet/util/ParsedTransaction.java | 5 +- .../browser/crypto_wallet/util/Utils.java | 83 +++++++++++++------ 6 files changed, 67 insertions(+), 33 deletions(-) diff --git a/android/java/org/chromium/chrome/browser/app/BraveActivity.java b/android/java/org/chromium/chrome/browser/app/BraveActivity.java index 41adbc8c44da..f77abb04ccad 100644 --- a/android/java/org/chromium/chrome/browser/app/BraveActivity.java +++ b/android/java/org/chromium/chrome/browser/app/BraveActivity.java @@ -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; @@ -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 diff --git a/android/java/org/chromium/chrome/browser/crypto_wallet/activities/AccountDetailActivity.java b/android/java/org/chromium/chrome/browser/crypto_wallet/activities/AccountDetailActivity.java index a2e4f5956187..2b171b4b6218 100644 --- a/android/java/org/chromium/chrome/browser/crypto_wallet/activities/AccountDetailActivity.java +++ b/android/java/org/chromium/chrome/browser/crypto_wallet/activities/AccountDetailActivity.java @@ -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 diff --git a/android/java/org/chromium/chrome/browser/crypto_wallet/activities/AssetDetailActivity.java b/android/java/org/chromium/chrome/browser/crypto_wallet/activities/AssetDetailActivity.java index fda3c7ba5df9..6ba5b931ce7f 100644 --- a/android/java/org/chromium/chrome/browser/crypto_wallet/activities/AssetDetailActivity.java +++ b/android/java/org/chromium/chrome/browser/crypto_wallet/activities/AssetDetailActivity.java @@ -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 diff --git a/android/java/org/chromium/chrome/browser/crypto_wallet/fragments/dapps/AddTokenFragment.java b/android/java/org/chromium/chrome/browser/crypto_wallet/fragments/dapps/AddTokenFragment.java index a6d47bca6550..7c8aace51b0c 100644 --- a/android/java/org/chromium/chrome/browser/crypto_wallet/fragments/dapps/AddTokenFragment.java +++ b/android/java/org/chromium/chrome/browser/crypto_wallet/fragments/dapps/AddTokenFragment.java @@ -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); } }); } diff --git a/android/java/org/chromium/chrome/browser/crypto_wallet/util/ParsedTransaction.java b/android/java/org/chromium/chrome/browser/crypto_wallet/util/ParsedTransaction.java index 30f940ad02d4..35184c92c488 100644 --- a/android/java/org/chromium/chrome/browser/crypto_wallet/util/ParsedTransaction.java +++ b/android/java/org/chromium/chrome/browser/crypto_wallet/util/ParsedTransaction.java @@ -542,8 +542,8 @@ public Pair 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); @@ -568,7 +568,8 @@ public Pair 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; } diff --git a/android/java/org/chromium/chrome/browser/crypto_wallet/util/Utils.java b/android/java/org/chromium/chrome/browser/crypto_wallet/util/Utils.java index 421f2ca9f42e..9282025c8cb5 100644 --- a/android/java/org/chromium/chrome/browser/crypto_wallet/util/Utils.java +++ b/android/java/org/chromium/chrome/browser/crypto_wallet/util/Utils.java @@ -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) { @@ -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, @@ -1154,13 +1155,37 @@ public static void setUpTransactionList(BraveWalletBaseActivity activity, pendingTxHelper.fetchTransactions(() -> { HashMap 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 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[] 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 pendingTxInfos, AccountInfo[] accounts, WalletListItemModel walletListItemModel, @@ -1168,7 +1193,7 @@ private static void workWithTransactions(BraveWalletBaseActivity activity, HashMap nativeAssetsBalances, HashMap> blockchainTokensBalances, RecyclerView rvTransactions, OnWalletListItemClick callback, - WalletCoinAdapter walletTxCoinAdapter) { + WalletCoinAdapter walletTxCoinAdapter, HashMap perTxSolanaFee) { walletTxCoinAdapter.setWalletCoinAdapterType( WalletCoinAdapter.AdapterType.VISIBLE_ASSETS_LIST); List walletListItemModelList = new ArrayList<>(); @@ -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);