Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions src/datastore/pg-store-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,38 +335,37 @@ export class PgStoreV2 extends BasePgStoreModule {
return await this.sqlTransaction(async sql => {
const limit = args.limit ?? TransactionLimitParamSchema.default;
const offset = args.offset ?? 0;
const filter =
args.block.type === 'latest'
? sql`index_block_hash = (SELECT index_block_hash FROM blocks WHERE canonical = TRUE ORDER BY block_height DESC LIMIT 1)`
: args.block.type === 'hash'
? sql`(
block_hash = ${normalizeHashString(args.block.hash)}
OR index_block_hash = ${normalizeHashString(args.block.hash)}
)`
: sql`block_height = ${args.block.height}`;
const blockCheck = await sql`SELECT index_block_hash FROM blocks WHERE ${filter} LIMIT 1`;
if (blockCheck.count === 0)
throw new InvalidRequestError(`Block not found`, InvalidRequestErrorType.invalid_param);
const txsQuery = await sql<(TxQueryResult & { total: number })[]>`
WITH tx_count AS (
SELECT tx_count AS total FROM blocks WHERE canonical = TRUE AND ${filter}
WITH block_ptr AS (
SELECT index_block_hash FROM blocks
WHERE ${
args.block.type === 'latest'
? sql`canonical = TRUE ORDER BY block_height DESC`
: args.block.type === 'hash'
? sql`(
block_hash = ${normalizeHashString(args.block.hash)}
OR index_block_hash = ${normalizeHashString(args.block.hash)}
) AND canonical = TRUE`
: sql`block_height = ${args.block.height} AND canonical = TRUE`
}
LIMIT 1
),
tx_count AS (
SELECT tx_count AS total
FROM blocks
WHERE index_block_hash = (SELECT index_block_hash FROM block_ptr)
)
SELECT ${sql(TX_COLUMNS)}, (SELECT total FROM tx_count)::int AS total
FROM txs
WHERE canonical = true
AND microblock_canonical = true
AND ${filter}
AND index_block_hash = (SELECT index_block_hash FROM block_ptr)
ORDER BY microblock_sequence ASC, tx_index ASC
LIMIT ${limit}
OFFSET ${offset}
`;
if (txsQuery.count === 0)
return {
limit,
offset,
results: [],
total: 0,
};
throw new InvalidRequestError(`Block not found`, InvalidRequestErrorType.invalid_param);
return {
limit,
offset,
Expand Down
14 changes: 13 additions & 1 deletion tests/api/tx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4198,7 +4198,7 @@ describe('tx tests', () => {
);
expect(result.status).toBe(200);
expect(result.type).toBe('application/json');
const json = JSON.parse(result.text);
let json = JSON.parse(result.text);
expect(json.total).toBe(2);
expect(json.results[0]).toStrictEqual({
anchor_mode: 'any',
Expand Down Expand Up @@ -4244,6 +4244,18 @@ describe('tx tests', () => {
tx_type: 'coinbase',
});

result = await supertest(api.server).get(`/extended/v2/blocks/latest/transactions`);
expect(result.status).toBe(200);
expect(result.type).toBe('application/json');
json = JSON.parse(result.text);
expect(json.total).toBe(2);

result = await supertest(api.server).get(`/extended/v2/blocks/1/transactions`);
expect(result.status).toBe(200);
expect(result.type).toBe('application/json');
json = JSON.parse(result.text);
expect(json.total).toBe(2);

// Try a non-existent block
result = await supertest(api.server).get(
`/extended/v2/blocks/0x00000000000000000001e2ee7f0c6bd5361b5e7afd76156ca7d6f524ee999999/transactions?limit=20&offset=0`
Expand Down
Loading