Skip to content

Commit

Permalink
Merge pull request #2093 from poanetwork/ab-erc721-transfer
Browse files Browse the repository at this point in the history
detect token transfer type
  • Loading branch information
ayrat555 authored Jun 5, 2019
2 parents 71ef767 + 2606bbb commit 55fd323
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 191 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- [#2103](https://github.com/poanetwork/blockscout/pull/2103) - ui issues for all themes
- [#2090](https://github.com/poanetwork/blockscout/pull/2090) - updated some ETC theme colors
- [#2096](https://github.com/poanetwork/blockscout/pull/2096) - RSK theme fixes
- [#2093](https://github.com/poanetwork/blockscout/pull/2093) - detect token transfer type for deprecated erc721 spec

### Chore

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,39 +169,22 @@
</div>
</div>

<%= cond do %>
<% erc20_token_transfer = assigns[:token_transfers] && erc20_token_transfer(@transaction, @token_transfers) -> %>

<%= case token_transfer_type(@transaction) do %>
<% {type, token_transfer} -> %>
<div class="col-md-12 col-lg-4 d-flex flex-column flex-md-row flex-lg-column pl-0">
<!-- Value -->
<div class="card card-background-1 flex-grow-1">
<div class="card-body card-body-flex-column-space-between">
<h2 class="card-title balance-card-title"><%= gettext "ERC-20" %> <%= gettext "Token Transfer" %></h2>
<h2 class="card-title balance-card-title"><%= if type == :erc20, do: gettext("ERC-20"), else: gettext("ERC-721")%><%= gettext " Token Transfer" %></h2>
<div class="text-right">
<h3 class="address-balance-text">
<%= token_transfer_amount(erc20_token_transfer) %>
<%= link(token_symbol(erc20_token_transfer.token), to: token_path(BlockScoutWeb.Endpoint, :show, erc20_token_transfer.token.contract_address_hash)) %>
</h3>
</div>
</div>
</div>
<% erc721_token_transfer = assigns[:token_transfers] && erc721_token_transfer(@transaction, @token_transfers) -> %>
<div class="col-md-12 col-lg-4 d-flex flex-column flex-md-row flex-lg-column">
<!-- Value -->
<div class="card card-primary flex-grow-1">
<div class="card-body">
<h2 class="card-title text-white"><%= gettext "ERC-721" %> <%= gettext "Token Transfer" %></h2>
<div class="text-right">
<h3 class="text-white">
<span class="col-12 col-md-7 ml-3 ml-sm-0">
<%= token_transfer_amount(erc721_token_transfer) %>
<%= link(token_symbol(erc721_token_transfer.token), to: token_path(BlockScoutWeb.Endpoint, :show, erc721_token_transfer.token.contract_address_hash)) %>
</span>
<%= token_transfer_amount(token_transfer) %>
<%= link(token_symbol(token_transfer.token), to: token_path(BlockScoutWeb.Endpoint, :show, token_transfer.token.contract_address_hash)) %>
</h3>
</div>
</div>
</div>
<% true -> %>
<% _ -> %>
<div class="col-md-12 col-lg-4 d-flex flex-column flex-md-row flex-lg-column pl-0-md">
<!-- Value -->
<div class="card card-background-1 flex-grow-1">
Expand All @@ -212,8 +195,8 @@
<%= value(@transaction) %>
</h3>
<%= if !empty_exchange_rate?(@exchange_rate) do %>
<p class="address-current-balance"
data-wei-value=<%= @transaction.value.value %>
<p class="address-current-balance"
data-wei-value=<%= @transaction.value.value %>
data-usd-exchange-rate=<%= @exchange_rate.usd_value %>>
</p>
<% end %>
Expand Down
88 changes: 3 additions & 85 deletions apps/block_scout_web/lib/block_scout_web/views/transaction_view.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
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, TokenTransfer, Transaction, Wei}
alias Explorer.Chain.{Address, Block, InternalTransaction, Transaction, Wei}
alias Explorer.ExchangeRates.Token
alias Timex.Duration

Expand All @@ -33,85 +32,10 @@ defmodule BlockScoutWeb.TransactionView do

def value_transfer?(_), do: false

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}]

[address, value] = decode_params(params, types)

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)

_ ->
nil
end
rescue
_ -> nil
def token_transfer_type(transaction) do
Chain.transaction_token_transfer_type(transaction)
end

def erc20_token_transfer(_, _) do
nil
end

def erc721_token_transfer(
%Transaction{
status: :ok,
created_contract_address_hash: nil,
input: input,
value: value
},
token_transfers
) do
zero_wei = %Wei{value: Decimal.new(0)}

# https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/token/ERC721/ERC721.sol#L35
{from_address, to_address} =
case {to_string(input), value} do
# transferFrom(address,address,uint256)
{"0x23b872dd" <> params, ^zero_wei} ->
types = [:address, :address, {:uint, 256}]
[from_address, to_address, _value] = decode_params(params, types)
{from_address, to_address}

# safeTransferFrom(address,address,uint256)
{"0x42842e0e" <> params, ^zero_wei} ->
types = [:address, :address, {:uint, 256}]
[from_address, to_address, _value] = decode_params(params, types)
{from_address, to_address}

# safeTransferFrom(address,address,uint256,bytes)
{"0xb88d4fde" <> params, ^zero_wei} ->
types = [:address, :address, {:uint, 256}, :bytes]
[from_address, to_address, _value, _data] = decode_params(params, types)
{from_address, to_address}

_ ->
nil
end

Enum.find(token_transfers, fn token_transfer ->
token_transfer.from_address_hash.bytes == from_address && token_transfer.to_address_hash.bytes == to_address
end)
rescue
_ -> nil
end

def erc721_token_transfer(_, _), do: nil

def processing_time_duration(%Transaction{block: nil}) do
:pending
end
Expand Down Expand Up @@ -339,10 +263,4 @@ defmodule BlockScoutWeb.TransactionView do
defp tab_name(["internal_transactions"]), do: gettext("Internal Transactions")
defp tab_name(["logs"]), do: gettext("Logs")
defp tab_name(["raw_trace"]), do: gettext("Raw Trace")

defp decode_params(params, types) do
params
|> Base.decode16!(case: :mixed)
|> TypeDecoder.decode_raw(types)
end
end
53 changes: 28 additions & 25 deletions apps/block_scout_web/priv/gettext/default.pot
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ msgid "%{subnetwork} Explorer - BlockScout"
msgstr ""

#, elixir-format
#: lib/block_scout_web/views/transaction_view.ex:209
#: lib/block_scout_web/views/transaction_view.ex:133
msgid "(Awaiting internal transactions for status)"
msgstr ""

Expand Down Expand Up @@ -165,7 +165,7 @@ msgid "Block Number"
msgstr ""

#, elixir-format
#: lib/block_scout_web/views/transaction_view.ex:22
#: lib/block_scout_web/views/transaction_view.ex:21
msgid "Block Pending"
msgstr ""

Expand Down Expand Up @@ -274,12 +274,12 @@ msgid "Contract Address Pending"
msgstr ""

#, elixir-format
#: lib/block_scout_web/views/transaction_view.ex:286
#: lib/block_scout_web/views/transaction_view.ex:210
msgid "Contract Call"
msgstr ""

#, elixir-format
#: lib/block_scout_web/views/transaction_view.ex:285
#: lib/block_scout_web/views/transaction_view.ex:209
msgid "Contract Creation"
msgstr ""

Expand Down Expand Up @@ -362,12 +362,12 @@ msgid "Error trying to fetch balances."
msgstr ""

#, elixir-format
#: lib/block_scout_web/views/transaction_view.ex:213
#: lib/block_scout_web/views/transaction_view.ex:137
msgid "Error: %{reason}"
msgstr ""

#, elixir-format
#: lib/block_scout_web/views/transaction_view.ex:211
#: lib/block_scout_web/views/transaction_view.ex:135
msgid "Error: (Awaiting internal transactions for reason)"
msgstr ""

Expand All @@ -377,7 +377,7 @@ msgstr ""
#: lib/block_scout_web/templates/layout/app.html.eex:55
#: lib/block_scout_web/templates/transaction/_pending_tile.html.eex:20
#: lib/block_scout_web/templates/transaction/_tile.html.eex:30
#: lib/block_scout_web/templates/transaction/overview.html.eex:209
#: lib/block_scout_web/templates/transaction/overview.html.eex:192
#: lib/block_scout_web/views/wei_helpers.ex:72
msgid "Ether"
msgstr ""
Expand Down Expand Up @@ -472,7 +472,7 @@ msgstr ""
#: lib/block_scout_web/templates/transaction/_tabs.html.eex:11
#: lib/block_scout_web/templates/transaction_internal_transaction/index.html.eex:6
#: lib/block_scout_web/views/address_view.ex:306
#: lib/block_scout_web/views/transaction_view.ex:339
#: lib/block_scout_web/views/transaction_view.ex:263
msgid "Internal Transactions"
msgstr ""

Expand All @@ -489,7 +489,7 @@ msgid "Less than"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/transaction/overview.html.eex:237
#: lib/block_scout_web/templates/transaction/overview.html.eex:220
msgid "Limit"
msgstr ""

Expand All @@ -499,7 +499,7 @@ msgstr ""
#: lib/block_scout_web/templates/transaction/_tabs.html.eex:17
#: lib/block_scout_web/templates/transaction_log/index.html.eex:8
#: lib/block_scout_web/views/address_view.ex:312
#: lib/block_scout_web/views/transaction_view.ex:340
#: lib/block_scout_web/views/transaction_view.ex:264
msgid "Logs"
msgstr ""

Expand All @@ -511,7 +511,7 @@ msgid "Market Cap"
msgstr ""

#, elixir-format
#: lib/block_scout_web/views/transaction_view.ex:194
#: lib/block_scout_web/views/transaction_view.ex:118
msgid "Max of"
msgstr ""

Expand Down Expand Up @@ -601,8 +601,8 @@ msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/layout/_topnav.html.eex:44
#: lib/block_scout_web/views/transaction_view.ex:208
#: lib/block_scout_web/views/transaction_view.ex:242
#: lib/block_scout_web/views/transaction_view.ex:132
#: lib/block_scout_web/views/transaction_view.ex:166
msgid "Pending"
msgstr ""

Expand Down Expand Up @@ -689,7 +689,7 @@ msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/transaction/_emission_reward_tile.html.eex:8
#: lib/block_scout_web/views/transaction_view.ex:210
#: lib/block_scout_web/views/transaction_view.ex:134
msgid "Success"
msgstr ""

Expand Down Expand Up @@ -793,10 +793,8 @@ msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/tokens/transfer/_token_transfer.html.eex:5
#: lib/block_scout_web/templates/transaction/overview.html.eex:179
#: lib/block_scout_web/templates/transaction/overview.html.eex:193
#: lib/block_scout_web/templates/transaction_token_transfer/_token_transfer.html.eex:4
#: lib/block_scout_web/views/transaction_view.ex:284
#: lib/block_scout_web/views/transaction_view.ex:208
msgid "Token Transfer"
msgstr ""

Expand All @@ -806,7 +804,7 @@ msgstr ""
#: lib/block_scout_web/templates/transaction/_tabs.html.eex:4
#: lib/block_scout_web/templates/transaction_token_transfer/index.html.eex:7
#: lib/block_scout_web/views/tokens/overview_view.ex:35
#: lib/block_scout_web/views/transaction_view.ex:338
#: lib/block_scout_web/views/transaction_view.ex:262
msgid "Token Transfers"
msgstr ""

Expand Down Expand Up @@ -846,7 +844,7 @@ msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/address_logs/_logs.html.eex:3
#: lib/block_scout_web/views/transaction_view.ex:287
#: lib/block_scout_web/views/transaction_view.ex:211
msgid "Transaction"
msgstr ""

Expand Down Expand Up @@ -908,7 +906,7 @@ msgid "Unique Token"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/transaction/overview.html.eex:231
#: lib/block_scout_web/templates/transaction/overview.html.eex:214
msgid "Used"
msgstr ""

Expand All @@ -928,7 +926,7 @@ msgid "Validations"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/transaction/overview.html.eex:209
#: lib/block_scout_web/templates/transaction/overview.html.eex:192
msgid "Value"
msgstr ""

Expand Down Expand Up @@ -1522,12 +1520,12 @@ msgid "Optimization runs"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/transaction/overview.html.eex:179
#: lib/block_scout_web/templates/transaction/overview.html.eex:178
msgid "ERC-20"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/transaction/overview.html.eex:193
#: lib/block_scout_web/templates/transaction/overview.html.eex:178
msgid "ERC-721"
msgstr ""

Expand All @@ -1547,14 +1545,14 @@ msgid "View All Transactions"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/transaction/overview.html.eex:227
#: lib/block_scout_web/templates/transaction/overview.html.eex:210
msgid "Gas"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/transaction/_tabs.html.eex:24
#: lib/block_scout_web/templates/transaction_raw_trace/index.html.eex:7
#: lib/block_scout_web/views/transaction_view.ex:341
#: lib/block_scout_web/views/transaction_view.ex:265
msgid "Raw Trace"
msgstr ""

Expand Down Expand Up @@ -1694,3 +1692,8 @@ msgstr ""
#: lib/block_scout_web/templates/address_contract_verification/new.html.eex:3
msgid "New Smart Contract Verification"
msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/transaction/overview.html.eex:178
msgid " Token Transfer"
msgstr ""
Loading

0 comments on commit 55fd323

Please sign in to comment.