Skip to content

Commit

Permalink
Update specs/addendum to koios-1.0.11rc (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
hodlonaut authored and rdlrt committed Sep 11, 2023
1 parent c6465fd commit c4772a3
Show file tree
Hide file tree
Showing 16 changed files with 2,242 additions and 831 deletions.
30 changes: 20 additions & 10 deletions files/grest/rpc/01_cached_tables/epoch_info_cache.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CREATE TABLE IF NOT EXISTS grest.epoch_info_cache (
i_avg_blk_reward lovelace,
i_last_tx_id bigint,
p_nonce text,
p_block_hash text,
p_block_hash text
);

COMMENT ON TABLE grest.epoch_info_cache IS 'Contains detailed info for epochs including protocol parameters';
Expand Down Expand Up @@ -76,6 +76,20 @@ BEGIN
DELETE FROM grest.epoch_info_cache
WHERE epoch_no >= _epoch_no_to_insert_from;

DROP TABLE IF EXISTS last_tx_id_subset;
CREATE TEMP TABLE last_tx_id_subset (
epoch_no bigint,
tx_id bigint
);

INSERT INTO last_tx_id_subset
SELECT b.epoch_no, MAX(tx.id)
FROM block AS b
INNER JOIN tx ON tx.block_id = b.id
WHERE b.block_no IS NOT NULL
AND b.tx_count != 0
GROUP BY b.epoch_no;

INSERT INTO grest.epoch_info_cache
SELECT DISTINCT ON (b.time)
e.no AS epoch_no,
Expand All @@ -93,7 +107,11 @@ BEGIN
WHEN e.no <= _curr_epoch THEN ROUND(reward_pot.amount / e.blk_count)
ELSE NULL
END AS i_avg_blk_reward,
last_tx.tx_id AS i_last_tx_id,
(
SELECT MAX(tx_id)
FROM last_tx_id_subset
WHERE epoch_no <= e.no
) AS i_last_tx_id,
ENCODE(ep.nonce, 'hex') AS p_nonce,
ENCODE(b.hash, 'hex') AS p_block_hash
FROM epoch AS e
Expand All @@ -109,14 +127,6 @@ BEGIN
GROUP BY
e.no
) AS reward_pot ON TRUE
LEFT JOIN LATERAL (
SELECT MAX(tx.id) AS tx_id
FROM block AS b
INNER JOIN tx ON tx.block_id = b.id
WHERE b.epoch_no <= e.no
AND b.block_no IS NOT NULL
AND b.tx_count != 0
) AS last_tx ON TRUE
WHERE e.no >= _epoch_no_to_insert_from
ORDER BY
b.time ASC,
Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/account/account_utxos.sql
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,4 @@ BEGIN
END;
$$;

COMMENT ON FUNCTION grest.address_utxos IS 'Get UTxO details for requested addresses'; -- noqa: LT01
COMMENT ON FUNCTION grest.account_utxos IS 'Get UTxO details for requested stake account'; -- noqa: LT01
17 changes: 9 additions & 8 deletions files/grest/rpc/script/datum_info.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
CREATE OR REPLACE FUNCTION grest.datum_info(_datum_hashes text [])
RETURNS TABLE (
hash text,
datum_hash text,
creation_tx_hash text,
value jsonb,
bytes text
)
Expand All @@ -13,14 +14,14 @@ BEGIN
FROM UNNEST(_datum_hashes) AS d_hash;
RETURN QUERY
SELECT
ENCODE(d.hash, 'hex'),
ENCODE(d.hash,'hex'),
ENCODE(tx.hash,'hex') AS creation_tx_hash,
d.value,
ENCODE(d.bytes, 'hex')
FROM
datum AS d
WHERE
d.hash = ANY(_datum_hashes_decoded);
ENCODE(d.bytes,'hex')
FROM datum AS d
INNER JOIN tx ON tx.id = d.tx_id
WHERE d.hash = ANY(_datum_hashes_decoded);
END;
$$;

COMMENT ON FUNCTION grest.datum_info IS 'Get information about a given data FROM hashes.'; -- noqa: LT01
COMMENT ON FUNCTION grest.datum_info IS 'Get information about a given datum FROM hashes.'; -- noqa: LT01
20 changes: 10 additions & 10 deletions files/grest/rpc/script/native_script_list.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ CREATE OR REPLACE FUNCTION grest.native_script_list()
RETURNS TABLE (
script_hash text,
creation_tx_hash text,
type scripttype,
script jsonb
type text,
size word31type
)
LANGUAGE plpgsql
AS $$
BEGIN
RETURN QUERY
SELECT
ENCODE(script.hash, 'hex'),
ENCODE(tx.hash, 'hex'),
script.type,
script.json
FROM script
INNER JOIN tx ON tx.id = script.tx_id
WHERE script.type IN ('timelock', 'multisig');
ENCODE(s.hash, 'hex')::text AS script_hash,
ENCODE(tx.hash, 'hex')::text AS creation_tx_hash,
s.type::text AS type,
s.serialised_size AS size
FROM script AS s
INNER JOIN tx ON tx.id = s.tx_id
WHERE s.type IN ('timelock', 'multisig');
END;
$$;

COMMENT ON FUNCTION grest.native_script_list IS 'Get a list of all native(multisig/timelock) script hashes with creation tx hash, type and script in json format.'; --noqa: LT01
COMMENT ON FUNCTION grest.native_script_list IS 'Get a list of all native(multisig/timelock) script hashes with creation tx hash, type and script size.'; --noqa: LT01
18 changes: 11 additions & 7 deletions files/grest/rpc/script/plutus_script_list.sql
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
CREATE OR REPLACE FUNCTION grest.plutus_script_list()
RETURNS TABLE (
script_hash text,
creation_tx_hash text
creation_tx_hash text,
type text,
size word31type
)
LANGUAGE plpgsql
AS $$
BEGIN
RETURN QUERY
SELECT
ENCODE(script.hash, 'hex') AS script_hash,
ENCODE(tx.hash, 'hex') AS creation_tx_hash
FROM script
INNER JOIN tx ON tx.id = script.tx_id
WHERE script.type IN ('plutusV1', 'plutusV2');
ENCODE(s.hash,'hex')::text AS script_hash,
ENCODE(tx.hash,'hex')::text AS creation_tx_hash,
s.type::text AS type,
s.serialised_size AS size
FROM script AS s
INNER JOIN tx ON tx.id = s.tx_id
WHERE s.type IN ('plutusV1', 'plutusV2');
END;
$$;

COMMENT ON FUNCTION grest.plutus_script_list IS 'Get a list of all plutus script hashes with creation tx hash.'; --noqa: LT01
COMMENT ON FUNCTION grest.plutus_script_list IS 'Get a list of all plutus script hashes with creation tx hash.'; --noqa: LT01
32 changes: 32 additions & 0 deletions files/grest/rpc/script/script_info.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
CREATE OR REPLACE FUNCTION grest.script_info(_script_hashes text [])
RETURNS TABLE (
script_hash text,
creation_tx_hash text,
type text,
value jsonb,
bytes text,
size word31type
)
LANGUAGE plpgsql
AS $$
DECLARE
_script_hashes_decoded bytea[];
BEGIN
SELECT INTO _script_hashes_decoded ARRAY_AGG(DECODE(s_hash, 'hex'))
FROM UNNEST(_script_hashes) AS s_hash;
RETURN QUERY
SELECT
ENCODE(s.hash,'hex') AS script_hash,
ENCODE(tx.hash,'hex') AS creation_tx_hash,
s.type::text AS type,
s.json AS value,
ENCODE(s.bytes,'hex')::text AS bytes,
s.serialised_size AS size
FROM script AS s
INNER JOIN tx ON tx.id = s.tx_id
WHERE s.hash = ANY(_script_hashes_decoded)
;
END;
$$;

COMMENT ON FUNCTION grest.script_info IS 'Get information about a given script FROM hashes.'; -- noqa: LT01
Loading

0 comments on commit c4772a3

Please sign in to comment.