Render token transfers info according to the token's type

pull/495/head
Felipe Renan 6 years ago
parent 265e5ff188
commit 4f07176e08
  1. 9
      apps/explorer_web/lib/explorer_web.ex
  2. 2
      apps/explorer_web/lib/explorer_web/templates/address_transaction/_transaction.html.eex
  3. 60
      apps/explorer_web/lib/explorer_web/views/token_helpers.ex
  4. 5
      apps/explorer_web/test/explorer_web/views/address_transaction_view_test.exs
  5. 62
      apps/explorer_web/test/explorer_web/views/token_helpers_test.exs

@ -41,7 +41,14 @@ defmodule ExplorerWeb do
# Use all HTML functionality (forms, tags, etc) # Use all HTML functionality (forms, tags, etc)
use Phoenix.HTML use Phoenix.HTML
import ExplorerWeb.{CurrencyHelpers, ErrorHelpers, Gettext, Router.Helpers, WeiHelpers} import ExplorerWeb.{
CurrencyHelpers,
ErrorHelpers,
Gettext,
Router.Helpers,
TokenHelpers,
WeiHelpers
}
end end
end end

@ -77,7 +77,7 @@
</span> </span>
</span> </span>
<span class="tile-title"> <span class="tile-title">
<%= format_according_to_decimals(token_transfer.amount, token_transfer.token.decimals) %> <%= token_transfer.token.symbol %> <%= token_transfer_amount(token_transfer) %> <%= token_symbol(token_transfer.token) %>
</span> </span>
</div> </div>
<div class="col-md-3 col-lg-2 d-flex flex-row flex-md-column align-items-end"> <div class="col-md-3 col-lg-2 d-flex flex-row flex-md-column align-items-end">

@ -0,0 +1,60 @@
defmodule ExplorerWeb.TokenHelpers do
@moduledoc """
Helper functions for intereacting with `t:ExplorerWeb.Chain.Token` attributes.
"""
alias Explorer.Chain.{Token, TokenTransfer}
alias ExplorerWeb.{CurrencyHelpers}
@doc """
Returns the token transfers' amount according to the token's type and decimails.
When the token's type is ERC-20, then we are going to format the amount according to the token's
decimals considering 0 when the decimals is nil. Case the amount is nil, this function will
return the symbol `--`.
When the token's type is ERC-721, the function will return a string with the token_id that
represents the ERC-721 token since this kind of token doesn't have amount and decimals.
"""
def token_transfer_amount(%TokenTransfer{token: token, amount: amount, token_id: token_id}) do
do_token_transfer_amount(token, amount, token_id)
end
defp do_token_transfer_amount(%Token{type: "ERC-20"}, nil, _token_id) do
"--"
end
defp do_token_transfer_amount(%Token{type: "ERC-20", decimals: nil}, amount, _token_id) do
CurrencyHelpers.format_according_to_decimals(amount, 0)
end
defp do_token_transfer_amount(%Token{type: "ERC-20", decimals: decimals}, amount, _token_id) do
CurrencyHelpers.format_according_to_decimals(amount, decimals)
end
defp do_token_transfer_amount(%Token{type: "ERC-721"}, _amount, token_id) do
"TokenID [#{token_id}]"
end
defp do_token_transfer_amount(_token, _amount, _token_id) do
nil
end
@doc """
Returns the token's symbol.
When the token's symbol is nil, the function will return the contract address hash.
"""
def token_symbol(%Token{symbol: nil, contract_address_hash: address_hash}) do
address_hash =
address_hash
|> to_string()
|> String.slice(0..6)
"#{address_hash}..."
end
def token_symbol(%Token{symbol: symbol}) do
symbol
end
end

@ -1,5 +0,0 @@
defmodule ExplorerWeb.AddresstransactionViewTest do
use ExplorerWeb.ConnCase, async: true
doctest ExplorerWeb.AddressTransactionView
end

@ -0,0 +1,62 @@
defmodule ExplorerWeb.TokenHelpersTest do
use ExplorerWeb.ConnCase, async: true
alias ExplorerWeb.{TokenHelpers}
describe "token_transfer_amount/1" do
test "returns the symbol -- with ERC-20 token and amount nil" do
token = build(:token, type: "ERC-20")
token_transfer = build(:token_transfer, token: token, amount: nil)
assert TokenHelpers.token_transfer_amount(token_transfer) == "--"
end
test "returns the formatted amount according to token decimals with ERC-20 token" do
token = build(:token, type: "ERC-20", decimals: 6)
token_transfer = build(:token_transfer, token: token, amount: Decimal.new(1_000_000))
assert TokenHelpers.token_transfer_amount(token_transfer) == "1"
end
test "returns the formatted amount when the decimals is nil with ERC-20 token" do
token = build(:token, type: "ERC-20", decimals: nil)
token_transfer = build(:token_transfer, token: token, amount: Decimal.new(1_000_000))
assert TokenHelpers.token_transfer_amount(token_transfer) == "1,000,000"
end
test "returns a string with the token_id with ERC-721 token" do
token = build(:token, type: "ERC-721", decimals: nil)
token_transfer = build(:token_transfer, token: token, amount: nil, token_id: 1)
assert TokenHelpers.token_transfer_amount(token_transfer) == "TokenID [1]"
end
test "returns nothing for unknow token's type" do
token = build(:token, type: "unknow")
token_transfer = build(:token_transfer, token: token)
assert TokenHelpers.token_transfer_amount(token_transfer) == nil
end
end
describe "token_symbol/1" do
test "returns the token symbol" do
token = build(:token, symbol: "BAT")
assert TokenHelpers.token_symbol(token) == "BAT"
end
test "returns the token contract address hash when the symbol is nil" do
address = build(:address)
token = build(:token, symbol: nil, contract_address_hash: address.hash)
address_hash =
address.hash
|> Explorer.Chain.Hash.to_string()
|> String.slice(0..6)
assert TokenHelpers.token_symbol(token) == "#{address_hash}..."
end
end
end
Loading…
Cancel
Save