Skip to content

Commit

Permalink
Various SQL Fixes (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
rdlrt committed Sep 15, 2024
1 parent 9e05ef6 commit 3d49a57
Show file tree
Hide file tree
Showing 22 changed files with 58 additions and 72 deletions.
6 changes: 3 additions & 3 deletions files/grest/rpc/01_cached_tables/pool_history_cache.sql
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ DECLARE
_pool_ids bigint [];
BEGIN
_pool_ids := (SELECT ARRAY_AGG(id) from pool_hash ph where ph.hash_raw = ANY(
SELECT ARRAY_AGG(DECODE(b32_decode(pool),'hex'))
FROM UNNEST(_pool_bech32) AS pool
SELECT DECODE(b32_decode(pool),'hex')
FROM UNNEST(_pool_bech32) AS pool)
);

RETURN QUERY
Expand Down Expand Up @@ -144,7 +144,7 @@ BEGIN
)

SELECT
actf.pool_id::text,
actf.pool_id::bigint,
actf.epoch_no::bigint,
actf.active_stake::lovelace,
actf.active_stake_pct,
Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/01_cached_tables/pool_info_cache.sql
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,8 @@ BEGIN
);
END LOOP;

CREATE INDEX IF NOT EXISTS idx_id ON grest.pool_info_cache (tx_id);
CREATE INDEX IF NOT EXISTS idx_tx_id ON grest.pool_info_cache (tx_id);
CREATE INDEX IF NOT EXISTS idx_pool_id_bech32 ON grest.pool_info_cache (pool_id_bech32);
CREATE INDEX IF NOT EXISTS idx_pool_hash_id ON grest.pool_info_cache (pool_hash_id);
CREATE INDEX IF NOT EXISTS idx_pool_status ON grest.pool_info_cache (pool_status);
CREATE INDEX IF NOT EXISTS idx_meta_id ON grest.pool_info_cache (meta_id);
Expand Down
8 changes: 5 additions & 3 deletions files/grest/rpc/03_utilities/cip5.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ RETURNS text
LANGUAGE plpgsql STABLE
AS $$
BEGIN
IF _raw IS NULL THEN RETURN NULL END IF;
IF _raw IS NULL THEN
RETURN NULL;
END IF;
IF SUBSTRING(ENCODE(_raw, 'hex') from 2 for 1) = '0' THEN
RETURN b32_encode('stake_test', hex::text);
RETURN b32_encode('stake_test', _raw::text);
ELSE
RETURN b32_encode('stake', hex::text);
RETURN b32_encode('stake', _raw::text);
END IF;
END;
$$;
2 changes: 1 addition & 1 deletion files/grest/rpc/account/account_addresses.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ BEGIN
stake_address
WHERE
stake_address.hash_raw = ANY(
SELECT ARRAY_AGG(DECODE(b32_decode(n), 'hex'))
SELECT DECODE(b32_decode(n), 'hex')
FROM UNNEST(_stake_addresses) AS n
);

Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/account/account_assets.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ BEGIN
INNER JOIN tx_out AS txo ON txo.id = mtx.tx_out_id
INNER JOIN stake_address AS sa ON sa.id = txo.stake_address_id
WHERE sa.hash_raw = ANY(
SELECT ARRAY_AGG(DECODE(b32_decode(n), 'hex'))
SELECT DECODE(b32_decode(n), 'hex')
FROM UNNEST(_stake_addresses) AS n
)
AND txo.consumed_by_tx_id IS NULL
Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/account/account_history.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ BEGIN
stake_address
WHERE
stake_address.hash_raw = ANY(
SELECT ARRAY_AGG(DECODE(b32_decode(n), 'hex'))
SELECT DECODE(b32_decode(n), 'hex')
FROM UNNEST(_stake_addresses) AS n
);

Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/account/account_info.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ BEGIN
array_agg(id)
FROM stake_address
WHERE stake_address.hash_raw = ANY(
SELECT ARRAY_AGG(DECODE(b32_decode(n), 'hex'))
SELECT DECODE(b32_decode(n), 'hex')
FROM UNNEST(_stake_addresses) AS n
);

Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/account/account_info_cached.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ BEGIN
stake_address
WHERE
stake_address.hash_raw = ANY(
SELECT ARRAY_AGG(DECODE(b32_decode(n), 'hex'))
SELECT DECODE(b32_decode(n), 'hex')
FROM UNNEST(_stake_addresses) AS n
);

Expand Down
6 changes: 3 additions & 3 deletions files/grest/rpc/account/account_rewards.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ BEGIN
stake_address
WHERE
stake_address.hash_raw = ANY(
SELECT ARRAY_AGG(DECODE(b32_decode(n), 'hex'))
SELECT DECODE(b32_decode(n), 'hex')
FROM UNNEST(_stake_addresses) AS n
);

Expand All @@ -28,7 +28,7 @@ BEGIN
'spendable_epoch', r.spendable_epoch,
'amount', r.amount::text,
'type', r.type,
'pool_id', b32_encode('pool', DECODE(ph.hash_raw,'hex')::text)
'pool_id', b32_encode('pool', ph.hash_raw::text)
)
) AS rewards
FROM
Expand All @@ -48,7 +48,7 @@ BEGIN
'spendable_epoch', r.spendable_epoch,
'amount', r.amount::text,
'type', r.type,
'pool_id', b32_encode('pool', DECODE(ph.hash_raw,'hex')::text)
'pool_id', b32_encode('pool', ph.hash_raw::text)
)
) AS rewards
FROM
Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/account/account_updates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ BEGIN
stake_address
WHERE
stake_address.hash_raw = ANY(
SELECT ARRAY_AGG(DECODE(b32_decode(n), 'hex'))
SELECT DECODE(b32_decode(n), 'hex')
FROM UNNEST(_stake_addresses) AS n
);

Expand Down
12 changes: 5 additions & 7 deletions files/grest/rpc/account/account_utxos.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ DECLARE
BEGIN
SELECT INTO sa_id_list
ARRAY_AGG(stake_address.id)
FROM
stake_address
WHERE
stake_address.hash_raw = ANY(
SELECT ARRAY_AGG(DECODE(b32_decode(n), 'hex'))
FROM stake_address
WHERE stake_address.hash_raw = ANY(
SELECT DECODE(b32_decode(n), 'hex')
FROM UNNEST(_stake_addresses) AS n
);

Expand All @@ -49,7 +47,7 @@ BEGIN
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 ANY(sa_id_list)
WHERE txo.stake_address_id = ANY(sa_id_list)
AND txo.consumed_by_tx_id IS NULL
GROUP BY txo.id
)
Expand Down Expand Up @@ -96,7 +94,7 @@ BEGIN
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.stake_address_id ANY(sa_id_list)
WHERE tx_out.stake_address_id = ANY(sa_id_list)
AND tx_out.consumed_by_tx_id IS NULL
;
END;
Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/blocks/block_info.sql
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ BEGIN
b.vrf_key,
ENCODE(b.op_cert::bytea, 'hex') AS op_cert,
b.op_cert_counter,
b32_encode('pool', DECODE(ph.hash_raw,'hex')::text) AS pool,
b32_encode('pool', ph.hash_raw::text) AS pool,
b.proto_major,
b.proto_minor,
block_data.total_output::text,
Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/blocks/blocks.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ AS $$
EXTRACT(EPOCH FROM b.time)::integer AS block_time,
b.tx_count,
b.vrf_key,
b32_encode('pool', DECODE(ph.hash_raw,'hex')::text) AS pool,
b32_encode('pool', ph.hash_raw::text) AS pool,
b.proto_major,
b.proto_minor,
b.op_cert_counter,
Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/governance/proposal_votes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ BEGIN
vp.voter_role::text,
CASE
WHEN dh.raw IS NOT NULL THEN grest.cip129_hex_to_drep_id(dh.raw, dh.has_script)
WHEN ph.id IS NOT NULL THEN b32_encode('pool', DECODE(ph.hash_raw,'hex')::text)
WHEN ph.id IS NOT NULL THEN b32_encode('pool', ph.hash_raw::text)
WHEN ch.raw IS NOT NULL THEN grest.cip129_hex_to_cc_hot(ch.raw, ch.has_script)
ELSE
'' -- shouldn't happen
Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/governance/proposal_voting_summary.sql
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ BEGIN
FROM combined_data AS c1
) AS y
ORDER BY 1 DESC
);
);
END;
$$;

Expand Down
8 changes: 4 additions & 4 deletions files/grest/rpc/pool/pool_delegators.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ BEGIN
) AS total_balance
FROM grest.stake_distribution_cache AS sdc
INNER JOIN public.stake_address AS sa ON sa.id = sdc.stake_address_id
WHERE sdc.pool_id = _pool_bech32
WHERE sdc.pool_id = (SELECT id FROM pool_hash WHERE view = _pool_bech32)

UNION ALL

Expand All @@ -52,7 +52,7 @@ BEGIN
)

SELECT DISTINCT ON (ad.stake_address_raw)
grest.cip5_hex_to_stake_addr(ad.stake_address_raw),
grest.cip5_hex_to_stake_addr(ad.stake_address_raw)::varchar,
ad.total_balance::text,
d.active_epoch_no,
ENCODE(tx.hash, 'hex')
Expand Down Expand Up @@ -97,7 +97,7 @@ BEGIN
) AS total_balance
FROM grest.stake_distribution_cache AS sdc
INNER JOIN public.stake_address AS sa ON sa.id = sdc.stake_address_id
WHERE sdc.pool_id = _pool_bech32
WHERE sdc.pool_id = _pool_id

UNION ALL

Expand All @@ -123,7 +123,7 @@ BEGIN
)

SELECT
grest.cip5_hex_to_stake_addr(ad.stake_address_raw),
grest.cip5_hex_to_stake_addr(ad.stake_address_raw)::varchar,
ad.total_balance::text
FROM _all_delegations AS ad;

Expand Down
44 changes: 15 additions & 29 deletions files/grest/rpc/pool/pool_delegators_history.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,21 @@ DECLARE
_pool_id bigint;
BEGIN
SELECT id INTO _pool_id FROM pool_hash WHERE pool_hash.hash_raw = DECODE(b32_decode(_pool_bech32),'hex');
IF _epoch_no IS NULL THEN
RETURN QUERY
SELECT
grest.cip5_hex_to_stake_addr(sa.hash_raw),
es.amount::text,
es.epoch_no
FROM
public.epoch_stake AS es
INNER JOIN public.stake_address AS sa ON es.addr_id = sa.id
WHERE
es.pool_id = _pool_id
ORDER BY
es.epoch_no DESC, es.amount DESC;
ELSE
RETURN QUERY
SELECT
grest.cip5_hex_to_stake_addr(sa.hash_raw),
es.amount::text,
es.epoch_no
FROM
public.epoch_stake AS es
INNER JOIN public.stake_address AS sa ON es.addr_id = sa.id
WHERE
es.pool_id = _pool_id
AND
es.epoch_no = _epoch_no
ORDER BY
es.amount DESC;
END IF;
RETURN QUERY
SELECT
grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar,
es.amount::text,
es.epoch_no
FROM public.epoch_stake AS es
INNER JOIN public.stake_address AS sa ON es.addr_id = sa.id
WHERE es.pool_id = _pool_id
AND (
CASE
WHEN _epoch_no IS NULL THEN TRUE
ELSE es.epoch_no = _epoch_no
END
)
ORDER BY es.epoch_no DESC;
END;
$$;

Expand Down
8 changes: 4 additions & 4 deletions files/grest/rpc/pool/pool_info.sql
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,24 @@ BEGIN
FROM grest.pool_info_cache AS pic
INNER JOIN public.pool_hash AS ph ON ph.id = pic.pool_hash_id
WHERE ph.hash_raw = ANY(
SELECT ARRAY_AGG(DECODE(b32_decode(p),'hex'))
SELECT DECODE(b32_decode(p),'hex')
FROM UNNEST(_pool_bech32_ids) AS p)
ORDER BY
pic.pool_hash_id,
pic.tx_id DESC
)
SELECT
api.pool_id_bech32,
api.pool_id_bech32::varchar,
ENCODE(ph.hash_raw::bytea, 'hex') AS pool_id_hex,
pu.active_epoch_no,
ENCODE(pu.vrf_key_hash, 'hex') AS vrf_key_hash,
pu.margin,
pu.fixed_cost::text,
pu.pledge::text,
pu.deposit::text,
grest.cip5_hex_to_stake_addr(sa.hash_raw) AS reward_addr,
grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS reward_addr,
ARRAY(
SELECT grest.cip5_hex_to_stake_addr(sa.hash_raw)
SELECT grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar
FROM public.pool_owner AS po
INNER JOIN public.stake_address AS sa ON sa.id = po.addr_id
WHERE po.pool_update_id = api.update_id
Expand Down
6 changes: 3 additions & 3 deletions files/grest/rpc/pool/pool_list.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ RETURNS TABLE (
LANGUAGE sql STABLE
AS $$
SELECT DISTINCT ON (pic.pool_hash_id)
b32_encode('pool', ph.hash_raw::text) AS pool_id_bech32,
b32_encode('pool', ph.hash_raw::text)::varchar AS pool_id_bech32,
ENCODE(ph.hash_raw,'hex') as pool_id_hex,
pu.active_epoch_no,
pu.margin,
pu.fixed_cost::text,
pu.pledge::text,
pu.deposit::text,
grest.cip5_hex_to_stake_addr(sa.hash_raw) AS reward_addr,
grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS reward_addr,
ARRAY(
SELECT grest.cip5_hex_to_stake_addr(sa.hash_raw)
SELECT grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar
FROM public.pool_owner AS po
INNER JOIN public.stake_address AS sa ON sa.id = po.addr_id
WHERE po.pool_update_id = pic.update_id
Expand Down
4 changes: 2 additions & 2 deletions files/grest/rpc/pool/pool_metadata.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ AS $$
BEGIN
RETURN QUERY
SELECT DISTINCT ON (ph.id)
b32_encode('pool', ph.hash_raw::text) AS pool_id_bech32,
b32_encode('pool', ph.hash_raw::text)::varchar AS pool_id_bech32,
pmr.url AS meta_url,
ENCODE(pmr.hash, 'hex') AS meta_hash,
ocpd.json AS meta_json
Expand All @@ -22,7 +22,7 @@ BEGIN
CASE
WHEN _pool_bech32_ids IS NULL THEN TRUE
WHEN _pool_bech32_ids IS NOT NULL THEN ph.hash_raw = ANY(
SELECT ARRAY_AGG(DECODE(b32_decode(p),'hex'))
SELECT DECODE(b32_decode(p),'hex')
FROM UNNEST(_pool_bech32_ids) AS p)
END
ORDER BY
Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/pool/pool_relays.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ RETURNS TABLE (
LANGUAGE sql STABLE
AS $$
SELECT DISTINCT ON (ph.id)
b32_encode('pool', ph.hash_raw::text) AS pool_id_bech32,
b32_encode('pool', ph.hash_raw::text)::varchar AS pool_id_bech32,
JSONB_AGG(JSONB_BUILD_OBJECT (
'ipv4', pr.ipv4,
'ipv6', pr.ipv6,
Expand Down
4 changes: 2 additions & 2 deletions files/grest/rpc/pool/pool_updates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ BEGIN
SELECT
ENCODE(tx.hash::bytea, 'hex') AS tx_hash,
EXTRACT(EPOCH FROM b.time)::integer AS block_time,
b32_encode('pool', ph.hash_raw::text) AS pool_id_bech32,
b32_encode('pool', ph.hash_raw::text)::varchar AS pool_id_bech32,
ENCODE(ph.hash_raw::bytea, 'hex') AS pool_id_hex,
pu.active_epoch_no,
ENCODE(pu.vrf_key_hash, 'hex') AS vrf_key_hash,
pu.margin,
pu.fixed_cost::text,
pu.pledge::text,
grest.cip5_hex_to_stake_addr(sa.hash_raw) AS reward_addr,
grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS reward_addr,
JSONB_AGG(po.stake_address) AS owners,
JSONB_AGG(JSONB_BUILD_OBJECT (
'ipv4', pr.ipv4,
Expand Down

0 comments on commit 3d49a57

Please sign in to comment.