Determine Transaction status based on block presence.

pull/2/head
CJ Bryan and Matt Olenick 7 years ago
parent f4c9030497
commit 1971309d22
  1. 35
      lib/explorer/forms/transaction_form.ex
  2. 13
      lib/explorer_web/controllers/transaction_controller.ex
  3. 2
      lib/explorer_web/templates/transaction/show.html.eex
  4. 6
      priv/gettext/default.pot
  5. 6
      priv/gettext/en/LC_MESSAGES/default.po
  6. 8
      test/explorer/forms/transaction_form_test.exs
  7. 1
      test/explorer_web/features/contributor_browsing_test.exs

@ -1,21 +1,20 @@
defmodule Explorer.TransactionForm do
@moduledoc "Format a Block and a Transaction for display."
import Ecto.Query
import ExplorerWeb.Gettext
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 = transaction.block
Map.merge(transaction, %{
block_number: block |> block_number,
age: block |> block_age,
formatted_timestamp: block |> format_timestamp,
@ -23,20 +22,10 @@ defmodule Explorer.TransactionForm do
to_address: transaction |> to_address,
from_address: transaction |> from_address,
confirmations: block |> confirmations,
status: transaction |> status,
})
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
@ -79,4 +68,12 @@ defmodule Explorer.TransactionForm do
query = from block in Block, select: max(block.number)
block && Repo.one(query) - block.number || 0
end
def status(transaction) do
if transaction.block do
gettext("Success")
else
gettext("Pending")
end
end
end

@ -20,11 +20,14 @@ defmodule ExplorerWeb.TransactionController do
end
def show(conn, params) do
transaction = Transaction
|> where(hash: ^params["id"])
|> first
|> Repo.one
|> TransactionForm.build
query = from transaction in Transaction,
left_join: block_transaction in assoc(transaction, :block_transaction),
left_join: block in assoc(block_transaction, :block),
preload: [block_transaction: block_transaction, block: block],
where: transaction.hash == ^params["id"],
limit: 1
transaction = query |> Repo.one |> TransactionForm.build
render(conn, "show.html", transaction: transaction)
end

@ -16,7 +16,7 @@
</div>
<div class="transaction__item">
<dt class="transaction__item-key"><%= gettext "Transaction Status" %></dt>
<dd class="transaction__item-value transaction__item-value--success"><%= gettext "Success" %></dd>
<dd class="transaction__item-value transaction__item-value--status"><%= @transaction.status %></dd>
</div>
<div class="transaction__item">
<dt class="transaction__item-key"><%= gettext "Block Number" %></dt>

@ -146,7 +146,7 @@ msgstr ""
msgid "Overview"
msgstr ""
#: lib/explorer_web/templates/transaction/show.html.eex:19
#: lib/explorer/forms/transaction_form.ex:75
msgid "Success"
msgstr ""
@ -184,3 +184,7 @@ msgstr ""
#: lib/explorer_web/templates/transaction/index.html.eex:3
msgid "Showing %{count} Transactions"
msgstr ""
#: lib/explorer/forms/transaction_form.ex:77
msgid "Pending"
msgstr ""

@ -158,7 +158,7 @@ msgstr "From"
msgid "Overview"
msgstr "Overview"
#: lib/explorer_web/templates/transaction/show.html.eex:19
#: lib/explorer/forms/transaction_form.ex:75
msgid "Success"
msgstr "Success"
@ -196,3 +196,7 @@ msgstr "Showing #%{start_block} to #%{end_block}"
#: lib/explorer_web/templates/transaction/index.html.eex:3
msgid "Showing %{count} Transactions"
msgstr "Showing %{count} Transactions"
#: lib/explorer/forms/transaction_form.ex:77
msgid "Pending"
msgstr "Pending"

@ -1,8 +1,9 @@
defmodule Explorer.TransactionFormTest do
use Explorer.DataCase
alias Explorer.TransactionForm
describe "build/1 when the transaction has a block" do
describe "build/1" do
test "that it returns the values we expect" do
insert(:block, number: 24)
time = Timex.now |> Timex.shift(hours: -2)
@ -15,6 +16,7 @@ defmodule Explorer.TransactionFormTest do
insert(:transaction)
|> with_block(block)
|> with_addresses(%{to: "0xsleepypuppy", from: "0xilovefrogs"})
|> Repo.preload(:block)
form = TransactionForm.build(transaction)
assert(form == Map.merge(transaction, %{
@ -25,11 +27,12 @@ defmodule Explorer.TransactionFormTest do
to_address: "0xsleepypuppy",
from_address: "0xilovefrogs",
confirmations: 23,
status: "Success",
}))
end
test "works when there is no block" do
transaction = insert(:transaction) |> with_addresses(%{to: "0xchadmuska", from: "0xtonyhawk"})
transaction = insert(:transaction) |> with_addresses(%{to: "0xchadmuska", from: "0xtonyhawk"}) |> Repo.preload(:block)
form = TransactionForm.build(transaction)
assert(form == Map.merge(transaction, %{
@ -40,6 +43,7 @@ defmodule Explorer.TransactionFormTest do
to_address: "0xchadmuska",
from_address: "0xtonyhawk",
confirmations: 0,
status: "Pending",
}))
end
end

@ -81,6 +81,7 @@ defmodule ExplorerWeb.UserListTest do
|> assert_has(css(".transaction__subheading", text: "0xSk8"))
|> assert_has(css(".transaction__item", text: "123,987"))
|> assert_has(css(".transaction__item", text: "5656 POA"))
|> assert_has(css(".transaction__item", text: "Success"))
|> assert_has(css(".transaction__item", text: "7,890,000,000,898,912,300,045"))
|> assert_has(css(".transaction__item", text: "1,230,000,000,000,123,123"))
|> assert_has(css(".transaction__item", text: "0x00012"))

Loading…
Cancel
Save