Skip to content

Commit

Permalink
[WIP - payment_creds & indexes pending] Update address references fro…
Browse files Browse the repository at this point in the history
…m tx_out to it's own table
  • Loading branch information
rdlrt committed Nov 11, 2024
1 parent c339fac commit 3edfae5
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 33 deletions.
6 changes: 4 additions & 2 deletions files/grest/rpc/account/account_addresses.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ BEGIN
FROM
(
SELECT
txo.address,
a.address,
txo.stake_address_id,
txo.id
FROM tx_out AS txo
LEFT JOIN address AS a ON a.id = txo.address_id
WHERE txo.stake_address_id = ANY(sa_id_list)
AND txo.consumed_by_tx_id IS NULL
) AS x
Expand All @@ -53,10 +54,11 @@ BEGIN
FROM
(
SELECT
txo.address,
a.address,
txo.stake_address_id,
txo.id
FROM tx_out AS txo
LEFT JOIN address AS a ON a.id = txo.address_id
WHERE txo.stake_address_id = ANY(sa_id_list)
LIMIT (CASE WHEN _first_only IS TRUE THEN 1 ELSE NULL END)
) AS x
Expand Down
3 changes: 2 additions & 1 deletion files/grest/rpc/account/account_utxos.sql
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ BEGIN
SELECT
ENCODE(tx.hash, 'hex')::text AS tx_hash,
tx_out.index::smallint,
tx_out.address::text,
a.address::text,
tx_out.value::text,
grest.cip5_hex_to_stake_addr(sa.hash_raw) as stake_address,
ENCODE(tx_out.payment_cred, 'hex') AS payment_cred,
Expand Down Expand Up @@ -89,6 +89,7 @@ BEGIN
END) AS is_spent
FROM tx_out
INNER JOIN tx ON tx_out.tx_id = tx.id
LEFT JOIN address AS a ON a.id = tx_out.address_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 datum ON datum.id = tx_out.inline_datum_id
Expand Down
7 changes: 4 additions & 3 deletions files/grest/rpc/address/address_assets.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ BEGIN

WITH _all_assets AS (
SELECT
txo.address,
a.address,
ma.policy,
ma.name,
ma.fingerprint,
Expand All @@ -24,10 +24,11 @@ BEGIN
INNER JOIN multi_asset AS ma ON ma.id = mtx.ident
LEFT JOIN grest.asset_info_cache AS aic ON aic.asset_id = ma.id
INNER JOIN tx_out AS txo ON txo.id = mtx.tx_out_id
WHERE txo.address = ANY(_addresses)
INNER JOIN address AS a ON a.id = tx_out.address_id
WHERE a.address = ANY(_addresses)
AND txo.consumed_by_tx_id IS NULL
GROUP BY
txo.address, ma.policy, ma.name, ma.fingerprint, aic.decimals
a.address, ma.policy, ma.name, ma.fingerprint, aic.decimals
)

SELECT
Expand Down
10 changes: 6 additions & 4 deletions files/grest/rpc/address/address_info.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,32 @@ DECLARE
BEGIN
CREATE TEMPORARY TABLE _known_addresses AS
SELECT
DISTINCT ON (tx_out.address) tx_out.address,
DISTINCT ON (tx_out.address_id) a.address,
sa.hash_raw AS stake_address_raw,
COALESCE(tx_out.address_has_script, 'false') AS script_address
FROM tx_out
INNER JOIN address AS a ON a.id = tx_out.address_id
LEFT JOIN stake_address AS sa ON sa.id = tx_out.stake_address_id
WHERE tx_out.address = ANY(_addresses);
WHERE a.address = ANY(_addresses);

RETURN QUERY
WITH _all_utxos AS (
SELECT
tx.id,
tx.hash,
tx_out.id AS txo_id,
tx_out.address,
a.address,
tx_out.value,
tx_out.index,
tx.block_id,
tx_out.data_hash,
tx_out.inline_datum_id,
tx_out.reference_script_id
FROM tx_out
INNER JOIN address AS a ON a.id = tx_out.address_id
INNER JOIN tx ON tx.id = tx_out.tx_id
WHERE tx_out.consumed_by_tx_id IS NULL
AND tx_out.address = ANY(_addresses)
AND a.address = ANY(_addresses)
)

SELECT
Expand Down
3 changes: 2 additions & 1 deletion files/grest/rpc/address/address_txs.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ BEGIN
--
SELECT consumed_by_tx_id
FROM tx_out
INNER JOIN address AS a ON a.id = tx_out.address_id
WHERE tx_out.consumed_by_tx_id IS NOT NULL
AND tx_out.address = ANY(_addresses)
AND a.address = ANY(_addresses)
AND tx_out.consumed_by_tx_id >= _tx_id_min
) AS tmp;

Expand Down
8 changes: 5 additions & 3 deletions files/grest/rpc/address/address_utxos.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,18 @@ BEGIN
)
END) as assets
FROM tx_out AS txo
INNER JOIN address AS a ON a.id = tx_out.address_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 txo.address = ANY(_addresses)
WHERE a.address = ANY(_addresses)
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,
tx_out.address::text,
a.address::text,
tx_out.value::text,
grest.cip5_hex_to_stake_addr(sa.hash_raw)::text as stake_address,
ENCODE(tx_out.payment_cred, 'hex') AS payment_cred,
Expand Down Expand Up @@ -79,13 +80,14 @@ BEGIN
ELSE true
END) AS is_spent
FROM tx_out
INNER JOIN address AS a ON a.id = tx_out.address_id
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 datum ON datum.id = tx_out.inline_datum_id
LEFT JOIN script ON script.id = tx_out.reference_script_id
LEFT JOIN _assets ON tx_out.id = _assets.id
WHERE tx_out.address = ANY(_addresses)
WHERE a.address = ANY(_addresses)
AND tx_out.consumed_by_tx_id IS NULL
;
END;
Expand Down
3 changes: 2 additions & 1 deletion files/grest/rpc/address/credential_utxos.sql
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ BEGIN
SELECT
ENCODE(tx.hash, 'hex')::text AS tx_hash,
tx_out.index::smallint,
tx_out.address::text,
a.address::text,
tx_out.value::text,
grest.cip5_hex_to_stake_addr(sa.hash_raw)::text as stake_address,
ENCODE(tx_out.payment_cred, 'hex') AS payment_cred,
Expand Down Expand Up @@ -86,6 +86,7 @@ BEGIN
END) AS is_spent
FROM tx_out
INNER JOIN tx ON tx_out.tx_id = tx.id
INNER JOIN address AS a ON a.id = tx_out.address_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 datum ON datum.id = tx_out.inline_datum_id
Expand Down
6 changes: 4 additions & 2 deletions files/grest/rpc/assets/asset_addresses.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ BEGIN
FROM
(
SELECT
txo.address,
a.address,
sa.hash_raw AS stake_address_raw,
atoc.quantity
FROM grest.asset_tx_out_cache AS atoc
LEFT JOIN tx_out AS txo ON atoc.txo_id = txo.id
LEFT JOIN address AS a ON a.id = tx_out.address_id
LEFT JOIN stake_address AS sa ON txo.stake_address_id = sa.id
WHERE atoc.ma_id = _asset_id
AND txo.consumed_by_tx_id IS NULL
Expand All @@ -52,11 +53,12 @@ BEGIN
FROM
(
SELECT
txo.address,
a.address,
sa.hash_raw AS stake_address_raw,
mto.quantity
FROM ma_tx_out AS mto
LEFT JOIN tx_out AS txo ON txo.id = mto.tx_out_id
LEFT JOIN address AS a ON a.id = tx_out.address_id
LEFT JOIN stake_address AS sa ON txo.stake_address_id = sa.id
WHERE mto.ident = _asset_id
AND txo.consumed_by_tx_id IS NULL
Expand Down
6 changes: 4 additions & 2 deletions files/grest/rpc/assets/asset_nft_address.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ BEGIN
IF EXISTS (SELECT * FROM ma_tx_mint WHERE ident = _asset_id and quantity < 0 LIMIT 1) THEN
RETURN QUERY
SELECT
txo.address,
a.address,
grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS stake_address
FROM tx_out AS txo
LEFT JOIN address AS a ON a.id = tx_out.address_id
LEFT JOIN stake_address AS sa ON txo.stake_address_id = sa.id
WHERE txo.id = (
SELECT MAX(tx_out_id)
Expand All @@ -39,9 +40,10 @@ BEGIN
ELSE
RETURN QUERY
SELECT
txo.address,
a.address,
grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS stake_address
FROM tx_out AS txo
LEFT JOIN address AS a ON a.id = tx_out.address_id
INNER JOIN ma_tx_out mto ON mto.tx_out_id = txo.id
LEFT JOIN stake_address AS sa ON txo.stake_address_id = sa.id
WHERE mto.ident = _asset_id
Expand Down
3 changes: 2 additions & 1 deletion files/grest/rpc/assets/asset_summary.sql
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ BEGIN
txo.tx_id AS tx_id,
txo.id AS tx_out_id,
txo.index AS tx_out_idx,
txo.address AS address,
a.address AS address,
txo.stake_address_id AS sa_id
FROM ma_tx_out AS mto
INNER JOIN tx_out AS txo ON txo.id = mto.tx_out_id
LEFT JOIN address AS a ON a.id = tx_out.address_id
WHERE mto.ident = _asset_id
AND txo.consumed_by_tx_id IS NULL)

Expand Down
3 changes: 2 additions & 1 deletion files/grest/rpc/assets/asset_utxos.sql
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ BEGIN
SELECT
ENCODE(tx.hash, 'hex')::text AS tx_hash,
tx_out.index::smallint,
tx_out.address::text,
a.address::text,
tx_out.value::text,
grest.cip5_hex_to_stake_addr(sa.hash_raw)::text as stake_address,
ENCODE(tx_out.payment_cred, 'hex') AS payment_cred,
Expand Down Expand Up @@ -99,6 +99,7 @@ BEGIN
END) AS is_spent
FROM tx_out
INNER JOIN tx ON tx_out.tx_id = tx.id
LEFT JOIN address AS a ON a.id = tx_out.address_id
INNER JOIN _assets ON tx_out.id = _assets.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
Expand Down
8 changes: 5 additions & 3 deletions files/grest/rpc/assets/policy_asset_addresses.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ BEGIN
(
SELECT
atoc.ma_id,
txo.address,
a.address,
grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS stake_address,
atoc.quantity
FROM grest.asset_tx_out_cache AS atoc
LEFT JOIN multi_asset AS ma ON ma.id = atoc.ma_id
LEFT JOIN tx_out AS txo ON txo.id = atoc.txo_id
LEFT JOIN address AS a ON a.id = tx_out.address_id
LEFT JOIN stake_address AS sa ON txo.stake_address_id = sa.id
WHERE ma.policy = DECODE(_asset_policy, 'hex')
AND txo.consumed_by_tx_id IS NULL
Expand All @@ -46,18 +47,19 @@ BEGIN
RETURN QUERY
SELECT
ENCODE(ma.name, 'hex') AS asset_name,
txo.address,
a.address,
grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS stake_address,
SUM(mto.quantity)::text
FROM multi_asset AS ma
LEFT JOIN ma_tx_out AS mto ON mto.ident = ma.id
LEFT JOIN tx_out AS txo ON txo.id = mto.tx_out_id
LEFT JOIN address AS a ON a.id = tx_out.address_id
LEFT JOIN stake_address AS sa ON txo.stake_address_id = sa.id
WHERE ma.policy = DECODE(_asset_policy, 'hex')
AND txo.consumed_by_tx_id IS NULL
GROUP BY
ma.name,
txo.address,
a.address,
sa.hash_raw;
END IF;
END;
Expand Down
3 changes: 2 additions & 1 deletion files/grest/rpc/script/script_utxos.sql
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ BEGIN
SELECT
ENCODE(tx.hash, 'hex')::text AS tx_hash,
tx_out.index::smallint,
tx_out.address::text,
a.address::text,
tx_out.value::text,
grest.cip5_hex_to_stake_addr(sa.hash_raw) as stake_address,
ENCODE(tx_out.payment_cred, 'hex') AS payment_cred,
Expand Down Expand Up @@ -81,6 +81,7 @@ BEGIN
END) AS is_spent
FROM tx_out
INNER JOIN tx ON tx_out.tx_id = tx.id
LEFT JOIN address AS a ON a.id = tx_out.address_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 datum ON datum.id = tx_out.inline_datum_id
Expand Down
15 changes: 10 additions & 5 deletions files/grest/rpc/transactions/tx_info.sql
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ BEGIN
_all_collateral_inputs AS (
SELECT
collateral_tx_in.tx_in_id AS tx_id,
tx_out.address AS payment_addr_bech32,
a.address AS payment_addr_bech32,
ENCODE(tx_out.payment_cred, 'hex') AS payment_addr_cred,
grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_addr,
ENCODE(tx.hash, 'hex') AS tx_hash,
Expand Down Expand Up @@ -129,6 +129,7 @@ BEGIN
INNER JOIN tx_out ON tx_out.tx_id = collateral_tx_in.tx_out_id
AND tx_out.index = collateral_tx_in.tx_out_index
INNER JOIN tx ON tx_out.tx_id = tx.id
LEFT JOIN address AS a ON a.id = tx_out.address_id
LEFT JOIN stake_address AS sa ON tx_out.stake_address_id = sa.id
LEFT JOIN ma_tx_out AS mto ON _assets IS TRUE AND mto.tx_out_id = tx_out.id
LEFT JOIN multi_asset AS ma ON _assets IS TRUE AND ma.id = mto.ident
Expand All @@ -143,7 +144,7 @@ BEGIN
_all_reference_inputs AS (
SELECT
reference_tx_in.tx_in_id AS tx_id,
tx_out.address AS payment_addr_bech32,
a.address AS payment_addr_bech32,
ENCODE(tx_out.payment_cred, 'hex') AS payment_addr_cred,
grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_addr,
ENCODE(tx.hash, 'hex') AS tx_hash,
Expand Down Expand Up @@ -184,6 +185,7 @@ BEGIN
INNER JOIN tx_out ON tx_out.tx_id = reference_tx_in.tx_out_id
AND tx_out.index = reference_tx_in.tx_out_index
INNER JOIN tx ON tx_out.tx_id = tx.id
LEFT JOIN address AS a ON a.id = tx_out.address_id
LEFT JOIN stake_address AS sa ON tx_out.stake_address_id = sa.id
LEFT JOIN ma_tx_out AS mto ON _assets IS TRUE AND mto.tx_out_id = tx_out.id
LEFT JOIN multi_asset AS ma ON _assets IS TRUE AND ma.id = mto.ident
Expand All @@ -198,7 +200,7 @@ BEGIN
_all_inputs AS (
SELECT
tx_out.consumed_by_tx_id AS tx_id,
tx_out.address AS payment_addr_bech32,
a.address AS payment_addr_bech32,
ENCODE(tx_out.payment_cred, 'hex') AS payment_addr_cred,
grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_addr,
ENCODE(tx.hash, 'hex') AS tx_hash,
Expand Down Expand Up @@ -237,6 +239,7 @@ BEGIN
) AS reference_script
FROM tx_out
INNER JOIN tx ON tx_out.tx_id = tx.id
LEFT JOIN address AS a ON a.id = tx_out.address_id
LEFT JOIN stake_address AS sa ON tx_out.stake_address_id = sa.id
LEFT JOIN ma_tx_out AS mto ON _assets IS TRUE AND mto.tx_out_id = tx_out.id
LEFT JOIN multi_asset AS ma ON _assets IS TRUE AND ma.id = mto.ident
Expand All @@ -250,7 +253,7 @@ BEGIN
_all_collateral_outputs AS (
SELECT
tx_out.tx_id,
tx_out.address AS payment_addr_bech32,
a.address AS payment_addr_bech32,
ENCODE(tx_out.payment_cred, 'hex') AS payment_addr_cred,
grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_addr,
ENCODE(tx.hash, 'hex') AS tx_hash,
Expand Down Expand Up @@ -280,6 +283,7 @@ BEGIN
FROM
collateral_tx_out AS tx_out
INNER JOIN tx ON tx_out.tx_id = tx.id
LEFT JOIN address AS a ON a.id = tx_out.address_id
LEFT JOIN stake_address AS sa ON tx_out.stake_address_id = sa.id
LEFT JOIN datum ON _scripts IS TRUE AND datum.id = tx_out.inline_datum_id
LEFT JOIN script ON _scripts IS TRUE AND script.id = tx_out.reference_script_id
Expand All @@ -290,7 +294,7 @@ BEGIN
_all_outputs AS (
SELECT
tx_out.tx_id,
tx_out.address AS payment_addr_bech32,
a.address AS payment_addr_bech32,
ENCODE(tx_out.payment_cred, 'hex') AS payment_addr_cred,
grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_addr,
ENCODE(tx.hash, 'hex') AS tx_hash,
Expand Down Expand Up @@ -329,6 +333,7 @@ BEGIN
) AS reference_script
FROM tx_out
INNER JOIN tx ON tx_out.tx_id = tx.id
LEFT JOIN address AS a ON a.id = tx_out.address_id
LEFT JOIN stake_address AS sa ON tx_out.stake_address_id = sa.id
LEFT JOIN ma_tx_out AS mto ON _assets IS TRUE AND mto.tx_out_id = tx_out.id
LEFT JOIN multi_asset AS ma ON _assets IS TRUE AND ma.id = mto.ident
Expand Down
Loading

0 comments on commit 3edfae5

Please sign in to comment.