Skip to content

Commit

Permalink
feat!: ingest missing ordhook fields, return delegate inscription con…
Browse files Browse the repository at this point in the history
…tent (#374)

* feat: ingest missing ordhook fields

* fix: display delegate content
  • Loading branch information
rafaelcr authored Aug 5, 2024
1 parent 939286e commit 04abd6e
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 6 deletions.
1 change: 0 additions & 1 deletion docker/docker-compose.dev.postgres.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: '3.7'
services:
postgres:
image: "postgres:15"
Expand Down
13 changes: 13 additions & 0 deletions migrations/1676395230930_inscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,25 @@ export function up(pgm: MigrationBuilder): void {
type: 'boolean',
default: false,
},
input_index: {
type: 'bigint',
notNull: true,
},
pointer: {
type: 'bigint',
},
metadata: {
type: 'text',
},
metaprotocol: {
type: 'text',
},
parent: {
type: 'text',
},
delegate: {
type: 'text',
},
timestamp: {
type: 'timestamptz',
notNull: true,
Expand Down
10 changes: 10 additions & 0 deletions src/api/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,17 @@ export const InscriptionResponse = Type.Object(
examples: ['1463d48e9248159084929294f64bda04487503d30ce7ab58365df1dc6fd58218i0'],
})
),
delegate: Nullable(
Type.String({
examples: ['1463d48e9248159084929294f64bda04487503d30ce7ab58365df1dc6fd58218i0'],
})
),
metadata: Nullable(Type.Any()),
meta_protocol: Nullable(
Type.String({
examples: ['brc20'],
})
),
},
{ title: 'Inscription Response' }
);
Expand Down
2 changes: 2 additions & 0 deletions src/api/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export function parseDbInscriptions(
recursion_refs: i.recursion_refs?.split(',') ?? null,
parent: i.parent,
metadata: i.metadata ? JSON.parse(i.metadata) : null,
delegate: i.delegate ?? null,
meta_protocol: i.metaprotocol ?? null,
}));
}
export function parseDbInscription(item: DbFullyLocatedInscriptionResult): InscriptionResponseType {
Expand Down
4 changes: 4 additions & 0 deletions src/pg/block-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export class BlockCache {
recursive: recursive_refs.length > 0,
metadata: reveal.metadata ? JSON.stringify(reveal.metadata) : null,
parent: reveal.parent,
delegate: reveal.delegate,
input_index: reveal.inscription_input_index,
pointer: reveal.inscription_pointer,
metaprotocol: reveal.metaprotocol,
timestamp: this.timestamp,
});
this.revealedNumbers.push(reveal.inscription_number.jubilee);
Expand Down
19 changes: 14 additions & 5 deletions src/pg/pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,13 +447,22 @@ export class PgStore extends BasePgStore {
args: InscriptionIdentifier
): Promise<DbInscriptionContent | undefined> {
const result = await this.sql<DbInscriptionContent[]>`
WITH content_id AS (
SELECT
CASE
WHEN delegate IS NOT NULL THEN delegate
ELSE genesis_id
END AS genesis_id
FROM inscriptions
WHERE ${
'genesis_id' in args
? this.sql`genesis_id = ${args.genesis_id}`
: this.sql`number = ${args.number}`
}
)
SELECT content, content_type, content_length
FROM inscriptions
WHERE ${
'genesis_id' in args
? this.sql`genesis_id = ${args.genesis_id}`
: this.sql`number = ${args.number}`
}
WHERE genesis_id = (SELECT genesis_id FROM content_id)
`;
if (result.count > 0) {
return result[0];
Expand Down
8 changes: 8 additions & 0 deletions src/pg/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export type DbInscriptionInsert = {
recursive: boolean;
metadata: string | null;
parent: string | null;
input_index: number;
pointer: number | null;
metaprotocol: string | null;
delegate: string | null;
timestamp: number;
};

Expand Down Expand Up @@ -102,6 +106,10 @@ export type DbFullyLocatedInscriptionResult = {
recursion_refs: string | null;
parent: string | null;
metadata: string | null;
input_index: number;
pointer: number | null;
metaprotocol: string | null;
delegate: string | null;
};

export enum DbLocationTransferType {
Expand Down
157 changes: 157 additions & 0 deletions tests/api/inscriptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ describe('/inscriptions', () => {
recursion_refs: null,
parent: null,
metadata: null,
meta_protocol: null,
delegate: null,
};

// By inscription id
Expand Down Expand Up @@ -284,6 +286,8 @@ describe('/inscriptions', () => {
],
parent: null,
metadata: null,
meta_protocol: null,
delegate: null,
};

// By inscription id
Expand Down Expand Up @@ -489,6 +493,8 @@ describe('/inscriptions', () => {
recursion_refs: null,
parent: null,
metadata: null,
meta_protocol: null,
delegate: null,
};

// By inscription id
Expand Down Expand Up @@ -573,6 +579,8 @@ describe('/inscriptions', () => {
recursion_refs: null,
parent: null,
metadata: null,
meta_protocol: null,
delegate: null,
};

// By inscription id
Expand Down Expand Up @@ -686,6 +694,8 @@ describe('/inscriptions', () => {
recursion_refs: null,
parent: null,
metadata: null,
meta_protocol: null,
delegate: null,
});

// Transfer 2
Expand Down Expand Up @@ -743,6 +753,8 @@ describe('/inscriptions', () => {
recursion_refs: null,
parent: null,
metadata: null,
meta_protocol: null,
delegate: null,
});
});

Expand Down Expand Up @@ -855,6 +867,8 @@ describe('/inscriptions', () => {
recursion_refs: null,
parent: null,
metadata: null,
meta_protocol: null,
delegate: null,
});
});

Expand Down Expand Up @@ -952,6 +966,8 @@ describe('/inscriptions', () => {
recursion_refs: null,
parent: null,
metadata: null,
meta_protocol: null,
delegate: null,
});

// Transfer 2
Expand Down Expand Up @@ -1009,8 +1025,141 @@ describe('/inscriptions', () => {
recursion_refs: null,
parent: null,
metadata: null,
meta_protocol: null,
delegate: null,
});
});

test('shows inscription content', async () => {
await db.updateInscriptions(
new TestChainhookPayloadBuilder()
.apply()
.block({
height: 775617,
hash: '0x00000000000000000002a90330a99f67e3f01eb2ce070b45930581e82fb7a91d',
timestamp: 1676913207,
})
.transaction({
hash: '0x38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dc',
})
.inscriptionRevealed({
content_bytes: '0x48656C6C6F', // Hello
content_type: 'text/plain',
content_length: 5,
inscription_number: { classic: 0, jubilee: 0 },
inscription_fee: 2805,
inscription_id: '38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dci0',
inscription_output_value: 10000,
inscriber_address: 'bc1p3cyx5e2hgh53w7kpxcvm8s4kkega9gv5wfw7c4qxsvxl0u8x834qf0u2td',
ordinal_number: 257418248345364,
ordinal_block_height: 51483,
ordinal_offset: 0,
satpoint_post_inscription:
'38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dc:0:0',
curse_type: { tag: 66 },
inscription_input_index: 0,
transfers_pre_inscription: 0,
tx_index: 0,
inscription_pointer: null,
delegate: null,
metaprotocol: null,
metadata: null,
parent: null,
})
.build()
);
const response = await fastify.inject({
method: 'GET',
url: '/ordinals/v1/inscriptions/38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dci0/content',
});
expect(response.statusCode).toBe(200);
expect(response.headers['content-length']).toBe('5');
expect(response.body).toBe('Hello');
});

test('shows delegate inscription content', async () => {
await db.updateInscriptions(
new TestChainhookPayloadBuilder()
.apply()
.block({
height: 775617,
hash: '0x00000000000000000002a90330a99f67e3f01eb2ce070b45930581e82fb7a91d',
timestamp: 1676913207,
})
.transaction({
hash: '0x38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dc',
})
.inscriptionRevealed({
content_bytes: '0x48656C6C6F', // Hello
content_type: 'text/plain',
content_length: 5,
inscription_number: { classic: 0, jubilee: 0 },
inscription_fee: 2805,
inscription_id: '38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dci0',
inscription_output_value: 10000,
inscriber_address: 'bc1p3cyx5e2hgh53w7kpxcvm8s4kkega9gv5wfw7c4qxsvxl0u8x834qf0u2td',
ordinal_number: 257418248345364,
ordinal_block_height: 51483,
ordinal_offset: 0,
satpoint_post_inscription:
'38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dc:0:0',
curse_type: { tag: 66 },
inscription_input_index: 0,
transfers_pre_inscription: 0,
tx_index: 0,
inscription_pointer: null,
delegate: null,
metaprotocol: null,
metadata: null,
parent: null,
})
.build()
);
await db.updateInscriptions(
new TestChainhookPayloadBuilder()
.apply()
.block({
height: 775618,
hash: '0x00000000000000000000ceb7a81bf3696de0b2260078942f3ab36a7127aff296',
timestamp: 1676913207,
})
.transaction({
hash: '0x42174ecc8a245841035793390bb53d63b3c2acb61366446f601b09e73b94b656',
})
.inscriptionRevealed({
content_bytes: '',
content_type: 'text/plain',
content_length: 0,
inscription_number: { classic: 1, jubilee: 1 },
inscription_fee: 2805,
inscription_id: '42174ecc8a245841035793390bb53d63b3c2acb61366446f601b09e73b94b656i0',
inscription_output_value: 10000,
inscriber_address: 'bc1p3cyx5e2hgh53w7kpxcvm8s4kkega9gv5wfw7c4qxsvxl0u8x834qf0u2td',
ordinal_number: 257418248345364,
ordinal_block_height: 51483,
ordinal_offset: 0,
satpoint_post_inscription:
'42174ecc8a245841035793390bb53d63b3c2acb61366446f601b09e73b94b656:0:0',
curse_type: { tag: 66 },
inscription_input_index: 0,
transfers_pre_inscription: 0,
tx_index: 0,
inscription_pointer: null,
delegate: '38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dci0',
metaprotocol: null,
metadata: null,
parent: null,
})
.build()
);
const response = await fastify.inject({
method: 'GET',
url: '/ordinals/v1/inscriptions/42174ecc8a245841035793390bb53d63b3c2acb61366446f601b09e73b94b656i0/content',
});
expect(response.statusCode).toBe(200);
expect(response.headers['content-length']).toBe('5');
expect(response.body).toBe('Hello');
});
});

describe('transfers', () => {
Expand Down Expand Up @@ -1656,6 +1805,8 @@ describe('/inscriptions', () => {
recursion_refs: null,
parent: null,
metadata: null,
meta_protocol: null,
delegate: null,
},
{
address: 'bc1pscktlmn99gyzlvymvrezh6vwd0l4kg06tg5rvssw0czg8873gz5sdkteqj',
Expand Down Expand Up @@ -1684,6 +1835,8 @@ describe('/inscriptions', () => {
recursion_refs: null,
parent: null,
metadata: null,
meta_protocol: null,
delegate: null,
},
]);
});
Expand Down Expand Up @@ -1799,6 +1952,8 @@ describe('/inscriptions', () => {
recursion_refs: null,
parent: null,
metadata: null,
meta_protocol: null,
delegate: null,
};
expect(responseJson1.results[0]).toStrictEqual(result1);

Expand Down Expand Up @@ -1836,6 +1991,8 @@ describe('/inscriptions', () => {
recursion_refs: null,
parent: null,
metadata: null,
meta_protocol: null,
delegate: null,
};
expect(responseJson2.results[0]).toStrictEqual(result2);

Expand Down
4 changes: 4 additions & 0 deletions tests/api/sats.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ describe('/sats', () => {
recursion_refs: null,
parent: null,
metadata: null,
meta_protocol: null,
delegate: null,
},
{
address: 'bc1p3cyx5e2hgh53w7kpxcvm8s4kkega9gv5wfw7c4qxsvxl0u8x834qf0u2td',
Expand Down Expand Up @@ -215,6 +217,8 @@ describe('/sats', () => {
recursion_refs: null,
parent: null,
metadata: null,
meta_protocol: null,
delegate: null,
},
]);

Expand Down

0 comments on commit 04abd6e

Please sign in to comment.