Make Contract Calls Great Again

This commit is contained in:
2026-05-06 12:46:26 +09:00
parent 8b390f8d82
commit b071467faa
3 changed files with 110 additions and 59 deletions
+64 -28
View File
@@ -25,6 +25,7 @@
prefs = #{} :: map(),
con_id = <<"">> :: binary(),
fundef = none :: none | fun_def(),
funret = none :: none | term(), % FIXME
build = none :: none | map(),
args = [] :: [#w{}],
kp = #w{} :: #w{},
@@ -94,14 +95,14 @@ init({Prefs, FunDef = {FunName, FunIlk}, ConID, Build, Selected, Keys}) ->
Trans = gd_jt:read_translations(?MODULE),
J = gd_jt:j(Lang, Trans),
{aaci, ConName, FunSpecs, _} = maps:get(aaci, Build),
FunSpec = maps:get(FunName, FunSpecs),
FunSpec = {FunArgs, FunReturn} = maps:get(FunName, FunSpecs),
{CallTypeLabel, ActionLabel} =
case FunIlk of
call -> {J("Contract Call"), J("Submit Call")};
dryr -> {J("Dry Run"), J("Submit Dry Run")};
init -> {J("Deploy"), J("Deploy")}
end,
Arity = integer_to_list(length(element(1, FunSpec))),
Arity = integer_to_list(length(FunArgs)),
Title = [CallTypeLabel, ": ", ConName, ".", FunName, "/", Arity],
Wx = wx:new(),
Frame = wxFrame:new(Wx, ?wxID_ANY, Title),
@@ -127,19 +128,19 @@ init({Prefs, FunDef = {FunName, FunIlk}, ConID, Build, Selected, Keys}) ->
TX_Hash = #w{wx = HashT} = gd_lib:mono_text(TX_Sz_Box, tx_hash, "", Single),
TX_Info = #w{wx = InfoT} = gd_lib:mono_text(TX_Sz_Box, tx_info, "", Multi),
_ = wxStaticBoxSizer:add(TX_Sz, HashT, zxw:flags({base, 5})),
_ = wxStaticBoxSizer:add(TX_Sz, InfoT, zxw:flags({base, 5})),
_ = wxStaticBoxSizer:add(TX_Sz, HashT, zxw:flags({base, 5})),
_ = wxStaticBoxSizer:add(TX_Sz, InfoT, zxw:flags({wide, 5})),
ArgSzArgs =
case HasArgs of
true -> {wide, 5};
false -> {base, 5}
true -> [{proportion, 2}, {flag, ?wxEXPAND bor ?wxALL}, {border, 5}];
false -> zxw:flags({base, 5})
end,
_ = wxSizer:add(MainSz, ArgSz, zxw:flags(ArgSzArgs)),
_ = wxSizer:add(MainSz, ArgSz, ArgSzArgs),
_ = wxSizer:add(MainSz, KeySz, zxw:flags({base, 5})),
_ = wxSizer:add(MainSz, ParamSz, zxw:flags({base, 5})),
_ = wxSizer:add(MainSz, ActionBn, zxw:flags({base, 5})),
_ = wxSizer:add(MainSz, TX_Sz, zxw:flags({base, 5})),
_ = wxSizer:add(MainSz, TX_Sz, [{proportion, 1}, {flag, ?wxEXPAND bor ?wxALL}, {border, 5}]),
_ = wxFrame:setSizer(Frame, MainSz),
_ = wxFrame:setSize(Frame, {900, 900}),
@@ -148,9 +149,9 @@ init({Prefs, FunDef = {FunName, FunIlk}, ConID, Build, Selected, Keys}) ->
ok = wxFrame:connect(Frame, command_button_clicked),
true = wxFrame:show(Frame),
State =
#s{wx = Wx, frame = Frame, j = J, prefs = Prefs,
fundef = FunDef, con_id = ConID, build = Build,
args = Args, kp = KP, params = Params,
#s{wx = Wx, frame = Frame, j = J, prefs = Prefs,
fundef = FunDef, funret = FunReturn, con_id = ConID, build = Build,
args = Args, kp = KP, params = Params,
return = Return, copy = Copy,
action = Action, status = none,
hash = TX_Hash, info = TX_Info},
@@ -306,7 +307,7 @@ code_change(_, State, _) ->
retire(State = #s{frame = Frame}) ->
ok = wxWindow:destroy(Frame),
{noreply, State}.
{stop, normal, State}.
terminate(Reason, State) ->
@@ -433,10 +434,44 @@ do_call2(State = #s{action = #w{wx = ActionB}}, SignedTX) ->
do_dry_run(State = #s{action = #w{wx = ActionB}}, ConID, TX) ->
_ = wxButton:disable(ActionB),
case hz:dry_run(TX) of
{ok, Result} -> update_info(State#s{tx_info = Result});
{ok, Result} -> dry_run2(State#s{tx_info = Result});
Other -> handle_troubling(State, {error, ConID, Other})
end.
dry_run2(State = #s{funret = ReturnType,
return = #w{wx = ReturnT},
copy = #w{wx = CopyB},
tx_data = TXData,
tx_info = TXInfo,
hash = #w{wx = HashT},
info = #w{wx = InfoT}}) ->
ReturnV =
case TXInfo of
#{"results" :=
[#{"call_obj" :=
#{"return_type" := "revert",
"return_value" := ReturnCB},
"result" := "ok","type" := "contract_call"}]} ->
io_lib:format("Revert: ~ts", [hz:decode_bytearray(ReturnCB, sophia)]);
#{"results" :=
[#{"call_obj" :=
#{"return_type" := "ok",
"return_value" := ReturnCB},
"result" := "ok","type" := "contract_call"}]} ->
hz:decode_bytearray(ReturnCB, {sophia, ReturnType});
Other ->
io_lib:format("???: ~tp", [Other])
end,
_ = wxButton:enable(CopyB),
FormattedHash = io_lib:format("~tp", [TXData]),
FormattedInfo = io_lib:format("~tp", [TXInfo]),
ok = wxTextCtrl:setValue(ReturnT, ReturnV),
ok = wxTextCtrl:setValue(HashT, FormattedHash),
ok = wxTextCtrl:setValue(InfoT, FormattedInfo),
State.
check_tx(State = #s{frame = Frame,
j = J,
fundef = {_, init},
@@ -448,15 +483,22 @@ check_tx(State = #s{frame = Frame,
case hz:tx_info(TXHash) of
{ok, Info = #{"call_info" := #{"return_type" := "ok", "contract_id" := ConID}}} ->
ok = tell(info, "Contract deployed: ~p", [Info]),
_ = wxButton:disable(ActionB),
ok = gd_con:open_contract(ConID),
ok = wxWindow:destroy(Frame),
exit(0);
self() ! retire,
State;
{error, "Tx not mined"} ->
ok = wxTextCtrl:setValue(InfoT, J("[Transaction not yet mined.]")),
ok = wxButton:setLabel(ActionB, J("Check Deployment Status")),
_ = wxButton:enable(ActionB),
State;
Other ->
FormattedJunk = io_lib:format("~tp", [Other]),
ok = wxTextCtrl:setValue(InfoT, FormattedJunk),
ok = wxButton:setLabel(ActionB, J("Deploy")),
ok = wxButton:setLabel(ActionB, J("Check Depoyment Status")),
_ = wxButton:enable(ActionB),
State#s{status = none}
State
end;
check_tx(State = #s{j = J,
con_id = ConID,
@@ -476,18 +518,18 @@ check_tx(State = #s{j = J,
_ = wxButton:disable(ActionB),
ok = wxTextCtrl:setValue(ReturnT, Value),
ok = wxTextCtrl:setValue(InfoT, FormattedInfo),
ok = gd_con:show_call_result(ConID, Info),
update_info(State#s{status = included, tx_info = Info});
ok = gd_con:show_call(ConID, Info),
State#s{status = included, tx_info = Info};
{ok, Reason = #{"call_info" := #{"return_type" := "revert"}}} ->
_ = wxButton:enable(CopyB),
_ = wxButton:disable(ActionB),
FormattedInfo = io_lib:format("~tp", [Reason]),
ok = wxTextCtrl:setValue(ReturnT, FormattedInfo), % FIXME
ok = wxTextCtrl:setValue(InfoT, FormattedInfo),
ok = gd_con:show_call_result(ConID, Reason),
ok = gd_con:show_call(ConID, Reason),
State#s{status = rejected, tx_info = Reason};
{error, "Tx not mined"} ->
ok = wxTextCtrl:setValue(InfoT, J("Transaction not yet mined.")),
ok = wxTextCtrl:setValue(InfoT, J("[Transaction not yet mined.]")),
ok = wxButton:setLabel(ActionB, J("Check Transaction Status")),
_ = wxButton:enable(ActionB),
State;
@@ -502,16 +544,10 @@ check_tx(State = #s{tx_data = TXData,
State.
update_info(State = #s{tx_info = TXInfo, return = #w{wx = Return}}) ->
Formatted = io_lib:format("TXInfo: ~p~n", [TXInfo]),
ok = wxTextCtrl:setValue(Return, Formatted),
State.
copy(#s{tx_info = none}) ->
ok;
copy(#s{return = #w{wx = Return}}) ->
Output = wxTextCtrl:getValue(Return),
copy(#s{return = #w{wx = ReturnT}}) ->
Output = wxTextCtrl:getValue(ReturnT),
gd_lib:copy_to_clipboard(Output).