From fabd8f3fa616e4da9eb0d5d74dc790c2ca1ddde0 Mon Sep 17 00:00:00 2001 From: Rafael Cardenas Date: Mon, 24 Jul 2023 22:01:01 -0600 Subject: [PATCH] feat: add inscription number sort option --- src/api/schemas.ts | 1 + src/pg/pg-store.ts | 5 +- tests/inscriptions.test.ts | 118 +++++++++++++++++++++++++++++++++++++ tests/sats.test.ts | 36 +++++------ 4 files changed, 141 insertions(+), 19 deletions(-) diff --git a/src/api/schemas.ts b/src/api/schemas.ts index ff4de892..ec1045b2 100644 --- a/src/api/schemas.ts +++ b/src/api/schemas.ts @@ -179,6 +179,7 @@ export const LimitParam = Type.Integer({ }); export enum OrderBy { + number = 'number', genesis_block_height = 'genesis_block_height', ordinal = 'ordinal', rarity = 'rarity', diff --git a/src/pg/pg-store.ts b/src/pg/pg-store.ts index fde29707..4fbce859 100644 --- a/src/pg/pg-store.ts +++ b/src/pg/pg-store.ts @@ -326,8 +326,11 @@ export class PgStore extends BasePgStore { // materialized views to speed up these queries. const countType = getIndexResultCountType(filters); // `ORDER BY` statement - let orderBy = sql`gen.block_height`; + let orderBy = sql`i.number`; switch (sort?.order_by) { + case OrderBy.genesis_block_height: + orderBy = sql`gen.block_height`; + break; case OrderBy.ordinal: orderBy = sql`i.sat_ordinal`; break; diff --git a/tests/inscriptions.test.ts b/tests/inscriptions.test.ts index 3952e437..07d785bc 100644 --- a/tests/inscriptions.test.ts +++ b/tests/inscriptions.test.ts @@ -2354,6 +2354,124 @@ describe('/inscriptions', () => { }); describe('ordering', () => { + test('index ordered by number', async () => { + await db.updateInscriptions( + new TestChainhookPayloadBuilder() + .apply() + .block({ + height: 778575, + hash: '0x00000000000000000002a90330a99f67e3f01eb2ce070b45930581e82fb7a91d', + timestamp: 1676913207, + }) + .transaction({ + hash: '9f4a9b73b0713c5da01c0a47f97c6c001af9028d6bdd9e264dfacbc4e6790201', + }) + .inscriptionRevealed({ + content_bytes: '0x48656C6C6F', + content_type: 'text/plain;charset=utf-8', + content_length: 5, + inscription_number: 7, + inscription_fee: 705, + inscription_id: '9f4a9b73b0713c5da01c0a47f97c6c001af9028d6bdd9e264dfacbc4e6790201i0', + inscription_output_value: 10000, + inscriber_address: 'bc1pscktlmn99gyzlvymvrezh6vwd0l4kg06tg5rvssw0czg8873gz5sdkteqj', + ordinal_number: 257418248345364, + ordinal_block_height: 650000, + ordinal_offset: 0, + satpoint_post_inscription: + '9f4a9b73b0713c5da01c0a47f97c6c001af9028d6bdd9e264dfacbc4e6790201:0:0', + inscription_input_index: 0, + transfers_pre_inscription: 0, + tx_index: 0, + }) + .build() + ); + await db.updateInscriptions( + new TestChainhookPayloadBuilder() + .apply() + .block({ + height: 775617, + hash: '00000000000000000002a90330a99f67e3f01eb2ce070b45930581e82fb7a91d', + timestamp: 1676913207, + }) + .transaction({ + hash: '38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dc', + }) + .inscriptionRevealed({ + content_bytes: '0x48656C6C6F', + content_type: 'text/plain;charset=utf-8', + content_length: 5, + inscription_number: 8, + inscription_fee: 705, + inscription_id: '38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dci0', + inscription_output_value: 10000, + inscriber_address: 'bc1p3cyx5e2hgh53w7kpxcvm8s4kkega9gv5wfw7c4qxsvxl0u8x834qf0u2td', + ordinal_number: 1050000000000000, + ordinal_block_height: 650000, + ordinal_offset: 0, + satpoint_post_inscription: + '38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dc:0:0', + inscription_input_index: 0, + transfers_pre_inscription: 0, + tx_index: 0, + }) + .build() + ); + await db.updateInscriptions( + new TestChainhookPayloadBuilder() + .apply() + .block({ + height: 778583, + hash: '00000000000000000002a90330a99f67e3f01eb2ce070b45930581e82fb7a91d', + timestamp: 1676913207, + }) + .transaction({ + hash: '567c7605439dfdc3a289d13fd2132237852f4a56e784b9364ba94499d5f9baf1', + }) + .inscriptionRevealed({ + content_bytes: '0x48656C6C6F', + content_type: 'image/png', + content_length: 5, + inscription_number: 9, + inscription_fee: 2805, + inscription_id: '567c7605439dfdc3a289d13fd2132237852f4a56e784b9364ba94499d5f9baf1i0', + inscription_output_value: 10000, + inscriber_address: 'bc1pxq6t85qp57aw8yf8eh9t7vsgd9zm5a8372rdll5jzrmc3cxqdpmqfucdry', + ordinal_number: 0, + ordinal_block_height: 0, + ordinal_offset: 0, + satpoint_post_inscription: + '567c7605439dfdc3a289d13fd2132237852f4a56e784b9364ba94499d5f9baf1:0:0', + inscription_input_index: 0, + transfers_pre_inscription: 0, + tx_index: 0, + }) + .build() + ); + + const response1 = await fastify.inject({ + method: 'GET', + url: '/ordinals/v1/inscriptions?order_by=number&order=asc', + }); + expect(response1.statusCode).toBe(200); + const responseJson1 = response1.json(); + expect(responseJson1.total).toBe(3); + expect(responseJson1.results[0].number).toStrictEqual(7); + expect(responseJson1.results[1].number).toStrictEqual(8); + expect(responseJson1.results[2].number).toStrictEqual(9); + + const response2 = await fastify.inject({ + method: 'GET', + url: '/ordinals/v1/inscriptions?order_by=number&order=desc', + }); + expect(response2.statusCode).toBe(200); + const responseJson2 = response2.json(); + expect(responseJson2.total).toBe(3); + expect(responseJson2.results[0].number).toStrictEqual(9); + expect(responseJson2.results[1].number).toStrictEqual(8); + expect(responseJson2.results[2].number).toStrictEqual(7); + }); + test('index ordered by sat rarity', async () => { await db.updateInscriptions( new TestChainhookPayloadBuilder() diff --git a/tests/sats.test.ts b/tests/sats.test.ts index 5beaf005..355d4771 100644 --- a/tests/sats.test.ts +++ b/tests/sats.test.ts @@ -146,22 +146,22 @@ describe('/sats', () => { content_length: 5, content_type: 'image/png', genesis_address: 'bc1p3cyx5e2hgh53w7kpxcvm8s4kkega9gv5wfw7c4qxsvxl0u8x834qf0u2td', - genesis_block_hash: '163de66dc9c0949905bfe8e148bde04600223cf88d19f26fdbeba1d6e6fa0f88', + genesis_block_hash: '000000000000000000002a244dc7dfcf8ab85e42d182531c27197fc125086f19', genesis_block_height: 775617, genesis_fee: '2805', - genesis_timestamp: 1677803510000, - genesis_tx_id: '38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dc', - id: '38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dci0', - location: '38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dc:0:0', + genesis_timestamp: 1676913207000, + genesis_tx_id: 'b9cd9489fe30b81d007f753663d12766f1368721a87f4c69056c8215caa57993', + id: 'b9cd9489fe30b81d007f753663d12766f1368721a87f4c69056c8215caa57993i0', + location: 'b9cd9489fe30b81d007f753663d12766f1368721a87f4c69056c8215caa57993:0:0', mime_type: 'image/png', - number: -7, + number: -1, offset: '0', - output: '38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dc:0', + output: 'b9cd9489fe30b81d007f753663d12766f1368721a87f4c69056c8215caa57993:0', sat_coinbase_height: 51483, sat_ordinal: '257418248345364', sat_rarity: 'common', - timestamp: 1677803510000, - tx_id: '38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dc', + timestamp: 1676913207000, + tx_id: 'b9cd9489fe30b81d007f753663d12766f1368721a87f4c69056c8215caa57993', value: '10000', curse_type: '"p2wsh"', }, @@ -170,22 +170,22 @@ describe('/sats', () => { content_length: 5, content_type: 'image/png', genesis_address: 'bc1p3cyx5e2hgh53w7kpxcvm8s4kkega9gv5wfw7c4qxsvxl0u8x834qf0u2td', - genesis_block_hash: '000000000000000000002a244dc7dfcf8ab85e42d182531c27197fc125086f19', + genesis_block_hash: '163de66dc9c0949905bfe8e148bde04600223cf88d19f26fdbeba1d6e6fa0f88', genesis_block_height: 775617, genesis_fee: '2805', - genesis_timestamp: 1676913207000, - genesis_tx_id: 'b9cd9489fe30b81d007f753663d12766f1368721a87f4c69056c8215caa57993', - id: 'b9cd9489fe30b81d007f753663d12766f1368721a87f4c69056c8215caa57993i0', - location: 'b9cd9489fe30b81d007f753663d12766f1368721a87f4c69056c8215caa57993:0:0', + genesis_timestamp: 1677803510000, + genesis_tx_id: '38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dc', + id: '38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dci0', + location: '38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dc:0:0', mime_type: 'image/png', - number: -1, + number: -7, offset: '0', - output: 'b9cd9489fe30b81d007f753663d12766f1368721a87f4c69056c8215caa57993:0', + output: '38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dc:0', sat_coinbase_height: 51483, sat_ordinal: '257418248345364', sat_rarity: 'common', - timestamp: 1676913207000, - tx_id: 'b9cd9489fe30b81d007f753663d12766f1368721a87f4c69056c8215caa57993', + timestamp: 1677803510000, + tx_id: '38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dc', value: '10000', curse_type: '"p2wsh"', },