Skip to content

Commit

Permalink
Merge pull request #286 from hirosystems/master
Browse files Browse the repository at this point in the history
merge into develop
  • Loading branch information
rafaelcr authored Dec 16, 2024
2 parents 0c02a15 + a1c253b commit 82ac443
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 313 deletions.
30 changes: 30 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
## [1.1.5](https://github.com/hirosystems/token-metadata-api/compare/v1.1.4...v1.1.5) (2024-12-16)


### Bug Fixes

* allow multiple sft mints for the same token per transaction ([#279](https://github.com/hirosystems/token-metadata-api/issues/279)) ([9b28880](https://github.com/hirosystems/token-metadata-api/commit/9b28880815a75f93f218fae69dc6e7147c908514))
* process nft and sft mints in batches ([#271](https://github.com/hirosystems/token-metadata-api/issues/271)) ([c98f0cd](https://github.com/hirosystems/token-metadata-api/commit/c98f0cd6a1001933764ab2fbb4026f8824127f35))
* upgrade to new chainhook ts client ([#280](https://github.com/hirosystems/token-metadata-api/issues/280)) ([b67dc8c](https://github.com/hirosystems/token-metadata-api/commit/b67dc8cd4e21f91a50a1c0c85880d2f265ab1d51))

## [1.1.5-beta.3](https://github.com/hirosystems/token-metadata-api/compare/v1.1.5-beta.2...v1.1.5-beta.3) (2024-10-22)


### Bug Fixes

* upgrade to new chainhook ts client ([#280](https://github.com/hirosystems/token-metadata-api/issues/280)) ([b67dc8c](https://github.com/hirosystems/token-metadata-api/commit/b67dc8cd4e21f91a50a1c0c85880d2f265ab1d51))

## [1.1.5-beta.2](https://github.com/hirosystems/token-metadata-api/compare/v1.1.5-beta.1...v1.1.5-beta.2) (2024-10-21)


### Bug Fixes

* allow multiple sft mints for the same token per transaction ([#279](https://github.com/hirosystems/token-metadata-api/issues/279)) ([9b28880](https://github.com/hirosystems/token-metadata-api/commit/9b28880815a75f93f218fae69dc6e7147c908514))

## [1.1.5-beta.1](https://github.com/hirosystems/token-metadata-api/compare/v1.1.4...v1.1.5-beta.1) (2024-10-07)


### Bug Fixes

* process nft and sft mints in batches ([#271](https://github.com/hirosystems/token-metadata-api/issues/271)) ([c98f0cd](https://github.com/hirosystems/token-metadata-api/commit/c98f0cd6a1001933764ab2fbb4026f8824127f35))

## [1.1.4](https://github.com/hirosystems/token-metadata-api/compare/v1.1.3...v1.1.4) (2024-09-23)


Expand Down
8 changes: 4 additions & 4 deletions 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"@fastify/type-provider-typebox": "^3.2.0",
"@google-cloud/storage": "^7.12.1",
"@hirosystems/api-toolkit": "^1.7.1",
"@hirosystems/chainhook-client": "^1.12.0",
"@hirosystems/chainhook-client": "^2.0.0",
"@sinclair/typebox": "^0.28.17",
"@stacks/transactions": "^6.1.0",
"@types/node": "^20.16.1",
Expand Down
57 changes: 10 additions & 47 deletions src/chainhook/server.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,23 @@
import * as fs from 'fs';
import {
ChainhookEventObserver,
ChainhookNodeOptions,
EventObserverOptions,
EventObserverPredicate,
Payload,
ServerOptions,
ServerPredicate,
StacksPayload,
} from '@hirosystems/chainhook-client';
import { PgStore } from '../pg/pg-store';
import { ENV } from '../env';
import { logger } from '@hirosystems/api-toolkit';
import { randomUUID } from 'node:crypto';

export function getPersistedPredicateFromDisk(): ServerPredicate | undefined {
const predicatePath = `${ENV.CHAINHOOK_PREDICATE_PATH}/predicate.json`;
try {
if (!fs.existsSync(predicatePath)) {
return;
}
const fileData = fs.readFileSync(predicatePath, 'utf-8');
return JSON.parse(fileData) as ServerPredicate;
} catch (error) {
logger.error(error, `ChainhookServer unable to get persisted predicate`);
}
}

export function persistPredicateToDisk(predicate: ServerPredicate) {
const predicatePath = `${ENV.CHAINHOOK_PREDICATE_PATH}/predicate.json`;
try {
fs.mkdirSync(ENV.CHAINHOOK_PREDICATE_PATH, { recursive: true });
fs.writeFileSync(predicatePath, JSON.stringify(predicate, null, 2));
} catch (error) {
logger.error(error, `ChainhookServer unable to persist predicate to disk`);
}
}

export async function startChainhookServer(args: { db: PgStore }): Promise<ChainhookEventObserver> {
const blockHeight = await args.db.getChainTipBlockHeight();
logger.info(`ChainhookServer is at block ${blockHeight}`);

const predicates: ServerPredicate[] = [];
const predicates: EventObserverPredicate[] = [];
if (ENV.CHAINHOOK_AUTO_PREDICATE_REGISTRATION) {
const existingPredicate = getPersistedPredicateFromDisk();
if (existingPredicate) {
logger.info(
`ChainhookServer will attempt to resume existing predicate ${existingPredicate.uuid}`
);
}
const header = {
uuid: existingPredicate?.uuid ?? randomUUID(),
name: 'block',
name: 'metadata-api-blocks',
version: 1,
chain: 'stacks',
};
Expand Down Expand Up @@ -87,38 +55,33 @@ export async function startChainhookServer(args: { db: PgStore }): Promise<Chain
}
}

const opts: ServerOptions = {
const observer: EventObserverOptions = {
hostname: ENV.API_HOST,
port: ENV.EVENT_PORT,
auth_token: ENV.CHAINHOOK_NODE_AUTH_TOKEN,
external_base_url: `http://${ENV.EXTERNAL_HOSTNAME}`,
wait_for_chainhook_node: ENV.CHAINHOOK_AUTO_PREDICATE_REGISTRATION,
validate_chainhook_payloads: false,
body_limit: ENV.EVENT_SERVER_BODY_LIMIT,
predicate_disk_file_path: ENV.CHAINHOOK_PREDICATE_PATH,
predicate_health_check_interval_ms: 300_000,
node_type: 'chainhook',
};
const chainhook: ChainhookNodeOptions = {
base_url: `http://${ENV.CHAINHOOK_NODE_RPC_HOST}:${ENV.CHAINHOOK_NODE_RPC_PORT}`,
};
const server = new ChainhookEventObserver(opts, chainhook);
await server.start(predicates, async (uuid: string, payload: Payload) => {
const server = new ChainhookEventObserver(observer, chainhook);
await server.start(predicates, async (payload: Payload) => {
logger.info(
`ChainhookServer received ${
payload.chainhook.is_streaming_blocks ? 'streamed' : 'replay'
} payload from predicate ${uuid}`
} payload from predicate ${payload.chainhook.uuid}`
);
await args.db.chainhook.processPayload(payload as StacksPayload);
});
if (predicates.length) persistPredicateToDisk(predicates[0]);
return server;
}

export async function closeChainhookServer(server: ChainhookEventObserver) {
try {
const predicatePath = `${ENV.CHAINHOOK_PREDICATE_PATH}/predicate.json`;
if (fs.existsSync(predicatePath)) fs.rmSync(predicatePath);
} catch (error) {
logger.error(error, `ChainhookServer unable to delete persisted predicate`);
}
await server.close();
}
Loading

0 comments on commit 82ac443

Please sign in to comment.