Skip to content

Commit 65a3570

Browse files
authored
fix: avoid expensive network requests by using chain agnostic notify (#170)
* fix: use chain agnostic notify * fix: lint * chore: remove metrics polling
1 parent 098f522 commit 65a3570

File tree

2 files changed

+4
-65
lines changed

2 files changed

+4
-65
lines changed

src/helpers/metrics.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import init, { client } from '@snapshot-labs/snapshot-metrics';
22
import type { Express } from 'express';
33
import { capture } from '@snapshot-labs/snapshot-sentry';
44
import db from './mysql';
5-
import { getSubscribersFromWalletConnect } from '../providers/walletconnectNotify';
65

76
export default function initMetrics(app: Express) {
87
init(app, {
@@ -49,11 +48,6 @@ new client.Gauge({
4948
)
5049
)[0].count as any
5150
);
52-
53-
try {
54-
const subscribers = await getSubscribersFromWalletConnect();
55-
this.set({ type: 'walletconnect' }, subscribers.length);
56-
} catch (e) {}
5751
}
5852
});
5953

src/providers/walletconnectNotify.ts

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -23,56 +23,6 @@ const PER_SECOND_RATE_LIMIT = 2;
2323
const WAIT_ERROR_MARGIN = 0.25;
2424
const WAIT_TIME = 1 / PER_SECOND_RATE_LIMIT + WAIT_ERROR_MARGIN;
2525

26-
// Fetch subscribers from WalletConnect Notify server
27-
export async function getSubscribersFromWalletConnect() {
28-
const fetchSubscribersUrl = `${WALLETCONNECT_NOTIFY_SERVER_URL}/${WALLETCONNECT_PROJECT_ID}/subscribers`;
29-
30-
try {
31-
const subscribersRs = await fetch(fetchSubscribersUrl, {
32-
headers: AUTH_HEADER
33-
});
34-
35-
const subscribers: string[] = await subscribersRs.json();
36-
37-
return subscribers;
38-
} catch (e) {
39-
capture(e);
40-
console.log('[WalletConnect] failed to fetch subscribers');
41-
return [];
42-
}
43-
}
44-
45-
// Find the CAIP10 of subscribers, since the Notify API requires CAIP10.
46-
async function crossReferenceSubscribers(
47-
space: { id: string },
48-
spaceSubscribers
49-
) {
50-
const subscribersFromDb = spaceSubscribers;
51-
const subscribersFromWalletConnect = await getSubscribersFromWalletConnect();
52-
53-
// optimistically reserve all subscribers from the db
54-
const crossReferencedSubscribers = new Array(subscribersFromDb.length);
55-
56-
// Create a hashmap for faster lookup
57-
const addressPrefixMap = new Map<string, string>();
58-
for (const subscriber of subscribersFromWalletConnect) {
59-
const unprefixedAddress = subscriber.split(':').pop();
60-
if (unprefixedAddress) {
61-
addressPrefixMap.set(unprefixedAddress, subscriber);
62-
}
63-
}
64-
65-
for (const subscriber of subscribersFromDb) {
66-
const crossReferencedAddress = addressPrefixMap.get(subscriber);
67-
if (crossReferencedAddress) {
68-
crossReferencedSubscribers.push(crossReferencedAddress);
69-
}
70-
}
71-
72-
// remove empty elements from the array, since some might not have been found in WalletConnect Notify server
73-
return crossReferencedSubscribers.filter(addresses => addresses);
74-
}
75-
7626
async function queueNotificationsToSend(notification, accounts: string[]) {
7727
for (let i = 0; i < accounts.length; i += MAX_ACCOUNTS_PER_REQUEST) {
7828
await sendNotification(
@@ -138,16 +88,11 @@ function formatMessage(event: Event, proposal) {
13888
};
13989
}
14090

141-
export async function send(event: Event, proposal, subscribers: string[]) {
91+
export async function send(event: Event, proposal, addresses: string[]) {
14292
if (event.event !== 'proposal/start') return;
143-
const crossReferencedSubscribers = await crossReferenceSubscribers(
144-
proposal.space,
145-
subscribers
146-
);
14793
const notificationMessage = formatMessage(event, proposal);
14894

149-
await queueNotificationsToSend(
150-
notificationMessage,
151-
crossReferencedSubscribers
152-
);
95+
const accounts = addresses.map(address => `eip155:1:${address}`);
96+
97+
await queueNotificationsToSend(notificationMessage, accounts);
15398
}

0 commit comments

Comments
 (0)