Skip to content

Commit

Permalink
refactor: uses calcExpiry for the new pairing
Browse files Browse the repository at this point in the history
  • Loading branch information
ganchoradkov committed Aug 28, 2024
1 parent 5125309 commit 9645dbf
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions packages/core/src/controllers/relayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ import {
Logger,
} from "@walletconnect/logger";
import { RelayJsonRpc } from "@walletconnect/relay-api";
import { ONE_MINUTE, ONE_SECOND, THIRTY_SECONDS, toMiliseconds } from "@walletconnect/time";
import {
FIVE_MINUTES,
ONE_MINUTE,
ONE_SECOND,
THIRTY_SECONDS,
toMiliseconds,
} from "@walletconnect/time";
import {
ICore,
IMessageTracker,
Expand All @@ -38,6 +44,7 @@ import {
getBundleId,
getInternalError,
isNode,
calcExpiry,
} from "@walletconnect/utils";

import {
Expand Down Expand Up @@ -348,6 +355,22 @@ export class Relayer extends IRelayer {
this.logger.trace(`Batch of ${sortedMessages.length} message events processed`);
}

public async onLinkMessageEvent(
messageEvent: RelayerTypes.MessageEvent,
opts: { sessionExists: boolean },
) {
const { topic } = messageEvent;

if (!opts.sessionExists) {
const expiry = calcExpiry(FIVE_MINUTES);
const pairing = { topic, expiry, relay: { protocol: "irn" }, active: false };
await this.core.pairing.pairings.set(topic, pairing);
}

this.events.emit(RELAYER_EVENTS.message, messageEvent);
await this.recordMessageEvent(messageEvent);
}

// ---------- Private ----------------------------------------------- //
/*
* In Node, we must detect when the connection is stalled and terminate it.
Expand Down Expand Up @@ -472,22 +495,6 @@ export class Relayer extends IRelayer {
await this.recordMessageEvent(messageEvent);
}

public async onLinkMessageEvent(
messageEvent: RelayerTypes.MessageEvent,
opts: { sessionExists: boolean },
) {
const { topic } = messageEvent;

if (!opts.sessionExists) {
const expiry = Math.floor(Date.now() / 1000) + 604800;
const pairing = { topic, expiry, relay: { protocol: "irn" }, active: false };
await this.core.pairing.pairings.set(topic, pairing);
}

this.events.emit(RELAYER_EVENTS.message, messageEvent);
await this.recordMessageEvent(messageEvent);
}

private async acknowledgePayload(payload: JsonRpcPayload) {
const response = formatJsonRpcResult(payload.id, true);
await this.provider.connection.send(response);
Expand Down

0 comments on commit 9645dbf

Please sign in to comment.