Skip to content

Commit

Permalink
Improve query speed and add to examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tspenov committed Nov 13, 2023
1 parent 2a49e0f commit 81f7680
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
22 changes: 13 additions & 9 deletions lib/sanbase/balances/balance.ex
Original file line number Diff line number Diff line change
Expand Up @@ -307,18 +307,22 @@ defmodule Sanbase.Balance do
def current_balance_top_addresses(slug, opts) do
with {:ok, {decimals, infrastructure, blockchain}} <- info_by_slug(slug),
{:ok, table} <- realtime_balances_table(slug, infrastructure) do
query_struct = top_addresses_query(slug, decimals, blockchain, table, opts)

ClickhouseRepo.query_transform(query_struct, fn [address, balance] ->
%{
address: address,
infrastructure: infrastructure,
balance: balance
}
end)
current_balance_top_addresses(slug, decimals, infrastructure, blockchain, table, opts)
end
end

def current_balance_top_addresses(slug, decimals, infrastructure, blockchain, table, opts) do
query_struct = top_addresses_query(slug, decimals, blockchain, table, opts)

ClickhouseRepo.query_transform(query_struct, fn [address, balance] ->
%{
address: address,
infrastructure: infrastructure,
balance: balance
}
end)
end

def realtime_balances_table_or_nil(slug, infr) do
case realtime_balances_table(slug, infr) do
{:ok, table} -> {:ok, table}
Expand Down
23 changes: 15 additions & 8 deletions lib/sanbase/balances/balance_sql_query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -359,16 +359,23 @@ defmodule Sanbase.Balance.SqlQuery do
sql = """
SELECT address, balance
FROM (
SELECT address, argMax(balance, dt) / pow(10, {{decimals}}) AS balance
FROM #{table}
PREWHERE
addressType = 'normal'
GROUP BY address
SELECT
ebr.address,
argMax(ebr.balance, ebr.dt) / pow(10, {{decimals}}) AS balance
FROM #{table} AS ebr
WHERE (ebr.address GLOBAL IN (
SELECT address
FROM eth_top_holders_daily
WHERE value > 1e10 AND(dt = toStartOfDay(today() - toIntervalDay(1))) AND (rank > 0)
ORDER BY value #{direction}
LIMIT {{limit}}*2
)) AND (ebr.addressType = 'normal')
GROUP BY ebr.address
)
#{labels_join_str}
WHERE balance > 1e-10
ORDER BY balance #{direction}
LIMIT {{limit}} OFFSET {{offset}}
LIMIT {{limit}}
OFFSET {{offset}}
"""

Sanbase.Clickhouse.Query.new(sql, params)
Expand Down Expand Up @@ -397,7 +404,7 @@ defmodule Sanbase.Balance.SqlQuery do
FROM #{table}
PREWHERE
assetRefId = (SELECT asset_ref_id FROM asset_metadata FINAL WHERE name = {{slug}} LIMIT 1) AND
addressType = 'normal'
addressType = 'normal' AND (dt > (now() - toIntervalDay(1)))
GROUP BY address
)
#{labels_join_str}
Expand Down
15 changes: 14 additions & 1 deletion lib/sanbase/run_examples.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ defmodule Sanbase.RunExamples do
:transfers,
:san_burn_credit_transactions,
:signals,
:additional_filters
:additional_filters,
:top_addresses
]

@from ~U[2023-01-01 00:00:00Z]
Expand Down Expand Up @@ -523,6 +524,18 @@ defmodule Sanbase.RunExamples do
infrastructure: "ETH",
address: @null_address
})
end

defp do_run(:top_addresses) do
{:ok, _} =
Sanbase.Balance.current_balance_top_addresses(
"ethereum",
18,
"ETH",
"ethereum",
"eth_balances_realtime",
labels: ["whale_usd_balance"]
)

{:ok, :success}
end
Expand Down

0 comments on commit 81f7680

Please sign in to comment.