Skip to content

Commit

Permalink
Merge pull request #22 from hirosystems/develop
Browse files Browse the repository at this point in the history
release new beta
  • Loading branch information
rafaelcr committed Mar 8, 2023
2 parents 1cf8a72 + d6d91e9 commit 2053f48
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 33 deletions.
6 changes: 5 additions & 1 deletion src/chainhook/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import { ChainhookPayloadCType } from './schemas';
* @param db - DB
*/
export async function processInscriptionRevealed(payload: unknown, db: PgStore): Promise<void> {
if (!ChainhookPayloadCType.Check(payload)) return;
if (!ChainhookPayloadCType.Check(payload)) {
const errors = [...ChainhookPayloadCType.Errors(payload)];
logger.warn(errors, `[inscription_revealed] invalid payload`);
return;
}
for (const event of payload.rollback) {
for (const tx of event.transactions) {
const genesis_id = tx.metadata.ordinal_operations[0].inscription_revealed.inscription_id;
Expand Down
6 changes: 4 additions & 2 deletions src/chainhook/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Static, Type } from '@sinclair/typebox';
import { Static, TSchema, Type } from '@sinclair/typebox';
import { TypeCompiler } from '@sinclair/typebox/compiler';

const Nullable = <T extends TSchema>(type: T) => Type.Union([type, Type.Null()]);

const Block = Type.Object({
index: Type.Integer(),
hash: Type.String(),
Expand Down Expand Up @@ -33,7 +35,7 @@ const Transaction = Type.Object({
metadata: Type.Object({
ordinal_operations: Type.Array(OrdinalOperation),
outputs: Type.Array(Output),
proof: Type.String(),
proof: Nullable(Type.String()),
}),
});

Expand Down
74 changes: 44 additions & 30 deletions src/chainhook/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,50 +38,64 @@ async function waitForChainhookNode(this: FastifyInstance) {
* the events server.
*/
async function registerChainhookPredicates(this: FastifyInstance) {
const lastObservedBlockHeight = await this.db.getChainTipBlockHeight();
logger.info(
`EventServer registering predicates starting from block ${lastObservedBlockHeight}...`
);
await request(`${CHAINHOOK_BASE_PATH}/v1/chainhooks`, {
method: 'POST',
body: JSON.stringify({
chain: 'bitcoin',
uuid: REVEAL__PREDICATE_UUID,
name: 'inscription_revealed',
version: 1,
start_block: lastObservedBlockHeight,
networks: {
mainnet: {
if_this: {
scope: 'ordinals',
operation: 'inscription_revealed',
const blockHeight = 779924; //await this.db.getChainTipBlockHeight();
logger.info(`EventServer registering predicates starting from block ${blockHeight}...`);

const register = async (name: string, uuid: string, blockHeight: number) => {
await request(`${CHAINHOOK_BASE_PATH}/v1/chainhooks`, {
method: 'POST',
body: JSON.stringify({
bitcoin: {
uuid: uuid,
name: name,
network: 'mainnet',
version: 1,
start_block: blockHeight,
predicate: {
scope: 'protocol',
ordinal: name,
},
then_that: {
action: {
http_post: {
url: `http://${ENV.EXTERNAL_HOSTNAME}/chainhook/inscription_revealed`,
authorization_header: `Bearer ${ENV.CHAINHOOK_NODE_AUTH_TOKEN}`,
},
},
},
},
}),
throwOnError: true,
});
logger.info(
`EventServer registered "inscription_revealed" predicate (${REVEAL__PREDICATE_UUID})`
);
}),
headers: { 'content-type': 'application/json' },
throwOnError: true,
});
logger.info(`EventServer registered '${name}' predicate (${uuid})`);
};

try {
await register('inscription_revealed', REVEAL__PREDICATE_UUID, blockHeight);
} catch (error) {
logger.error(error, `EventServer unable to register predicate`);
}
}

/**
* Remove previously registered predicates. This is executed before closing the events server.
*/
async function removeChainhookPredicates(this: FastifyInstance) {
logger.info(`EventServer closing predicates...`);
await request(`${CHAINHOOK_BASE_PATH}/v1/chainhooks/bitcoin/${REVEAL__PREDICATE_UUID}`, {
method: 'DELETE',
throwOnError: true,
});
logger.info(`EventServer removed "inscription_revealed" predicate (${REVEAL__PREDICATE_UUID})`);

const deregister = async (name: string, uuid: string) => {
await request(`${CHAINHOOK_BASE_PATH}/v1/chainhooks/bitcoin/${uuid}`, {
method: 'DELETE',
headers: { 'content-type': 'application/json' },
throwOnError: true,
});
logger.info(`EventServer removed '${name}' predicate (${uuid})`);
};

try {
await deregister('inscription_revealed', REVEAL__PREDICATE_UUID);
} catch (error) {
logger.error(error, `EventServer unable to deregister predicate`);
}
}

/**
Expand Down

0 comments on commit 2053f48

Please sign in to comment.