Skip to content

Commit

Permalink
pool_list: Add all cache fields & remove filter
Browse files Browse the repository at this point in the history
  • Loading branch information
rdlrt committed Aug 14, 2023
1 parent f1e2d9e commit 9dcdde4
Show file tree
Hide file tree
Showing 27 changed files with 362 additions and 264 deletions.
8 changes: 4 additions & 4 deletions files/grest/rpc/01_cached_tables/asset_info_cache.sql
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ BEGIN
WHERE state = 'active'
AND query ILIKE '%grest.asset_info_cache_update%'
AND datname = (SELECT current_database())
) THEN
) THEN
RAISE EXCEPTION 'Previous asset_info_cache_update query still running but should have completed! Exiting...';
END IF;

Expand All @@ -56,7 +56,7 @@ BEGIN
tx_mint_meta AS (
SELECT
mtm.ident,
MIN(mtm.tx_id) AS first_mint_tx_id,
MIN(mtm.tx_id) AS first_mint_tx_id,
MAX(mtm.tx_id) AS last_mint_tx_id
FROM ma_tx_mint AS mtm
INNER JOIN tx_metadata AS tm ON tm.tx_id = mtm.tx_id
Expand All @@ -74,7 +74,7 @@ BEGIN
tx_mint_nometa AS (
SELECT
mtm.ident,
MIN(mtm.tx_id) AS first_mint_tx_id,
MIN(mtm.tx_id) AS first_mint_tx_id,
MAX(mtm.tx_id) AS last_mint_tx_id
FROM ma_tx_mint AS mtm
LEFT JOIN tx_mint_meta ON tx_mint_meta.ident = mtm.ident
Expand Down Expand Up @@ -111,7 +111,7 @@ BEGIN
FROM tx_mint_nometa
)

INSERT INTO grest.asset_info_cache
INSERT INTO grest.asset_info_cache
SELECT
ma.id,
MIN(B.time) AS creation_time,
Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/01_cached_tables/asset_registry_cache.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ BEGIN
decimals
)
VALUES(
_asset_policy,
_asset_policy,
_asset_name,
_name,
_description,
Expand Down
4 changes: 2 additions & 2 deletions files/grest/rpc/01_cached_tables/epoch_info_cache.sql
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ BEGIN
EXTRACT(EPOCH FROM e.start_time) AS i_first_block_time,
EXTRACT(EPOCH FROM e.end_time) AS i_last_block_time,
CASE -- populated in epoch n + 2
WHEN e.no <= _curr_epoch - 2 THEN reward_pot.amount
WHEN e.no <= _curr_epoch - 2 THEN reward_pot.amount
ELSE NULL
END AS i_total_rewards,
CASE -- populated in epoch n + 2
WHEN e.no <= _curr_epoch THEN ROUND(reward_pot.amount / e.blk_count)
ELSE NULL
END AS i_avg_blk_reward,
END AS i_avg_blk_reward,
last_tx.tx_id AS i_last_tx_id,
ep.min_fee_a AS p_min_fee_a,
ep.min_fee_b AS p_min_fee_b,
Expand Down
12 changes: 6 additions & 6 deletions files/grest/rpc/01_cached_tables/pool_info_cache.sql
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ BEGIN
ORDER BY pr.id
LIMIT 1;

IF _retiring_epoch IS NULL THEN
IF _retiring_epoch IS NULL THEN
_pool_status := 'registered';
ELSIF _retiring_epoch > _current_epoch_no THEN
_pool_status := 'retiring';
Expand All @@ -69,7 +69,7 @@ BEGIN
tx_hash,
block_time,
pool_hash_id,
pool_id_bech32,
pool_id_bech32,
pool_id_hex,
active_epoch_no,
vrf_key_hash,
Expand All @@ -88,7 +88,7 @@ BEGIN
SELECT
_tx_id,
_update_id,
encode(tx.hash::bytea, 'hex'),
encode(tx.hash::bytea, 'hex'),
EXTRACT(EPOCH FROM b.time),
_hash_id,
ph.view,
Expand Down Expand Up @@ -174,7 +174,7 @@ BEGIN
ORDER BY pr.id
LIMIT 1;

IF _retiring_epoch IS NULL THEN
IF _retiring_epoch IS NULL THEN
_pool_status := 'registered';
ELSIF _retiring_epoch > _current_epoch_no THEN
_pool_status := 'retiring';
Expand Down Expand Up @@ -222,8 +222,8 @@ BEGIN
END IF;

ELSIF (tg_table_name = 'pool_relay') THEN
SELECT pic.id INTO _latest_pool_update_id
FROM grest.pool_info_cache AS pic
SELECT pic.id INTO _latest_pool_update_id
FROM grest.pool_info_cache AS pic
INNER JOIN public.pool_update AS pu ON pu.hash_id = pic.pool_hash_id AND pu.registered_tx_id = pic.tx_id
WHERE pu.id = new.update_id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ BEGIN
SELECT (_current_block_height - _last_update_block_height) INTO _last_update_block_diff;
-- Do nothing until there is a 180 blocks difference in height - 60 minutes theoretical time
-- 185 in check because last block height considered is 5 blocks behind tip

Raise NOTICE 'Last stake distribution update was % blocks ago...',
_last_update_block_diff;
IF (_last_update_block_diff >= 180
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 @@ -9,7 +9,7 @@ DECLARE
sa_id_list integer[] DEFAULT NULL;
BEGIN
SELECT INTO sa_id_list
ARRAY_AGG(stake_address.id)
ARRAY_AGG(stake_address.id)
FROM
stake_address
WHERE
Expand Down
6 changes: 2 additions & 4 deletions files/grest/rpc/address/address_assets.sql
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ BEGIN
'quantity', aa.quantity::text
)
) AS asset_list
FROM
_all_assets AS aa
GROUP BY
aa.address
FROM _all_assets AS aa
GROUP BY aa.address
) assets_grouped;
END;
$$;
Expand Down
50 changes: 22 additions & 28 deletions files/grest/rpc/address/address_info.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ BEGIN
DISTINCT ON (tx_out.address) tx_out.address,
sa.view AS stake_address,
COALESCE(tx_out.address_has_script, 'false') AS script_address
FROM
tx_out
LEFT JOIN stake_address sa ON sa.id = tx_out.stake_address_id
WHERE
tx_out.address = ANY(_addresses);
FROM tx_out
LEFT JOIN stake_address AS sa ON sa.id = tx_out.stake_address_id
WHERE tx_out.address = ANY(_addresses);

RETURN QUERY
WITH _all_utxos AS (
Expand All @@ -35,15 +33,12 @@ BEGIN
tx_out.data_hash,
tx_out.inline_datum_id,
tx_out.reference_script_id
FROM
tx_out
LEFT JOIN tx_in ON tx_in.tx_out_id = tx_out.tx_id
AND tx_in.tx_out_index = tx_out.index
INNER JOIN tx ON tx.id = tx_out.tx_id
WHERE
tx_in.tx_out_id IS NULL
AND
tx_out.address = ANY(_addresses)
FROM tx_out
LEFT JOIN tx_in ON tx_in.tx_out_id = tx_out.tx_id
AND tx_in.tx_out_index = tx_out.index
INNER JOIN tx ON tx.id = tx_out.tx_id
WHERE tx_in.tx_out_id IS NULL
AND tx_out.address = ANY(_addresses)
)

SELECT
Expand All @@ -57,7 +52,7 @@ BEGIN
) THEN
JSONB_AGG(
JSONB_BUILD_OBJECT(
'tx_hash', ENCODE(au.hash, 'hex'),
'tx_hash', ENCODE(au.hash, 'hex'),
'tx_index', au.index,
'block_height', block.block_no,
'block_time', EXTRACT(EPOCH FROM block.time)::integer,
Expand Down Expand Up @@ -98,12 +93,10 @@ BEGIN
'decimals', COALESCE(aic.decimals, 0),
'quantity', mtx.quantity::text
))
FROM
ma_tx_out AS mtx
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
WHERE
mtx.tx_out_id = au.txo_id
FROM ma_tx_out AS mtx
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
WHERE mtx.tx_out_id = au.txo_id
),
JSONB_BUILD_ARRAY()
)
Expand All @@ -112,14 +105,15 @@ BEGIN
ELSE
'[]'::jsonb
END AS utxo_set
FROM
_known_addresses AS ka
LEFT OUTER JOIN _all_utxos AS au ON au.address = ka.address
LEFT JOIN public.block ON block.id = au.block_id
LEFT JOIN datum ON datum.id = au.inline_datum_id
LEFT JOIN script ON script.id = au.reference_script_id
FROM _known_addresses AS ka
LEFT OUTER JOIN _all_utxos AS au ON au.address = ka.address
LEFT JOIN public.block ON block.id = au.block_id
LEFT JOIN datum ON datum.id = au.inline_datum_id
LEFT JOIN script ON script.id = au.reference_script_id
GROUP BY
ka.address, ka.stake_address, ka.script_address;
ka.address,
ka.stake_address,
ka.script_address;
DROP TABLE _known_addresses;
END;
$$;
Expand Down
24 changes: 10 additions & 14 deletions files/grest/rpc/assets/asset_history.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ DECLARE
BEGIN
SELECT DECODE(_asset_policy, 'hex') INTO _asset_policy_decoded;
SELECT DECODE(
CASE WHEN _asset_name IS NULL
THEN ''
ELSE
_asset_name
CASE WHEN _asset_name IS NULL THEN ''
ELSE _asset_name
END,
'hex'
) INTO _asset_name_decoded;
Expand Down Expand Up @@ -48,16 +46,15 @@ BEGIN
'key', tm.key::text,
'json', tm.json
)
)
)
END
) AS metadata
FROM
ma_tx_mint AS mtm
INNER JOIN multi_asset AS ma ON ma.id = mtm.ident
INNER JOIN tx ON tx.id = MTM.tx_id
INNER JOIN block AS b ON b.id = tx.block_id
LEFT JOIN tx_metadata AS tm ON tm.tx_id = tx.id
WHERE ma.policy = _asset_policy_decoded
FROM ma_tx_mint AS mtm
INNER JOIN multi_asset AS ma ON ma.id = mtm.ident
INNER JOIN tx ON tx.id = MTM.tx_id
INNER JOIN block AS b ON b.id = tx.block_id
LEFT JOIN tx_metadata AS tm ON tm.tx_id = tx.id
WHERE ma.policy = _asset_policy_decoded
AND ma.name = _asset_name_decoded
GROUP BY
ma.fingerprint,
Expand All @@ -66,8 +63,7 @@ BEGIN
mtm.quantity,
tm.key
) AS minting_data
GROUP BY
minting_data.fingerprint;
GROUP BY minting_data.fingerprint;
END;
$$;

Expand Down
22 changes: 11 additions & 11 deletions files/grest/rpc/assets/asset_nft_address.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ DECLARE
BEGIN
SELECT DECODE(_asset_policy, 'hex') INTO _asset_policy_decoded;
SELECT DECODE(
CASE WHEN _asset_name IS NULL
THEN ''
ELSE
_asset_name
CASE WHEN _asset_name IS NULL THEN ''
ELSE _asset_name
END,
'hex'
) INTO _asset_name_decoded;

SELECT id INTO _asset_id
FROM
multi_asset AS ma
INNER JOIN grest.asset_info_cache AS aic ON aic.asset_id = ma.id
WHERE
ma.policy = _asset_policy_decoded
SELECT id INTO _asset_id
FROM multi_asset AS ma
INNER JOIN grest.asset_info_cache AS aic ON aic.asset_id = ma.id
WHERE ma.policy = _asset_policy_decoded
AND ma.name = _asset_name_decoded
AND aic.total_supply = 1;

RETURN QUERY
SELECT address
FROM tx_out
WHERE id = (SELECT MAX(tx_out_id) FROM ma_tx_out WHERE ident = _asset_id);
WHERE id = (
SELECT MAX(tx_out_id)
FROM ma_tx_out
WHERE ident = _asset_id
);
END;
$$;

Expand Down
61 changes: 24 additions & 37 deletions files/grest/rpc/assets/asset_summary.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ DECLARE
BEGIN
SELECT DECODE(_asset_policy, 'hex') INTO _asset_policy_decoded;
SELECT DECODE(
CASE WHEN _asset_name IS NULL
THEN ''
ELSE
_asset_name
CASE WHEN _asset_name IS NULL THEN ''
ELSE _asset_name
END,
'hex'
) INTO _asset_name_decoded;
SELECT id INTO _asset_id FROM multi_asset AS ma WHERE ma.policy = _asset_policy_decoded AND ma.name = _asset_name_decoded;
SELECT id INTO _asset_id
FROM multi_asset AS ma
WHERE ma.policy = _asset_policy_decoded
AND ma.name = _asset_name_decoded;
RETURN QUERY
with _asset_utxos AS (
SELECT
Expand All @@ -32,48 +33,34 @@ BEGIN
txo.index AS tx_out_idx,
txo.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 tx_in AS txi ON txi.tx_out_id = txo.tx_id
WHERE
mto.ident = _asset_id
AND
txi.tx_out_id IS NULL)

FROM ma_tx_out AS mto
INNER JOIN tx_out AS txo ON txo.id = mto.tx_out_id
LEFT JOIN tx_in AS txi ON txi.tx_out_id = txo.tx_id
WHERE mto.ident = _asset_id
AND txi.tx_out_id IS NULL)

SELECT
_asset_policy,
_asset_name,
ma.fingerprint,
(
SELECT
COUNT(DISTINCT(txo.tx_id))
FROM
ma_tx_out mto
INNER JOIN tx_out txo ON txo.id = mto.tx_out_id
WHERE
ident = _asset_id
SELECT COUNT(DISTINCT(txo.tx_id))
FROM ma_tx_out mto
INNER JOIN tx_out txo ON txo.id = mto.tx_out_id
WHERE ident = _asset_id
) AS total_transactions,
(
SELECT
COUNT(DISTINCT(_asset_utxos.sa_id))
FROM
_asset_utxos
WHERE
_asset_utxos.sa_id IS NOT NULL
SELECT COUNT(DISTINCT(_asset_utxos.sa_id))
FROM _asset_utxos
WHERE _asset_utxos.sa_id IS NOT NULL
) AS staked_wallets,
(
SELECT
COUNT(DISTINCT(_asset_utxos.address))
FROM
_asset_utxos
WHERE
_asset_utxos.sa_id IS NULL
SELECT COUNT(DISTINCT(_asset_utxos.address))
FROM _asset_utxos
WHERE _asset_utxos.sa_id IS NULL
) AS unstaked_addresses
FROM
multi_asset AS ma
WHERE
ma.id = _asset_id;
FROM multi_asset AS ma
WHERE ma.id = _asset_id;
END;
$$;

Expand Down
Loading

0 comments on commit 9dcdde4

Please sign in to comment.