Add TokenID to tokentx API endpoint

Since ERC-721 tokens don't have a value, their value is replaced by
their token ID
pull/1052/head
William Sanches 6 years ago
parent 88a95150ed
commit c046923f30
No known key found for this signature in database
GPG Key ID: 27250E49FB133014
  1. 10
      apps/block_scout_web/lib/block_scout_web/views/api/rpc/address_view.ex
  2. 35
      apps/block_scout_web/test/block_scout_web/controllers/api/rpc/address_controller_test.exs
  3. 4
      apps/explorer/lib/explorer/etherscan.ex

@ -106,7 +106,7 @@ defmodule BlockScoutWeb.API.RPC.AddressView do
"from" => to_string(token_transfer.from_address_hash),
"contractAddress" => to_string(token_transfer.token_contract_address_hash),
"to" => to_string(token_transfer.to_address_hash),
"value" => to_string(token_transfer.amount),
"value" => get_token_value(token_transfer),
"tokenName" => token_transfer.token_name,
"tokenSymbol" => token_transfer.token_symbol,
"tokenDecimal" => to_string(token_transfer.token_decimals),
@ -120,6 +120,14 @@ defmodule BlockScoutWeb.API.RPC.AddressView do
}
end
defp get_token_value(%{token_type: "ERC-721"} = token_transfer) do
to_string(token_transfer.token_id)
end
defp get_token_value(token_transfer) do
to_string(token_transfer.amount)
end
defp prepare_block(block) do
%{
"blockNumber" => to_string(block.number),

@ -1584,6 +1584,41 @@ defmodule BlockScoutWeb.API.RPC.AddressControllerTest do
assert response["message"] == "No token transfers found"
end
test "has correct value for ERC-721", %{conn: conn} do
transaction =
:transaction
|> insert()
|> with_block()
token_address = insert(:contract_address)
insert(:token, %{contract_address: token_address, type: "ERC-721"})
token_transfer =
insert(:token_transfer, %{
token_contract_address: token_address,
token_id: 666,
transaction: transaction
})
{:ok, _} = Chain.token_from_address_hash(token_transfer.token_contract_address_hash)
params = %{
"module" => "account",
"action" => "tokentx",
"address" => to_string(token_transfer.from_address.hash)
}
assert response =
%{"result" => [result]} =
conn
|> get("/api", params)
|> json_response(200)
assert result["value"] == to_string(token_transfer.token_id)
assert response["status"] == "1"
assert response["message"] == "OK"
end
test "returns all the required fields", %{conn: conn} do
transaction =
%{block: block} =

@ -344,9 +344,11 @@ defmodule Explorer.Etherscan do
block_number: b.number,
block_timestamp: b.timestamp,
confirmations: fragment("? - ?", ^max_block_number, t.block_number),
token_id: tt.token_id,
token_name: tkn.name,
token_symbol: tkn.symbol,
token_decimals: tkn.decimals
token_decimals: tkn.decimals,
token_type: tkn.type
})
)

Loading…
Cancel
Save