Skip to content

Commit

Permalink
Merge pull request #5343 from WalletConnect/feat/init-event
Browse files Browse the repository at this point in the history
feat: init event
  • Loading branch information
ganchoradkov authored Sep 16, 2024
2 parents 720b72e + 4a96e1f commit ed0fae5
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 15 deletions.
48 changes: 39 additions & 9 deletions packages/core/src/controllers/events.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { generateChildLogger, Logger } from "@walletconnect/logger";
import { ICore, IEventClient, EventClientTypes } from "@walletconnect/types";
import { uuidv4 } from "@walletconnect/utils";
import { formatUA, isTestRun, uuidv4 } from "@walletconnect/utils";
import {
CORE_STORAGE_PREFIX,
EVENTS_CLIENT_API_URL,
Expand All @@ -16,12 +16,12 @@ export class EventClient extends IEventClient {
public readonly context = EVENTS_STORAGE_CONTEXT;
private readonly storagePrefix = CORE_STORAGE_PREFIX;
private readonly storageVersion = EVENTS_STORAGE_VERSION;

private events = new Map<string, EventClientTypes.Event>();
private shouldPersist = false;
constructor(public core: ICore, public logger: Logger, telemetryEnabled = true) {
super(core, logger, telemetryEnabled);
this.logger = generateChildLogger(logger, this.context);
this.telemetryEnabled = telemetryEnabled;
if (telemetryEnabled) {
this.restore().then(async () => {
await this.submit();
Expand All @@ -39,6 +39,31 @@ export class EventClient extends IEventClient {
);
}

public init: IEventClient["init"] = async () => {
if (isTestRun()) return;
try {
const initEvent = {
eventId: uuidv4(),
timestamp: Date.now(),
props: {
event: "INIT",
type: "",
properties: {
client_id: await this.core.crypto.getClientId(),
user_agent: formatUA(
this.core.relayer.protocol,
this.core.relayer.version,
RELAYER_SDK_VERSION,
),
},
},
};
await this.sendEvent([initEvent] as unknown as EventClientTypes.Event[]);
} catch (error) {
this.logger.warn(error);
}
};

public createEvent: IEventClient["createEvent"] = (params) => {
const {
event = "ERROR",
Expand Down Expand Up @@ -172,13 +197,7 @@ export class EventClient extends IEventClient {
if (eventsToSend.length === 0) return;

try {
const response = await fetch(
`${EVENTS_CLIENT_API_URL}?projectId=${this.core.projectId}&st=events_sdk&sv=js-${RELAYER_SDK_VERSION}`,
{
method: "POST",
body: JSON.stringify(eventsToSend),
},
);
const response = await this.sendEvent(eventsToSend);
if (response.ok) {
for (const event of eventsToSend) {
this.events.delete(event.eventId);
Expand All @@ -189,4 +208,15 @@ export class EventClient extends IEventClient {
this.logger.warn(error);
}
};

private sendEvent = async (events: EventClientTypes.Event[]) => {
const response = await fetch(
`${EVENTS_CLIENT_API_URL}?projectId=${this.core.projectId}&st=events_sdk&sv=js-${RELAYER_SDK_VERSION}`,
{
method: "POST",
body: JSON.stringify(events),
},
);
return response;
};
}
4 changes: 2 additions & 2 deletions packages/core/src/controllers/verify.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { generateChildLogger, getLoggerContext, Logger } from "@walletconnect/logger";
import { ICore, IVerify } from "@walletconnect/types";
import { isBrowser, isNode, P256KeyDataType, verifyP256Jwt } from "@walletconnect/utils";
import { isBrowser, isTestRun, P256KeyDataType, verifyP256Jwt } from "@walletconnect/utils";
import { FIVE_SECONDS, ONE_SECOND, toMiliseconds } from "@walletconnect/time";
import { getDocument } from "@walletconnect/window-getters";
import { decodeJWT } from "@walletconnect/relay-auth";
Expand Down Expand Up @@ -40,7 +40,7 @@ export class Verify extends IVerify {
super(core, logger, store);
this.logger = generateChildLogger(logger, this.name);
this.abortController = new AbortController();
this.isDevEnv = isNode() && process.env.IS_VITEST;
this.isDevEnv = isTestRun();
this.init();
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export class Core extends ICore {
await this.relayer.init();
await this.heartbeat.init();
await this.pairing.init();
this.eventClient.init();
this.linkModeSupportedApps = (await this.storage.getItem(WALLETCONNECT_LINK_MODE_APPS)) || [];

this.initialized = true;
Expand Down
22 changes: 22 additions & 0 deletions packages/core/test/events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,26 @@ describe("Events Client", () => {
// @ts-expect-error - accessing private properties
expect(core.eventClient.events.size).toBe(0);
});

it("should send init event", async () => {
process.env.IS_VITEST = false as any;
const core = new Core({ ...TEST_CORE_OPTIONS, telemetryEnabled: false });
let initCalled = false;
// @ts-expect-error - accessing private properties
core.eventClient.sendEvent = async (payload: any) => {
initCalled = true;
expect(payload).toBeDefined();
expect(payload.length).to.eql(1);
expect(payload[0].props.event).to.eql("INIT");
expect(payload[0].props.properties.client_id).to.eql(await core.crypto.getClientId());
};
await core.start();
await new Promise((resolve) => setTimeout(resolve, 500));

if (!initCalled) {
throw new Error("init not called");
}

process.env.IS_VITEST = true as any;
});
});
6 changes: 2 additions & 4 deletions packages/sign-client/src/controllers/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ import {
BASE64URL,
getSearchParamFromURL,
isReactNative,
isTestRun,
} from "@walletconnect/utils";
import EventEmmiter from "events";
import {
Expand Down Expand Up @@ -2914,10 +2915,7 @@ export class Engine extends IEngine {
};

private registerLinkModeListeners = async () => {
if (
(typeof process !== "undefined" && process.env.IS_VITEST) ||
(isReactNative() && this.client.metadata.redirect?.linkMode)
) {
if (isTestRun() || (isReactNative() && this.client.metadata.redirect?.linkMode)) {
const linking = (global as any)?.Linking;
// global.Linking is set by react-native-compat
if (typeof linking !== "undefined") {
Expand Down
2 changes: 2 additions & 0 deletions packages/types/src/core/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export abstract class IEventClient {

constructor(public core: ICore, public logger: Logger, public telemetryEnabled: boolean) {}

public abstract init(): Promise<void>;

public abstract createEvent(params: {
event?: "ERROR";
type?: string;
Expand Down
4 changes: 4 additions & 0 deletions packages/types/src/core/relayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ export interface RelayerClientMetadata {
}

export abstract class IRelayer extends IEvents {
public abstract protocol: string;

public abstract version: number;

public abstract core: ICore;

public abstract logger: Logger;
Expand Down
4 changes: 4 additions & 0 deletions packages/utils/src/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,7 @@ export function uuidv4() {
return v.toString(16);
});
}

export function isTestRun() {
return typeof process !== "undefined" && process.env.IS_VITEST === "true";
}

0 comments on commit ed0fae5

Please sign in to comment.