Skip to content

Commit

Permalink
Fix script_utxos, closes #272
Browse files Browse the repository at this point in the history
  • Loading branch information
rdlrt committed Apr 8, 2024
1 parent aa427c3 commit 7fd1b10
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions files/grest/rpc/script/script_utxos.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ DECLARE
known_addresses varchar[];
BEGIN
RETURN QUERY
WITH
_assets AS (
SELECT
txo.id,
JSONB_AGG(CASE WHEN ma.policy IS NULL THEN NULL
ELSE JSONB_BUILD_OBJECT(
'policy_id', ENCODE(ma.policy, 'hex'),
'asset_name', ENCODE(ma.name, 'hex'),
'fingerprint', ma.fingerprint,
'decimals', aic.decimals,
'quantity', mto.quantity::text
)
END) as assets
FROM tx_out AS txo
INNER JOIN script ON script.tx_id = txo.tx_id
INNER JOIN ma_tx_out AS mto ON mto.tx_out_id = txo.id
LEFT JOIN multi_asset AS ma ON ma.id = mto.ident
LEFT JOIN grest.asset_info_cache AS aic ON aic.asset_id = ma.id
WHERE script.hash = DECODE(_script_hash,'hex')
AND txo.consumed_by_tx_id IS NULL
GROUP BY txo.id
)
SELECT
ENCODE(tx.hash, 'hex')::text AS tx_hash,
tx_out.index::smallint,
Expand Down Expand Up @@ -49,17 +71,10 @@ BEGIN
'size', script.serialised_size
)
END) AS reference_script,
(CASE
WHEN _extended = false OR ma.policy IS NULL THEN NULL
ELSE JSONB_BUILD_OBJECT(
'policy_id', ENCODE(ma.policy, 'hex'),
'asset_name', ENCODE(ma.name, 'hex'),
'fingerprint', ma.fingerprint,
'decimals', aic.decimals,
'quantity', mto.quantity::text
)
END
) AS asset_list,
CASE
WHEN _extended = false THEN NULL
ELSE COALESCE(assets, JSONB_BUILD_ARRAY())
END AS asset_list,
(CASE
WHEN tx_out.consumed_by_tx_id IS NULL THEN false
ELSE true
Expand All @@ -68,11 +83,9 @@ BEGIN
INNER JOIN tx ON tx_out.tx_id = tx.id
INNER JOIN script ON script.tx_id = tx.id
LEFT JOIN stake_address AS sa ON tx_out.stake_address_id = sa.id
LEFT JOIN block AS b ON b.id = tx.block_id
LEFT JOIN ma_tx_out AS mto ON mto.tx_out_id = tx_out.id
LEFT JOIN multi_asset AS ma ON ma.id = mto.ident
LEFT JOIN grest.asset_info_cache AS aic ON aic.asset_id = ma.id
LEFT JOIN datum ON datum.id = tx_out.inline_datum_id
LEFT JOIN block AS b ON b.id = tx.block_id
LEFT JOIN _assets ON tx_out.id = _assets.id
WHERE script.hash = DECODE(_script_hash,'hex')
AND tx_out.consumed_by_tx_id IS NULL
;
Expand Down

0 comments on commit 7fd1b10

Please sign in to comment.