import axios from "axios" ; import { Logger } from "pino" ; import { generateChildLogger } from "@walletconnect/logger" ; import { LegacySocketMessage , Notification } from "./types" ; import { HttpService } from "./http" ; import { JSONRPC_EVENTS , LEGACY_EVENTS , NOTIFICATION_CONTEXT } from "./constants" ; import { RelayJsonRpc } from "@walletconnect/relay-api" ; export class NotificationService { public context = NOTIFICATION_CONTEXT ; constructor ( public server , public logger ) { this . server = server ; this . logger = generateChildLogger ( logger , this . context ) ; this . initialize ( ) ; } public async register ( topic , webhook ) { await this . server . redis . setNotification ( { topic , webhook } ) ; } public async push ( topic ) { const notifications = await this . server . redis . getNotification ( topic ) ; if ( notifications && notifications . length ) { notifications . forEach ( ( notification ) => { axios . post ( notification . webhook , { topic } ) ; this . logger . info ( { type : "push" , webhook : notification . webhook , topic , } ) ; } ) ; } } private initialize ( ) { this . logger . trace ( `Initialized` ) ; this . registerEventListeners ( ) ; } private registerEventListeners ( ) { this . server . events . on ( LEGACY_EVENTS . publish , async ( socketId , message ) => { if ( ! message . silent ) { await this . server . notification . push ( message . topic ) ; } } ) ; this . server . events . on ( JSONRPC_EVENTS . publish , async ( params ) => { await this . server . notification . push ( params . topic ) ; } ) ; } }