Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const REGIONALIZED_RELAYER_ENDPOINTS: RelayerType[] = [
];

export const EXTEND_PAIRING = 60 * 60 * 24 * 30; // 30 days
export const SESSION_PING_INTERVAL = 30 * 1000; // 30 seconds

export const FIRST_CHAIN_ID_SYMBOL = 2;
export const LAST_CHAIN_ID_SYMBOL = 34;
58 changes: 57 additions & 1 deletion src/renderer/features/wallet-connect-wallet/model/signClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
DEFAULT_PROJECT_ID,
DEFAULT_RELAY_URL,
EXTEND_PAIRING,
SESSION_PING_INTERVAL,
} from '../lib/constants';

import { walletConnectWalletFeature } from './feature';
Expand All @@ -17,6 +18,8 @@ const $client = createStore<Client | null>(null);
const changeConnectionStatus = createEvent<boolean>();
const $connected = restore(changeConnectionStatus, false);

let pingInterval: NodeJS.Timeout | null = null;

const createClientFx = createEffect(() => {
const core = new Core({
logger: DEFAULT_LOGGER,
Expand Down Expand Up @@ -56,14 +59,67 @@ const extendSessionsFx = createEffect(async (client: Client) => {
);
});

const pingSessionsFx = createEffect(async (client: Client) => {
const sessions = client.session.getAll();

await Promise.all(
sessions.map(async session => {
try {
await client.ping({ topic: session.topic });
console.log(`Pinged session ${session.topic.substring(0, 8)}...`);
} catch (error) {
console.warn(`Failed to ping session ${session.topic.substring(0, 8)}:`, error);
}
}),
);
});

const startPingIntervalFx = createEffect((client: Client) => {
// Clear existing interval if any
if (pingInterval) {
clearInterval(pingInterval);
}

// Start periodic ping
pingInterval = setInterval(() => {
pingSessionsFx(client);
}, SESSION_PING_INTERVAL);

console.log(`Started WalletConnect session ping with ${SESSION_PING_INTERVAL}ms interval`);
});

const stopPingIntervalFx = createEffect(() => {
if (pingInterval) {
clearInterval(pingInterval);
pingInterval = null;
console.log('Stopped WalletConnect session ping');
}
});

sample({
clock: walletConnectWalletFeature.running,
target: createClientFx,
});

sample({
clock: createClientFx.doneData,
target: [$client, extendSessionsFx],
target: [$client, extendSessionsFx, startPingIntervalFx],
});

// Start ping when connection is established
sample({
clock: changeConnectionStatus,
source: $client,
filter: (client, isConnected) => isConnected && client !== null,
fn: client => client!,
target: startPingIntervalFx,
});

// Stop ping when disconnected
sample({
clock: changeConnectionStatus,
filter: isConnected => !isConnected,
target: stopPingIntervalFx,
});

export const signClient = {
Expand Down
Loading