Step 4: Initialize the Provider on Component Mount
Add a useEffect hook to initialize the provider when the component mounts:
useEffect(() => {
initializeProvider()
.then((prov) => {
console.log('Provider initialized successfully');
setProvider(prov);
// Check if we already have an active session
const activeSessions = Object.values(prov.session || {});
if (activeSessions.length > 0) {
console.log('Found active session:', activeSessions[0]);
setSession(activeSessions[0] as SessionTypes.Struct);
// Extract account if available
const starknetAccounts =
activeSessions[0]?.namespaces?.starknet?.accounts;
if (starknetAccounts && starknetAccounts.length > 0) {
const accountAddress = starknetAccounts[0].split(':')[2];
setAccount(accountAddress);
}
}
})
.catch((err) => {
console.error('Provider initialization failed:', err);
setError('Setup failed: ' + (err?.message || 'Unknown error'));
});
}, []);
Step 5: Implement Deep Linking to Argent Mobile
Add a function to open the Argent Mobile wallet:
const openWallet = async (uri: string) => {
const encodedUri = encodeURIComponent(uri);
// Use Argent's deep link scheme
const argentScheme = `argent://wc?uri=${encodedUri}`;
console.log('Opening Argent with scheme:', argentScheme);
try {
await Linking.openURL(argentScheme);
console.log('Successfully opened Argent');
} catch (err) {
console.error('Failed to open Argent:', err);
setError(
'Failed to open Argent wallet. Please make sure it is installed.'
);
}
};
Step 6: Connect to Argent Mobile
const connect = async () => {
if (!provider) return;
try {
// Request connection
const { uri, approval } = await provider.client.connect({
requiredNamespaces: {
starknet: {
chains: ['starknet:SNMAIN'], // Use SNSEPL for Sepolia testnet
methods: ['starknet_account', 'starknet_requestAddInvokeTransaction'],
events: ['accountsChanged', 'chainChanged']
}
}
});
// Open wallet with URI
if (uri) {
const encodedUri = encodeURIComponent(uri);
await Linking.openURL(`argent://wc?uri=${encodedUri}`);
}
// Wait for approval
const newSession = await approval();
setSession(newSession);
// Extract account
if (newSession?.namespaces?.starknet?.accounts?.length > 0) {
setAccount(newSession.namespaces.starknet.accounts[0].split(':')[2]);
}
} catch (err) {
setError(err.message);
}
};