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/transaction_form.ex

82 lines
2.3 KiB

defmodule Explorer.TransactionForm do
alias Cldr.Number
alias Explorer.Address
alias Explorer.Block
alias Explorer.BlockTransaction
alias Explorer.FromAddress
alias Explorer.Repo
alias Explorer.ToAddress
alias Explorer.Transaction
import Ecto.Query
@moduledoc "Format a Block and a Transaction for display."
def build(transaction) do
block = with_block(transaction)
transaction
|> Map.merge(%{
block_number: block |> block_number,
age: block |> block_age,
formatted_timestamp: block |> format_timestamp,
cumulative_gas_used: block |> cumulative_gas_used,
to_address: transaction |> to_address,
from_address: transaction |> from_address,
confirmations: block |> confirmations,
})
end
def with_block(transaction) do
Repo.one(
from block in Block,
join: block_transaction in BlockTransaction,
where: block_transaction.block_id == block.id,
join: transaction in Transaction,
where: transaction.id == block_transaction.transaction_id,
where: transaction.id == ^transaction.id
)
end
def block_number(block) do
block && block.number || ""
end
def block_age(block) do
block && block.timestamp |> Timex.from_now || ""
end
def format_timestamp(block) do
block && block.timestamp |> Timex.format!("%b-%d-%Y %H:%M:%S %p %Z", :strftime) || ""
end
def cumulative_gas_used(block) do
block && block.gas_used |> Number.to_string! || ""
end
def to_address(transaction) do
query = from address in Address,
join: to_address in ToAddress,
where: to_address.address_id == address.id,
join: transaction in Transaction,
where: transaction.id == to_address.transaction_id,
where: transaction.id == ^transaction.id
Repo.one(query).hash
end
def from_address(transaction) do
query = from address in Address,
join: from_address in FromAddress,
where: from_address.address_id == address.id,
join: transaction in Transaction,
where: transaction.id == from_address.transaction_id,
where: transaction.id == ^transaction.id
Repo.one(query).hash
end
def confirmations(block) do
query = from block in Block, select: max(block.number)
block && Repo.one(query) - block.number || 0
end
end