Skip to content

Commit

Permalink
Flatten account_assets/address_assets
Browse files Browse the repository at this point in the history
  • Loading branch information
rdlrt committed Aug 28, 2023
1 parent 0eb88e1 commit 0938e84
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 70 deletions.
58 changes: 21 additions & 37 deletions files/grest/rpc/account/account_assets.sql
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
CREATE OR REPLACE FUNCTION grest.account_assets(_stake_addresses text [])
RETURNS TABLE (
stake_address varchar,
asset_list jsonb
policy_id text,
asset_name text,
fingerprint varchar,
decimals integer,
quantity text
)
LANGUAGE plpgsql
AS $$
DECLARE
sa_id_list integer[];
BEGIN
SELECT INTO sa_id_list
ARRAY_AGG(stake_address.id)
FROM
stake_address
WHERE
stake_address.view = ANY(_stake_addresses);
RETURN QUERY

WITH _all_assets AS (
SELECT
sa.view,
Expand All @@ -23,39 +20,26 @@ BEGIN
ma.fingerprint,
COALESCE(aic.decimals, 0) AS decimals,
SUM(mtx.quantity) AS quantity
FROM
ma_tx_out AS mtx
INNER JOIN multi_asset AS ma ON ma.id = mtx.ident
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
LEFT JOIN grest.asset_info_cache AS aic ON aic.asset_id = ma.id
WHERE
sa.id = ANY(sa_id_list)
AND tx_out.consumed_by_tx_in_id IS NULL
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
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.view = ANY(_stake_addresses)
AND txo.consumed_by_tx_in_id IS NULL
GROUP BY
sa.view, ma.policy, ma.name, ma.fingerprint, aic.decimals
)

SELECT
assets_grouped.view AS stake_address,
assets_grouped.assets
FROM (
SELECT
aa.view,
JSONB_AGG(
JSONB_BUILD_OBJECT(
'policy_id', ENCODE(aa.policy, 'hex'),
'asset_name', ENCODE(aa.name, 'hex'),
'fingerprint', aa.fingerprint,
'decimals', COALESCE(aa.decimals, 0),
'quantity', aa.quantity::text
)
) AS assets
FROM
_all_assets AS aa
GROUP BY
aa.view
) AS assets_grouped;
aa.view AS stake_address,
ENCODE(aa.policy, 'hex') AS policy_id,
ENCODE(aa.name, 'hex') AS asset_name,
aa.fingerprint AS fingerprint,
aa.decimals AS decimals,
aa.quantity::text AS quantity
FROM _all_assets AS aa
ORDER BY aa.view;
END;
$$;

Expand Down
61 changes: 28 additions & 33 deletions files/grest/rpc/address/address_assets.sql
Original file line number Diff line number Diff line change
@@ -1,49 +1,44 @@
CREATE OR REPLACE FUNCTION grest.address_assets(_addresses text [])
RETURNS TABLE (
address varchar,
asset_list jsonb
policy_id text,
asset_name text,
fingerprint varchar,
decimals integer,
quantity text
)
LANGUAGE plpgsql
AS $$
BEGIN
RETURN QUERY

WITH _all_assets AS (
SELECT
txo.address,
ma.policy,
ma.name,
ma.fingerprint,
COALESCE(aic.decimals, 0) AS decimals,
SUM(mtx.quantity) AS quantity
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
INNER JOIN tx_out AS txo ON txo.id = mtx.tx_out_id
WHERE txo.address = ANY(_addresses)
AND tx_out.consumed_by_tx_in_id IS NULL
GROUP BY
txo.address, ma.policy, ma.name, ma.fingerprint, aic.decimals
)
WITH _all_assets AS (
SELECT
txo.address,
ma.policy,
ma.name,
ma.fingerprint,
COALESCE(aic.decimals, 0) AS decimals,
SUM(mtx.quantity) AS quantity
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
INNER JOIN tx_out AS txo ON txo.id = mtx.tx_out_id
WHERE txo.address = ANY(_addresses)
AND txo.consumed_by_tx_in_id IS NULL
GROUP BY
txo.address, ma.policy, ma.name, ma.fingerprint, aic.decimals
)

SELECT
assets_grouped.address,
assets_grouped.asset_list
FROM (
SELECT
aa.address,
JSONB_AGG(
JSONB_BUILD_OBJECT(
'policy_id', ENCODE(aa.policy, 'hex'),
'asset_name', ENCODE(aa.name, 'hex'),
'fingerprint', aa.fingerprint,
'decimals', aa.decimals,
'quantity', aa.quantity::text
)
) AS asset_list
ENCODE(aa.policy, 'hex') AS policy_id,
ENCODE(aa.name, 'hex') AS asset_name,
aa.fingerprint AS fingerprint,
aa.decimals AS decimals,
aa.quantity::text AS quantity
FROM _all_assets AS aa
GROUP BY aa.address
) assets_grouped;
ORDER BY aa.address;
END;
$$;

Expand Down

0 comments on commit 0938e84

Please sign in to comment.