Skip to content

Commit

Permalink
Fix *_utxos to show assets as array instead of objects
Browse files Browse the repository at this point in the history
  • Loading branch information
rdlrt committed Aug 29, 2023
1 parent 0938e84 commit 7c17dbf
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 90 deletions.
46 changes: 29 additions & 17 deletions files/grest/rpc/account/account_utxos.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ 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 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 txo.stake_address_id IN (SELECT sa.id FROM stake_address AS sa WHERE sa.view = ANY(_stake_addresses))
AND txo.consumed_by_tx_in_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 +70,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_in_id IS NULL THEN false
ELSE true
Expand All @@ -68,13 +82,11 @@ BEGIN
INNER JOIN tx ON tx_out.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 script ON script.tx_id = tx.id
WHERE
tx_out.stake_address_id IN (SELECT sa.id FROM stake_address AS sa WHERE sa.view = ANY(_stake_addresses))
LEFT JOIN script ON script.tx_id = tx_out.reference_script_id
LEFT JOIN _assets ON tx_out.id = _assets.id
WHERE tx_out.stake_address_id IN (SELECT sa.id FROM stake_address AS sa WHERE sa.view = ANY(_stake_addresses))
AND tx_out.consumed_by_tx_in_id IS NULL
;
END;
$$;
Expand Down
46 changes: 29 additions & 17 deletions files/grest/rpc/address/address_utxos.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ 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 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 txo.address = ANY(_addresses)
AND txo.consumed_by_tx_in_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 +70,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_in_id IS NULL THEN false
ELSE true
Expand All @@ -68,13 +82,11 @@ BEGIN
INNER JOIN tx ON tx_out.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 script ON script.tx_id = tx.id
WHERE
tx_out.address = ANY(_addresses)
LEFT JOIN script ON script.tx_id = tx_out.reference_script_id
LEFT JOIN _assets ON tx_out.id = _assets.id
WHERE tx_out.address = ANY(_addresses)
AND tx_out.consumed_by_tx_in_id IS NULL
;
END;
$$;
Expand Down
46 changes: 29 additions & 17 deletions files/grest/rpc/address/credential_utxos.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ BEGIN
) AS tmp;

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 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 txo.payment_cred = ANY(_payment_cred_bytea)
AND txo.consumed_by_tx_in_id IS NULL
GROUP BY txo.id
)
SELECT
ENCODE(tx.hash, 'hex')::text AS tx_hash,
tx_out.index::smallint,
Expand Down Expand Up @@ -55,17 +76,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_in_id IS NULL THEN false
ELSE true
Expand All @@ -74,13 +88,11 @@ BEGIN
INNER JOIN tx ON tx_out.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 script ON script.tx_id = tx.id
WHERE
tx_out.payment_cred = ANY(_payment_cred_bytea)
LEFT JOIN script ON script.tx_id = tx_out.reference_script_id
LEFT JOIN _assets ON tx_out.id = _assets.id
WHERE tx_out.payment_cred = ANY(_payment_cred_bytea)
AND tx_out.consumed_by_tx_in_id IS NULL
;
END;
$$;
Expand Down
48 changes: 30 additions & 18 deletions files/grest/rpc/assets/asset_utxos.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ BEGIN
) AS tmp;

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 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 mto.ident = ANY(_asset_id_list)
AND txo.consumed_by_tx_in_id IS NULL
GROUP BY txo.id
)
SELECT
ENCODE(tx.hash, 'hex')::text AS tx_hash,
tx_out.index::smallint,
Expand Down Expand Up @@ -62,17 +83,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_in_id IS NULL THEN false
ELSE true
Expand All @@ -81,15 +95,13 @@ BEGIN
INNER JOIN tx ON tx_out.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 script ON script.tx_id = tx.id
WHERE
mto.ident = ANY(_asset_id_list)
LEFT JOIN script ON script.tx_id = tx_out.reference_script_id
LEFT JOIN _assets ON tx_out.id = _assets.id
WHERE mto.ident = ANY(_asset_id_list)
AND tx_out.consumed_by_tx_in_id IS NULL
;
END;
$$;

COMMENT ON FUNCTION grest.asset_utxos IS 'Get UTxO details for requested assets'; -- noqa: LT01
COMMENT ON FUNCTION grest.asset_utxos IS 'Get UTxO details for requested assets'; -- noqa: LT01
8 changes: 4 additions & 4 deletions files/grest/rpc/transactions/tx_info.sql
Original file line number Diff line number Diff line change
Expand Up @@ -663,9 +663,9 @@ BEGIN
ind.hash AS ind_hash,
ind.value AS ind_value
FROM redeemer
INNER JOIN tx_in ON tx_in.redeemer_id = redeemer.id
INNER JOIN tx_out AS inutxo ON inutxo.tx_id = tx_in.tx_out_id AND inutxo.index = tx_in.tx_out_index
INNER JOIN datum AS ind ON ind.hash = inutxo.data_hash
INNER JOIN tx_in ON tx_in.redeemer_id = redeemer.id
INNER JOIN tx_out AS inutxo ON inutxo.tx_id = tx_in.tx_out_id AND inutxo.index = tx_in.tx_out_index
INNER JOIN datum AS ind ON ind.hash = inutxo.data_hash
WHERE redeemer.tx_id = ANY(_tx_id_list)
)

Expand Down Expand Up @@ -845,7 +845,7 @@ BEGIN
COALESCE((SELECT ans.list FROM _all_native_scripts AS ans WHERE ans.tx_id = atx.id), JSONB_BUILD_ARRAY()),
COALESCE((SELECT apc.list FROM _all_plutus_contracts AS apc WHERE apc.tx_id = atx.id), JSONB_BUILD_ARRAY())
FROM _all_tx AS atx
WHERE atx.tx_hash = ANY(_tx_hashes_bytea)
WHERE atx.id = ANY(_tx_id_list)
);

END;
Expand Down
44 changes: 27 additions & 17 deletions files/grest/rpc/transactions/utxo_info.sql
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ BEGIN
) AS tmp;

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 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 txo.id = ANY(_tx_id_list)
GROUP BY txo.id
)
SELECT
ENCODE(tx.hash, 'hex')::text AS tx_hash,
tx_out.index::smallint,
Expand Down Expand Up @@ -69,17 +89,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_in_id IS NULL THEN false
ELSE true
Expand All @@ -88,13 +101,10 @@ BEGIN
INNER JOIN tx ON tx_out.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 script ON script.tx_id = tx.id
WHERE
tx_out.id = ANY(_tx_id_list)
LEFT JOIN script ON script.tx_id = tx_out.reference_script_id
LEFT JOIN _assets ON tx_out.id = _assets.id
WHERE tx_out.id = ANY(_tx_id_list)
;

END;
Expand Down

0 comments on commit 7c17dbf

Please sign in to comment.