From 3abb209487760f86fcb954399551e96d7f285cce Mon Sep 17 00:00:00 2001 From: "Ola [AHLNET]" Date: Thu, 25 Apr 2024 21:31:17 +0200 Subject: [PATCH] tx_info: new logic for spend contracts mapping to inputs (#278) ## Description closes https://github.com/cardano-community/koios-artifacts/issues/277 --- files/grest/rpc/transactions/tx_info.sql | 44 ++++++++++++++++++++---- specs/results/koiosapi-guild.yaml | 10 ++++++ specs/results/koiosapi-mainnet.yaml | 10 ++++++ specs/results/koiosapi-preprod.yaml | 10 ++++++ specs/results/koiosapi-preview.yaml | 10 ++++++ specs/templates/4-api-schemas.yaml | 10 ++++++ 6 files changed, 88 insertions(+), 6 deletions(-) diff --git a/files/grest/rpc/transactions/tx_info.sql b/files/grest/rpc/transactions/tx_info.sql index 9e333f3d..9dfb82e4 100644 --- a/files/grest/rpc/transactions/tx_info.sql +++ b/files/grest/rpc/transactions/tx_info.sql @@ -191,7 +191,7 @@ BEGIN ENCODE(tx.hash, 'hex') AS tx_hash, tx_out.index AS tx_index, tx_out.value::text AS value, - ENCODE(tx_out.data_hash, 'hex') AS datum_hash, + tx_out.data_hash AS datum_hash, (CASE WHEN ma.policy IS NULL THEN NULL ELSE JSONB_BUILD_OBJECT( @@ -652,17 +652,37 @@ BEGIN WHERE redeemer.tx_id = ANY(_tx_id_list) ), + _all_inputs_sorted AS ( + SELECT + ROW_NUMBER () OVER ( + PARTITION BY ai.tx_id + ORDER BY ai.tx_hash, ai.tx_index + ) - 1 AS sorted_index, + ai.* + FROM ( + SELECT DISTINCT ON (_ai.tx_hash, _ai.tx_index) + _ai.tx_id, + _ai.tx_hash, + _ai.tx_index, + _ai.payment_addr_bech32 as address, + _ai.datum_hash + from _all_inputs as _ai + ) as ai + ), + spend_redeemers AS ( SELECT DISTINCT ON (redeemer.id) redeemer.id, - inutxo.address, + ais.address, + ais.tx_hash, + ais.tx_index, ind.hash AS ind_hash, ind.value AS ind_value FROM redeemer - INNER JOIN tx_out AS inutxo ON inutxo.consumed_by_tx_id = redeemer.tx_id - INNER JOIN script ON redeemer.script_hash = inutxo.payment_cred - INNER JOIN datum AS ind ON ind.hash = inutxo.data_hash + INNER JOIN _all_inputs_sorted AS ais ON ais.tx_id = redeemer.tx_id AND ais.sorted_index = redeemer.index + INNER JOIN datum AS ind ON ind.hash = ais.datum_hash WHERE redeemer.tx_id = ANY(_tx_id_list) + AND redeemer.purpose = 'spend' ) SELECT @@ -673,6 +693,18 @@ BEGIN WHEN ar.purpose = 'spend' THEN (SELECT address FROM spend_redeemers AS sr WHERE sr.id = ar.id) END, + 'spends_input', + CASE + WHEN ar.purpose = 'spend' THEN + ( + SELECT JSONB_BUILD_OBJECT( + 'tx_hash', sr.tx_hash, + 'tx_index', sr.tx_index + ) + FROM spend_redeemers AS sr + WHERE sr.id = ar.id + ) + END, 'script_hash', ENCODE(ar.script_hash, 'hex'), 'bytecode', ENCODE(ar.script_bytes, 'hex'), 'size', ar.script_serialised_size, @@ -800,7 +832,7 @@ BEGIN 'tx_hash', ai.tx_hash, 'tx_index', tx_index, 'value', value, - 'datum_hash', datum_hash, + 'datum_hash', ENCODE(datum_hash, 'hex'), 'inline_datum', inline_datum, 'reference_script', reference_script, 'asset_list', COALESCE(JSONB_AGG(asset_list) FILTER (WHERE asset_list IS NOT NULL), JSONB_BUILD_ARRAY()) diff --git a/specs/results/koiosapi-guild.yaml b/specs/results/koiosapi-guild.yaml index 575bc164..e2ee5b93 100644 --- a/specs/results/koiosapi-guild.yaml +++ b/specs/results/koiosapi-guild.yaml @@ -4021,6 +4021,16 @@ components: - 'null' description: Plutus script address example: addr1w999n67e86jn6xal07pzxtrmqynspgx0fwmcmpua4wc6yzsxpljz3 + spends_input: + type: + - object + - 'null' + properties: + tx_hash: + $ref: "#/components/schemas/utxo_infos/items/properties/tx_hash" + tx_index: + $ref: "#/components/schemas/utxo_infos/items/properties/tx_index" + description: Input utxo this contract spends script_hash: $ref: "#/components/schemas/script_info/items/properties/script_hash" bytecode: diff --git a/specs/results/koiosapi-mainnet.yaml b/specs/results/koiosapi-mainnet.yaml index 423962dd..c24134c3 100644 --- a/specs/results/koiosapi-mainnet.yaml +++ b/specs/results/koiosapi-mainnet.yaml @@ -4021,6 +4021,16 @@ components: - 'null' description: Plutus script address example: addr1w999n67e86jn6xal07pzxtrmqynspgx0fwmcmpua4wc6yzsxpljz3 + spends_input: + type: + - object + - 'null' + properties: + tx_hash: + $ref: "#/components/schemas/utxo_infos/items/properties/tx_hash" + tx_index: + $ref: "#/components/schemas/utxo_infos/items/properties/tx_index" + description: Input utxo this contract spends script_hash: $ref: "#/components/schemas/script_info/items/properties/script_hash" bytecode: diff --git a/specs/results/koiosapi-preprod.yaml b/specs/results/koiosapi-preprod.yaml index 6e1e6f71..0b2b13c7 100644 --- a/specs/results/koiosapi-preprod.yaml +++ b/specs/results/koiosapi-preprod.yaml @@ -4021,6 +4021,16 @@ components: - 'null' description: Plutus script address example: addr1w999n67e86jn6xal07pzxtrmqynspgx0fwmcmpua4wc6yzsxpljz3 + spends_input: + type: + - object + - 'null' + properties: + tx_hash: + $ref: "#/components/schemas/utxo_infos/items/properties/tx_hash" + tx_index: + $ref: "#/components/schemas/utxo_infos/items/properties/tx_index" + description: Input utxo this contract spends script_hash: $ref: "#/components/schemas/script_info/items/properties/script_hash" bytecode: diff --git a/specs/results/koiosapi-preview.yaml b/specs/results/koiosapi-preview.yaml index 155fda50..1235e025 100644 --- a/specs/results/koiosapi-preview.yaml +++ b/specs/results/koiosapi-preview.yaml @@ -4021,6 +4021,16 @@ components: - 'null' description: Plutus script address example: addr1w999n67e86jn6xal07pzxtrmqynspgx0fwmcmpua4wc6yzsxpljz3 + spends_input: + type: + - object + - 'null' + properties: + tx_hash: + $ref: "#/components/schemas/utxo_infos/items/properties/tx_hash" + tx_index: + $ref: "#/components/schemas/utxo_infos/items/properties/tx_index" + description: Input utxo this contract spends script_hash: $ref: "#/components/schemas/script_info/items/properties/script_hash" bytecode: diff --git a/specs/templates/4-api-schemas.yaml b/specs/templates/4-api-schemas.yaml index 7f918310..5efd7665 100644 --- a/specs/templates/4-api-schemas.yaml +++ b/specs/templates/4-api-schemas.yaml @@ -1661,6 +1661,16 @@ schemas: - 'null' description: Plutus script address example: addr1w999n67e86jn6xal07pzxtrmqynspgx0fwmcmpua4wc6yzsxpljz3 + spends_input: + type: + - object + - 'null' + properties: + tx_hash: + $ref: "#/components/schemas/utxo_infos/items/properties/tx_hash" + tx_index: + $ref: "#/components/schemas/utxo_infos/items/properties/tx_index" + description: Input utxo this contract spends script_hash: $ref: "#/components/schemas/script_info/items/properties/script_hash" bytecode: