From 3212046ff1ef0e34f7c3151ef49a9317b6fb374b Mon Sep 17 00:00:00 2001 From: Ivan Ivanov Date: Fri, 24 Nov 2023 15:48:35 +0200 Subject: [PATCH] Migrate ExchangeAddress to use labeling framework v2 --- lib/sanbase/clickhouse/exchange_address.ex | 121 +++++++++------------ 1 file changed, 52 insertions(+), 69 deletions(-) diff --git a/lib/sanbase/clickhouse/exchange_address.ex b/lib/sanbase/clickhouse/exchange_address.ex index 2e05085e4f..a6d9a3cd9b 100644 --- a/lib/sanbase/clickhouse/exchange_address.ex +++ b/lib/sanbase/clickhouse/exchange_address.ex @@ -67,45 +67,18 @@ defmodule Sanbase.Clickhouse.ExchangeAddress do end sql = """ - SELECT address, owner FROM - ( - SELECT address, owner - FROM - ( - SELECT address, label_id + SELECT DISTINCT dictGet('labels', 'value', label_id) + FROM current_label_addresses + WHERE + blockchain = {{blockchain}} AND + label_id IN ( SELECT label_id FROM label_metadata WHERE key = 'owner' ) AND + address IN ( + SELECT DISTINCT address FROM current_label_addresses WHERE - label_id IN ( SELECT label_id FROM label_metadata WHERE key = 'owner' ) AND - blockchain = {{blockchain}} + blockchain = {{blockchain}} AND + #{exchange_type_filter(exchange_type)} ) - INNER JOIN - ( - SELECT label_id, value AS owner - FROM label_metadata - WHERE key = 'owner' - ) USING (label_id) - ) - INNER JOIN - ( - SELECT address - FROM current_label_addresses - WHERE #{exchange_type_filter(exchange_type)} AND blockchain = {{blockchain}} - ) USING (address) - - """ - - sql = """ - SELECT DISTINCT lower(JSONExtractString(metadata, 'owner')) AS exchange - FROM ( - SELECT argMax(metadata, version) AS metadata, argMax(sign, version) AS sign - FROM blockchain_address_labels - PREWHERE - blockchain = {{blockchain}} AND - #{exchange_type_filter(exchange_type)} - GROUP BY blockchain, asset_id, label, address - HAVING sign = 1 - ) - ORDER BY exchange """ params = %{ @@ -117,15 +90,23 @@ defmodule Sanbase.Clickhouse.ExchangeAddress do defp exchange_addresses_query(blockchain, limit) do sql = """ - SELECT DISTINCT(address), label, lower(JSONExtractString(metadata, 'owner')) AS owner - FROM( - SELECT address, label, argMax(metadata, version) AS metadata, argMax(sign, version) AS sign - FROM blockchain_address_labels - PREWHERE blockchain = {{blockchain}} AND #{exchange_type_filter(:both)} - GROUP BY blockchain, asset_id, label, address - HAVING sign = 1 - ) - LIMIT {{limit}} + SELECT address, label, owner FROM ( + SELECT address, owner FROM ( + SELECT address, label_id FROM current_label_addresses + WHERE + label_id IN ( SELECT label_id FROM label_metadata WHERE key = 'owner' ) AND + blockchain = {{blockchain}} + ) INNER JOIN ( + SELECT label_id, value AS owner + FROM label_metadata + WHERE key = 'owner' + ) USING label_id + ) INNER JOIN ( + SELECT address, dictGet('labels', 'key', label_id) AS label + FROM current_label_addresses + WHERE #{exchange_type_filter(:both)} + AND blockchain = {{blockchain}} + ) USING address """ params = %{ @@ -138,17 +119,23 @@ defmodule Sanbase.Clickhouse.ExchangeAddress do defp exchange_addresses_for_exchange_query(blockchain, owner, limit) do sql = """ - SELECT DISTINCT(address), label, lower(JSONExtractString(metadata, 'owner')) AS owner - FROM( - SELECT address, label, argMax(metadata, version) AS metadata, argMax(sign, version) AS sign - FROM blockchain_address_labels - PREWHERE - blockchain = {{blockchain}} AND - lower(JSONExtractString(metadata, 'owner')) = {{owner}} AND - #{exchange_type_filter(:both)} - GROUP BY blockchain, asset_id, label, address - HAVING sign = 1 - ) + SELECT DISTINCT address FROM ( + SELECT address, owner FROM ( + SELECT address, label_id FROM current_label_addresses + WHERE + label_id IN ( SELECT label_id FROM label_metadata WHERE key = 'owner' AND value = {{owner}}) AND + blockchain = {{blockchain}} + ) INNER JOIN ( + SELECT label_id, value AS owner + FROM label_metadata + WHERE key = 'owner' + ) USING label_id + ) INNER JOIN ( + SELECT address, dictGet('labels', 'key', label_id) AS label + FROM current_label_addresses + WHERE #{exchange_type_filter(:both)} + AND blockchain = {{blockchain}} + ) USING address LIMIT {{limit}} """ @@ -161,20 +148,16 @@ defmodule Sanbase.Clickhouse.ExchangeAddress do Sanbase.Clickhouse.Query.new(sql, params) end - defp exchange_type_filter(:cex) do - "label_id = dictGet('default.labels_by_fqn', 'label_id', tuple('santiment/centralized_exchange:v1')))" - end + defp exchange_type_filter(type) when type in [:cex, :dex, :both] do + case type do + :cex -> + "label_id = (SELECT label_id FROM label_metadata WHERE key = 'centralized_exchange' LIMIT 1)" - defp exchange_type_filter(:dex) do - "label_id = dictGet('default.labels_by_fqn', 'label_id', tuple('santiment/decentralized_exchange:v1')))" - end + :dex -> + "label_id = (SELECT label_id FROM label_metadata WHERE key = 'decentralized_exchange' LIMIT 1)" - defp exchange_type_filter(:both) do - """ - label IN ( - dictGet('default.labels_by_fqn', 'label_id', tuple('santiment/centralized_exchange:v1'))), - dictGet('default.labels_by_fqn', 'label_id', tuple('santiment/decentralized_exchange:v1'))) - ) - """ + :both -> + "label_id IN (SELECT label_id FROM label_metadata WHERE key IN ('centralized_exchange', 'decentralized_exchange'))" + end end end