Skip to content
This repository has been archived by the owner on Jun 12, 2023. It is now read-only.

Fix jsonrpc ledger balance for entry v2 #1808

Merged
merged 1 commit into from
Sep 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions src/jsonrpc/miner_jsonrpc_ledger.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@

handle_rpc(<<"ledger_balance">>, []) ->
%% get all
Ledger = get_ledger(),
Entries = maps:filter(
fun(K, _V) ->
is_binary(K)
end,
blockchain_ledger_v1:entries(get_ledger())
blockchain_ledger_v1:entries(Ledger)
),
[format_ledger_balance(A, E) || {A, E} <- maps:to_list(Entries)];
[format_ledger_balance(A, E, Ledger) || {A, E} <- maps:to_list(Entries)];
handle_rpc(<<"ledger_balance">>, #{<<"address">> := Address}) ->
%% get for address
Ledger = get_ledger(),
try
BinAddr = ?B58_TO_BIN(Address),
case blockchain_ledger_v1:find_entry(BinAddr, get_ledger()) of
case blockchain_ledger_v1:find_entry(BinAddr, Ledger) of
{error, not_found} -> ?jsonrpc_error({not_found, Address});
{ok, Entry} -> format_ledger_balance(BinAddr, Entry)
{ok, Entry} -> format_ledger_balance(BinAddr, Entry, Ledger)
end
catch
_:_ -> ?jsonrpc_error({invalid_params, Address})
Expand Down Expand Up @@ -166,12 +168,21 @@ get_ledger() ->
blockchain:ledger(Chain)
end.

format_ledger_balance(Addr, Entry) ->
#{
<<"address">> => ?BIN_TO_B58(Addr),
<<"nonce">> => blockchain_ledger_entry_v1:nonce(Entry),
<<"balance">> => blockchain_ledger_entry_v1:balance(Entry)
}.
format_ledger_balance(Addr, Entry, Ledger) ->
case blockchain_ledger_v1:config(token_version, Ledger) of
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could call blockchain_ledger_v1:versioned_entry_mod_and_entries_cf/1 and not have to do the token_version check here by hand. So this function can be reduced to something like this:

{EntryMod, _} = blockchain_ledger_v1:versioned_entry_mod_and_entries_cf(Ledger),
#{<<"nonce">> = EntryMod:nonce(Entry), ...} 

{ok, N} when N >=2 ->
#{
<<"address">> => ?BIN_TO_B58(Addr),
<<"nonce">> => blockchain_ledger_entry_v2:nonce(Entry),
<<"balance">> => blockchain_ledger_entry_v2:balance(Entry)
};
_ ->
#{
<<"address">> => ?BIN_TO_B58(Addr),
<<"nonce">> => blockchain_ledger_entry_v1:nonce(Entry),
<<"balance">> => blockchain_ledger_entry_v1:balance(Entry)
}
end.

format_ledger_gateway_entry(Addr, GW, Height, Verbose) ->
GWAddr = ?BIN_TO_B58(Addr),
Expand Down