Skip to content

Commit

Permalink
tx_info: new logic for spend contracts mapping to inputs (#278)
Browse files Browse the repository at this point in the history
## Description
closes #277
  • Loading branch information
Scitz0 authored Apr 25, 2024
1 parent c96a79a commit 3abb209
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 6 deletions.
44 changes: 38 additions & 6 deletions files/grest/rpc/transactions/tx_info.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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())
Expand Down
10 changes: 10 additions & 0 deletions specs/results/koiosapi-guild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions specs/results/koiosapi-mainnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions specs/results/koiosapi-preprod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions specs/results/koiosapi-preview.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions specs/templates/4-api-schemas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 3abb209

Please sign in to comment.