Edit API account balance actions to return Wei

Why:

* Currently, the `account#balance` and `account#balancemulti` API calls
return the Ether balance. It was built this way initially because the
Etherscan docs say those API calls should return Ether. This seems to be
a bug in their documentation because the API responds with the balance
in Wei.
* Issue link: related to
https://github.com/poanetwork/poa-explorer/issues/138

This change addresses the need by:

* Editing the `account#balance` and `account#balancemulti` API calls to
return Wei instead of Ether.
* Editing `API.RPC.AddressControllerTest` to assert account balances are
returned in Wei.
pull/509/head
Sebastian Abondano 6 years ago
parent 8525f3d5fd
commit e3f748d912
  1. 9
      apps/explorer_web/lib/explorer_web/views/api/rpc/address_view.ex
  2. 37
      apps/explorer_web/test/explorer_web/controllers/api/rpc/address_controller_test.exs

@ -4,8 +4,7 @@ defmodule ExplorerWeb.API.RPC.AddressView do
alias ExplorerWeb.API.RPC.RPCView alias ExplorerWeb.API.RPC.RPCView
def render("balance.json", %{addresses: [address]}) do def render("balance.json", %{addresses: [address]}) do
ether_balance = wei_to_ether(address.fetched_balance) RPCView.render("show.json", data: "#{address.fetched_balance.value}")
RPCView.render("show.json", data: ether_balance)
end end
def render("balance.json", assigns) do def render("balance.json", assigns) do
@ -17,7 +16,7 @@ defmodule ExplorerWeb.API.RPC.AddressView do
Enum.map(addresses, fn address -> Enum.map(addresses, fn address ->
%{ %{
"account" => "#{address.hash}", "account" => "#{address.hash}",
"balance" => wei_to_ether(address.fetched_balance) "balance" => "#{address.fetched_balance.value}"
} }
end) end)
@ -33,10 +32,6 @@ defmodule ExplorerWeb.API.RPC.AddressView do
RPCView.render("error.json", assigns) RPCView.render("error.json", assigns)
end end
defp wei_to_ether(wei) do
format_wei_value(wei, :ether, include_unit_label: false)
end
defp prepare_transaction(transaction) do defp prepare_transaction(transaction) do
%{ %{
"blockNumber" => "#{transaction.block_number}", "blockNumber" => "#{transaction.block_number}",

@ -2,7 +2,7 @@ defmodule ExplorerWeb.API.RPC.AddressControllerTest do
use ExplorerWeb.ConnCase use ExplorerWeb.ConnCase
alias Explorer.Chain alias Explorer.Chain
alias Explorer.Chain.{Transaction, Wei} alias Explorer.Chain.Transaction
describe "balance" do describe "balance" do
test "with missing address hash", %{conn: conn} do test "with missing address hash", %{conn: conn} do
@ -66,17 +66,12 @@ defmodule ExplorerWeb.API.RPC.AddressControllerTest do
"address" => "#{address.hash}" "address" => "#{address.hash}"
} }
expected_balance =
address.fetched_balance
|> Wei.to(:ether)
|> Decimal.to_string(:normal)
assert response = assert response =
conn conn
|> get("/api", params) |> get("/api", params)
|> json_response(200) |> json_response(200)
assert response["result"] == expected_balance assert response["result"] == "#{address.fetched_balance.value}"
assert response["status"] == "1" assert response["status"] == "1"
assert response["message"] == "OK" assert response["message"] == "OK"
end end
@ -100,12 +95,7 @@ defmodule ExplorerWeb.API.RPC.AddressControllerTest do
expected_result = expected_result =
Enum.map(addresses, fn address -> Enum.map(addresses, fn address ->
expected_balance = %{"account" => "#{address.hash}", "balance" => "#{address.fetched_balance.value}"}
address.fetched_balance
|> Wei.to(:ether)
|> Decimal.to_string(:normal)
%{"account" => "#{address.hash}", "balance" => expected_balance}
end) end)
assert response = assert response =
@ -185,12 +175,7 @@ defmodule ExplorerWeb.API.RPC.AddressControllerTest do
expected_result = expected_result =
Enum.map(addresses, fn address -> Enum.map(addresses, fn address ->
expected_balance = %{"account" => "#{address.hash}", "balance" => "#{address.fetched_balance.value}"}
address.fetched_balance
|> Wei.to(:ether)
|> Decimal.to_string(:normal)
%{"account" => "#{address.hash}", "balance" => expected_balance}
end) end)
assert response = assert response =
@ -213,14 +198,9 @@ defmodule ExplorerWeb.API.RPC.AddressControllerTest do
"address" => "#{address1.hash},#{address2_hash}" "address" => "#{address1.hash},#{address2_hash}"
} }
expected_balance1 =
address1.fetched_balance
|> Wei.to(:ether)
|> Decimal.to_string(:normal)
expected_result = [ expected_result = [
%{"account" => address2_hash, "balance" => "0"}, %{"account" => address2_hash, "balance" => "0"},
%{"account" => "#{address1.hash}", "balance" => expected_balance1} %{"account" => "#{address1.hash}", "balance" => "#{address1.fetched_balance.value}"}
] ]
assert response = assert response =
@ -266,13 +246,8 @@ defmodule ExplorerWeb.API.RPC.AddressControllerTest do
"address" => "#{address.hash}" "address" => "#{address.hash}"
} }
expected_balance =
address.fetched_balance
|> Wei.to(:ether)
|> Decimal.to_string(:normal)
expected_result = [ expected_result = [
%{"account" => "#{address.hash}", "balance" => expected_balance} %{"account" => "#{address.hash}", "balance" => "#{address.fetched_balance.value}"}
] ]
assert response = assert response =

Loading…
Cancel
Save