% @doc grids cache -module(fd_gridsd). -vsn("0.2.0"). -behavior(gen_server). -export_type([ ]). -export([ %% caller context get_url/2, %% api start_link/0, %% process context init/1, handle_call/3, handle_cast/2, handle_info/2, code_change/3, terminate/2 ]). -include("$zx_include/zx_logger.hrl"). -type mh_str() :: string(). %% for craig's autism -record(sp, {recipient :: string(), amount :: non_neg_integer(), payload :: binary()}). -type search_pattern() :: #sp{}. -record(s, {current_gen_height :: pos_integer(), current_gen_hash :: string(), current_gen_seen_mb_hashes :: [mh_str()], past_gen_seen_mb_hashes :: [mh_str()], looking_for :: [search_pattern()]}). -type state() :: #s{}. %%----------------------------------------------------------------------------- %% caller context %%----------------------------------------------------------------------------- -spec get_url(Amount, Payload) -> {ok, URL, QR_PNG} | {error, term()} when Amount :: none | pos_integer(), Payload :: none | binary(), URL :: string(), QR_PNG :: binary(). get_url(Amount, Payload) -> gen_server:call(?MODULE, {get_url, Amount, Payload}). %% gen_server callbacks start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, none, []). %%----------------------------------------------------------------------------- %% process context below this line %%----------------------------------------------------------------------------- %% gen_server callbacks init(none) -> tell("starting fd_gridsd"), InitState = #s{}, {ok, InitState}. handle_call({get_url, Amount, Payload}, From, State) -> case i_get_url(Amount, Payload, From, State) of {ok, URL, PNG, NewState} -> {reply, {ok, URL, PNG}, NewState}; Error -> {reply, Error, State} end; handle_call(Unexpected, From, State) -> tell("~tp: unexpected call from ~tp: ~tp", [?MODULE, Unexpected, From]), {noreply, State}. handle_cast(Unexpected, State) -> tell("~tp: unexpected cast: ~tp", [?MODULE, Unexpected]), {noreply, State}. handle_info(Unexpected, State) -> tell("~tp: unexpected info: ~tp", [?MODULE, Unexpected]), {noreply, State}. code_change(_, State, _) -> {ok, State}. terminate(_, _) -> ok. %%----------------------------------------------------------------------------- %% internals %%----------------------------------------------------------------------------- -spec i_get_url(Amount, Payload, From, State) -> Result when Amount :: none | pos_integer(), Payload :: none | binary(), From :: {pid(), reference()}, State :: state(), URL :: string(), QR_PNG :: binary(). Result :: {ok, URL, QR_PNG, NewState} | {error, term()}. i_get_url(Amount, Payload, From, State) -> NetworkId = fewd:network_id(), Pubkey = fewd:pubkey(), URL = gmgrids:encode({spend, NetworkId, Pubkey}, [{amount, Amount}, {payload, Payload}]),