Update views for pending transactions.

pull/2/head
CJ Bryan and Matt Olenick 7 years ago
parent 85d5149ea0
commit f4c9030497
  1. 5
      lib/explorer_web/controllers/chain_controller.ex
  2. 4
      lib/explorer_web/templates/chain/show.html.eex
  3. 107
      test/explorer/forms/transaction_form_test.exs
  4. 2
      test/explorer_web/controllers/chain_controller_test.exs

@ -7,7 +7,6 @@ defmodule ExplorerWeb.ChainController do
alias Explorer.BlockForm
alias Explorer.Repo.NewRelic, as: Repo
alias Explorer.Transaction
alias Explorer.TransactionForm
def show(conn, _params) do
blocks = from b in Block,
@ -26,9 +25,7 @@ defmodule ExplorerWeb.ChainController do
conn,
"show.html",
blocks: blocks |> Repo.all |> Enum.map(&BlockForm.build/1),
transactions: transactions
|> Repo.all
|> Enum.map(&TransactionForm.build/1)
transactions: transactions |> Repo.all
)
end
end

@ -48,8 +48,8 @@
<%= link(transaction.hash, to: transaction_path(@conn, :show, @conn.assigns.locale, transaction.hash), class: "blocks__link") %>
</div>
</td>
<td class="transactions__column transactions__column--block"><%= transaction.block.number %></td>
<td class="transactions__column transactions__column--age"><%= transaction.block.timestamp |> Timex.from_now %></td>
<td class="transactions__column transactions__column--block"><%= transaction.block && transaction.block.number || "" %></td>
<td class="transactions__column transactions__column--age"><%= transaction.block && transaction.block.timestamp |> Timex.from_now || "" %></td>
<td class="transactions__column transactions__column--value"><%= Decimal.div(Decimal.new(transaction.value), Decimal.new(1_000_000_000_000_000_000)) |> Decimal.to_string(:normal) %> <%= gettext "POA" %></td>
</tr>
<% end %>

@ -2,100 +2,45 @@ defmodule Explorer.TransactionFormTest do
use Explorer.DataCase
alias Explorer.TransactionForm
describe "build/1" do
setup _context do
insert(:block, %{number: 24})
date = "Feb-02-2010 10:48:56 AM Etc/UTC"
describe "build/1 when the transaction has a block" do
test "that it returns the values we expect" do
insert(:block, number: 24)
time = Timex.now |> Timex.shift(hours: -2)
block = insert(:block, %{
number: 1,
gas_used: 99523,
timestamp: Timex.parse!(date, "%b-%d-%Y %H:%M:%S %p %Z", :strftime),
timestamp: time,
})
transaction =
insert(:transaction)
|> with_block(block)
|> with_addresses(%{to: "0xsleepypuppy", from: "0xilovefrogs"})
form = TransactionForm.build(transaction)
{:ok, %{form: form}}
end
test "that it has a block number when it has a block", %{form: form} do
assert form.block_number == 1
end
test "shows a blank block number when the transaction is pending" do
transaction = insert(:transaction) |> with_addresses
assert TransactionForm.build(transaction).block_number == ""
end
test "that it returns the block's age when has a block" do
block = insert(:block, %{
number: 1,
gas_used: 99523,
timestamp: Timex.now |> Timex.shift(hours: -2),
})
transaction = insert(:transaction) |> with_block(block) |> with_addresses(%{to: "0xsiskelnebert", from: "0xleonardmaltin"})
assert TransactionForm.build(transaction).age == "2 hours ago"
end
test "that it has an empty age when it is pending" do
transaction = insert(:transaction) |> with_addresses
assert TransactionForm.build(transaction).age == ""
end
test "formats the timestamp when it has a block", %{form: form} do
assert form.formatted_timestamp == "Feb-02-2010 10:48:56 AM Etc/UTC"
end
test "formats the timestamp when the transaction is pending" do
transaction = insert(:transaction) |> with_addresses
assert TransactionForm.build(transaction).formatted_timestamp == ""
end
test "that it returns the cumulative gas used for validating the block", %{form: form} do
assert form.cumulative_gas_used == "99,523"
end
test "shows the cumulative gas used for a pending transaction" do
transaction = insert(:transaction) |> with_addresses
assert TransactionForm.build(transaction).cumulative_gas_used == ""
end
test "that it returns a 'to address'", %{form: form} do
assert form.to_address == "0xsleepypuppy"
end
test "that it returns a 'from address'", %{form: form} do
assert form.from_address == "0xilovefrogs"
end
test "that it returns confirmations", %{form: form} do
assert form.confirmations == 23
assert(form == Map.merge(transaction, %{
block_number: 1,
age: "2 hours ago",
formatted_timestamp: block.timestamp |> Timex.format!("%b-%d-%Y %H:%M:%S %p %Z", :strftime),
cumulative_gas_used: "99,523",
to_address: "0xsleepypuppy",
from_address: "0xilovefrogs",
confirmations: 23,
}))
end
test "shows confirmations when the transaction is pending" do
transaction = insert(:transaction) |> with_addresses
assert TransactionForm.build(transaction).confirmations == 0
end
end
describe "cumulative_gas_used/1" do
test "when there is a block" do
block = insert(:block, %{gas_used: 1_000})
assert TransactionForm.cumulative_gas_used(block) == "1,000"
end
test "when there is not a block" do
assert TransactionForm.cumulative_gas_used(nil) == ""
end
end
test "works when there is no block" do
transaction = insert(:transaction) |> with_addresses(%{to: "0xchadmuska", from: "0xtonyhawk"})
form = TransactionForm.build(transaction)
describe "confirmations/1" do
test "when there is only one block" do
block = insert(:block, %{number: 1})
insert(:transaction) |> with_block(block) |> with_addresses
assert TransactionForm.confirmations(block) == 0
assert(form == Map.merge(transaction, %{
block_number: "",
age: "",
formatted_timestamp: "",
cumulative_gas_used: "",
to_address: "0xchadmuska",
from_address: "0xtonyhawk",
confirmations: 0,
}))
end
end
end

@ -35,7 +35,7 @@ defmodule ExplorerWeb.ChainControllerTest do
conn = get conn, "/en"
assert(List.first(conn.assigns.transactions).hash == "0xDECAFBAD")
assert(List.first(conn.assigns.transactions).block_number == 33)
assert(List.first(conn.assigns.transactions).block.number == 33)
end
end
end

Loading…
Cancel
Save