Skip to content

Commit

Permalink
Merge pull request #17 from hirosystems/develop
Browse files Browse the repository at this point in the history
beta release
  • Loading branch information
rafaelcr committed Mar 6, 2023
2 parents 6fd82d7 + fc4a549 commit 5adc1f2
Show file tree
Hide file tree
Showing 8 changed files with 1,545 additions and 1,168 deletions.
16 changes: 13 additions & 3 deletions src/api/routes/inscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Value } from '@sinclair/typebox/value';
import { FastifyPluginAsync, FastifyPluginCallback } from 'fastify';
import { Server } from 'http';
import {
AddressParam,
InscriptionResponse,
LimitParam,
NotFoundResponse,
Expand All @@ -26,6 +25,9 @@ import {
OrdinalParam,
InscriptionNumberParam,
TimestampParam,
AddressesParam,
InscriptionIdsParam,
InscriptionNumbersParam,
} from '../schemas';
import { handleInscriptionCache, handleInscriptionTransfersCache } from '../util/cache';
import {
Expand All @@ -45,6 +47,10 @@ function blockParam(param: string | undefined, name: string) {
return out;
}

function inscriptionIdArrayParam(param: string | number) {
return InscriptionIdParamCType.Check(param) ? { genesis_id: [param] } : { number: [param] };
}

function inscriptionIdParam(param: string | number) {
return InscriptionIdParamCType.Check(param) ? { genesis_id: param } : { number: param };
}
Expand Down Expand Up @@ -78,8 +84,10 @@ const IndexRoutes: FastifyPluginCallback<Record<never, never>, Server, TypeBoxTy
to_sat_coinbase_height: Type.Optional(BlockHeightParam),
from_number: Type.Optional(InscriptionNumberParam),
to_number: Type.Optional(InscriptionNumberParam),
id: Type.Optional(InscriptionIdsParam),
number: Type.Optional(InscriptionNumbersParam),
output: Type.Optional(OutputParam),
address: Type.Optional(AddressParam),
address: Type.Optional(AddressesParam),
mime_type: Type.Optional(MimeTypesParam),
rarity: Type.Optional(SatoshiRaritiesParam),
// Pagination
Expand Down Expand Up @@ -110,6 +118,8 @@ const IndexRoutes: FastifyPluginCallback<Record<never, never>, Server, TypeBoxTy
to_sat_ordinal: bigIntParam(request.query.to_sat_ordinal),
from_number: request.query.from_number,
to_number: request.query.to_number,
genesis_id: request.query.id,
number: request.query.number,
output: request.query.output,
address: request.query.address,
mime_type: request.query.mime_type,
Expand Down Expand Up @@ -155,7 +165,7 @@ const ShowRoutes: FastifyPluginCallback<Record<never, never>, Server, TypeBoxTyp
},
async (request, reply) => {
const inscription = await fastify.db.getInscriptions({
...inscriptionIdParam(request.params.id),
...inscriptionIdArrayParam(request.params.id),
limit: 1,
offset: 0,
});
Expand Down
31 changes: 31 additions & 0 deletions src/api/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,35 @@ export const AddressParam = Type.String({
examples: ['bc1p8aq8s3z9xl87e74twfk93mljxq6alv4a79yheadx33t9np4g2wkqqt8kc5'],
});

export const AddressesParam = Type.Array(AddressParam, {
title: 'Addresses',
description: 'Array of Bitcoin addresses',
examples: [
[
'bc1p8aq8s3z9xl87e74twfk93mljxq6alv4a79yheadx33t9np4g2wkqqt8kc5',
'bc1pscktlmn99gyzlvymvrezh6vwd0l4kg06tg5rvssw0czg8873gz5sdkteqj',
],
],
});

export const InscriptionIdParam = Type.RegEx(/^[a-fA-F0-9]{64}i[0-9]+$/, {
title: 'Inscription ID',
description: 'Inscription ID',
examples: ['38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dci0'],
});
export const InscriptionIdParamCType = TypeCompiler.Compile(InscriptionIdParam);

export const InscriptionIdsParam = Type.Array(InscriptionIdParam, {
title: 'Inscription IDs',
description: 'Array of inscription IDs',
examples: [
[
'38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dci0',
'e3af144354367de58c675e987febcb49f17d6c19e645728b833fe95408feab85i0',
],
],
});

export const InscriptionNumberParam = Type.Integer({
minimum: 0,
title: 'Inscription Number',
Expand All @@ -59,6 +81,12 @@ export const InscriptionNumberParam = Type.Integer({
});
export const InscriptionNumberParamCType = TypeCompiler.Compile(InscriptionNumberParam);

export const InscriptionNumbersParam = Type.Array(InscriptionNumberParam, {
title: 'Inscription Numbers',
description: 'Array of inscription numbers',
examples: [['10500', '65']],
});

export const InscriptionIdentifierParam = Type.Union([InscriptionIdParam, InscriptionNumberParam], {
title: 'Inscription Identifier',
description: 'Inscription unique identifier (number or ID)',
Expand Down Expand Up @@ -195,6 +223,9 @@ export const InscriptionResponse = Type.Object({
}),
genesis_fee: Type.String({ examples: ['3179'] }),
genesis_timestamp: Type.Integer({ exmaples: [1677733170000] }),
tx_id: Type.String({
examples: ['1463d48e9248159084929294f64bda04487503d30ce7ab58365df1dc6fd58218'],
}),
location: Type.String({
examples: ['1463d48e9248159084929294f64bda04487503d30ce7ab58365df1dc6fd58218:0:0'],
}),
Expand Down
1 change: 1 addition & 0 deletions src/api/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function parseDbInscriptions(
genesis_tx_id: i.genesis_tx_id,
genesis_fee: i.genesis_fee.toString(),
genesis_timestamp: i.genesis_timestamp.valueOf(),
tx_id: i.tx_id,
location: `${i.output}:${i.offset}`,
output: i.output,
value: i.value.toString(),
Expand Down
22 changes: 17 additions & 5 deletions src/chainhook/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';
import { randomUUID } from 'crypto';
import Fastify, { FastifyPluginCallback, FastifyReply, FastifyRequest } from 'fastify';
import Fastify, {
FastifyInstance,
FastifyPluginCallback,
FastifyReply,
FastifyRequest,
} from 'fastify';
import { Server } from 'http';
import { request } from 'undici';
import { ENV } from '../env';
Expand All @@ -12,7 +17,10 @@ import { processInscriptionRevealed, processInscriptionTransferred } from './hel
export const CHAINHOOK_BASE_PATH = `http://${ENV.CHAINHOOK_NODE_RPC_HOST}:${ENV.CHAINHOOK_NODE_RPC_PORT}`;
export const REVEAL__PREDICATE_UUID = randomUUID();

async function waitForChainhookNode() {
/**
* Ping the chainhooks node indefinitely until it's ready.
*/
async function waitForChainhookNode(this: FastifyInstance) {
logger.info(`EventServer connecting to chainhook node...`);
while (true) {
try {
Expand All @@ -29,15 +37,19 @@ async function waitForChainhookNode() {
* Register required ordinals predicates in the chainhooks node. This is executed before starting
* the events server.
*/
async function registerChainhookPredicates() {
logger.info(`EventServer registering predicates...`);
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: {
Expand All @@ -63,7 +75,7 @@ async function registerChainhookPredicates() {
/**
* Remove previously registered predicates. This is executed before closing the events server.
*/
async function removeChainhookPredicates() {
async function removeChainhookPredicates(this: FastifyInstance) {
logger.info(`EventServer closing predicates...`);
await request(`${CHAINHOOK_BASE_PATH}/v1/chainhooks/bitcoin/${REVEAL__PREDICATE_UUID}`, {
method: 'DELETE',
Expand Down
19 changes: 13 additions & 6 deletions src/pg/pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class PgStore extends BasePgStore {
}

async getInscriptions(args: {
genesis_id?: string;
genesis_id?: string[];
genesis_block_height?: number;
genesis_block_hash?: string;
from_genesis_block_height?: number;
Expand All @@ -182,10 +182,10 @@ export class PgStore extends BasePgStore {
to_genesis_timestamp?: number;
from_sat_coinbase_height?: number;
to_sat_coinbase_height?: number;
number?: number;
number?: number[];
from_number?: number;
to_number?: number;
address?: string;
address?: string[];
mime_type?: string[];
output?: string;
sat_rarity?: SatoshiRarity[];
Expand Down Expand Up @@ -223,6 +223,7 @@ export class PgStore extends BasePgStore {
gen.tx_id AS genesis_tx_id,
gen.timestamp AS genesis_timestamp,
gen.address AS genesis_address,
loc.tx_id,
loc.address,
loc.output,
loc.offset,
Expand All @@ -236,7 +237,11 @@ export class PgStore extends BasePgStore {
INNER JOIN locations AS loc ON loc.inscription_id = i.id
INNER JOIN locations AS gen ON gen.inscription_id = i.id
WHERE loc.current = TRUE AND gen.genesis = TRUE
${args.genesis_id ? this.sql`AND i.genesis_id = ${args.genesis_id}` : this.sql``}
${
args.genesis_id?.length
? this.sql`AND i.genesis_id IN ${this.sql(args.genesis_id)}`
: this.sql``
}
${
args.genesis_block_height
? this.sql`AND gen.block_height = ${args.genesis_block_height}`
Expand Down Expand Up @@ -285,10 +290,12 @@ export class PgStore extends BasePgStore {
${
args.to_sat_ordinal ? this.sql`AND loc.sat_ordinal <= ${args.to_sat_ordinal}` : this.sql``
}
${args.number ? this.sql`AND i.number = ${args.number}` : this.sql``}
${args.number?.length ? this.sql`AND i.number IN ${this.sql(args.number)}` : this.sql``}
${args.from_number ? this.sql`AND i.number >= ${args.from_number}` : this.sql``}
${args.to_number ? this.sql`AND i.number <= ${args.to_number}` : this.sql``}
${args.address ? this.sql`AND loc.address = ${args.address}` : this.sql``}
${
args.address?.length ? this.sql`AND loc.address IN ${this.sql(args.address)}` : this.sql``
}
${
args.mime_type?.length
? this.sql`AND i.mime_type IN ${this.sql(args.mime_type)}`
Expand Down
1 change: 1 addition & 0 deletions src/pg/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type DbFullyLocatedInscriptionResult = {
genesis_address: string;
number: number;
address: string;
tx_id: string;
output: string;
offset: bigint;
value: bigint;
Expand Down
Loading

0 comments on commit 5adc1f2

Please sign in to comment.