From ae8c659c037a35c7f15bb5a1d29e72c59d2c794b Mon Sep 17 00:00:00 2001 From: Craig Everett Date: Mon, 4 May 2026 23:58:02 +0900 Subject: [PATCH] WIP: Add gd_con:list_calls/0 and gd_v_call:tx_*/1 --- src/gd_con.erl | 14 ++++++++- src/gd_v_call.erl | 80 +++++++++++++++++++++++++++++++---------------- 2 files changed, 66 insertions(+), 28 deletions(-) diff --git a/src/gd_con.erl b/src/gd_con.erl index a26c234..1e74652 100644 --- a/src/gd_con.erl +++ b/src/gd_con.erl @@ -16,7 +16,7 @@ refresh/0, nonce/1, spend/1, chain_id/0, grids/1, sign_mess/1, sign_binary/1, sign_tx/1, sign_call/3, - deploy/1, prompt_call/3, + deploy/1, prompt_call/3, list_calls/0, make_key/6, recover_key/1, mnemonic/1, rename_key/2, drop_key/1, list_keys/0, add_node/1, set_sole_node/1]). -export([tic/1, update_balance/2]). @@ -217,6 +217,18 @@ prompt_call(FunDef, ConID, Build) -> gen_server:cast(?MODULE, {prompt_call, FunDef, ConID, Build}). +-spec list_calls() -> Calls + when Calls :: [{Name, Process}], + Name :: term(), + Process :: wx:wx_object() | pid(). + +%% @doc +%% List any active contract call tasks. + +list_calls() -> + gen_server:call(?MODULE, list_calls). + + -spec make_key(Type, Size, Name, Seed, Encoding, Transform) -> ok when Type :: {eddsa, ed25519}, Size :: 256, diff --git a/src/gd_v_call.erl b/src/gd_v_call.erl index 568a30d..f89db0c 100644 --- a/src/gd_v_call.erl +++ b/src/gd_v_call.erl @@ -7,7 +7,7 @@ -behavior(wx_object). %-behavior(gd_v). -include_lib("wx/include/wx.hrl"). --export([to_front/1, result/2]). +-export([to_front/1, tx_hash/1, tx_data/1, tx_info/1]). -export([start_link/1]). -export([init/1, terminate/2, code_change/3, handle_call/3, handle_cast/2, handle_info/2, handle_event/2]). @@ -36,8 +36,9 @@ status = none :: status(), action = #w{} :: #w{}, tx_data = none :: none | map(), - tx_hash = #w{} :: #w{}, % wxTextCtrl, single-line - tx_info = none :: #w{}, % wxTextCtrl, multi-line + tx_info = none :: none | map(), + hash = #w{} :: #w{}, % wxTextCtrl, single-line + info = #w{} :: #w{}, % wxTextCtrl, multi-line out = #w{} :: #w{}, copy = #w{} :: #w{}}). @@ -47,9 +48,9 @@ -type fun_def() :: {fun_name(), fun_type()}. -type param() :: {Label :: string(), Check :: fun(), #w{}}. -type status() :: none - | {submitted, TX_Hash :: string()} - | {rejected, Reason :: string()} - | {included, TX_Info :: map()}. + | submitted + | rejected + | included. %%% Interface @@ -61,12 +62,28 @@ to_front(Win) -> wx_object:cast(Win, to_front). --spec result(Win, Outcome) -> ok - when Win :: wx:wx_object(), - Outcome :: term(). +-spec tx_hash(Win) -> Result + when Win :: pid() | wx:wx_object(), + Result :: none | string(). -result(Win, Outcome) -> - wx_object:cast(Win, {result, Outcome}). +tx_hash(Win) -> + wx_object:call(Win, tx_hash). + + +-spec tx_data(Win) -> Result + when Win :: pid() | wx:wx_object(), + Result :: none | map(). + +tx_hash(Win) -> + wx_object:call(Win, tx_data). + + +-spec tx_info(Win) -> Result + when Win :: pid() | wx:wx_object(), + Result :: none | map(). + +tx_hash(Win) -> + wx_object:call(Win, tx_info). @@ -146,7 +163,7 @@ init({Prefs, FunDef = {FunName, FunType}, ConID, Build, Selected, Keys}) -> #s{wx = Wx, frame = Frame, j = J, prefs = Prefs, fundef = FunDef, con_id = ConID, build = Build, args = Args, kp = KP, params = Params, - action = Action, + action = Action, status = none, tx_data = TX_Data, tx_hash = TX_Hash, tx_info = TX_Info, out = Out, copy = Copy}, {Frame, State}. @@ -234,6 +251,12 @@ call_param_sizer(Frame, J) -> {ParamSz, Params}. +handle_call(tx_hash, _, State = #s{tx_hash = TXHash}) -> + {reply, TXHash, State}; +handle_call(tx_data, _, State = #s{tx_data = TXData}) -> + {reply, TXData, State}; +handle_call(tx_info, _, State = #s{tx_info = TXInfo}) -> + {reply, TXInfo, State}; handle_call(Unexpected, From, State) -> ok = log(warning, "Unexpected call from ~tp: ~tp~n", [From, Unexpected]), {noreply, State}. @@ -249,9 +272,13 @@ handle_info(Unexpected, State) -> {noreply, State}. +handle_event(#wx{event = #wxCommand{type = command_button_clicked}, id = ID}, + State = #s{action = #w{id = ID}, status = none}) -> + NewState = prep_call(State), + {noreply, NewState}; handle_event(#wx{event = #wxCommand{type = command_button_clicked}, id = ID}, State = #s{action = #w{id = ID}}) -> - NewState = engage(State), + NewState = check_tx(State), {noreply, NewState}; handle_event(#wx{event = #wxCommand{type = command_button_clicked}, id = ID}, State = #s{copy = #w{id = ID}}) -> @@ -280,34 +307,33 @@ handle_troubling(State = #s{frame = Frame}, Info) -> State. -engage(State) -> +prep_call(State) -> case gd_con:chain_id() of - {ok, ChainID} -> engage2(State, ChainID); + {ok, ChainID} -> prep_call2(State, ChainID); Error -> handle_troubling(State, Error) end. -engage2(State, ChainID) -> +prep_call2(State, ChainID) -> tell(info, "ChainID: ~p", [ChainID]), case params(State) of - {ok, Params} -> engage3(State, ChainID, Params); + {ok, Params} -> prep_call3(State, ChainID, Params); Error -> handle_troubling(State, Error) end. -engage3(State, ChainID, Params) -> +prep_call3(State, ChainID, Params) -> tell(info, "Params: ~p", [Params]), case args(State) of - {ok, Args} -> engage4(State, ChainID, Params, {sophia, Args}); + {ok, Args} -> prep_call4(State, ChainID, Params, {sophia, Args}); Error -> handle_troubling(State, Error) end. -engage4(State = #s{fundef = {"init", init}, build = Build}, ChainID, Params, Args) -> - tell(info, "Args: ~p", [Args]), +prep_call4(State = #s{fundef = {"init", init}, build = Build}, ChainID, Params, Args) -> {CallerID, Nonce, TTL, Gas, GP, Amount} = Params, case hz:contract_create_built(CallerID, Nonce, Amount, TTL, Gas, GP, Build, Args) of {ok, CreateTX} -> deploy(State, ChainID, CallerID, CreateTX); Error -> handle_troubling(State, Error) end; -engage4(State = #s{fundef = {Name, Type}, con_id = ConID, build = Build}, ChainID, Params, Args) -> +prep_call4(State = #s{fundef = {Name, Type}, con_id = ConID, build = Build}, ChainID, Params, Args) -> {CallerID, Nonce, TTL, Gas, GP, Amount} = Params, AACI = maps:get(aaci, Build), case hz:contract_call(CallerID, Nonce, Gas, GP, Amount, TTL, AACI, ConID, Name, Args) of @@ -357,11 +383,11 @@ deploy(State, ChainID, CallerID, CreateTX) -> deploy2(State, SignedTX) -> case hz:post_tx(SignedTX) of - {ok, Data} -> - case maps:is_key("reason", Data) of - false -> check_tx(State#s{tx_data = Data}); - true -> handle_troubling(State, {error, Data}) - end; + {ok, Data = #{"tx_hash" := TXHash}} -> + ok = log(info, "Submitted transaction ~s", [TXHash]), + check_tx(State#s{tx_data = Data, status = submitted}); + {ok, #{"reason" := Reason}} -> + handle_troubling(State, {error, Reason}); Error -> handle_troubling(State, Error) end.