Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update ordhook URLs on typescript client #460

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions components/client/typescript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/client/typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hirosystems/chainhook-client",
"version": "1.4.1",
"version": "1.4.2",
"description": "Chainhook TypeScript client",
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
Expand Down
47 changes: 25 additions & 22 deletions components/client/typescript/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ const ServerOptionsSchema = Type.Object({
validate_chainhook_payloads: Type.Optional(Type.Boolean({ default: false })),
/** Size limit for received chainhook payloads (default 40MB) */
body_limit: Type.Optional(Type.Number({ default: 41943040 })),
/** Node type: `chainhook` or `ordhook` */
node_type: Type.Optional(
Type.Union([Type.Literal('chainhook'), Type.Literal('ordhook')], {
default: 'chainhook',
})
),
});
/** Local event server connection and authentication options */
export type ServerOptions = Static<typeof ServerOptionsSchema>;
Expand Down Expand Up @@ -115,22 +121,22 @@ export async function buildServer(
logger.info(`ChainhookEventObserver does not have predicates to register`);
return;
}
logger.info(
predicates,
`ChainhookEventObserver registering predicates at ${chainhookOpts.base_url}`
);
const nodeType = serverOpts.node_type ?? 'chainhook';
const path = nodeType === 'chainhook' ? `/v1/chainhooks` : `/v1/observers`;
const registerUrl = `${chainhookOpts.base_url}${path}`;
logger.info(predicates, `ChainhookEventObserver registering predicates at ${registerUrl}`);
for (const predicate of predicates) {
const thenThat: ThenThatHttpPost = {
http_post: {
url: `${serverOpts.external_base_url}/chainhook/${encodeURIComponent(predicate.uuid)}`,
url: `${serverOpts.external_base_url}/payload`,
authorization_header: `Bearer ${serverOpts.auth_token}`,
},
};
try {
const body = predicate as Predicate;
if ('mainnet' in body.networks) body.networks.mainnet.then_that = thenThat;
if ('testnet' in body.networks) body.networks.testnet.then_that = thenThat;
await request(`${chainhookOpts.base_url}/v1/chainhooks`, {
await request(registerUrl, {
method: 'POST',
body: JSON.stringify(body),
headers: { 'content-type': 'application/json' },
Expand All @@ -151,19 +157,19 @@ export async function buildServer(
return;
}
logger.info(`ChainhookEventObserver closing predicates at ${chainhookOpts.base_url}`);
const nodeType = serverOpts.node_type ?? 'chainhook';
const removals = predicates.map(
predicate =>
new Promise<void>((resolve, reject) => {
request(
`${chainhookOpts.base_url}/v1/chainhooks/${predicate.chain}/${encodeURIComponent(
predicate.uuid
)}`,
{
method: 'DELETE',
headers: { 'content-type': 'application/json' },
throwOnError: true,
}
)
const path =
nodeType === 'chainhook'
? `/v1/chainhooks/${predicate.chain}/${encodeURIComponent(predicate.uuid)}`
: `/v1/observers/${encodeURIComponent(predicate.uuid)}`;
request(`${chainhookOpts.base_url}${path}`, {
method: 'DELETE',
headers: { 'content-type': 'application/json' },
throwOnError: true,
})
.then(() => {
logger.info(
`ChainhookEventObserver removed '${predicate.name}' predicate (${predicate.uuid})`
Expand Down Expand Up @@ -195,12 +201,9 @@ export async function buildServer(
const compiledPayloadSchema = TypeCompiler.Compile(PayloadSchema);
fastify.addHook('preHandler', isEventAuthorized);
fastify.post(
'/chainhook/:uuid',
'/payload',
{
schema: {
params: Type.Object({
uuid: Type.String({ format: 'uuid' }),
}),
body: PayloadSchema,
},
},
Expand All @@ -217,7 +220,7 @@ export async function buildServer(
return;
}
try {
await callback(request.params.uuid, request.body);
await callback(request.body.chainhook.uuid, request.body);
await reply.code(200).send();
} catch (error) {
if (error instanceof BadPayloadRequestError) {
Expand All @@ -237,7 +240,7 @@ export async function buildServer(
trustProxy: true,
logger: PINO_CONFIG,
pluginTimeout: 0, // Disable so ping can retry indefinitely
bodyLimit: serverOpts.body_limit ?? 41943040, // 40MB
bodyLimit: serverOpts.body_limit ?? 41943040, // 40MB default
}).withTypeProvider<TypeBoxTypeProvider>();

if (serverOpts.wait_for_chainhook_node ?? true) {
Expand Down
Loading