|
|
@ -7,7 +7,7 @@ defmodule Explorer.Chain.Address.CoinBalance do |
|
|
|
use Explorer.Schema |
|
|
|
use Explorer.Schema |
|
|
|
|
|
|
|
|
|
|
|
alias Explorer.PagingOptions |
|
|
|
alias Explorer.PagingOptions |
|
|
|
alias Explorer.Chain.{Address, Block, Hash, Wei} |
|
|
|
alias Explorer.Chain.{Address, Block, Hash, Transaction, Wei} |
|
|
|
alias Explorer.Chain.Address.CoinBalance |
|
|
|
alias Explorer.Chain.Address.CoinBalance |
|
|
|
|
|
|
|
|
|
|
|
@optional_fields ~w(value value_fetched_at)a |
|
|
|
@optional_fields ~w(value value_fetched_at)a |
|
|
@ -42,6 +42,8 @@ defmodule Explorer.Chain.Address.CoinBalance do |
|
|
|
field(:value, Wei) |
|
|
|
field(:value, Wei) |
|
|
|
field(:value_fetched_at, :utc_datetime_usec) |
|
|
|
field(:value_fetched_at, :utc_datetime_usec) |
|
|
|
field(:delta, Wei, virtual: true) |
|
|
|
field(:delta, Wei, virtual: true) |
|
|
|
|
|
|
|
field(:transaction_hash, Hash.Full, virtual: true) |
|
|
|
|
|
|
|
field(:transaction_value, Wei, virtual: true) |
|
|
|
field(:block_timestamp, :utc_datetime_usec, virtual: true) |
|
|
|
field(:block_timestamp, :utc_datetime_usec, virtual: true) |
|
|
|
|
|
|
|
|
|
|
|
timestamps() |
|
|
|
timestamps() |
|
|
@ -82,10 +84,18 @@ defmodule Explorer.Chain.Address.CoinBalance do |
|
|
|
query = |
|
|
|
query = |
|
|
|
from( |
|
|
|
from( |
|
|
|
cb in CoinBalance, |
|
|
|
cb in CoinBalance, |
|
|
|
|
|
|
|
left_join: tx in Transaction, |
|
|
|
|
|
|
|
on: |
|
|
|
|
|
|
|
cb.block_number == tx.block_number and tx.value > ^0 and |
|
|
|
|
|
|
|
(cb.address_hash == tx.to_address_hash or cb.address_hash == tx.from_address_hash), |
|
|
|
where: cb.address_hash == ^address_hash, |
|
|
|
where: cb.address_hash == ^address_hash, |
|
|
|
where: not is_nil(cb.value), |
|
|
|
where: not is_nil(cb.value), |
|
|
|
order_by: [desc: :block_number], |
|
|
|
order_by: [desc: :block_number], |
|
|
|
select_merge: %{delta: fragment("value - coalesce(lead(value, 1) over (order by block_number desc), 0)")} |
|
|
|
select_merge: %{ |
|
|
|
|
|
|
|
delta: fragment("a0.value - coalesce(lead(a0.value, 1) over (order by a0.block_number desc), 0)"), |
|
|
|
|
|
|
|
transaction_hash: tx.hash, |
|
|
|
|
|
|
|
transaction_value: tx.value |
|
|
|
|
|
|
|
} |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
from(balance in subquery(query), |
|
|
|
from(balance in subquery(query), |
|
|
|