@ -104,19 +104,19 @@ defmodule Explorer.Chain.Address.CoinBalance do
Application . get_env ( :block_scout_web , BlockScoutWeb.Chain.Address.CoinBalance ) [ :coin_balance_history_days ]
CoinBalance
|> join ( :inner , [ cb ] , b in Block , on : cb . block_number == b . number )
|> join ( :inner , [ cb ] , block in Block , on : cb . block_number == block . number )
|> where ( [ cb ] , cb . address_hash == ^ address_hash )
|> limit_time_interval ( days_to_consider , block_timestamp )
|> group_by ( [ cb , b ] , fragment ( " date_trunc('day', ?) " , b . timestamp ) )
|> order_by ( [ cb , b ] , fragment ( " date_trunc('day', ?) " , b . timestamp ) )
|> select ( [ cb , b ] , %{ date : type ( fragment ( " date_trunc('day', ?) " , b . timestamp ) , :date ) , value : max ( cb . value ) } )
|> group_by ( [ cb , block ] , fragment ( " date_trunc('day', ?) " , block . timestamp ) )
|> order_by ( [ cb , block ] , fragment ( " date_trunc('day', ?) " , block . timestamp ) )
|> select ( [ cb , block ] , %{ date : type ( fragment ( " date_trunc('day', ?) " , block . timestamp ) , :date ) , value : max ( cb . value ) } )
end
def limit_time_interval ( query , days_to_consider , nil ) do
query
|> where (
[ cb , b ] ,
b . timestamp >=
[ cb , block ] ,
block . timestamp >=
fragment ( " date_trunc('day', now() - CAST(? AS INTERVAL)) " , ^ % Postgrex.Interval { days : days_to_consider } )
)
end
@ -124,8 +124,8 @@ defmodule Explorer.Chain.Address.CoinBalance do
def limit_time_interval ( query , days_to_consider , %{ timestamp : timestamp } ) do
query
|> where (
[ cb , b ] ,
b . timestamp >=
[ cb , block ] ,
block . timestamp >=
fragment (
" (? AT TIME ZONE ?) - CAST(? AS INTERVAL) " ,
^ timestamp ,
@ -146,7 +146,7 @@ defmodule Explorer.Chain.Address.CoinBalance do
cb in subquery ( coin_balance_query ) ,
inner_join : block in Block ,
on : cb . block_number == block . number ,
where : block . consensus ,
where : block . consensus == true ,
select : %{ timestamp : block . timestamp , value : cb . value }
)
end