From acc676f78f6488f931a268ebfaddf033321a874e Mon Sep 17 00:00:00 2001 From: Francis6-git Date: Tue, 24 Mar 2026 15:19:34 +0100 Subject: [PATCH] refactor: replace unsafe 'any' type with WalletConnection in CryptoPaymentScreen #25 --- src/screens/CryptoPaymentScreen.tsx | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/screens/CryptoPaymentScreen.tsx b/src/screens/CryptoPaymentScreen.tsx index cfedcf4..13c4119 100644 --- a/src/screens/CryptoPaymentScreen.tsx +++ b/src/screens/CryptoPaymentScreen.tsx @@ -16,7 +16,11 @@ import { useNavigation, useRoute } from '@react-navigation/native'; import { colors, spacing, typography, borderRadius, shadows } from '../utils/constants'; import { Button } from '../components/common/Button'; import { Card } from '../components/common/Card'; -import walletServiceManager, { TokenBalance, GasEstimate } from '../services/walletService'; +import walletServiceManager, { + TokenBalance, + GasEstimate, + WalletConnection +} from '../services/walletService'; interface RouteParams { subscriptionId?: string; @@ -43,7 +47,7 @@ const CryptoPaymentScreen: React.FC = () => { const [isEstimatingGas, setIsEstimatingGas] = useState(false); const [availableTokens, setAvailableTokens] = useState([]); - const [connection, setConnection] = useState(null); + const [connection, setConnection] = useState(null); useEffect(() => { loadWalletData(); @@ -55,10 +59,15 @@ const CryptoPaymentScreen: React.FC = () => { } }, [amount, recipientAddress, connection, selectedProtocol, selectedToken]); + // Internal validation for type narrowing + const isWalletConnected = (conn: WalletConnection | null): conn is WalletConnection => { + return conn !== null && conn.isConnected; + }; + const loadWalletData = async () => { try { const conn = walletServiceManager.getConnection(); - if (!conn) { + if (!conn || !conn.isConnected) { Alert.alert('Error', 'Please connect a wallet first'); navigation.goBack(); return; @@ -77,7 +86,7 @@ const CryptoPaymentScreen: React.FC = () => { }; const estimateGas = async () => { - if (!connection || !amount || !recipientAddress) return; + if (!isWalletConnected(connection) || !amount || !recipientAddress) return; try { setIsEstimatingGas(true); @@ -135,6 +144,11 @@ const CryptoPaymentScreen: React.FC = () => { const handleCreateStream = async () => { if (!validateForm()) return; + + if (!isWalletConnected(connection)) { + Alert.alert('Error', 'Wallet not connected'); + return; + } try { setIsLoading(true); @@ -324,7 +338,7 @@ const CryptoPaymentScreen: React.FC = () => { )} {/* Create Stream Button */} - +