Skip to content

Commit 5cce3b3

Browse files
authored
Merge pull request #126 from Francis6-git/fix/issue-25-connection-typing
refactor: replace unsafe 'any' type with WalletConnection in CryptoPa…
2 parents b5ec308 + b118112 commit 5cce3b3

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/screens/CryptoPaymentScreen.tsx

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ import { useNavigation, useRoute } from '@react-navigation/native';
1515
import { colors, spacing, typography, borderRadius } from '../utils/constants';
1616
import { Button } from '../components/common/Button';
1717
import { Card } from '../components/common/Card';
18-
import walletServiceManager, { TokenBalance, GasEstimate } from '../services/walletService';
19-
import { ethers } from 'ethers';
18+
import walletServiceManager, {
19+
TokenBalance,
20+
GasEstimate,
21+
WalletConnection
22+
} from '../services/walletService';
2023

2124
interface RouteParams {
2225
subscriptionId?: string;
@@ -42,7 +45,7 @@ const CryptoPaymentScreen: React.FC = () => {
4245
const [isLoading, setIsLoading] = useState(false);
4346

4447
const [availableTokens, setAvailableTokens] = useState<TokenBalance[]>([]);
45-
const [connection, setConnection] = useState<any>(null);
48+
const [connection, setConnection] = useState<WalletConnection | null>(null);
4649

4750
useEffect(() => {
4851
loadWalletData();
@@ -54,10 +57,15 @@ const CryptoPaymentScreen: React.FC = () => {
5457
}
5558
}, [amount, recipientAddress, connection, selectedProtocol, selectedToken]);
5659

60+
// Internal validation for type narrowing
61+
const isWalletConnected = (conn: WalletConnection | null): conn is WalletConnection => {
62+
return conn !== null && conn.isConnected;
63+
};
64+
5765
const loadWalletData = async () => {
5866
try {
5967
const conn = walletServiceManager.getConnection();
60-
if (!conn) {
68+
if (!conn || !conn.isConnected) {
6169
Alert.alert('Error', 'Please connect a wallet first');
6270
navigation.goBack();
6371
return;
@@ -76,7 +84,7 @@ const CryptoPaymentScreen: React.FC = () => {
7684
};
7785

7886
const estimateGas = async () => {
79-
if (!connection || !amount || !recipientAddress) return;
87+
if (!isWalletConnected(connection) || !amount || !recipientAddress) return;
8088

8189
try {
8290
if (selectedProtocol === 'superfluid') {
@@ -131,6 +139,11 @@ const CryptoPaymentScreen: React.FC = () => {
131139

132140
const handleCreateStream = async () => {
133141
if (!validateForm()) return;
142+
143+
if (!isWalletConnected(connection)) {
144+
Alert.alert('Error', 'Wallet not connected');
145+
return;
146+
}
134147

135148
try {
136149
setIsLoading(true);
@@ -308,7 +321,7 @@ const CryptoPaymentScreen: React.FC = () => {
308321
)}
309322

310323
{/* Create Stream Button */}
311-
<View style={styles.footer}>
324+
<div style={styles.footer}>
312325
<Button
313326
title={isLoading ? 'Creating Stream...' : 'Create Payment Stream'}
314327
onPress={handleCreateStream}
@@ -317,7 +330,7 @@ const CryptoPaymentScreen: React.FC = () => {
317330
fullWidth
318331
size="large"
319332
/>
320-
</View>
333+
</div>
321334
</ScrollView>
322335
</KeyboardAvoidingView>
323336
</SafeAreaView>

0 commit comments

Comments
 (0)