Android ERC721 Send
This commit is contained in:
+7
-13
@@ -23,6 +23,7 @@ import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import org.chromium.base.Callback;
|
||||
import org.chromium.base.Log;
|
||||
import org.chromium.brave_wallet.mojom.AccountInfo;
|
||||
import org.chromium.brave_wallet.mojom.AssetPriceTimeframe;
|
||||
@@ -294,20 +295,13 @@ public class AssetDetailActivity
|
||||
private void getBlockchainToken() {
|
||||
if (mAsset != null || !mNativeInitialized) return;
|
||||
|
||||
TokenUtils.getUserAssetsFiltered(
|
||||
mBraveWalletService, mChainId, TokenUtils.TokenType.ALL, (userAssets) -> {
|
||||
for (BlockchainToken userAsset : userAssets) {
|
||||
if (mChainId.equals(userAsset.chainId)
|
||||
&& mAssetSymbol.equals(userAsset.symbol)
|
||||
&& mAssetName.equals(userAsset.name)
|
||||
&& (mAssetId.isEmpty() || mAssetId.equals(userAsset.tokenId))
|
||||
&& mContractAddress.equals(userAsset.contractAddress)
|
||||
&& mAssetDecimals == userAsset.decimals) {
|
||||
mAsset = userAsset;
|
||||
}
|
||||
Utils.getExactUserAsset(getBraveWalletService(), mChainId, mAssetSymbol, mAssetName,
|
||||
mAssetId, mContractAddress, mAssetDecimals, new Callback<BlockchainToken>() {
|
||||
@Override
|
||||
public void onResult(BlockchainToken token) {
|
||||
assert token != null;
|
||||
mAsset = token;
|
||||
}
|
||||
|
||||
assert mAsset != null;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
+30
-25
@@ -179,6 +179,7 @@ public class BuySendSwapActivity extends BraveWalletBaseActivity
|
||||
private TextView mToAssetText;
|
||||
private TextView mSendToValidation;
|
||||
private TextView mFromSendValueValidation;
|
||||
private LinearLayout mFromValueBlock;
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
@@ -227,6 +228,8 @@ public class BuySendSwapActivity extends BraveWalletBaseActivity
|
||||
mSendToValidation = findViewById(R.id.to_send_error_text);
|
||||
mFromSendValueValidation = findViewById(R.id.from_send_value_error_text);
|
||||
|
||||
mFromValueBlock = findViewById(R.id.from_value_block);
|
||||
|
||||
onInitialLayoutInflationComplete();
|
||||
mInitialLayoutInflationComplete = true;
|
||||
|
||||
@@ -499,6 +502,7 @@ public class BuySendSwapActivity extends BraveWalletBaseActivity
|
||||
if (swapToAsset.equals(swapFromAssetSymbol)) { // swap from BAT
|
||||
updateBuySendSwapAsset(eth.symbol, eth, false);
|
||||
} else {
|
||||
// Only ERC20 tokens can be swapped
|
||||
mBlockchainRegistry.getTokenBySymbol(
|
||||
BraveWalletConstants.MAINNET_CHAIN_ID, CoinType.ETH, swapToAsset, token -> {
|
||||
if (token != null) {
|
||||
@@ -606,6 +610,16 @@ public class BuySendSwapActivity extends BraveWalletBaseActivity
|
||||
}
|
||||
populateBalance(balance, from);
|
||||
});
|
||||
} else if (blockchainToken.isErc721) {
|
||||
mJsonRpcService.getErc721TokenBalance(blockchainToken.contractAddress,
|
||||
blockchainToken.tokenId, address, mCurrentChainId,
|
||||
(balance, error, errorMessage) -> {
|
||||
warnWhenError(TAG, "getERC721TokenBalance", error, errorMessage);
|
||||
if (error != ProviderError.SUCCESS) {
|
||||
return;
|
||||
}
|
||||
populateBalance(balance, from);
|
||||
});
|
||||
} else {
|
||||
mJsonRpcService.getErc20TokenBalance(blockchainToken.contractAddress, address,
|
||||
mCurrentChainId, (balance, error, errorMessage) -> {
|
||||
@@ -664,8 +678,8 @@ public class BuySendSwapActivity extends BraveWalletBaseActivity
|
||||
private void setSendToFromValueValidationResult(
|
||||
String validationResult, boolean disableButtonOnError, boolean sendTo) {
|
||||
boolean validationSucceeded = (validationResult == null || validationResult.isEmpty());
|
||||
final TextView otherValueValidation = sendTo ? mFromSendValueValidation : mSendToValidation;
|
||||
final TextView thisValueValidation = sendTo ? mSendToValidation : mFromSendValueValidation;
|
||||
final TextView otherValueValidation = sendTo ? mFromSendValueValidation : mSendToValidation;
|
||||
|
||||
boolean otherValidationError = otherValueValidation.getVisibility() == View.VISIBLE;
|
||||
boolean buttonShouldBeEnabled = validationSucceeded || (sendTo && !disableButtonOnError);
|
||||
@@ -891,9 +905,8 @@ public class BuySendSwapActivity extends BraveWalletBaseActivity
|
||||
Utils.toHexWei(value, mCurrentBlockchainToken.decimals), from,
|
||||
mCurrentBlockchainToken.contractAddress);
|
||||
} else if (mCurrentBlockchainToken.isErc721) {
|
||||
// TODO tokenId text field
|
||||
// addUnapprovedTransactionERC721(from, to, tokenId,
|
||||
// mCurrentBlockchainToken.contractAddress);
|
||||
addUnapprovedTransactionERC721(from, to, mCurrentBlockchainToken.tokenId,
|
||||
mCurrentBlockchainToken.contractAddress);
|
||||
}
|
||||
} else if (mActivityType == ActivityType.BUY) {
|
||||
if (mCurrentChainId.equals(BraveWalletConstants.MAINNET_CHAIN_ID)) {
|
||||
@@ -1273,6 +1286,7 @@ public class BuySendSwapActivity extends BraveWalletBaseActivity
|
||||
}
|
||||
|
||||
assert mTxService != null;
|
||||
TxDataUnion txDataUnion = new TxDataUnion();
|
||||
if (isEIP1559) {
|
||||
TxData1559 txData1559 = new TxData1559();
|
||||
txData1559.baseData = data;
|
||||
@@ -1287,28 +1301,18 @@ public class BuySendSwapActivity extends BraveWalletBaseActivity
|
||||
txData1559.gasEstimation.fastMaxPriorityFeePerGas = "";
|
||||
txData1559.gasEstimation.fastMaxFeePerGas = "";
|
||||
txData1559.gasEstimation.baseFeePerGas = "";
|
||||
TxDataUnion txDataUnion = new TxDataUnion();
|
||||
txDataUnion.setEthTxData1559(txData1559);
|
||||
mTxService.addUnapprovedTransaction(
|
||||
txDataUnion, from, null, (success, tx_meta_id, error_message) -> {
|
||||
// Do nothing here when success as we will receive an
|
||||
// unapproved transaction in TxServiceObserver.
|
||||
// When we have error, let the user know,
|
||||
// error_message is localized, do not disable send button
|
||||
setSendToFromValueValidationResult(error_message, false, true);
|
||||
});
|
||||
} else {
|
||||
TxDataUnion txDataUnion = new TxDataUnion();
|
||||
txDataUnion.setEthTxData(data);
|
||||
mTxService.addUnapprovedTransaction(
|
||||
txDataUnion, from, null, (success, tx_meta_id, error_message) -> {
|
||||
// Do nothing here when success as we will receive an
|
||||
// unapproved transaction in TxServiceObserver.
|
||||
// When we have error, let the user know,
|
||||
// error_message is localized, do not disable send button
|
||||
setSendToFromValueValidationResult(error_message, false, true);
|
||||
});
|
||||
}
|
||||
mTxService.addUnapprovedTransaction(
|
||||
txDataUnion, from, null, (success, tx_meta_id, error_message) -> {
|
||||
// Do nothing here when success as we will receive an
|
||||
// unapproved transaction in TxServiceObserver.
|
||||
// When we have error, let the user know,
|
||||
// error_message is localized, do not disable send button
|
||||
setSendToFromValueValidationResult(error_message, false, true);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1381,9 +1385,9 @@ public class BuySendSwapActivity extends BraveWalletBaseActivity
|
||||
public void updateBuySendSwapAsset(
|
||||
String asset, BlockchainToken blockchainToken, boolean buySend) {
|
||||
if (!buySend && mCurrentBlockchainToken != null
|
||||
&& mCurrentBlockchainToken.symbol.equals(blockchainToken.symbol)
|
||||
&& TokenUtils.isSameToken(mCurrentBlockchainToken, blockchainToken)
|
||||
|| buySend && mCurrentSwapToBlockchainToken != null
|
||||
&& mCurrentSwapToBlockchainToken.symbol.equals(blockchainToken.symbol))
|
||||
&& TokenUtils.isSameToken(mCurrentSwapToBlockchainToken, blockchainToken))
|
||||
return;
|
||||
|
||||
TextView assetText = buySend ? mFromAssetText : mToAssetText;
|
||||
@@ -1412,8 +1416,9 @@ public class BuySendSwapActivity extends BraveWalletBaseActivity
|
||||
(float) 0.5);
|
||||
}
|
||||
if (buySend && token.isErc721) {
|
||||
mFromValueText.setText("1");
|
||||
mFromValueBlock.setVisibility(View.GONE);
|
||||
} else {
|
||||
mFromValueBlock.setVisibility(View.VISIBLE);
|
||||
mFromValueText.setText("");
|
||||
}
|
||||
if (buySend && mActivityType == ActivityType.SWAP || !buySend) {
|
||||
|
||||
+74
-39
@@ -254,10 +254,23 @@ public class ApproveTxBottomSheetDialogFragment extends BottomSheetDialogFragmen
|
||||
BlockchainRegistry blockchainRegistry = getBlockchainRegistry();
|
||||
assert blockchainRegistry != null;
|
||||
TokenUtils.getAllTokensFiltered(getBraveWalletService(), blockchainRegistry,
|
||||
chainId, TokenUtils.TokenType.ERC721,
|
||||
tokens
|
||||
-> {
|
||||
// TODO erc721 send
|
||||
chainId, TokenUtils.TokenType.ERC721, tokens -> {
|
||||
boolean foundToken = false;
|
||||
for (BlockchainToken token : tokens) {
|
||||
if (token.contractAddress.toLowerCase(Locale.getDefault())
|
||||
.equals(mTxInfo.txDataUnion.getEthTxData1559()
|
||||
.baseData.to.toLowerCase(
|
||||
Locale.getDefault()))) {
|
||||
fillAssetDependentControls(
|
||||
token.symbol, view, token.decimals);
|
||||
foundToken = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Fallback to ETH to avoid empty screen
|
||||
if (!foundToken) {
|
||||
fillAssetDependentControls(mChainSymbol, view, mChainDecimals);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (mTxInfo.txDataUnion.getEthTxData1559()
|
||||
@@ -325,48 +338,70 @@ public class ApproveTxBottomSheetDialogFragment extends BottomSheetDialogFragmen
|
||||
private void fillAssetDependentControls(String asset, View view, int decimals) {
|
||||
String valueToConvert = mTxInfo.txDataUnion.getEthTxData1559().baseData.value;
|
||||
String to = mTxInfo.txDataUnion.getEthTxData1559().baseData.to;
|
||||
double value = Utils.fromHexWei(valueToConvert, decimals);
|
||||
String amountText =
|
||||
String.format(getResources().getString(R.string.crypto_wallet_amount_asset),
|
||||
String.format(Locale.getDefault(), "%.4f", value), asset);
|
||||
// TODO (Wengling): Transaction Parser
|
||||
// (components/brave_wallet_ui/common/hooks/transaction-parser.ts)
|
||||
if (mTxInfo.txType == TransactionType.ERC20_TRANSFER && mTxInfo.txArgs.length > 1) {
|
||||
valueToConvert = mTxInfo.txArgs[1];
|
||||
value = Utils.fromHexWei(mTxInfo.txArgs[1], decimals);
|
||||
to = mTxInfo.txArgs[0];
|
||||
amountText =
|
||||
String.format(getResources().getString(R.string.crypto_wallet_amount_asset),
|
||||
String.format(Locale.getDefault(), "%.4f", value), asset);
|
||||
} else if ((mTxInfo.txType == TransactionType.ERC721_TRANSFER_FROM
|
||||
|| mTxInfo.txType == TransactionType.ERC721_SAFE_TRANSFER_FROM)
|
||||
&& mTxInfo.txArgs.length > 2) {
|
||||
to = mTxInfo.txArgs[1];
|
||||
String tokenId = mTxInfo.txArgs[2];
|
||||
amountText = String.format(
|
||||
getResources().getString(R.string.crypto_wallet_amount_asset_erc721), asset,
|
||||
String.format(Locale.getDefault(), "%d", (int) Utils.fromHexWei(tokenId, 0)));
|
||||
}
|
||||
TextView fromTo = view.findViewById(R.id.from_to);
|
||||
fromTo.setText(String.format(getResources().getString(R.string.crypto_wallet_from_to),
|
||||
mAccountName, Utils.stripAccountAddress(to)));
|
||||
TextView amountAsset = view.findViewById(R.id.amount_asset);
|
||||
amountAsset.setText(
|
||||
String.format(getResources().getString(R.string.crypto_wallet_amount_asset),
|
||||
String.format(Locale.getDefault(), "%.4f",
|
||||
Utils.fromHexWei(valueToConvert, decimals)),
|
||||
asset));
|
||||
AssetRatioService assetRatioService = getAssetRatioService();
|
||||
assert assetRatioService != null;
|
||||
String[] assets = {asset.toLowerCase(Locale.getDefault())};
|
||||
String[] toCurr = {"usd"};
|
||||
assetRatioService.getPrice(assets, toCurr, AssetPriceTimeframe.LIVE, (success, values) -> {
|
||||
String valueFiat = "0";
|
||||
if (values.length != 0) {
|
||||
valueFiat = values[0].price;
|
||||
}
|
||||
String valueAsset = mTxInfo.txDataUnion.getEthTxData1559().baseData.value;
|
||||
if (mTxInfo.txType == TransactionType.ERC20_TRANSFER && mTxInfo.txArgs.length > 1) {
|
||||
valueAsset = mTxInfo.txArgs[1];
|
||||
}
|
||||
double value = Utils.fromHexWei(valueAsset, decimals);
|
||||
double price = Double.valueOf(valueFiat);
|
||||
mTotalPrice = value * price;
|
||||
TextView amountFiat = view.findViewById(R.id.amount_fiat);
|
||||
amountFiat.setText(
|
||||
String.format(getResources().getString(R.string.crypto_wallet_amount_fiat),
|
||||
String.format(Locale.getDefault(), "%.2f", mTotalPrice)));
|
||||
ViewPager viewPager = view.findViewById(R.id.navigation_view_pager);
|
||||
ApproveTxFragmentPageAdapter adapter =
|
||||
new ApproveTxFragmentPageAdapter(getChildFragmentManager(), mTxInfo, asset,
|
||||
decimals, mChainSymbol, mChainDecimals, mTotalPrice, getActivity());
|
||||
viewPager.setAdapter(adapter);
|
||||
viewPager.setOffscreenPageLimit(adapter.getCount() - 1);
|
||||
TabLayout tabLayout = view.findViewById(R.id.tabs);
|
||||
tabLayout.setupWithViewPager(viewPager);
|
||||
});
|
||||
amountAsset.setText(amountText);
|
||||
TextView amountFiat = view.findViewById(R.id.amount_fiat);
|
||||
final double valueFinal = value;
|
||||
if (mTxInfo.txType == TransactionType.ERC721_TRANSFER_FROM
|
||||
|| mTxInfo.txType == TransactionType.ERC721_SAFE_TRANSFER_FROM) {
|
||||
amountFiat.setVisibility(View.GONE);
|
||||
setupPager(asset, view, decimals);
|
||||
} else {
|
||||
// ETH or ERC20
|
||||
AssetRatioService assetRatioService = getAssetRatioService();
|
||||
assert assetRatioService != null;
|
||||
String[] assets = {asset.toLowerCase(Locale.getDefault())};
|
||||
String[] toCurr = {"usd"};
|
||||
assetRatioService.getPrice(
|
||||
assets, toCurr, AssetPriceTimeframe.LIVE, (success, values) -> {
|
||||
String valueFiat = "0";
|
||||
if (values.length != 0) {
|
||||
valueFiat = values[0].price;
|
||||
}
|
||||
double price = Double.valueOf(valueFiat);
|
||||
mTotalPrice = valueFinal * price;
|
||||
amountFiat.setVisibility(View.VISIBLE);
|
||||
amountFiat.setText(String.format(
|
||||
getResources().getString(R.string.crypto_wallet_amount_fiat),
|
||||
String.format(Locale.getDefault(), "%.2f", mTotalPrice)));
|
||||
setupPager(asset, view, decimals);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void setupPager(String asset, View view, int decimals) {
|
||||
ViewPager viewPager = view.findViewById(R.id.navigation_view_pager);
|
||||
ApproveTxFragmentPageAdapter adapter =
|
||||
new ApproveTxFragmentPageAdapter(getChildFragmentManager(), mTxInfo, asset,
|
||||
decimals, mChainSymbol, mChainDecimals, mTotalPrice, getActivity());
|
||||
viewPager.setAdapter(adapter);
|
||||
viewPager.setOffscreenPageLimit(adapter.getCount() - 1);
|
||||
TabLayout tabLayout = view.findViewById(R.id.tabs);
|
||||
tabLayout.setupWithViewPager(viewPager);
|
||||
}
|
||||
|
||||
private void rejectTransaction(boolean dismiss) {
|
||||
|
||||
+2
-3
@@ -230,7 +230,7 @@ public class EditVisibleAssetsBottomSheetDialogFragment extends BottomSheetDialo
|
||||
if (mType == WalletCoinAdapter.AdapterType.EDIT_VISIBLE_ASSETS_LIST) {
|
||||
assert mChainId != null && !mChainId.isEmpty();
|
||||
TokenUtils.getUserAssetsFiltered(
|
||||
braveWalletService, mChainId, TokenUtils.TokenType.ALL, (userAssets) -> {
|
||||
braveWalletService, mChainId, TokenUtils.TokenType.ALL, userAssets -> {
|
||||
TokenUtils.getAllTokensFiltered(braveWalletService, blockchainRegistry,
|
||||
mChainId, TokenUtils.TokenType.ALL, tokens -> {
|
||||
tokens = Utils.fixupTokensRegistry(tokens, mChainId);
|
||||
@@ -238,10 +238,9 @@ public class EditVisibleAssetsBottomSheetDialogFragment extends BottomSheetDialo
|
||||
});
|
||||
});
|
||||
} else if (mType == WalletCoinAdapter.AdapterType.SEND_ASSETS_LIST) {
|
||||
// TODO: ERC721 Send
|
||||
assert mChainId != null && !mChainId.isEmpty();
|
||||
TokenUtils.getUserAssetsFiltered(braveWalletService, mChainId,
|
||||
TokenUtils.TokenType.ERC20,
|
||||
TokenUtils.TokenType.ALL,
|
||||
tokens -> { setUpAssetsList(view, tokens, new BlockchainToken[0]); });
|
||||
} else if (mType == WalletCoinAdapter.AdapterType.SWAP_TO_ASSETS_LIST
|
||||
|| mType == WalletCoinAdapter.AdapterType.SWAP_FROM_ASSETS_LIST) {
|
||||
|
||||
@@ -407,13 +407,21 @@ public class TxFragment extends Fragment {
|
||||
String.format(getResources().getString(R.string.crypto_wallet_gas_fee_amount),
|
||||
String.format(Locale.getDefault(), "%.8f", totalGas), mChainSymbol));
|
||||
String valueAsset = mTxInfo.txDataUnion.getEthTxData1559().baseData.value;
|
||||
// TODO (Wengling): Transaction Parser
|
||||
// (components/brave_wallet_ui/common/hooks/transaction-parser.ts)
|
||||
String valueAssetText =
|
||||
String.format(Locale.getDefault(), "%.8f", Utils.fromHexWei(valueAsset, mDecimals));
|
||||
if (mTxInfo.txType == TransactionType.ERC20_TRANSFER && mTxInfo.txArgs.length > 1) {
|
||||
valueAsset = mTxInfo.txArgs[1];
|
||||
valueAssetText = String.format(
|
||||
Locale.getDefault(), "%.8f", Utils.fromHexWei(valueAsset, mDecimals));
|
||||
} else if (mTxInfo.txType == TransactionType.ERC721_TRANSFER_FROM
|
||||
|| mTxInfo.txType == TransactionType.ERC721_SAFE_TRANSFER_FROM) {
|
||||
valueAssetText = "1";
|
||||
}
|
||||
TextView totalAmount = view.findViewById(R.id.total_amount);
|
||||
totalAmount.setText(String.format(
|
||||
getResources().getString(R.string.crypto_wallet_total_amount),
|
||||
String.format(Locale.getDefault(), "%.8f", Utils.fromHexWei(valueAsset, mDecimals)),
|
||||
getResources().getString(R.string.crypto_wallet_total_amount), valueAssetText,
|
||||
mAsset, String.format(Locale.getDefault(), "%.8f", totalGas), mChainSymbol));
|
||||
AssetRatioService assetRatioService = getAssetRatioService();
|
||||
if (assetRatioService != null) {
|
||||
|
||||
+5
-3
@@ -143,13 +143,15 @@ public class WalletListItemModel {
|
||||
return mIcon;
|
||||
}
|
||||
|
||||
// ERC721 has format [Title #ID]
|
||||
public String getTitle() {
|
||||
// ERC721 has format [Title #ID]
|
||||
return mTitle + (isErc721() ? "#" + Utils.hexToIntString(mId) : "");
|
||||
return isErc721() ? Utils.formatErc721TokenTitle(mTitle, Utils.hexToIntString(mId))
|
||||
: mTitle;
|
||||
}
|
||||
|
||||
public String getSubTitle() {
|
||||
return mSubTitle;
|
||||
return isErc721() ? Utils.formatErc721TokenTitle(mSubTitle, Utils.hexToIntString(mId))
|
||||
: mSubTitle;
|
||||
}
|
||||
|
||||
public String getText1() {
|
||||
|
||||
@@ -105,6 +105,19 @@ public class AsyncUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static class GetErc721TokenBalanceResponseContext extends GetBalanceResponseBaseContext
|
||||
implements JsonRpcService.GetErc721TokenBalance_Response {
|
||||
public GetErc721TokenBalanceResponseContext(Runnable responseCompleteCallback) {
|
||||
super(responseCompleteCallback);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void call(String balance, Integer error, String errorMessage) {
|
||||
warnWhenError(TAG, "getErc721TokenBalance", error, errorMessage);
|
||||
super.callBase(balance, error, errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
public static class GetBalanceResponseContext
|
||||
extends GetBalanceResponseBaseContext implements JsonRpcService.GetBalance_Response {
|
||||
public GetBalanceResponseContext(Runnable responseCompleteCallback) {
|
||||
|
||||
@@ -131,6 +131,16 @@ public class PortfolioHelper {
|
||||
contexts.add(context);
|
||||
mJsonRpcService.getBalance(
|
||||
accountInfo.address, CoinType.ETH, mChainId, context);
|
||||
} else if (userAsset.isErc721) {
|
||||
AsyncUtils.GetErc721TokenBalanceResponseContext context =
|
||||
new AsyncUtils.GetErc721TokenBalanceResponseContext(
|
||||
balancesMultiResponse.singleResponseComplete);
|
||||
context.userAsset = userAsset;
|
||||
contexts.add(context);
|
||||
mJsonRpcService.getErc721TokenBalance(
|
||||
Utils.getContractAddress(mChainId, userAsset.symbol,
|
||||
userAsset.contractAddress),
|
||||
userAsset.tokenId, accountInfo.address, mChainId, context);
|
||||
} else {
|
||||
AsyncUtils.GetErc20TokenBalanceResponseContext context =
|
||||
new AsyncUtils.GetErc20TokenBalanceResponseContext(
|
||||
@@ -149,18 +159,23 @@ public class PortfolioHelper {
|
||||
for (AsyncUtils.GetBalanceResponseBaseContext context : contexts) {
|
||||
String currentAssetSymbol =
|
||||
context.userAsset.symbol.toLowerCase(Locale.getDefault());
|
||||
String currentAssetKey = context.userAsset.isErc721
|
||||
? Utils.formatErc721TokenTitle(
|
||||
currentAssetSymbol, context.userAsset.tokenId)
|
||||
: currentAssetSymbol;
|
||||
|
||||
// Update crypto balances first
|
||||
int decimals = (context.userAsset.decimals != 0)
|
||||
int decimals =
|
||||
(context.userAsset.decimals != 0 || context.userAsset.isErc721)
|
||||
? context.userAsset.decimals
|
||||
: 18;
|
||||
Double thisBalanceCryptoPart = (context.error == ProviderError.SUCCESS)
|
||||
? fromHexWei(context.balance, decimals)
|
||||
: 0.0d;
|
||||
|
||||
Double prevThisTokenCryptoSum = Utils.getOrDefault(
|
||||
mPerTokenCryptoSum, currentAssetSymbol, 0.0d);
|
||||
mPerTokenCryptoSum.put(currentAssetSymbol,
|
||||
Double prevThisTokenCryptoSum =
|
||||
Utils.getOrDefault(mPerTokenCryptoSum, currentAssetKey, 0.0d);
|
||||
mPerTokenCryptoSum.put(currentAssetKey,
|
||||
prevThisTokenCryptoSum + thisBalanceCryptoPart);
|
||||
}
|
||||
|
||||
@@ -226,13 +241,17 @@ public class PortfolioHelper {
|
||||
for (BlockchainToken userAsset : mUserAssets) {
|
||||
String currentAssetSymbol =
|
||||
userAsset.symbol.toLowerCase(Locale.getDefault());
|
||||
String currentAssetKey = userAsset.isErc721
|
||||
? Utils.formatErc721TokenTitle(
|
||||
currentAssetSymbol, userAsset.tokenId)
|
||||
: currentAssetSymbol;
|
||||
Double usdPerThisToken = Utils.getOrDefault(
|
||||
tokenToUsdRatios, currentAssetSymbol, 0.0d);
|
||||
Double currentAssetBalance = Utils.getOrDefault(
|
||||
mPerTokenCryptoSum, currentAssetSymbol, 0.0d);
|
||||
mPerTokenCryptoSum, currentAssetKey, 0.0d);
|
||||
Double thisTokenFiatSum = usdPerThisToken * currentAssetBalance;
|
||||
|
||||
mPerTokenFiatSum.put(currentAssetSymbol, thisTokenFiatSum);
|
||||
mPerTokenFiatSum.put(currentAssetKey, thisTokenFiatSum);
|
||||
|
||||
mTotalFiatSum += thisTokenFiatSum;
|
||||
}
|
||||
|
||||
@@ -114,4 +114,17 @@ public class TokenUtils {
|
||||
|
||||
return both.toArray(new BlockchainToken[both.size()]);
|
||||
}
|
||||
|
||||
public static boolean isSameToken(BlockchainToken token1, BlockchainToken token2) {
|
||||
if (token1.chainId.equals(token2.chainId) && token1.symbol.equals(token2.symbol)
|
||||
&& token1.name.equals(token2.name)
|
||||
&& ((token1.tokenId.isEmpty() && token2.tokenId.isEmpty())
|
||||
|| token1.tokenId.equals(token2.tokenId))
|
||||
&& token1.contractAddress.equals(token2.contractAddress)
|
||||
&& token1.decimals == token2.decimals) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,6 +53,7 @@ import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import org.chromium.base.ApiCompatibilityUtils;
|
||||
import org.chromium.base.Callback;
|
||||
import org.chromium.base.ContextUtils;
|
||||
import org.chromium.base.Log;
|
||||
import org.chromium.base.Predicate;
|
||||
@@ -170,15 +171,14 @@ public class Utils {
|
||||
public static String getTextFromClipboard(Context context) {
|
||||
ClipboardManager clipboard =
|
||||
(ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
ClipData clipData = clipboard.getPrimaryClip();
|
||||
String pasteData = "";
|
||||
if (!(clipboard.hasPrimaryClip()) || clipboard.getPrimaryClipDescription() == null) {
|
||||
return pasteData;
|
||||
} else if (!(clipboard.getPrimaryClipDescription().hasMimeType(MIMETYPE_TEXT_PLAIN))) {
|
||||
return pasteData;
|
||||
if (clipData != null && clipData.getDescription().hasMimeType(MIMETYPE_TEXT_PLAIN)
|
||||
&& clipData.getItemCount() > 0) {
|
||||
return clipData.getItemAt(0).getText().toString();
|
||||
}
|
||||
ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
|
||||
|
||||
return item.getText().toString();
|
||||
return "";
|
||||
}
|
||||
|
||||
public static void clearClipboard(String textToCompare, int delay) {
|
||||
@@ -1072,6 +1072,8 @@ public class Utils {
|
||||
txInfo, jsonRpcService, activity, getAccountName(accountInfos, txInfo.fromAddress));
|
||||
}
|
||||
|
||||
// TODO (Wengling): this chain of functions can be cleaned-up with transaction parser
|
||||
// (components/brave_wallet_ui/common/hooks/transaction-parser.ts)
|
||||
public static void setUpTransactionList(AccountInfo[] accountInfos,
|
||||
AssetRatioService assetRatioService, TxService txService,
|
||||
BlockchainRegistry blockchainRegistry, BraveWalletService braveWalletService,
|
||||
@@ -1184,20 +1186,21 @@ public class Utils {
|
||||
String chainSymbol, int chainDecimals, WalletCoinAdapter walletTxCoinAdapter) {
|
||||
assert chainId != null;
|
||||
assert blockchainRegistry != null;
|
||||
// TODO: Transaction Parser (components/brave_wallet_ui/common/hooks/transaction-parser.ts)
|
||||
|
||||
TokenUtils.getAllTokensFiltered(braveWalletService, blockchainRegistry, chainId,
|
||||
TokenUtils.TokenType.ERC20, tokens -> {
|
||||
TokenUtils.TokenType.ALL, tokens -> {
|
||||
// Replace USDC and DAI contract addresses for Ropsten network
|
||||
tokens = fixupTokensRegistry(tokens, chainId);
|
||||
HashMap<String, String> assets = new HashMap<String, String>();
|
||||
HashMap<String, Integer> assetsDecimals = new HashMap<String, Integer>();
|
||||
for (String accountName : pendingTxInfos.keySet()) {
|
||||
TransactionInfo[] txInfos = pendingTxInfos.get(accountName);
|
||||
for (TransactionInfo txInfo : txInfos) {
|
||||
if (txInfo.txType == TransactionType.ERC20_TRANSFER
|
||||
|| txInfo.txType == TransactionType.ERC20_APPROVE) {
|
||||
|| txInfo.txType == TransactionType.ERC20_APPROVE
|
||||
|| txInfo.txType == TransactionType.ERC721_TRANSFER_FROM
|
||||
|| txInfo.txType == TransactionType.ERC721_SAFE_TRANSFER_FROM) {
|
||||
for (BlockchainToken token : tokens) {
|
||||
// Replace USDC and DAI contract addresses for Ropsten network
|
||||
token.contractAddress = getContractAddress(
|
||||
chainId, token.symbol, token.contractAddress);
|
||||
String symbol = token.symbol;
|
||||
int decimals = token.decimals;
|
||||
if (txInfo.txType == TransactionType.ERC20_APPROVE) {
|
||||
@@ -1325,6 +1328,17 @@ public class Utils {
|
||||
context.getResources().getString(R.string.wallet_tx_info_approved_unlimited),
|
||||
assetSymbol, "0x Exchange Proxy");
|
||||
valueToDisplay = "0.0000 " + assetSymbol;
|
||||
} else if ((txInfo.txType == TransactionType.ERC721_TRANSFER_FROM
|
||||
|| txInfo.txType == TransactionType.ERC721_SAFE_TRANSFER_FROM)
|
||||
&& txInfo.txArgs.length > 2) {
|
||||
to = txInfo.txArgs[1];
|
||||
String tokenId = txInfo.txArgs[2];
|
||||
valueToDisplay =
|
||||
String.format(Locale.getDefault(), "%d", (int) Utils.fromHexWei(tokenId, 0));
|
||||
action = String.format(
|
||||
context.getResources().getString(R.string.wallet_tx_info_sent_erc721),
|
||||
accountName, assetSymbol, valueToDisplay, strDate);
|
||||
detailInfo = accountName + " -> " + Utils.getAccountName(accountInfos, to);
|
||||
}
|
||||
if (txInfo.txDataUnion.getEthTxData1559()
|
||||
.baseData.to.toLowerCase(Locale.getDefault())
|
||||
@@ -1562,19 +1576,15 @@ public class Utils {
|
||||
|
||||
for (BlockchainToken userAsset : userAssets) {
|
||||
String currentAssetSymbol = userAsset.symbol.toLowerCase(Locale.getDefault());
|
||||
Double fiatBalance = Utils.getOrDefault(perTokenFiatSum, currentAssetSymbol, 0.0d);
|
||||
String currentAssetKey = userAsset.isErc721
|
||||
? Utils.formatErc721TokenTitle(currentAssetSymbol, userAsset.tokenId)
|
||||
: currentAssetSymbol;
|
||||
Double fiatBalance = Utils.getOrDefault(perTokenFiatSum, currentAssetKey, 0.0d);
|
||||
String fiatBalanceString = String.format(Locale.getDefault(), "$%,.2f", fiatBalance);
|
||||
Double cryptoBalance = Utils.getOrDefault(perTokenCryptoSum, currentAssetSymbol, 0.0d);
|
||||
Double cryptoBalance = Utils.getOrDefault(perTokenCryptoSum, currentAssetKey, 0.0d);
|
||||
String cryptoBalanceString =
|
||||
String.format(Locale.getDefault(), "%.4f %s", cryptoBalance, userAsset.symbol);
|
||||
|
||||
// Override amount to 1 for ERC721
|
||||
if (userAsset.isErc721) {
|
||||
fiatBalanceString = "1";
|
||||
cryptoBalanceString =
|
||||
String.format(Locale.getDefault(), "%d %s", 1, userAsset.symbol);
|
||||
}
|
||||
|
||||
WalletListItemModel walletListItemModel = new WalletListItemModel(R.drawable.ic_eth,
|
||||
userAsset.name, userAsset.symbol, userAsset.tokenId,
|
||||
// Amount in USD
|
||||
@@ -1595,4 +1605,29 @@ public class Utils {
|
||||
|
||||
return walletCoinAdapter;
|
||||
}
|
||||
|
||||
public static void getExactUserAsset(BraveWalletService braveWalletService, String chainId,
|
||||
String assetSymbol, String assetName, String assetId, String contractAddress,
|
||||
int assetDecimals, Callback<BlockchainToken> callback) {
|
||||
TokenUtils.getUserAssetsFiltered(
|
||||
braveWalletService, chainId, TokenUtils.TokenType.ALL, (userAssets) -> {
|
||||
BlockchainToken resultToken = null;
|
||||
for (BlockchainToken userAsset : userAssets) {
|
||||
if (chainId.equals(userAsset.chainId)
|
||||
&& assetSymbol.equals(userAsset.symbol)
|
||||
&& assetName.equals(userAsset.name)
|
||||
&& (assetId.isEmpty() || assetId.equals(userAsset.tokenId))
|
||||
&& contractAddress.equals(userAsset.contractAddress)
|
||||
&& assetDecimals == userAsset.decimals) {
|
||||
resultToken = userAsset;
|
||||
}
|
||||
}
|
||||
|
||||
callback.onResult(resultToken);
|
||||
});
|
||||
}
|
||||
|
||||
public static String formatErc721TokenTitle(String title, String id) {
|
||||
return title + " #" + id;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,6 +96,7 @@
|
||||
android:baselineAligned="false">
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/from_value_block"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
|
||||
Reference in New Issue
Block a user