Skip to content

Commit 517ca68

Browse files
authored
fix: simplify transaction events query (#2279)
* fix: simplify transaction events query * fix: default filters, fix reduce
1 parent 413dae7 commit 517ca68

File tree

1 file changed

+87
-92
lines changed

1 file changed

+87
-92
lines changed

src/datastore/pg-store.ts

Lines changed: 87 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ import {
8787
} from './helpers';
8888
import { PgNotifier } from './pg-notifier';
8989
import { SyntheticPoxEventName } from '../pox-helpers';
90-
import { BasePgStore, PgSqlClient, connectPostgres } from '@hirosystems/api-toolkit';
90+
import { BasePgStore, PgSqlClient, PgSqlQuery, connectPostgres } from '@hirosystems/api-toolkit';
9191
import {
9292
PgServer,
9393
getConnectionArgs,
@@ -1782,7 +1782,91 @@ export class PgStore extends BasePgStore {
17821782
return await this.sqlTransaction(async sql => {
17831783
const refValue = args.addressOrTxId.address ?? args.addressOrTxId.txId;
17841784
const isAddress = args.addressOrTxId.address !== undefined;
1785-
const emptyEvents = sql`SELECT NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL`;
1785+
const eventTypeFilter =
1786+
args.eventTypeFilter && args.eventTypeFilter.length > 0
1787+
? args.eventTypeFilter
1788+
: [
1789+
DbEventTypeId.SmartContractLog,
1790+
DbEventTypeId.StxAsset,
1791+
DbEventTypeId.FungibleTokenAsset,
1792+
DbEventTypeId.NonFungibleTokenAsset,
1793+
DbEventTypeId.StxLock,
1794+
];
1795+
const eventQueries: PgSqlQuery[] = [];
1796+
if (eventTypeFilter.includes(DbEventTypeId.StxLock)) {
1797+
eventQueries.push(sql`
1798+
SELECT
1799+
tx_id, event_index, tx_index, block_height, locked_address as sender, NULL as recipient,
1800+
locked_amount as amount, unlock_height, NULL as asset_identifier, NULL as contract_identifier,
1801+
'0'::bytea as value, NULL as topic, null::bytea as memo, contract_name,
1802+
${DbEventTypeId.StxLock}::integer as event_type_id, 0 as asset_event_type_id
1803+
FROM stx_lock_events
1804+
WHERE ${isAddress ? sql`locked_address = ${refValue}` : sql`tx_id = ${refValue}`}
1805+
AND canonical = true AND microblock_canonical = true
1806+
`);
1807+
}
1808+
if (eventTypeFilter.includes(DbEventTypeId.StxAsset)) {
1809+
eventQueries.push(sql`
1810+
SELECT
1811+
tx_id, event_index, tx_index, block_height, sender, recipient,
1812+
amount, 0 as unlock_height, NULL as asset_identifier, NULL as contract_identifier,
1813+
'0'::bytea as value, NULL as topic, memo, NULL as contract_name,
1814+
${DbEventTypeId.StxAsset}::integer as event_type_id, asset_event_type_id
1815+
FROM stx_events
1816+
WHERE ${
1817+
isAddress
1818+
? sql`(sender = ${refValue} OR recipient = ${refValue})`
1819+
: sql`tx_id = ${refValue}`
1820+
}
1821+
AND canonical = true AND microblock_canonical = true
1822+
`);
1823+
}
1824+
if (eventTypeFilter.includes(DbEventTypeId.FungibleTokenAsset)) {
1825+
eventQueries.push(sql`
1826+
SELECT
1827+
tx_id, event_index, tx_index, block_height, sender, recipient,
1828+
amount, 0 as unlock_height, asset_identifier, NULL as contract_identifier,
1829+
'0'::bytea as value, NULL as topic, null::bytea as memo, NULL as contract_name,
1830+
${DbEventTypeId.FungibleTokenAsset}::integer as event_type_id, asset_event_type_id
1831+
FROM ft_events
1832+
WHERE ${
1833+
isAddress
1834+
? sql`(sender = ${refValue} OR recipient = ${refValue})`
1835+
: sql`tx_id = ${refValue}`
1836+
}
1837+
AND canonical = true AND microblock_canonical = true
1838+
`);
1839+
}
1840+
if (eventTypeFilter.includes(DbEventTypeId.NonFungibleTokenAsset)) {
1841+
eventQueries.push(sql`
1842+
SELECT
1843+
tx_id, event_index, tx_index, block_height, sender, recipient,
1844+
0 as amount, 0 as unlock_height, asset_identifier, NULL as contract_identifier,
1845+
value, NULL as topic, null::bytea as memo, NULL as contract_name,
1846+
${DbEventTypeId.NonFungibleTokenAsset}::integer as event_type_id,
1847+
asset_event_type_id
1848+
FROM nft_events
1849+
WHERE ${
1850+
isAddress
1851+
? sql`(sender = ${refValue} OR recipient = ${refValue})`
1852+
: sql`tx_id = ${refValue}`
1853+
}
1854+
AND canonical = true AND microblock_canonical = true
1855+
`);
1856+
}
1857+
if (eventTypeFilter.includes(DbEventTypeId.SmartContractLog)) {
1858+
eventQueries.push(sql`
1859+
SELECT
1860+
tx_id, event_index, tx_index, block_height, NULL as sender, NULL as recipient,
1861+
0 as amount, 0 as unlock_height, NULL as asset_identifier, contract_identifier,
1862+
value, topic, null::bytea as memo, NULL as contract_name,
1863+
${DbEventTypeId.SmartContractLog}::integer as event_type_id,
1864+
0 as asset_event_type_id
1865+
FROM contract_logs
1866+
WHERE ${isAddress ? sql`contract_identifier = ${refValue}` : sql`tx_id = ${refValue}`}
1867+
AND canonical = true AND microblock_canonical = true
1868+
`);
1869+
}
17861870
const eventsResult = await sql<
17871871
{
17881872
tx_id: string;
@@ -1804,96 +1888,7 @@ export class PgStore extends BasePgStore {
18041888
}[]
18051889
>`
18061890
WITH events AS (
1807-
${
1808-
args.eventTypeFilter.includes(DbEventTypeId.StxLock)
1809-
? sql`
1810-
SELECT
1811-
tx_id, event_index, tx_index, block_height, locked_address as sender, NULL as recipient,
1812-
locked_amount as amount, unlock_height, NULL as asset_identifier, NULL as contract_identifier,
1813-
'0'::bytea as value, NULL as topic, null::bytea as memo, contract_name,
1814-
${DbEventTypeId.StxLock}::integer as event_type_id, 0 as asset_event_type_id
1815-
FROM stx_lock_events
1816-
WHERE ${isAddress ? sql`locked_address = ${refValue}` : sql`tx_id = ${refValue}`}
1817-
AND canonical = true AND microblock_canonical = true
1818-
`
1819-
: emptyEvents
1820-
}
1821-
UNION
1822-
${
1823-
args.eventTypeFilter.includes(DbEventTypeId.StxAsset)
1824-
? sql`
1825-
SELECT
1826-
tx_id, event_index, tx_index, block_height, sender, recipient,
1827-
amount, 0 as unlock_height, NULL as asset_identifier, NULL as contract_identifier,
1828-
'0'::bytea as value, NULL as topic, memo, NULL as contract_name,
1829-
${DbEventTypeId.StxAsset}::integer as event_type_id, asset_event_type_id
1830-
FROM stx_events
1831-
WHERE ${
1832-
isAddress
1833-
? sql`(sender = ${refValue} OR recipient = ${refValue})`
1834-
: sql`tx_id = ${refValue}`
1835-
}
1836-
AND canonical = true AND microblock_canonical = true
1837-
`
1838-
: emptyEvents
1839-
}
1840-
UNION
1841-
${
1842-
args.eventTypeFilter.includes(DbEventTypeId.FungibleTokenAsset)
1843-
? sql`
1844-
SELECT
1845-
tx_id, event_index, tx_index, block_height, sender, recipient,
1846-
amount, 0 as unlock_height, asset_identifier, NULL as contract_identifier,
1847-
'0'::bytea as value, NULL as topic, null::bytea as memo, NULL as contract_name,
1848-
${DbEventTypeId.FungibleTokenAsset}::integer as event_type_id, asset_event_type_id
1849-
FROM ft_events
1850-
WHERE ${
1851-
isAddress
1852-
? sql`(sender = ${refValue} OR recipient = ${refValue})`
1853-
: sql`tx_id = ${refValue}`
1854-
}
1855-
AND canonical = true AND microblock_canonical = true
1856-
`
1857-
: emptyEvents
1858-
}
1859-
UNION
1860-
${
1861-
args.eventTypeFilter.includes(DbEventTypeId.NonFungibleTokenAsset)
1862-
? sql`
1863-
SELECT
1864-
tx_id, event_index, tx_index, block_height, sender, recipient,
1865-
0 as amount, 0 as unlock_height, asset_identifier, NULL as contract_identifier,
1866-
value, NULL as topic, null::bytea as memo, NULL as contract_name,
1867-
${DbEventTypeId.NonFungibleTokenAsset}::integer as event_type_id,
1868-
asset_event_type_id
1869-
FROM nft_events
1870-
WHERE ${
1871-
isAddress
1872-
? sql`(sender = ${refValue} OR recipient = ${refValue})`
1873-
: sql`tx_id = ${refValue}`
1874-
}
1875-
AND canonical = true AND microblock_canonical = true
1876-
`
1877-
: emptyEvents
1878-
}
1879-
UNION
1880-
${
1881-
args.eventTypeFilter.includes(DbEventTypeId.SmartContractLog)
1882-
? sql`
1883-
SELECT
1884-
tx_id, event_index, tx_index, block_height, NULL as sender, NULL as recipient,
1885-
0 as amount, 0 as unlock_height, NULL as asset_identifier, contract_identifier,
1886-
value, topic, null::bytea as memo, NULL as contract_name,
1887-
${DbEventTypeId.SmartContractLog}::integer as event_type_id,
1888-
0 as asset_event_type_id
1889-
FROM contract_logs
1890-
WHERE ${
1891-
isAddress ? sql`contract_identifier = ${refValue}` : sql`tx_id = ${refValue}`
1892-
}
1893-
AND canonical = true AND microblock_canonical = true
1894-
`
1895-
: emptyEvents
1896-
}
1891+
${eventQueries.reduce((accum, query) => sql`${accum} UNION ${query}`)}
18971892
)
18981893
SELECT *
18991894
FROM events JOIN txs USING(tx_id)

0 commit comments

Comments
 (0)