Skip to content

Commit

Permalink
Get balance from the last address
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmanzanera committed Jan 31, 2023
1 parent a83f265 commit 74cf7eb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/archethic_web/graphql_schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ defmodule ArchethicWeb.GraphQLSchema do
end

@desc """
Query the network to find a balance from an address
Query the network to find a balance from an address coming from the latest transaction on the chain
"""
field :balance, :balance do
arg(:address, non_null(:address))
Expand Down
29 changes: 13 additions & 16 deletions lib/archethic_web/graphql_schema/resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,19 @@ defmodule ArchethicWeb.GraphQLSchema.Resolver do
end

def get_balance(address) do
case Archethic.get_balance(address) do
{:ok, %{uco: uco, token: token_balances}} ->
balance = %{
uco: uco,
token:
token_balances
|> Enum.map(fn {{address, token_id}, amount} ->
%{address: address, amount: amount, token_id: token_id}
end)
|> Enum.sort_by(& &1.amount)
}

{:ok, balance}

{:error, :network_issue} = e ->
e
with {:ok, last_address} <- Archethic.get_last_transaction_address(address),
{:ok, %{uco: uco, token: token_balances}} <- Archethic.get_balance(last_address) do
balance = %{
uco: uco,
token:
token_balances
|> Enum.map(fn {{address, token_id}, amount} ->
%{address: address, amount: amount, token_id: token_id}
end)
|> Enum.sort_by(& &1.amount)
}

{:ok, balance}
end
end

Expand Down

0 comments on commit 74cf7eb

Please sign in to comment.