diff --git a/files/grest/rpc/db-scripts/asset_cache_control.sql b/files/grest/rpc/01_cached_tables/asset_cache_control.sql similarity index 100% rename from files/grest/rpc/db-scripts/asset_cache_control.sql rename to files/grest/rpc/01_cached_tables/asset_cache_control.sql diff --git a/files/grest/rpc/assets/asset_nft_address.sql b/files/grest/rpc/assets/asset_nft_address.sql index 221d6cc4..f4251395 100644 --- a/files/grest/rpc/assets/asset_nft_address.sql +++ b/files/grest/rpc/assets/asset_nft_address.sql @@ -23,14 +23,25 @@ BEGIN 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 - ); + IF EXISTS (SELECT * FROM ma_tx_mint WHERE ident = _asset_id and quantity < 0 LIMIT 1) THEN + RETURN QUERY + SELECT address + FROM tx_out + WHERE id = ( + SELECT MAX(tx_out_id) + FROM ma_tx_out + WHERE ident = _asset_id + ); + ELSE + RETURN QUERY + SELECT address + FROM tx_out + INNER JOIN ma_tx_out mto ON mto.tx_out_id = tx_out.id + WHERE mto.ident = _asset_id + AND tx_out.consumed_by_tx_in_id IS NULL + ORDER BY tx_out.id DESC + LIMIT 1; + END IF; END; $$; diff --git a/files/grest/rpc/db-scripts/basics.sql b/files/grest/rpc/db-scripts/basics.sql index a002738e..8cd57625 100644 --- a/files/grest/rpc/db-scripts/basics.sql +++ b/files/grest/rpc/db-scripts/basics.sql @@ -115,6 +115,11 @@ BEGIN END $do$; +-- DROP EXISTING GREST ADDED TRIGGERS ON PUBLIC SCHEMA +FOR r IN (SELECT trigger_name, event_object_table FROM information_schema.triggers WHERE trigger_schema = 'public' AND action_statement LIKE '%grest.%') LOOP + EXECUTE 'DROP TRIGGER IF EXISTS ' || quote_ident(r.trigger_name) || ' ON ' || quote_ident(r.event_object_table); +END LOOP; + -- HELPER FUNCTIONS -- CREATE FUNCTION grest.get_query_pids_partial_match(_query text) RETURNS TABLE ( diff --git a/files/grest/rpc/db-scripts/reset_grest.sql b/files/grest/rpc/db-scripts/reset_grest.sql index 48a26cb2..5bf5ab9a 100644 --- a/files/grest/rpc/db-scripts/reset_grest.sql +++ b/files/grest/rpc/db-scripts/reset_grest.sql @@ -1,8 +1,3 @@ --- Drop triggers first that depend on grest.functions() -SELECT 'DROP TRIGGER ' || trigger_name || ' ON ' || event_object_table || ';' -FROM information_schema.triggers -WHERE trigger_schema = 'public'; - -- Recreate grest schema DROP SCHEMA IF EXISTS grest CASCADE;