Skip to content

Commit

Permalink
feat: filter by coinbase block height range
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr committed Mar 2, 2023
1 parent b53f4c8 commit 5698ebc
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 1 deletion.
4 changes: 4 additions & 0 deletions migrations/1677284495299_locations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export function up(pgm: MigrationBuilder): void {
type: 'text',
notNull: true,
},
sat_coinbase_height: {
type: 'int',
notNull: true,
},
timestamp: {
type: 'timestamptz',
notNull: true,
Expand Down
4 changes: 4 additions & 0 deletions src/api/routes/inscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ const IndexRoutes: FastifyPluginCallback<Record<never, never>, Server, TypeBoxTy
to_genesis_timestamp: Type.Optional(TimestampParam),
from_sat_ordinal: Type.Optional(OrdinalParam),
to_sat_ordinal: Type.Optional(OrdinalParam),
from_sat_coinbase_height: Type.Optional(BlockHeightParam),
to_sat_coinbase_height: Type.Optional(BlockHeightParam),
output: Type.Optional(OutputParam),
address: Type.Optional(AddressParam),
mime_type: Type.Optional(MimeTypesParam),
Expand All @@ -97,6 +99,8 @@ const IndexRoutes: FastifyPluginCallback<Record<never, never>, Server, TypeBoxTy
...blockParam(request.query.genesis_block, 'genesis_block'),
...blockParam(request.query.from_genesis_block_height, 'from_genesis_block'),
...blockParam(request.query.to_genesis_block_height, 'to_genesis_block'),
...blockParam(request.query.from_sat_coinbase_height, 'from_sat_coinbase'),
...blockParam(request.query.to_sat_coinbase_height, 'to_sat_coinbase'),
from_genesis_timestamp: request.query.from_genesis_timestamp,
to_genesis_timestamp: request.query.to_genesis_timestamp,
from_sat_ordinal: bigIntParam(request.query.from_sat_ordinal),
Expand Down
1 change: 1 addition & 0 deletions src/api/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ export const InscriptionResponse = Type.Object({
offset: Type.String({ examples: ['0'] }),
sat_ordinal: Type.String({ examples: ['1232735286933201'] }),
sat_rarity: Type.String({ examples: ['common'] }),
sat_coinbase_height: Type.Integer({ examples: [430521] }),
mime_type: Type.String({ examples: ['text/plain'] }),
content_type: Type.String({ examples: ['text/plain;charset=utf-8'] }),
content_length: Type.Integer({ examples: [59] }),
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 @@ -22,6 +22,7 @@ export function parseDbInscriptions(
offset: i.offset.toString(),
sat_ordinal: i.sat_ordinal.toString(),
sat_rarity: i.sat_rarity,
sat_coinbase_height: i.sat_coinbase_height,
mime_type: i.mime_type,
content_type: i.content_type,
content_length: i.content_length,
Expand Down
14 changes: 13 additions & 1 deletion src/pg/pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ export class PgStore extends BasePgStore {
to_genesis_block_height?: number;
from_genesis_timestamp?: number;
to_genesis_timestamp?: number;
from_sat_coinbase_height?: number;
to_sat_coinbase_height?: number;
number?: number;
address?: string;
mime_type?: string[];
Expand Down Expand Up @@ -194,7 +196,7 @@ export class PgStore extends BasePgStore {
gen.block_hash AS genesis_block_hash, gen.tx_id AS genesis_tx_id, i.fee AS genesis_fee,
loc.output, loc.offset, i.mime_type, i.content_type, i.content_length, loc.sat_ordinal,
loc.sat_rarity, loc.timestamp, gen.timestamp AS genesis_timestamp, loc.value,
gen.address AS genesis_address,
gen.address AS genesis_address, loc.sat_coinbase_height,
COUNT(*) OVER() as total
FROM inscriptions AS i
INNER JOIN locations AS loc ON loc.inscription_id = i.id
Expand All @@ -221,6 +223,16 @@ export class PgStore extends BasePgStore {
? this.sql`AND gen.block_height <= ${args.to_genesis_block_height}`
: this.sql``
}
${
args.from_sat_coinbase_height
? this.sql`AND loc.sat_coinbase_height >= ${args.from_sat_coinbase_height}`
: this.sql``
}
${
args.to_sat_coinbase_height
? this.sql`AND loc.sat_coinbase_height <= ${args.to_sat_coinbase_height}`
: this.sql``
}
${
args.from_genesis_timestamp
? this.sql`AND gen.timestamp >= to_timestamp(${args.from_genesis_timestamp})`
Expand Down
4 changes: 4 additions & 0 deletions src/pg/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type DbFullyLocatedInscriptionResult = {
value: bigint;
sat_ordinal: bigint;
sat_rarity: string;
sat_coinbase_height: number;
mime_type: string;
content_type: string;
content_length: number;
Expand All @@ -42,6 +43,7 @@ export type DbLocationInsert = {
value: bigint;
sat_ordinal: bigint;
sat_rarity: string;
sat_coinbase_height: number;
timestamp: number;
genesis: boolean;
current: boolean;
Expand All @@ -59,6 +61,7 @@ export type DbLocation = {
value: bigint;
sat_ordinal: bigint;
sat_rarity: string;
sat_coinbase_height: number;
timestamp: number;
genesis: boolean;
current: boolean;
Expand All @@ -76,6 +79,7 @@ export const LOCATIONS_COLUMNS = [
'value',
'sat_ordinal',
'sat_rarity',
'sat_coinbase_height',
'timestamp',
'genesis',
'current',
Expand Down
3 changes: 3 additions & 0 deletions tests/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe('ETag cache', () => {
timestamp: 1676913207,
sat_ordinal: 257418248345364n,
sat_rarity: 'common',
sat_coinbase_height: 650000,
genesis: true,
current: true,
},
Expand Down Expand Up @@ -118,6 +119,7 @@ describe('ETag cache', () => {
timestamp: 1676913207,
sat_ordinal: 257418248345364n,
sat_rarity: 'common',
sat_coinbase_height: 650000,
genesis: true,
current: true,
},
Expand All @@ -144,6 +146,7 @@ describe('ETag cache', () => {
timestamp: 1676913207,
sat_ordinal: 257418248345364n,
sat_rarity: 'common',
sat_coinbase_height: 650000,
genesis: true,
current: true,
},
Expand Down
Loading

0 comments on commit 5698ebc

Please sign in to comment.