@@ -27,8 +27,7 @@ const ServerOptionsSchema = Type.Object({
27
27
export type ServerOptions = Static < typeof ServerOptionsSchema > ;
28
28
29
29
const ChainhookNodeOptionsSchema = Type . Object ( {
30
- hostname : Type . String ( ) ,
31
- port : Type . Integer ( ) ,
30
+ base_url : Type . String ( ) ,
32
31
} ) ;
33
32
/** Chainhook node connection options */
34
33
export type ChainhookNodeOptions = Static < typeof ChainhookNodeOptionsSchema > ;
@@ -77,13 +76,13 @@ export async function buildServer(
77
76
predicates : [ ServerPredicate ] ,
78
77
callback : OnEventCallback
79
78
) {
80
- const base_path = `http://${ chainhookOpts . hostname } :${ chainhookOpts . port } ` ;
81
-
82
79
async function waitForNode ( this : FastifyInstance ) {
83
- logger . info ( `EventServer connecting to chainhook node at ${ base_path } ...` ) ;
80
+ logger . info (
81
+ `ChainhookEventObserver connecting to chainhook node at ${ chainhookOpts . base_url } ...`
82
+ ) ;
84
83
while ( true ) {
85
84
try {
86
- await request ( `${ base_path } /ping` , { method : 'GET' , throwOnError : true } ) ;
85
+ await request ( `${ chainhookOpts . base_url } /ping` , { method : 'GET' , throwOnError : true } ) ;
87
86
break ;
88
87
} catch ( error ) {
89
88
logger . error ( error , 'Chainhook node not available, retrying...' ) ;
@@ -93,45 +92,64 @@ export async function buildServer(
93
92
}
94
93
95
94
async function registerPredicates ( this : FastifyInstance ) {
96
- logger . info ( predicates , `EventServer registering predicates at ${ base_path } ...` ) ;
95
+ logger . info (
96
+ predicates ,
97
+ `ChainhookEventObserver registering predicates at ${ chainhookOpts . base_url } `
98
+ ) ;
97
99
for ( const predicate of predicates ) {
98
100
const thenThat : ThenThatHttpPost = {
99
101
http_post : {
100
- url : `${ serverOpts . external_base_url } /chainhook/${ predicate . uuid } ` ,
102
+ url : `${ serverOpts . external_base_url } /chainhook/${ encodeURIComponent ( predicate . uuid ) } ` ,
101
103
authorization_header : `Bearer ${ serverOpts . auth_token } ` ,
102
104
} ,
103
105
} ;
104
106
try {
105
107
const body = predicate as Predicate ;
106
108
if ( 'mainnet' in body . networks ) body . networks . mainnet . then_that = thenThat ;
107
109
if ( 'testnet' in body . networks ) body . networks . testnet . then_that = thenThat ;
108
- await request ( `${ base_path } /v1/chainhooks` , {
110
+ await request ( `${ chainhookOpts . base_url } /v1/chainhooks` , {
109
111
method : 'POST' ,
110
112
body : JSON . stringify ( body ) ,
111
113
headers : { 'content-type' : 'application/json' } ,
112
114
throwOnError : true ,
113
115
} ) ;
114
- logger . info ( `EventServer registered '${ predicate . name } ' predicate (${ predicate . uuid } )` ) ;
116
+ logger . info (
117
+ `ChainhookEventObserver registered '${ predicate . name } ' predicate (${ predicate . uuid } )`
118
+ ) ;
115
119
} catch ( error ) {
116
- logger . error ( error , `EventServer unable to register predicate` ) ;
120
+ logger . error ( error , `ChainhookEventObserver unable to register predicate` ) ;
117
121
}
118
122
}
119
123
}
120
124
121
125
async function removePredicates ( this : FastifyInstance ) {
122
- logger . info ( `EventServer closing predicates at ${ base_path } ...` ) ;
123
- for ( const predicate of predicates ) {
124
- try {
125
- await request ( `${ base_path } /v1/chainhooks/${ predicate . chain } /${ predicate . uuid } ` , {
126
- method : 'DELETE' ,
127
- headers : { 'content-type' : 'application/json' } ,
128
- throwOnError : true ,
129
- } ) ;
130
- logger . info ( `EventServer removed '${ predicate . name } ' predicate (${ predicate . uuid } )` ) ;
131
- } catch ( error ) {
132
- logger . error ( error , `EventServer unable to deregister predicate` ) ;
133
- }
134
- }
126
+ logger . info ( `ChainhookEventObserver closing predicates at ${ chainhookOpts . base_url } ` ) ;
127
+ const removals = predicates . map (
128
+ predicate =>
129
+ new Promise < void > ( ( resolve , reject ) => {
130
+ request (
131
+ `${ chainhookOpts . base_url } /v1/chainhooks/${ predicate . chain } /${ encodeURIComponent (
132
+ predicate . uuid
133
+ ) } `,
134
+ {
135
+ method : 'DELETE' ,
136
+ headers : { 'content-type' : 'application/json' } ,
137
+ throwOnError : true ,
138
+ }
139
+ )
140
+ . then ( ( ) => {
141
+ logger . info (
142
+ `ChainhookEventObserver removed '${ predicate . name } ' predicate (${ predicate . uuid } )`
143
+ ) ;
144
+ resolve ( ) ;
145
+ } )
146
+ . catch ( error => {
147
+ logger . error ( error , `ChainhookEventObserver unable to deregister predicate` ) ;
148
+ reject ( error ) ;
149
+ } ) ;
150
+ } )
151
+ ) ;
152
+ await Promise . allSettled ( removals ) ;
135
153
}
136
154
137
155
async function isEventAuthorized ( request : FastifyRequest , reply : FastifyReply ) {
@@ -142,11 +160,11 @@ export async function buildServer(
142
160
await reply . code ( 403 ) . send ( ) ;
143
161
}
144
162
145
- const EventServer : FastifyPluginCallback < Record < never , never > , Server , TypeBoxTypeProvider > = (
146
- fastify ,
147
- options ,
148
- done
149
- ) => {
163
+ const ChainhookEventObserver : FastifyPluginCallback <
164
+ Record < never , never > ,
165
+ Server ,
166
+ TypeBoxTypeProvider
167
+ > = ( fastify , options , done ) => {
150
168
fastify . addHook ( 'preHandler' , isEventAuthorized ) ;
151
169
fastify . post (
152
170
'/chainhook/:uuid' ,
@@ -162,7 +180,7 @@ export async function buildServer(
162
180
try {
163
181
await callback ( request . params . uuid , request . body ) ;
164
182
} catch ( error ) {
165
- logger . error ( error , `EventServer error processing payload` ) ;
183
+ logger . error ( error , `ChainhookEventObserver error processing payload` ) ;
166
184
await reply . code ( 422 ) . send ( ) ;
167
185
}
168
186
await reply . code ( 200 ) . send ( ) ;
@@ -182,6 +200,6 @@ export async function buildServer(
182
200
fastify . addHook ( 'onReady' , registerPredicates ) ;
183
201
fastify . addHook ( 'onClose' , removePredicates ) ;
184
202
185
- await fastify . register ( EventServer ) ;
203
+ await fastify . register ( ChainhookEventObserver ) ;
186
204
return fastify ;
187
205
}
0 commit comments