Skip to content

Commit

Permalink
Move utilities to 000 instead of 03 (as 00 has dependent objects), an…
Browse files Browse the repository at this point in the history
…d some more fixes
  • Loading branch information
rdlrt committed Sep 17, 2024
1 parent 7c070f0 commit 9c51b4d
Show file tree
Hide file tree
Showing 22 changed files with 49 additions and 55 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
-- CIP References
-- 0005: Common bech32 prefixes https://cips.cardano.org/cip/CIP-0005
-- 0019: Cardano Addresses https://cips.cardano.org/cip/CIP-0019

CREATE OR REPLACE FUNCTION grest.cip5_hex_to_stake_addr(_raw bytea)
RETURNS text
LANGUAGE plpgsql STABLE
AS $$
BEGIN
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', _raw::text);
ELSE
RETURN b32_encode('stake', _raw::text);
END IF;
END;
-- CIP References
-- 0005: Common bech32 prefixes https://cips.cardano.org/cip/CIP-0005
-- 0019: Cardano Addresses https://cips.cardano.org/cip/CIP-0019

CREATE OR REPLACE FUNCTION grest.cip5_hex_to_stake_addr(_raw bytea)
RETURNS text
LANGUAGE plpgsql STABLE
AS $$
BEGIN
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', _raw::text);
ELSE
RETURN b32_encode('stake', _raw::text);
END IF;
END;
$$;
File renamed without changes.
4 changes: 2 additions & 2 deletions files/grest/rpc/account/account_addresses.sql
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ BEGIN
)

SELECT
grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_address,
grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS stake_address,
JSONB_AGG(txo_addr.address) AS addresses
FROM
txo_addr
Expand All @@ -63,7 +63,7 @@ BEGIN
)

SELECT
grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_address,
grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS stake_address,
JSONB_AGG(txo_addr.address) AS addresses
FROM
txo_addr
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 @@ -35,7 +35,7 @@ BEGIN
)

SELECT
grest.cip5_hex_to_stake_addr(aa.hash_raw) AS stake_address,
grest.cip5_hex_to_stake_addr(aa.hash_raw)::varchar AS stake_address,
ENCODE(aa.policy, 'hex') AS policy_id,
ENCODE(aa.name, 'hex') AS asset_name,
aa.fingerprint AS fingerprint,
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 @@ -21,7 +21,7 @@ BEGIN
IF _epoch_no IS NOT NULL THEN
RETURN QUERY
SELECT
grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_address,
grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS stake_address,
JSONB_AGG(
JSONB_BUILD_OBJECT(
'pool_id', b32_encode('pool', ph.hash_raw::text),
Expand Down
6 changes: 3 additions & 3 deletions files/grest/rpc/account/account_info.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ BEGIN
RETURN QUERY

SELECT
grest.cip5_hex_to_stake_addr(status_t.hash_raw) AS stake_address,
grest.cip5_hex_to_stake_addr(status_t.hash_raw)::varchar AS stake_address,
CASE WHEN status_t.registered = TRUE THEN
'registered'
ELSE
Expand Down Expand Up @@ -91,9 +91,9 @@ BEGIN
LEFT JOIN (
SELECT
delegation.addr_id,
b32_encode('pool', DECODE(ph.hash_raw,'hex')::text) AS delegated_pool
b32_encode('pool', ph.hash_raw::text)::varchar AS delegated_pool
FROM delegation
INNER JOIN pool_hash ON pool_hash.id = delegation.pool_hash_id
INNER JOIN pool_hash AS ph ON ph.id = delegation.pool_hash_id
WHERE delegation.addr_id = ANY(sa_id_list)
AND NOT EXISTS (
SELECT TRUE
Expand Down
16 changes: 6 additions & 10 deletions files/grest/rpc/account/account_rewards.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ BEGIN
IF _epoch_no IS NULL THEN
RETURN QUERY
SELECT
grest.cip5_hex_to_stake_addr(sa.hash_raw),
grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar,
JSONB_AGG(
JSONB_BUILD_OBJECT(
'earned_epoch', r.earned_epoch,
Expand All @@ -31,17 +31,15 @@ BEGIN
'pool_id', b32_encode('pool', ph.hash_raw::text)
)
) AS rewards
FROM
reward AS r
FROM reward AS r
LEFT JOIN pool_hash AS ph ON r.pool_id = ph.id
INNER JOIN stake_address AS sa ON sa.id = r.addr_id
WHERE
r.addr_id = ANY(sa_id_list)
WHERE r.addr_id = ANY(sa_id_list)
GROUP BY sa.hash_raw;
ELSE
RETURN QUERY
SELECT
grest.cip5_hex_to_stake_addr(sa.hash_raw),
grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar,
JSONB_AGG(
JSONB_BUILD_OBJECT(
'earned_epoch', r.earned_epoch,
Expand All @@ -51,12 +49,10 @@ BEGIN
'pool_id', b32_encode('pool', ph.hash_raw::text)
)
) AS rewards
FROM
reward AS r
FROM reward AS r
LEFT JOIN pool_hash AS ph ON r.pool_id = ph.id
INNER JOIN stake_address AS sa ON sa.id = r.addr_id
WHERE
r.addr_id = ANY(sa_id_list)
WHERE r.addr_id = ANY(sa_id_list)
AND r.earned_epoch = _epoch_no
GROUP BY sa.hash_raw;
END IF;
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 @@ -20,7 +20,7 @@ BEGIN

RETURN QUERY
SELECT
grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_address,
grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS stake_address,
JSONB_AGG(
JSONB_BUILD_OBJECT(
'action_type', actions.action_type,
Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/address/address_info.sql
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ BEGIN
SELECT
ka.address,
COALESCE(SUM(au.value), '0')::text AS balance,
grest.cip5_hex_to_stake_addr(ka.stake_address_raw),
grest.cip5_hex_to_stake_addr(ka.stake_address_raw)::varchar,
ka.script_address,
CASE
WHEN EXISTS (
Expand Down
4 changes: 2 additions & 2 deletions files/grest/rpc/assets/asset_addresses.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ BEGIN
RETURN QUERY
SELECT
x.address,
grest.cip5_hex_to_stake_addr(x.stake_address_raw),
grest.cip5_hex_to_stake_addr(x.stake_address_raw)::varchar,
SUM(x.quantity)::text
FROM
(
Expand All @@ -47,7 +47,7 @@ BEGIN
RETURN QUERY
SELECT
x.address,
grest.cip5_hex_to_stake_addr(x.stake_address_raw),
grest.cip5_hex_to_stake_addr(x.stake_address_raw)::varchar,
SUM(x.quantity)::text
FROM
(
Expand Down
4 changes: 2 additions & 2 deletions files/grest/rpc/assets/asset_nft_address.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ BEGIN
RETURN QUERY
SELECT
txo.address,
grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_address
grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS stake_address
FROM tx_out AS txo
LEFT JOIN stake_address AS sa ON txo.stake_address_id = sa.id
WHERE txo.id = (
Expand All @@ -40,7 +40,7 @@ BEGIN
RETURN QUERY
SELECT
txo.address,
grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_address
grest.cip5_hex_to_stake_addr(sa.hash_raw)::varchar AS stake_address
FROM tx_out AS txo
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
Expand Down
4 changes: 2 additions & 2 deletions files/grest/rpc/assets/policy_asset_addresses.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ BEGIN
SELECT
atoc.ma_id,
txo.address,
grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_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
Expand All @@ -47,7 +47,7 @@ BEGIN
SELECT
ENCODE(ma.name, 'hex') AS asset_name,
txo.address,
grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_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
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', ph.hash_raw::text) AS pool,
b32_encode('pool', ph.hash_raw::text)::varchar AS pool,
b.proto_major,
b.proto_minor,
block_data.total_output::text,
Expand Down
4 changes: 2 additions & 2 deletions files/grest/rpc/governance/proposal_list.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ AS $$
gap.type,
gap.description,
gap.deposit::text,
grest.cip5_hex_to_stake_addr(sa.hash_raw),
grest.cip5_hex_to_stake_addr(sa.hash_raw)::text,
b.epoch_no,
gap.ratified_epoch,
gap.enacted_epoch,
Expand All @@ -51,7 +51,7 @@ AS $$
ELSE
JSONB_BUILD_OBJECT(
'stake_address', (
SELECT grest.cip5_hex_to_stake_addr(sa2.hash_raw)
SELECT grest.cip5_hex_to_stake_addr(sa2.hash_raw)::text
FROM stake_address AS sa2
WHERE sa2.id = tw.stake_address_id
),
Expand Down
4 changes: 2 additions & 2 deletions files/grest/rpc/governance/voter_proposal_list.sql
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ BEGIN
gap.type,
gap.description,
gap.deposit::text,
grest.cip5_hex_to_stake_addr(sa.hash_raw),
grest.cip5_hex_to_stake_addr(sa.hash_raw)::text,
b.epoch_no,
gap.ratified_epoch,
gap.enacted_epoch,
Expand All @@ -81,7 +81,7 @@ BEGIN
ELSE
JSONB_BUILD_OBJECT(
'stake_address', (
SELECT grest.cip5_hex_to_stake_addr(sa2.hash_raw)
SELECT grest.cip5_hex_to_stake_addr(sa2.hash_raw)::text
FROM stake_address AS sa2
WHERE sa2.id = tw.stake_address_id
),
Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/pool/pool_history.sql
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ BEGIN
member_rewards::text,
COALESCE(epoch_ros, 0)
FROM grest.pool_history_cache AS phc
WHERE phc.pool_id = _pool_bech32
WHERE phc.pool_id = (SELECT id FROM pool_hash AS ph WHERE ph.hash_raw = DECODE(b32_decode(_pool_bech32),'hex'))
AND phc.epoch_no < (_curr_epoch - 2) -- temporary condition for testing, until cache table population fixed, then can be removed
UNION
SELECT
Expand Down
3 changes: 1 addition & 2 deletions files/grest/rpc/pool/pool_metadata.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ BEGIN
SELECT DECODE(b32_decode(p),'hex')
FROM UNNEST(_pool_bech32_ids) AS p)
END
ORDER BY
ph.hash_raw,
ORDER BY ph.id,
pmr.registered_tx_id DESC;
END;
$$;
Expand Down
4 changes: 2 additions & 2 deletions files/grest/rpc/pool/pool_relays.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ AS $$
FROM public.pool_hash AS ph
LEFT JOIN public.pool_update AS pu ON pu.hash_id = ph.id
LEFT JOIN public.pool_relay AS pr ON pu.id = pr.update_id
GROUP BY ph.hash_raw,pu.registered_tx_id
GROUP BY ph.id, ph.hash_raw,pu.registered_tx_id
ORDER BY
ph.hash_raw,
ph.id,
pu.registered_tx_id DESC
;
$$;
Expand Down
3 changes: 1 addition & 2 deletions files/grest/rpc/pool/pool_stake_snapshot.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ BEGIN
grest.pool_active_stake_cache AS pasc
INNER JOIN grest.epoch_active_stake_cache AS easc ON easc.epoch_no = pasc.epoch_no
LEFT JOIN grest.epoch_info_cache AS eic ON eic.epoch_no = pasc.epoch_no
WHERE
pasc.pool_id = _pool_bech32
WHERE pasc.pool_id = (SELECT id FROM pool_hash AS ph WHERE ph.hash_raw = DECODE(b32_decode(_pool_bech32),'hex'))
AND pasc.epoch_no BETWEEN _go AND _mark
ORDER BY
pasc.epoch_no;
Expand Down

0 comments on commit 9c51b4d

Please sign in to comment.