Skip to content

Commit

Permalink
[api] add rel filter on emerald/transactions
Browse files Browse the repository at this point in the history
address comments
  • Loading branch information
Andrew7234 committed Feb 14, 2023
1 parent 3001073 commit 2cbc692
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
6 changes: 4 additions & 2 deletions storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,7 @@ func (c *StorageClient) RuntimeTransactions(ctx context.Context, p apiTypes.GetR
QueryFactoryFromCtx(ctx).RuntimeTransactionsQuery(),
p.Block,
nil, // tx_hash; filter not supported by this endpoint
p.Rel,
p.Limit,
p.Offset,
)
Expand Down Expand Up @@ -1023,8 +1024,9 @@ func (c *StorageClient) RuntimeTransaction(ctx context.Context, txHash string) (
QueryFactoryFromCtx(ctx).RuntimeTransactionsQuery(),
nil, // block; filter not supported by this endpoint
txHash,
1, // limit
0, // offset
nil, // rel; filter not supported by this endpoint
1, // limit
0, // offset
).Scan(
&t.Round,
&t.Index,
Expand Down
30 changes: 22 additions & 8 deletions storage/client/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,28 @@ func (qf QueryFactory) RuntimeBlocksQuery() string {

func (qf QueryFactory) RuntimeTransactionsQuery() string {
return fmt.Sprintf(`
SELECT round, tx_index, tx_hash, tx_eth_hash, timestamp, raw, result_raw
FROM %[1]s.runtime_transactions
WHERE (runtime = '%[2]s') AND
($1::bigint IS NULL OR round = $1::bigint) AND
($2::text IS NULL OR tx_hash = $2::text)
ORDER BY round DESC, tx_index DESC
LIMIT $3::bigint
OFFSET $4::bigint`, qf.chainID, qf.runtime)
SELECT
txs.round,
txs.tx_index,
txs.tx_hash,
txs.tx_eth_hash,
txs.timestamp,
txs.raw,
txs.result_raw
FROM %[1]s.runtime_transactions AS txs
LEFT JOIN %[1]s.runtime_related_transactions AS rel_accounts ON txs.round = rel_accounts.tx_round
AND txs.tx_index = rel_accounts.tx_index
AND txs.runtime = rel_accounts.runtime
-- When related_address ($3) is NULL and hence we do no filtering on it, avoid the join altogether.
-- Otherwise, every tx will be returned as many times as there are related addresses for it.
AND $3::text IS NOT NULL
WHERE (txs.runtime = '%[2]s') AND
($1 IS NULL OR txs.round = $1::bigint) AND
($2 IS NULL OR txs.tx_hash = $2::text) AND
($3 IS NULL OR rel_accounts.account_address = $3::text)
ORDER BY txs.round DESC, txs.tx_index DESC
LIMIT $4::bigint
OFFSET $5::bigint`, qf.chainID, qf.runtime)
}

func (qf QueryFactory) RuntimeEventsQuery() string {
Expand Down

0 comments on commit 2cbc692

Please sign in to comment.