Skip to content

Commit

Permalink
Migrate ExchangeAddress to use labeling framework v2
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanIvanoff committed Nov 24, 2023
1 parent 7139dd7 commit 3212046
Showing 1 changed file with 52 additions and 69 deletions.
121 changes: 52 additions & 69 deletions lib/sanbase/clickhouse/exchange_address.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 = %{
Expand All @@ -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 = %{
Expand All @@ -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}}
"""

Expand All @@ -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

0 comments on commit 3212046

Please sign in to comment.