From 57d5a0adbf010a77d146a3487480a54fdbea5896 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Thu, 18 Apr 2019 13:27:29 +0300 Subject: [PATCH 1/4] optionally show token transfer info --- .../templates/transaction/overview.html.eex | 39 +++++++++++----- .../block_scout_web/views/transaction_view.ex | 44 ++++++++++++++++++- 2 files changed, 70 insertions(+), 13 deletions(-) diff --git a/apps/block_scout_web/lib/block_scout_web/templates/transaction/overview.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/transaction/overview.html.eex index 1605a58cd0..90cfe3ee86 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/transaction/overview.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/transaction/overview.html.eex @@ -138,20 +138,35 @@ -
- -
-
-

<%= gettext "Ether" %> <%= gettext "Value" %>

-
-

- <%= value(@transaction) %> -

- data-usd-exchange-rate=<%= @exchange_rate.usd_value %>> + <%= if erc20_token_transfer?(@transaction) do %> +
+ +
+
+

<%= gettext "ERC-20" %> <%= gettext "Token Transfer" %>

+
+

+ <%= erc20_token_transfer_params(@transaction, @token_transfers) %> +

+ data-usd-exchange-rate=<%= @exchange_rate.usd_value %>> +
-
- + <% else %> +
+ +
+
+

<%= gettext "Ether" %> <%= gettext "Value" %>

+
+

+ <%= value(@transaction) %> +

+ data-usd-exchange-rate=<%= @exchange_rate.usd_value %>> +
+
+
+ <% end %>
diff --git a/apps/block_scout_web/lib/block_scout_web/views/transaction_view.ex b/apps/block_scout_web/lib/block_scout_web/views/transaction_view.ex index 88013fd4d6..327c65e675 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/transaction_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/transaction_view.ex @@ -1,11 +1,12 @@ defmodule BlockScoutWeb.TransactionView do use BlockScoutWeb, :view + alias ABI.TypeDecoder alias BlockScoutWeb.{AddressView, BlockView, TabHelpers} alias Cldr.Number alias Explorer.Chain alias Explorer.Chain.Block.Reward - alias Explorer.Chain.{Address, Block, InternalTransaction, Transaction, Wei} + alias Explorer.Chain.{Address, Block, InternalTransaction, TokenTransfer, Transaction, Wei} alias Explorer.ExchangeRates.Token alias Timex.Duration @@ -31,6 +32,47 @@ defmodule BlockScoutWeb.TransactionView do def value_transfer?(_), do: false + def erc20_token_transfer?(%Transaction{ + status: :ok, + created_contract_address_hash: nil, + input: input + }) do + case to_string(input) do + unquote(TokenTransfer.transfer_function_signature()) <> params -> + types = [:address, {:uint, 256}] + + try do + [_address, _value] = + params + |> Base.decode16!(case: :mixed) + |> TypeDecoder.decode_raw(types) + + true + rescue + _ -> false + end + + _ -> + false + end + end + + def erc20_token_transfer?(_) do + false + end + + def erc20_token_transfer_params(transaction, _token_transfers) do + types = [:address, {:uint, 256}] + unquote(TokenTransfer.transfer_function_signature()) <> params = to_string(transaction.input) + + [_address, value] = + params + |> Base.decode16!(case: :mixed) + |> TypeDecoder.decode_raw(types) + + Wei.to(%Wei{value: Decimal.new(value)}, :ether) + end + def processing_time_duration(%Transaction{block: nil}) do :pending end From 48f257013af067cde068b902393271ac5e599537 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Thu, 18 Apr 2019 14:29:07 +0300 Subject: [PATCH 2/4] finish erc20 info --- .../templates/transaction/overview.html.eex | 9 ++-- .../block_scout_web/views/transaction_view.ex | 49 +++++++++---------- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/apps/block_scout_web/lib/block_scout_web/templates/transaction/overview.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/transaction/overview.html.eex index 90cfe3ee86..c952d57793 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/transaction/overview.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/transaction/overview.html.eex @@ -138,7 +138,8 @@
- <%= if erc20_token_transfer?(@transaction) do %> + <% token_transfer = erc20_token_transfer(@transaction, @token_transfers) %> + <%= if token_transfer do %>
@@ -146,9 +147,11 @@

<%= gettext "ERC-20" %> <%= gettext "Token Transfer" %>

- <%= erc20_token_transfer_params(@transaction, @token_transfers) %> + + <%= token_transfer_amount(token_transfer) %> + <%= link(token_symbol(token_transfer.token), to: token_path(BlockScoutWeb.Endpoint, :show, token_transfer.token.contract_address_hash)) %> +

- data-usd-exchange-rate=<%= @exchange_rate.usd_value %>>
diff --git a/apps/block_scout_web/lib/block_scout_web/views/transaction_view.ex b/apps/block_scout_web/lib/block_scout_web/views/transaction_view.ex index 327c65e675..a6f58ac33e 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/transaction_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/transaction_view.ex @@ -11,6 +11,7 @@ defmodule BlockScoutWeb.TransactionView do alias Timex.Duration import BlockScoutWeb.Gettext + import BlockScoutWeb.Tokens.Helpers @tabs ["token_transfers", "internal_transactions", "logs"] @@ -32,45 +33,43 @@ defmodule BlockScoutWeb.TransactionView do def value_transfer?(_), do: false - def erc20_token_transfer?(%Transaction{ - status: :ok, - created_contract_address_hash: nil, - input: input - }) do - case to_string(input) do - unquote(TokenTransfer.transfer_function_signature()) <> params -> + def erc20_token_transfer( + %Transaction{ + status: :ok, + created_contract_address_hash: nil, + input: input, + value: value + }, + token_transfers + ) do + zero_wei = %Wei{value: Decimal.new(0)} + + case {to_string(input), value} do + {unquote(TokenTransfer.transfer_function_signature()) <> params, ^zero_wei} -> types = [:address, {:uint, 256}] try do - [_address, _value] = + [address, value] = params |> Base.decode16!(case: :mixed) |> TypeDecoder.decode_raw(types) - true + decimal_value = Decimal.new(value) + + Enum.find(token_transfers, fn token_transfer -> + token_transfer.to_address_hash.bytes == address && token_transfer.amount == decimal_value + end) rescue - _ -> false + _ -> nil end _ -> - false + nil end end - def erc20_token_transfer?(_) do - false - end - - def erc20_token_transfer_params(transaction, _token_transfers) do - types = [:address, {:uint, 256}] - unquote(TokenTransfer.transfer_function_signature()) <> params = to_string(transaction.input) - - [_address, value] = - params - |> Base.decode16!(case: :mixed) - |> TypeDecoder.decode_raw(types) - - Wei.to(%Wei{value: Decimal.new(value)}, :ether) + def erc20_token_transfer(_, _) do + nil end def processing_time_duration(%Transaction{block: nil}) do From b3cd440361a3225d6bb6b3844ba99f4981cd371b Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Thu, 18 Apr 2019 14:30:46 +0300 Subject: [PATCH 3/4] add CHANGELOG entry --- CHANGELOG.md | 1 + apps/block_scout_web/priv/gettext/default.pot | 46 +++++++++++-------- .../priv/gettext/en/LC_MESSAGES/default.po | 46 +++++++++++-------- 3 files changed, 53 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c6b953228..fc5156caa0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - [#1739](https://github.com/poanetwork/blockscout/pull/1739) - highlight decompiled source code - [#1696](https://github.com/poanetwork/blockscout/pull/1696) - full-text search by tokens - [#1742](https://github.com/poanetwork/blockscout/pull/1742) - Support RSK +- [#1777](https://github.com/poanetwork/blockscout/pull/1777) - show ERC-20 token transfer info on transaction page ### Fixes diff --git a/apps/block_scout_web/priv/gettext/default.pot b/apps/block_scout_web/priv/gettext/default.pot index ec9b93cf71..7010a68126 100644 --- a/apps/block_scout_web/priv/gettext/default.pot +++ b/apps/block_scout_web/priv/gettext/default.pot @@ -49,7 +49,7 @@ msgid "%{subnetwork} Explorer - BlockScout" msgstr "" #, elixir-format -#: lib/block_scout_web/views/transaction_view.ex:128 +#: lib/block_scout_web/views/transaction_view.ex:169 msgid "(Awaiting internal transactions for status)" msgstr "" @@ -165,7 +165,7 @@ msgid "Block Number" msgstr "" #, elixir-format -#: lib/block_scout_web/views/transaction_view.ex:20 +#: lib/block_scout_web/views/transaction_view.ex:22 msgid "Block Pending" msgstr "" @@ -278,12 +278,12 @@ msgid "Contract Address Pending" msgstr "" #, elixir-format -#: lib/block_scout_web/views/transaction_view.ex:205 +#: lib/block_scout_web/views/transaction_view.ex:246 msgid "Contract Call" msgstr "" #, elixir-format -#: lib/block_scout_web/views/transaction_view.ex:204 +#: lib/block_scout_web/views/transaction_view.ex:245 msgid "Contract Creation" msgstr "" @@ -377,12 +377,12 @@ msgid "Error trying to fetch balances." msgstr "" #, elixir-format -#: lib/block_scout_web/views/transaction_view.ex:132 +#: lib/block_scout_web/views/transaction_view.ex:173 msgid "Error: %{reason}" msgstr "" #, elixir-format -#: lib/block_scout_web/views/transaction_view.ex:130 +#: lib/block_scout_web/views/transaction_view.ex:171 msgid "Error: (Awaiting internal transactions for reason)" msgstr "" @@ -392,7 +392,7 @@ msgstr "" #: lib/block_scout_web/templates/layout/app.html.eex:66 #: lib/block_scout_web/templates/transaction/_pending_tile.html.eex:20 #: lib/block_scout_web/templates/transaction/_tile.html.eex:27 -#: lib/block_scout_web/templates/transaction/overview.html.eex:145 +#: lib/block_scout_web/templates/transaction/overview.html.eex:163 #: lib/block_scout_web/views/wei_helpers.ex:72 msgid "Ether" msgstr "" @@ -431,7 +431,7 @@ msgid "GET" msgstr "" #, elixir-format -#: lib/block_scout_web/templates/transaction/overview.html.eex:158 +#: lib/block_scout_web/templates/transaction/overview.html.eex:176 msgid "Gas" msgstr "" @@ -495,7 +495,7 @@ msgstr "" #: lib/block_scout_web/templates/transaction/_tabs.html.eex:43 #: lib/block_scout_web/templates/transaction_internal_transaction/index.html.eex:10 #: lib/block_scout_web/views/address_view.ex:295 -#: lib/block_scout_web/views/transaction_view.ex:258 +#: lib/block_scout_web/views/transaction_view.ex:299 msgid "Internal Transactions" msgstr "" @@ -513,7 +513,7 @@ msgid "Less than" msgstr "" #, elixir-format -#: lib/block_scout_web/templates/transaction/overview.html.eex:170 +#: lib/block_scout_web/templates/transaction/overview.html.eex:188 msgid "Limit" msgstr "" @@ -521,7 +521,7 @@ msgstr "" #: lib/block_scout_web/templates/transaction/_tabs.html.eex:21 #: lib/block_scout_web/templates/transaction/_tabs.html.eex:48 #: lib/block_scout_web/templates/transaction_log/index.html.eex:10 -#: lib/block_scout_web/views/transaction_view.ex:259 +#: lib/block_scout_web/views/transaction_view.ex:300 msgid "Logs" msgstr "" @@ -533,7 +533,7 @@ msgid "Market Cap" msgstr "" #, elixir-format -#: lib/block_scout_web/views/transaction_view.ex:113 +#: lib/block_scout_web/views/transaction_view.ex:154 msgid "Max of" msgstr "" @@ -661,8 +661,8 @@ msgstr "" #, elixir-format #: lib/block_scout_web/templates/layout/_topnav.html.eex:44 -#: lib/block_scout_web/views/transaction_view.ex:127 -#: lib/block_scout_web/views/transaction_view.ex:161 +#: lib/block_scout_web/views/transaction_view.ex:168 +#: lib/block_scout_web/views/transaction_view.ex:202 msgid "Pending" msgstr "" @@ -762,7 +762,7 @@ msgstr "" #, elixir-format #: lib/block_scout_web/templates/transaction/_emission_reward_tile.html.eex:8 -#: lib/block_scout_web/views/transaction_view.ex:129 +#: lib/block_scout_web/views/transaction_view.ex:170 msgid "Success" msgstr "" @@ -872,8 +872,9 @@ msgstr "" #, elixir-format #: lib/block_scout_web/templates/tokens/transfer/_token_transfer.html.eex:4 +#: lib/block_scout_web/templates/transaction/overview.html.eex:147 #: lib/block_scout_web/templates/transaction_token_transfer/_token_transfer.html.eex:4 -#: lib/block_scout_web/views/transaction_view.ex:203 +#: lib/block_scout_web/views/transaction_view.ex:244 msgid "Token Transfer" msgstr "" @@ -885,7 +886,7 @@ msgstr "" #: lib/block_scout_web/templates/transaction/_tabs.html.eex:36 #: lib/block_scout_web/templates/transaction_token_transfer/index.html.eex:10 #: lib/block_scout_web/views/tokens/overview_view.ex:35 -#: lib/block_scout_web/views/transaction_view.ex:257 +#: lib/block_scout_web/views/transaction_view.ex:298 msgid "Token Transfers" msgstr "" @@ -926,7 +927,7 @@ msgid "Total transactions" msgstr "" #, elixir-format -#: lib/block_scout_web/views/transaction_view.ex:206 +#: lib/block_scout_web/views/transaction_view.ex:247 msgid "Transaction" msgstr "" @@ -993,7 +994,7 @@ msgid "Unique Token" msgstr "" #, elixir-format -#: lib/block_scout_web/templates/transaction/overview.html.eex:163 +#: lib/block_scout_web/templates/transaction/overview.html.eex:181 msgid "Used" msgstr "" @@ -1014,7 +1015,7 @@ msgid "Validations" msgstr "" #, elixir-format -#: lib/block_scout_web/templates/transaction/overview.html.eex:145 +#: lib/block_scout_web/templates/transaction/overview.html.eex:163 msgid "Value" msgstr "" @@ -1732,3 +1733,8 @@ msgstr "" #: lib/block_scout_web/templates/address_contract_verification/new.html.eex:52 msgid "Optimization runs" msgstr "" + +#, elixir-format +#: lib/block_scout_web/templates/transaction/overview.html.eex:147 +msgid "ERC-20" +msgstr "" diff --git a/apps/block_scout_web/priv/gettext/en/LC_MESSAGES/default.po b/apps/block_scout_web/priv/gettext/en/LC_MESSAGES/default.po index 2056f772d9..8a18c60018 100644 --- a/apps/block_scout_web/priv/gettext/en/LC_MESSAGES/default.po +++ b/apps/block_scout_web/priv/gettext/en/LC_MESSAGES/default.po @@ -49,7 +49,7 @@ msgid "%{subnetwork} Explorer - BlockScout" msgstr "" #, elixir-format -#: lib/block_scout_web/views/transaction_view.ex:128 +#: lib/block_scout_web/views/transaction_view.ex:169 msgid "(Awaiting internal transactions for status)" msgstr "" @@ -165,7 +165,7 @@ msgid "Block Number" msgstr "" #, elixir-format -#: lib/block_scout_web/views/transaction_view.ex:20 +#: lib/block_scout_web/views/transaction_view.ex:22 msgid "Block Pending" msgstr "" @@ -278,12 +278,12 @@ msgid "Contract Address Pending" msgstr "" #, elixir-format -#: lib/block_scout_web/views/transaction_view.ex:205 +#: lib/block_scout_web/views/transaction_view.ex:246 msgid "Contract Call" msgstr "" #, elixir-format -#: lib/block_scout_web/views/transaction_view.ex:204 +#: lib/block_scout_web/views/transaction_view.ex:245 msgid "Contract Creation" msgstr "" @@ -377,12 +377,12 @@ msgid "Error trying to fetch balances." msgstr "" #, elixir-format -#: lib/block_scout_web/views/transaction_view.ex:132 +#: lib/block_scout_web/views/transaction_view.ex:173 msgid "Error: %{reason}" msgstr "" #, elixir-format -#: lib/block_scout_web/views/transaction_view.ex:130 +#: lib/block_scout_web/views/transaction_view.ex:171 msgid "Error: (Awaiting internal transactions for reason)" msgstr "" @@ -392,7 +392,7 @@ msgstr "" #: lib/block_scout_web/templates/layout/app.html.eex:66 #: lib/block_scout_web/templates/transaction/_pending_tile.html.eex:20 #: lib/block_scout_web/templates/transaction/_tile.html.eex:27 -#: lib/block_scout_web/templates/transaction/overview.html.eex:145 +#: lib/block_scout_web/templates/transaction/overview.html.eex:163 #: lib/block_scout_web/views/wei_helpers.ex:72 msgid "Ether" msgstr "POA" @@ -431,7 +431,7 @@ msgid "GET" msgstr "" #, elixir-format -#: lib/block_scout_web/templates/transaction/overview.html.eex:158 +#: lib/block_scout_web/templates/transaction/overview.html.eex:176 msgid "Gas" msgstr "" @@ -495,7 +495,7 @@ msgstr "" #: lib/block_scout_web/templates/transaction/_tabs.html.eex:43 #: lib/block_scout_web/templates/transaction_internal_transaction/index.html.eex:10 #: lib/block_scout_web/views/address_view.ex:295 -#: lib/block_scout_web/views/transaction_view.ex:258 +#: lib/block_scout_web/views/transaction_view.ex:299 msgid "Internal Transactions" msgstr "" @@ -513,7 +513,7 @@ msgid "Less than" msgstr "" #, elixir-format -#: lib/block_scout_web/templates/transaction/overview.html.eex:170 +#: lib/block_scout_web/templates/transaction/overview.html.eex:188 msgid "Limit" msgstr "" @@ -521,7 +521,7 @@ msgstr "" #: lib/block_scout_web/templates/transaction/_tabs.html.eex:21 #: lib/block_scout_web/templates/transaction/_tabs.html.eex:48 #: lib/block_scout_web/templates/transaction_log/index.html.eex:10 -#: lib/block_scout_web/views/transaction_view.ex:259 +#: lib/block_scout_web/views/transaction_view.ex:300 msgid "Logs" msgstr "" @@ -533,7 +533,7 @@ msgid "Market Cap" msgstr "" #, elixir-format -#: lib/block_scout_web/views/transaction_view.ex:113 +#: lib/block_scout_web/views/transaction_view.ex:154 msgid "Max of" msgstr "" @@ -661,8 +661,8 @@ msgstr "" #, elixir-format #: lib/block_scout_web/templates/layout/_topnav.html.eex:44 -#: lib/block_scout_web/views/transaction_view.ex:127 -#: lib/block_scout_web/views/transaction_view.ex:161 +#: lib/block_scout_web/views/transaction_view.ex:168 +#: lib/block_scout_web/views/transaction_view.ex:202 msgid "Pending" msgstr "" @@ -762,7 +762,7 @@ msgstr "" #, elixir-format #: lib/block_scout_web/templates/transaction/_emission_reward_tile.html.eex:8 -#: lib/block_scout_web/views/transaction_view.ex:129 +#: lib/block_scout_web/views/transaction_view.ex:170 msgid "Success" msgstr "" @@ -872,8 +872,9 @@ msgstr "" #, elixir-format #: lib/block_scout_web/templates/tokens/transfer/_token_transfer.html.eex:4 +#: lib/block_scout_web/templates/transaction/overview.html.eex:147 #: lib/block_scout_web/templates/transaction_token_transfer/_token_transfer.html.eex:4 -#: lib/block_scout_web/views/transaction_view.ex:203 +#: lib/block_scout_web/views/transaction_view.ex:244 msgid "Token Transfer" msgstr "" @@ -885,7 +886,7 @@ msgstr "" #: lib/block_scout_web/templates/transaction/_tabs.html.eex:36 #: lib/block_scout_web/templates/transaction_token_transfer/index.html.eex:10 #: lib/block_scout_web/views/tokens/overview_view.ex:35 -#: lib/block_scout_web/views/transaction_view.ex:257 +#: lib/block_scout_web/views/transaction_view.ex:298 msgid "Token Transfers" msgstr "" @@ -926,7 +927,7 @@ msgid "Total transactions" msgstr "" #, elixir-format -#: lib/block_scout_web/views/transaction_view.ex:206 +#: lib/block_scout_web/views/transaction_view.ex:247 msgid "Transaction" msgstr "" @@ -993,7 +994,7 @@ msgid "Unique Token" msgstr "" #, elixir-format -#: lib/block_scout_web/templates/transaction/overview.html.eex:163 +#: lib/block_scout_web/templates/transaction/overview.html.eex:181 msgid "Used" msgstr "" @@ -1014,7 +1015,7 @@ msgid "Validations" msgstr "" #, elixir-format -#: lib/block_scout_web/templates/transaction/overview.html.eex:145 +#: lib/block_scout_web/templates/transaction/overview.html.eex:163 msgid "Value" msgstr "" @@ -1732,3 +1733,8 @@ msgstr "" #: lib/block_scout_web/templates/address_contract_verification/new.html.eex:52 msgid "Optimization runs" msgstr "" + +#, elixir-format +#: lib/block_scout_web/templates/transaction/overview.html.eex:147 +msgid "ERC-20" +msgstr "" From 251aff28ecbc0575eb4c4f3f42430400c3df8c21 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Thu, 18 Apr 2019 15:12:26 +0300 Subject: [PATCH 4/4] check assigns --- .../lib/block_scout_web/templates/transaction/overview.html.eex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/block_scout_web/lib/block_scout_web/templates/transaction/overview.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/transaction/overview.html.eex index c952d57793..98acd3b3e6 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/transaction/overview.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/transaction/overview.html.eex @@ -138,7 +138,7 @@
- <% token_transfer = erc20_token_transfer(@transaction, @token_transfers) %> + <% token_transfer = assigns[:token_transfers] && erc20_token_transfer(@transaction, @token_transfers) %> <%= if token_transfer do %>