1
1
import fetch from 'node-fetch' ;
2
+ import snapshot from '@snapshot-labs/snapshot.js' ;
2
3
import { capture } from '@snapshot-labs/snapshot-sentry' ;
3
4
import { timeOutgoingRequest } from '../helpers/metrics' ;
5
+ import type { Event } from '../types' ;
4
6
5
7
const WALLETCONNECT_NOTIFY_SERVER_URL =
6
8
process . env . WALLETCONNECT_NOTIFY_SERVER_URL ;
@@ -21,13 +23,6 @@ const PER_SECOND_RATE_LIMIT = 2;
21
23
const WAIT_ERROR_MARGIN = 0.25 ;
22
24
const WAIT_TIME = 1 / PER_SECOND_RATE_LIMIT + WAIT_ERROR_MARGIN ;
23
25
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
-
31
26
// Fetch subscribers from WalletConnect Notify server
32
27
export async function getSubscribersFromWalletConnect ( ) {
33
28
const fetchSubscribersUrl = `${ WALLETCONNECT_NOTIFY_SERVER_URL } /${ WALLETCONNECT_PROJECT_ID } /subscribers` ;
@@ -41,7 +36,8 @@ export async function getSubscribersFromWalletConnect() {
41
36
42
37
return subscribers ;
43
38
} catch ( e ) {
44
- capture ( '[WalletConnect] failed to fetch subscribers' ) ;
39
+ capture ( e ) ;
40
+ console . log ( '[WalletConnect] failed to fetch subscribers' ) ;
45
41
return [ ] ;
46
42
}
47
43
}
@@ -84,7 +80,7 @@ async function queueNotificationsToSend(notification, accounts: string[]) {
84
80
accounts . slice ( i , i + MAX_ACCOUNTS_PER_REQUEST )
85
81
) ;
86
82
87
- await wait ( WAIT_TIME ) ;
83
+ await snapshot . utils . sleep ( WAIT_TIME ) ;
88
84
}
89
85
}
90
86
@@ -113,14 +109,15 @@ export async function sendNotification(notification, accounts) {
113
109
success = true ;
114
110
return notifySuccess ;
115
111
} catch ( e ) {
116
- capture ( '[WalletConnect] failed to notify subscribers' , e ) ;
112
+ capture ( e ) ;
113
+ console . log ( '[WalletConnect] failed to notify subscribers' , e ) ;
117
114
} finally {
118
115
end ( { status : success ? 200 : 500 } ) ;
119
116
}
120
117
}
121
118
122
119
// Transform proposal event into notification format.
123
- function formatMessage ( event , proposal ) {
120
+ function formatMessage ( event : Event , proposal ) {
124
121
const space = proposal . space ;
125
122
if ( ! space ) return null ;
126
123
@@ -137,7 +134,7 @@ function formatMessage(event, proposal) {
137
134
} ;
138
135
}
139
136
140
- export async function send ( event , proposal , subscribers ) {
137
+ export async function send ( event : Event , proposal , subscribers : string [ ] ) {
141
138
if ( event . event !== 'proposal/start' ) return ;
142
139
const crossReferencedSubscribers = await crossReferenceSubscribers (
143
140
proposal . space ,
0 commit comments