mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-10-26 06:08:40 +01:00
Pay anything fixes (#3224)
* fix: Default to current chain for generic evm format addresses * fix: Handle pay anything triggers in monero.com builds
This commit is contained in:
@@ -77,48 +77,77 @@ class _TokenSelectionContentState extends State<_TokenSelectionContent> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
selectedNetwork = widget.fixedNetwork ?? WalletType.ethereum;
|
||||
final baseNetwork = widget.fixedNetwork ?? WalletType.ethereum;
|
||||
selectedNetwork = _resolveGenericETHDetectionResultToSpecificChain(baseNetwork);
|
||||
_autoSelectToken();
|
||||
}
|
||||
|
||||
bool get _isNetworkSelectable =>
|
||||
widget.fixedNetwork == null || isEVMCompatibleChain(widget.fixedNetwork!);
|
||||
|
||||
CryptoCurrency? _tokenMatchingContract(List<CryptoCurrency> tokens, String? rawContract) {
|
||||
final trimmed = rawContract?.trim();
|
||||
if (trimmed == null || trimmed.isEmpty) return null;
|
||||
|
||||
for (final token in tokens) {
|
||||
if (token is Erc20Token && token.contractAddress.toLowerCase() == trimmed.toLowerCase()) {
|
||||
return token;
|
||||
} else if (token is SPLToken && token.mintAddress == trimmed) {
|
||||
return token;
|
||||
} else if (token is TronToken && token.contractAddress == trimmed) {
|
||||
return token;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
WalletType _resolveGenericETHDetectionResultToSpecificChain(WalletType network) {
|
||||
if (network != WalletType.ethereum) return network;
|
||||
final current = widget.paymentViewModel.currentWalletType;
|
||||
if (isEVMCompatibleChain(current)) return current;
|
||||
return network;
|
||||
}
|
||||
|
||||
Future<void> _autoSelectToken() async {
|
||||
if (selectedNetwork == null) return;
|
||||
final initialNetwork = selectedNetwork;
|
||||
if (initialNetwork == null || !mounted) return;
|
||||
|
||||
final network = _resolveGenericETHDetectionResultToSpecificChain(initialNetwork);
|
||||
|
||||
setState(() {
|
||||
selectedNetwork = network;
|
||||
isLoadingTokens = true;
|
||||
tokenLoadError = null;
|
||||
});
|
||||
|
||||
try {
|
||||
final tokens = await TokenUtilities.getAvailableTokensForNetwork(
|
||||
selectedNetwork!,
|
||||
);
|
||||
final tokens = await TokenUtilities.getAvailableTokensForNetwork(network);
|
||||
if (!mounted) return;
|
||||
|
||||
if (tokens.isEmpty) {
|
||||
setState(() {
|
||||
tokenLoadError = 'No tokens available';
|
||||
isLoadingTokens = false;
|
||||
selectedToken = null;
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
final matchedToken =
|
||||
_tokenMatchingContract(tokens, widget.paymentRequest.contractAddress) ?? tokens.first;
|
||||
|
||||
setState(() {
|
||||
selectedToken = tokens.firstWhere((e) {
|
||||
return (e is Erc20Token && e.contractAddress == widget.paymentRequest.contractAddress) ||
|
||||
(e is SPLToken && e.mintAddress == widget.paymentRequest.contractAddress) ||
|
||||
(e is TronToken && e.contractAddress == widget.paymentRequest.contractAddress);
|
||||
}, orElse: () => tokens.first);
|
||||
selectedToken = matchedToken;
|
||||
isLoadingTokens = false;
|
||||
tokenLoadError = null;
|
||||
});
|
||||
} catch (e) {
|
||||
printV('Auto-select token error: $e');
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
tokenLoadError = 'Failed to load tokens';
|
||||
isLoadingTokens = false;
|
||||
selectedToken = walletTypeToCryptoCurrency(selectedNetwork!);
|
||||
selectedToken = walletTypeToCryptoCurrency(network);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ import 'dart:async';
|
||||
import 'package:cake_wallet/core/universal_address_detector.dart';
|
||||
import 'package:cake_wallet/evm/evm.dart';
|
||||
import 'package:cake_wallet/reactions/wallet_connect.dart';
|
||||
import 'package:cake_wallet/solana/solana.dart';
|
||||
import 'package:cake_wallet/tron/tron.dart';
|
||||
import 'package:cake_wallet/store/app_store.dart';
|
||||
import 'package:cw_core/wallet_type.dart';
|
||||
import 'package:cw_core/wallet_info.dart';
|
||||
@@ -41,7 +43,7 @@ abstract class PaymentViewModelBase with Store {
|
||||
return getChainIdByCryptoCurrency(_lastDetectionResult!.detectedCurrency!);
|
||||
}
|
||||
|
||||
return evm!.getChainIdByWalletType(detectedWalletType!);
|
||||
return evm?.getChainIdByWalletType(detectedWalletType!);
|
||||
}
|
||||
|
||||
@observable
|
||||
@@ -52,7 +54,7 @@ abstract class PaymentViewModelBase with Store {
|
||||
|
||||
@computed
|
||||
int? get currentChainId {
|
||||
if (!isEVMCompatibleChain(currentWalletType)) return null;
|
||||
if (!isEVMCompatibleChain(currentWalletType) || evm == null) return null;
|
||||
|
||||
return evm!.getSelectedChainId(appStore.wallet!);
|
||||
}
|
||||
@@ -74,6 +76,18 @@ abstract class PaymentViewModelBase with Store {
|
||||
return PaymentFlowResult.incompatible('Unable to detect address type');
|
||||
}
|
||||
|
||||
if (detectedWalletType == WalletType.solana && solana == null) {
|
||||
return PaymentFlowResult.incompatible('Solana is not available in this app build.');
|
||||
}
|
||||
if (detectedWalletType == WalletType.tron && tron == null) {
|
||||
return PaymentFlowResult.incompatible('Tron is not available in this app build.');
|
||||
}
|
||||
if (isEVMCompatibleChain(detectedWalletType!) && evm == null) {
|
||||
return PaymentFlowResult.incompatible(
|
||||
'Ethereum-compatible addresses are not supported in this app build.',
|
||||
);
|
||||
}
|
||||
|
||||
final currentWallet = appStore.wallet;
|
||||
|
||||
if (currentWallet != null &&
|
||||
@@ -124,7 +138,7 @@ abstract class PaymentViewModelBase with Store {
|
||||
|
||||
@action
|
||||
Future<void> selectChain() async {
|
||||
if (detectedWalletType == null) return;
|
||||
if (detectedWalletType == null || evm == null || detectedChainId == null) return;
|
||||
|
||||
final node =
|
||||
appStore.settingsStore.getCurrentNode(detectedWalletType!, chainId: detectedChainId);
|
||||
@@ -173,7 +187,7 @@ class PaymentFlowResult {
|
||||
chainId = getChainIdByCryptoCurrency(addressDetectionResult.detectedCurrency!);
|
||||
}
|
||||
if (chainId == null && addressDetectionResult.detectedWalletType != null) {
|
||||
chainId = evm!.getChainIdByWalletType(addressDetectionResult.detectedWalletType!);
|
||||
chainId = evm?.getChainIdByWalletType(addressDetectionResult.detectedWalletType!);
|
||||
}
|
||||
|
||||
return PaymentFlowResult._(
|
||||
@@ -228,7 +242,7 @@ class PaymentFlowResult {
|
||||
chainId = getChainIdByCryptoCurrency(addressDetectionResult.detectedCurrency!);
|
||||
}
|
||||
if (chainId == null) {
|
||||
chainId = evm!.getChainIdByWalletType(wallet.type);
|
||||
chainId = evm?.getChainIdByWalletType(wallet.type);
|
||||
}
|
||||
|
||||
return PaymentFlowResult._(
|
||||
@@ -250,7 +264,7 @@ class PaymentFlowResult {
|
||||
chainId = getChainIdByCryptoCurrency(addressDetectionResult.detectedCurrency!);
|
||||
}
|
||||
if (chainId == null) {
|
||||
chainId = evm!.getChainIdByWalletType(wallets.first.type);
|
||||
chainId = evm?.getChainIdByWalletType(wallets.first.type);
|
||||
}
|
||||
|
||||
return PaymentFlowResult._(
|
||||
@@ -272,7 +286,7 @@ class PaymentFlowResult {
|
||||
chainId = getChainIdByCryptoCurrency(addressDetectionResult.detectedCurrency!);
|
||||
}
|
||||
if (chainId == null) {
|
||||
chainId = evm!.getChainIdByWalletType(walletType);
|
||||
chainId = evm?.getChainIdByWalletType(walletType);
|
||||
}
|
||||
|
||||
return PaymentFlowResult._(
|
||||
|
||||
Reference in New Issue
Block a user