Skip to content

Commit

Permalink
[api] add rel filter on emerald/transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew7234 committed Feb 14, 2023
1 parent 96a1407 commit 7fdfff4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 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
28 changes: 21 additions & 7 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
SELECT
%[1]s.runtime_transactions.round,
%[1]s.runtime_transactions.tx_index,
%[1]s.runtime_transactions.tx_hash,
%[1]s.runtime_transactions.tx_eth_hash,
%[1]s.runtime_transactions.timestamp,
%[1]s.runtime_transactions.raw,
%[1]s.runtime_transactions.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)
LEFT JOIN %[1]s.runtime_related_transactions ON %[1]s.runtime_transactions.round = %[1]s.runtime_related_transactions.tx_round
AND %[1]s.runtime_transactions.tx_index = %[1]s.runtime_related_transactions.tx_index
AND %[1]s.runtime_transactions.runtime = %[1]s.runtime_related_transactions.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 (%[1]s.runtime_transactions.runtime = '%[2]s') AND
($1::bigint IS NULL OR %[1]s.runtime_transactions.round = $1::bigint) AND
($2::text IS NULL OR %[1]s.runtime_transactions.tx_hash = $2::text) AND
($3::text IS NULL OR %[1]s.runtime_related_transactions.account_address = $3::text)
ORDER BY %[1]s.runtime_transactions.round DESC, %[1]s.runtime_transactions.tx_index DESC
LIMIT $4::bigint
OFFSET $5::bigint`, qf.chainID, qf.runtime)
}

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

0 comments on commit 7fdfff4

Please sign in to comment.