fewd/src/fd_hz.erl
Peter Harpending bc870e5f2d wip
2026-03-02 18:27:05 -08:00

82 lines
1.5 KiB
Erlang

% @doc hz helper functions
-module(fd_hz).
-export_type([
]).
-export([
txs_current/0,
txs_since_height/1,
txs_minus/1,
txs_from_to/2,
current_height/0,
txs_of_height/1,
mhs_of_height/1,
txs_of_mh/1,
test/0
]).
-spec test() -> no_return().
test() ->
io:format("~tp~n", [hz:gen_current()]),
io:format("~tp~n", [hz:kb_current()]),
ok.
txs_current() ->
H = current_height(),
txs_of_height(H).
txs_since_height(H) ->
txs_from_to(H, current_height()).
txs_minus(N) ->
H = current_height(),
L = H - N,
txs_from_to(L, H).
txs_from_to(Min, Max) when Min =< Max ->
lists:append([txs_of_height(H) || H <- lists:seq(Min, Max)]);
txs_from_to(Min, Max) when Min > Max ->
[].
current_height() ->
{ok, H} = hz:kb_current_height(),
H.
txs_of_height(Height) ->
lists:append([txs_of_mh(MH) || MH <- mhs_of_height(Height)]).
-spec mhs_of_height(Height :: pos_integer()) -> term().
mhs_of_height(Height) ->
{ok, #{"micro_blocks" := MHs}} = hz:gen_by_height(Height),
MHs.
txs_of_mh(MH) ->
{ok, TXs} = hz:mb_txs(MH),
filter_spends(TXs).
filter_spends(TXs) ->
lists:filtermap(fun filter_spend/1, TXs).
filter_spend(#{"tx" := TX = #{"type" := "SpendTx"}}) -> {true, spend_info(TX)};
filter_spend(_) -> false.
spend_info(#{"type" := "SpendTx",
"recipient_id" := Recipient,
"amount" := Amount,
"payload" := Payload}) ->
{sp, Recipient, Amount, Payload}.