Blockchain explorer for Ethereum based network and a tool for inspecting and analyzing EVM based blockchains.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
blockscout/lib/explorer/forms/address_form.ex

42 lines
1.1 KiB

defmodule Explorer.AddressForm do
@moduledoc false
alias Explorer.Address
alias Explorer.FromAddress
alias Explorer.Repo
alias Explorer.ToAddress
alias Explorer.Transaction
import Ecto.Query
def build(address) do
address
|> Map.merge(%{
balance: address |> calculate_balance,
})
end
def calculate_balance(address) do
Decimal.sub(credits(address), debits(address))
end
def credits(address) do
query = from transaction in Transaction,
join: to_address in ToAddress,
where: to_address.transaction_id == transaction.id,
join: address in Address,
where: to_address.address_id == address.id,
select: sum(transaction.value),
where: address.id == ^address.id
Repo.one(query) || Decimal.new(0)
end
def debits(address) do
query = from transaction in Transaction,
join: from_address in FromAddress,
where: from_address.transaction_id == transaction.id,
join: address in Address,
where: from_address.address_id == address.id,
select: sum(transaction.value),
where: address.id == ^address.id
Repo.one(query) || Decimal.new(0)
end
end