Skip to content

Commit 6ef700c

Browse files
authored
fix: fix metrics error on walletconnectNotify subscribers fetch error (#167)
* fix: fix metrics error on walletconnectNotify subscribers fetch error * fix: remove extra invalid character
1 parent 74ed462 commit 6ef700c

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

src/helpers/metrics.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ new client.Gauge({
4949
)
5050
)[0].count as any
5151
);
52-
this.set(
53-
{ type: 'walletconnect' },
54-
(await getSubscribersFromWalletConnect()).length
55-
);
52+
53+
try {
54+
const subscribers = await getSubscribersFromWalletConnect();
55+
this.set({ type: 'walletconnect' }, subscribers.length);
56+
} catch (e) {}
5657
}
5758
});
5859

src/providers/walletconnectNotify.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import fetch from 'node-fetch';
2+
import snapshot from '@snapshot-labs/snapshot.js';
23
import { capture } from '@snapshot-labs/snapshot-sentry';
34
import { timeOutgoingRequest } from '../helpers/metrics';
5+
import type { Event } from '../types';
46

57
const WALLETCONNECT_NOTIFY_SERVER_URL =
68
process.env.WALLETCONNECT_NOTIFY_SERVER_URL;
@@ -21,13 +23,6 @@ const PER_SECOND_RATE_LIMIT = 2;
2123
const WAIT_ERROR_MARGIN = 0.25;
2224
const WAIT_TIME = 1 / PER_SECOND_RATE_LIMIT + WAIT_ERROR_MARGIN;
2325

24-
// Rate limiting logic:
25-
async function wait(seconds: number) {
26-
return new Promise<void>(resolve => {
27-
setTimeout(resolve, seconds * 1_000);
28-
});
29-
}
30-
3126
// Fetch subscribers from WalletConnect Notify server
3227
export async function getSubscribersFromWalletConnect() {
3328
const fetchSubscribersUrl = `${WALLETCONNECT_NOTIFY_SERVER_URL}/${WALLETCONNECT_PROJECT_ID}/subscribers`;
@@ -41,7 +36,8 @@ export async function getSubscribersFromWalletConnect() {
4136

4237
return subscribers;
4338
} catch (e) {
44-
capture('[WalletConnect] failed to fetch subscribers');
39+
capture(e);
40+
console.log('[WalletConnect] failed to fetch subscribers');
4541
return [];
4642
}
4743
}
@@ -84,7 +80,7 @@ async function queueNotificationsToSend(notification, accounts: string[]) {
8480
accounts.slice(i, i + MAX_ACCOUNTS_PER_REQUEST)
8581
);
8682

87-
await wait(WAIT_TIME);
83+
await snapshot.utils.sleep(WAIT_TIME);
8884
}
8985
}
9086

@@ -113,14 +109,15 @@ export async function sendNotification(notification, accounts) {
113109
success = true;
114110
return notifySuccess;
115111
} catch (e) {
116-
capture('[WalletConnect] failed to notify subscribers', e);
112+
capture(e);
113+
console.log('[WalletConnect] failed to notify subscribers', e);
117114
} finally {
118115
end({ status: success ? 200 : 500 });
119116
}
120117
}
121118

122119
// Transform proposal event into notification format.
123-
function formatMessage(event, proposal) {
120+
function formatMessage(event: Event, proposal) {
124121
const space = proposal.space;
125122
if (!space) return null;
126123

@@ -137,7 +134,7 @@ function formatMessage(event, proposal) {
137134
};
138135
}
139136

140-
export async function send(event, proposal, subscribers) {
137+
export async function send(event: Event, proposal, subscribers: string[]) {
141138
if (event.event !== 'proposal/start') return;
142139
const crossReferencedSubscribers = await crossReferenceSubscribers(
143140
proposal.space,

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export type Event = {
2+
event: string;
3+
space: string;
4+
id: string;
5+
};

0 commit comments

Comments
 (0)