Skip to content

Commit

Permalink
make input addresses and metadata optional (as an arg)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecioppettini committed Jan 30, 2024
1 parent b3f2530 commit 88c9b02
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export class TransactionHistoryController extends Controller {
limit: requestBody.limit ?? ADDRESS_LIMIT.RESPONSE,
until,
dbTx,
withInputContext: !!requestBody.withInputContext
};
const result = await Promise.all([
historyForCredentials({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface ISqlHistoryForAddressesParams {
after_tx_id?: NumberOrString | null | void;
limit?: NumberOrString | null | void;
until_tx_id?: NumberOrString | null | void;
with_input_context: boolean;
}

/** 'SqlHistoryForAddresses' return type */
Expand All @@ -22,10 +23,10 @@ export interface ISqlHistoryForAddressesResult {
era: number;
hash: Buffer;
height: number;
id: string;
id: string | null;
input_addresses: Json | null;
is_valid: boolean;
metadata: Buffer;
metadata: Buffer | null;
payload: Buffer;
slot: number;
tx_index: number;
Expand All @@ -37,7 +38,7 @@ export interface ISqlHistoryForAddressesQuery {
result: ISqlHistoryForAddressesResult;
}

const sqlHistoryForAddressesIR: any = {"usedParamSet":{"addresses":true,"until_tx_id":true,"after_tx_id":true,"limit":true},"params":[{"name":"addresses","required":false,"transform":{"type":"scalar"},"locs":[{"a":91,"b":100}]},{"name":"until_tx_id","required":false,"transform":{"type":"scalar"},"locs":[{"a":373,"b":384},{"a":788,"b":799},{"a":1250,"b":1261}]},{"name":"after_tx_id","required":false,"transform":{"type":"scalar"},"locs":[{"a":440,"b":451},{"a":854,"b":865},{"a":1325,"b":1336}]},{"name":"limit","required":false,"transform":{"type":"scalar"},"locs":[{"a":516,"b":521},{"a":929,"b":934},{"a":1409,"b":1414},{"a":2329,"b":2334}]}],"statement":"WITH\n address_row AS (\n SELECT *\n FROM \"Address\"\n WHERE \"Address\".payload = ANY (:addresses)\n ),\n outputs AS (\n SELECT DISTINCT ON (\"TransactionOutput\".tx_id) \"TransactionOutput\".tx_id\n FROM \"TransactionOutput\"\n INNER JOIN address_row ON \"TransactionOutput\".address_id = address_row.id\n WHERE\n \"TransactionOutput\".tx_id <= (:until_tx_id)\n AND\n \"TransactionOutput\".tx_id > (:after_tx_id)\n ORDER BY \"TransactionOutput\".tx_id ASC\n LIMIT (:limit)\n ),\n inputs AS (\n SELECT DISTINCT ON (\"TransactionInput\".tx_id) \"TransactionInput\".tx_id\n FROM \"TransactionInput\"\n INNER JOIN address_row ON \"TransactionInput\".address_id = address_row.id\n WHERE\n \"TransactionInput\".tx_id <= (:until_tx_id)\n AND\n \"TransactionInput\".tx_id > (:after_tx_id)\n ORDER BY \"TransactionInput\".tx_id ASC\n LIMIT (:limit)\n ),\n ref_inputs AS (\n SELECT DISTINCT ON (\"TransactionReferenceInput\".tx_id) \"TransactionReferenceInput\".tx_id\n FROM \"TransactionReferenceInput\"\n INNER JOIN address_row ON \"TransactionReferenceInput\".address_id = address_row.id\n WHERE\n \"TransactionReferenceInput\".tx_id <= (:until_tx_id)\n AND\n \"TransactionReferenceInput\".tx_id > (:after_tx_id)\n ORDER BY \"TransactionReferenceInput\".tx_id ASC\n LIMIT (:limit)\n )\nSELECT \"Transaction\".id,\n \"Transaction\".payload,\n \"Transaction\".hash,\n \"Transaction\".tx_index,\n \"Transaction\".is_valid,\n \"Block\".hash AS block_hash,\n \"Block\".epoch,\n \"Block\".slot,\n \"Block\".era,\n \"Block\".height,\n \"TransactionMetadata\".payload AS metadata,\n json_agg(DISTINCT \"Address\".PAYLOAD) input_addresses\nFROM \"Transaction\"\nINNER JOIN \"Block\" ON \"Transaction\".block_id = \"Block\".id\nINNER JOIN \"TransactionInput\" ON \"TransactionInput\".tx_id = \"Transaction\".id\nINNER JOIN \"Address\" ON \"Address\".id = \"TransactionInput\".address_id\nLEFT JOIN \"TransactionMetadata\" ON \"Transaction\".id = \"TransactionMetadata\".tx_id\nWHERE \"Transaction\".id IN (SELECT * FROM inputs UNION ALL SELECT * from ref_inputs UNION ALL SELECT * from outputs)\nGROUP BY \"Transaction\".id, \"Block\".id, \"TransactionMetadata\".id\nORDER BY \"Transaction\".id ASC\nLIMIT (:limit)"};
const sqlHistoryForAddressesIR: any = {"usedParamSet":{"addresses":true,"until_tx_id":true,"after_tx_id":true,"limit":true,"with_input_context":true},"params":[{"name":"addresses","required":false,"transform":{"type":"scalar"},"locs":[{"a":91,"b":100}]},{"name":"until_tx_id","required":false,"transform":{"type":"scalar"},"locs":[{"a":373,"b":384},{"a":788,"b":799},{"a":1250,"b":1261}]},{"name":"after_tx_id","required":false,"transform":{"type":"scalar"},"locs":[{"a":440,"b":451},{"a":854,"b":865},{"a":1325,"b":1336}]},{"name":"limit","required":false,"transform":{"type":"scalar"},"locs":[{"a":516,"b":521},{"a":929,"b":934},{"a":1409,"b":1414},{"a":2215,"b":2220},{"a":3446,"b":3451}]},{"name":"with_input_context","required":true,"transform":{"type":"scalar"},"locs":[{"a":3493,"b":3512},{"a":3576,"b":3595}]}],"statement":"WITH\n address_row AS (\n SELECT *\n FROM \"Address\"\n WHERE \"Address\".payload = ANY (:addresses)\n ),\n outputs AS (\n SELECT DISTINCT ON (\"TransactionOutput\".tx_id) \"TransactionOutput\".tx_id\n FROM \"TransactionOutput\"\n INNER JOIN address_row ON \"TransactionOutput\".address_id = address_row.id\n WHERE\n \"TransactionOutput\".tx_id <= (:until_tx_id)\n AND\n \"TransactionOutput\".tx_id > (:after_tx_id)\n ORDER BY \"TransactionOutput\".tx_id ASC\n LIMIT (:limit)\n ),\n inputs AS (\n SELECT DISTINCT ON (\"TransactionInput\".tx_id) \"TransactionInput\".tx_id\n FROM \"TransactionInput\"\n INNER JOIN address_row ON \"TransactionInput\".address_id = address_row.id\n WHERE\n \"TransactionInput\".tx_id <= (:until_tx_id)\n AND\n \"TransactionInput\".tx_id > (:after_tx_id)\n ORDER BY \"TransactionInput\".tx_id ASC\n LIMIT (:limit)\n ),\n ref_inputs AS (\n SELECT DISTINCT ON (\"TransactionReferenceInput\".tx_id) \"TransactionReferenceInput\".tx_id\n FROM \"TransactionReferenceInput\"\n INNER JOIN address_row ON \"TransactionReferenceInput\".address_id = address_row.id\n WHERE\n \"TransactionReferenceInput\".tx_id <= (:until_tx_id)\n AND\n \"TransactionReferenceInput\".tx_id > (:after_tx_id)\n ORDER BY \"TransactionReferenceInput\".tx_id ASC\n LIMIT (:limit)\n ),\n base_query AS (\n SELECT \"Transaction\".id,\n \"Transaction\".payload as \"payload!\",\n \"Transaction\".hash as \"hash!\",\n \"Transaction\".tx_index as \"tx_index!\",\n \"Transaction\".is_valid as \"is_valid!\",\n \"Block\".hash AS \"block_hash!\",\n \"Block\".epoch as \"epoch!\",\n \"Block\".slot as \"slot!\",\n \"Block\".era as \"era!\",\n \"Block\".height as \"height!\",\n NULL :: bytea as metadata,\n NULL :: json as input_addresses\n FROM \"Transaction\"\n INNER JOIN \"Block\" ON \"Transaction\".block_id = \"Block\".id\n WHERE \"Transaction\".id IN (SELECT * FROM inputs UNION ALL SELECT * from ref_inputs UNION ALL SELECT * from outputs)\n ORDER BY \"Transaction\".id ASC\n LIMIT (:limit)\n ),\n query_with_inputs_and_metadata AS (\n SELECT \"Transaction\".id,\n \"Transaction\".payload as \"payload!\",\n \"Transaction\".hash as \"hash!\",\n \"Transaction\".tx_index as \"tx_index!\",\n \"Transaction\".is_valid as \"is_valid!\",\n \"Block\".hash AS \"block_hash!\",\n \"Block\".epoch as \"epoch!\",\n \"Block\".slot as \"slot!\",\n \"Block\".era as \"era!\",\n \"Block\".height as \"height!\",\n \"TransactionMetadata\".payload AS metadata,\n json_agg(DISTINCT \"Address\".PAYLOAD) input_addresses\n FROM \"Transaction\"\n INNER JOIN \"Block\" ON \"Transaction\".block_id = \"Block\".id\n INNER JOIN \"TransactionInput\" ON \"TransactionInput\".tx_id = \"Transaction\".id\n INNER JOIN \"Address\" ON \"Address\".id = \"TransactionInput\".address_id\n LEFT JOIN \"TransactionMetadata\" ON \"Transaction\".id = \"TransactionMetadata\".tx_id\n WHERE \"Transaction\".id IN (SELECT * FROM inputs UNION ALL SELECT * from ref_inputs UNION ALL SELECT * from outputs)\n GROUP BY \"Transaction\".id, \"Block\".id, \"TransactionMetadata\".id\n ORDER BY \"Transaction\".id ASC\n LIMIT (:limit)\n )\nSELECT * FROM base_query WHERE NOT :with_input_context!\nUNION ALL\n(SELECT * from query_with_inputs_and_metadata WHERE :with_input_context!)"};

/**
* Query generated from SQL:
Expand Down Expand Up @@ -80,28 +81,52 @@ const sqlHistoryForAddressesIR: any = {"usedParamSet":{"addresses":true,"until_t
* "TransactionReferenceInput".tx_id > (:after_tx_id)
* ORDER BY "TransactionReferenceInput".tx_id ASC
* LIMIT (:limit)
* ),
* base_query AS (
* SELECT "Transaction".id,
* "Transaction".payload as "payload!",
* "Transaction".hash as "hash!",
* "Transaction".tx_index as "tx_index!",
* "Transaction".is_valid as "is_valid!",
* "Block".hash AS "block_hash!",
* "Block".epoch as "epoch!",
* "Block".slot as "slot!",
* "Block".era as "era!",
* "Block".height as "height!",
* NULL :: bytea as metadata,
* NULL :: json as input_addresses
* FROM "Transaction"
* INNER JOIN "Block" ON "Transaction".block_id = "Block".id
* WHERE "Transaction".id IN (SELECT * FROM inputs UNION ALL SELECT * from ref_inputs UNION ALL SELECT * from outputs)
* ORDER BY "Transaction".id ASC
* LIMIT (:limit)
* ),
* query_with_inputs_and_metadata AS (
* SELECT "Transaction".id,
* "Transaction".payload as "payload!",
* "Transaction".hash as "hash!",
* "Transaction".tx_index as "tx_index!",
* "Transaction".is_valid as "is_valid!",
* "Block".hash AS "block_hash!",
* "Block".epoch as "epoch!",
* "Block".slot as "slot!",
* "Block".era as "era!",
* "Block".height as "height!",
* "TransactionMetadata".payload AS metadata,
* json_agg(DISTINCT "Address".PAYLOAD) input_addresses
* FROM "Transaction"
* INNER JOIN "Block" ON "Transaction".block_id = "Block".id
* INNER JOIN "TransactionInput" ON "TransactionInput".tx_id = "Transaction".id
* INNER JOIN "Address" ON "Address".id = "TransactionInput".address_id
* LEFT JOIN "TransactionMetadata" ON "Transaction".id = "TransactionMetadata".tx_id
* WHERE "Transaction".id IN (SELECT * FROM inputs UNION ALL SELECT * from ref_inputs UNION ALL SELECT * from outputs)
* GROUP BY "Transaction".id, "Block".id, "TransactionMetadata".id
* ORDER BY "Transaction".id ASC
* LIMIT (:limit)
* )
* SELECT "Transaction".id,
* "Transaction".payload,
* "Transaction".hash,
* "Transaction".tx_index,
* "Transaction".is_valid,
* "Block".hash AS block_hash,
* "Block".epoch,
* "Block".slot,
* "Block".era,
* "Block".height,
* "TransactionMetadata".payload AS metadata,
* json_agg(DISTINCT "Address".PAYLOAD) input_addresses
* FROM "Transaction"
* INNER JOIN "Block" ON "Transaction".block_id = "Block".id
* INNER JOIN "TransactionInput" ON "TransactionInput".tx_id = "Transaction".id
* INNER JOIN "Address" ON "Address".id = "TransactionInput".address_id
* LEFT JOIN "TransactionMetadata" ON "Transaction".id = "TransactionMetadata".tx_id
* WHERE "Transaction".id IN (SELECT * FROM inputs UNION ALL SELECT * from ref_inputs UNION ALL SELECT * from outputs)
* GROUP BY "Transaction".id, "Block".id, "TransactionMetadata".id
* ORDER BY "Transaction".id ASC
* LIMIT (:limit)
* SELECT * FROM base_query WHERE NOT :with_input_context!
* UNION ALL
* (SELECT * from query_with_inputs_and_metadata WHERE :with_input_context!)
* ```
*/
export const sqlHistoryForAddresses = new PreparedQuery<ISqlHistoryForAddressesParams,ISqlHistoryForAddressesResult>(sqlHistoryForAddressesIR);
Expand Down
66 changes: 45 additions & 21 deletions webserver/server/app/models/transaction/sqlHistoryForAddresses.sql
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,49 @@ WITH
"TransactionReferenceInput".tx_id > (:after_tx_id)
ORDER BY "TransactionReferenceInput".tx_id ASC
LIMIT (:limit)
),
base_query AS (
SELECT "Transaction".id,
"Transaction".payload as "payload!",
"Transaction".hash as "hash!",
"Transaction".tx_index as "tx_index!",
"Transaction".is_valid as "is_valid!",
"Block".hash AS "block_hash!",
"Block".epoch as "epoch!",
"Block".slot as "slot!",
"Block".era as "era!",
"Block".height as "height!",
NULL :: bytea as metadata,
NULL :: json as input_addresses
FROM "Transaction"
INNER JOIN "Block" ON "Transaction".block_id = "Block".id
WHERE "Transaction".id IN (SELECT * FROM inputs UNION ALL SELECT * from ref_inputs UNION ALL SELECT * from outputs)
ORDER BY "Transaction".id ASC
LIMIT (:limit)
),
query_with_inputs_and_metadata AS (
SELECT "Transaction".id,
"Transaction".payload as "payload!",
"Transaction".hash as "hash!",
"Transaction".tx_index as "tx_index!",
"Transaction".is_valid as "is_valid!",
"Block".hash AS "block_hash!",
"Block".epoch as "epoch!",
"Block".slot as "slot!",
"Block".era as "era!",
"Block".height as "height!",
"TransactionMetadata".payload AS metadata,
json_agg(DISTINCT "Address".PAYLOAD) input_addresses
FROM "Transaction"
INNER JOIN "Block" ON "Transaction".block_id = "Block".id
INNER JOIN "TransactionInput" ON "TransactionInput".tx_id = "Transaction".id
INNER JOIN "Address" ON "Address".id = "TransactionInput".address_id
LEFT JOIN "TransactionMetadata" ON "Transaction".id = "TransactionMetadata".tx_id
WHERE "Transaction".id IN (SELECT * FROM inputs UNION ALL SELECT * from ref_inputs UNION ALL SELECT * from outputs)
GROUP BY "Transaction".id, "Block".id, "TransactionMetadata".id
ORDER BY "Transaction".id ASC
LIMIT (:limit)
)
SELECT "Transaction".id,
"Transaction".payload,
"Transaction".hash,
"Transaction".tx_index,
"Transaction".is_valid,
"Block".hash AS block_hash,
"Block".epoch,
"Block".slot,
"Block".era,
"Block".height,
"TransactionMetadata".payload AS metadata,
json_agg(DISTINCT "Address".PAYLOAD) input_addresses
FROM "Transaction"
INNER JOIN "Block" ON "Transaction".block_id = "Block".id
INNER JOIN "TransactionInput" ON "TransactionInput".tx_id = "Transaction".id
INNER JOIN "Address" ON "Address".id = "TransactionInput".address_id
LEFT JOIN "TransactionMetadata" ON "Transaction".id = "TransactionMetadata".tx_id
WHERE "Transaction".id IN (SELECT * FROM inputs UNION ALL SELECT * from ref_inputs UNION ALL SELECT * from outputs)
GROUP BY "Transaction".id, "Block".id, "TransactionMetadata".id
ORDER BY "Transaction".id ASC
LIMIT (:limit);
SELECT * FROM base_query WHERE NOT :with_input_context!
UNION ALL
(SELECT * from query_with_inputs_and_metadata WHERE :with_input_context!);
Loading

0 comments on commit 88c9b02

Please sign in to comment.