Make Contract Calls Great Again #35
+72
-61
@@ -91,23 +91,26 @@ init({Prefs, {FunName, FunType}, ConID, Build, Selected, Keys}) ->
|
||||
|
||||
KeySz = wxStaticBoxSizer:new(?wxVERTICAL, Frame, [{label, J("Signature Key")}]),
|
||||
KeyPicker = wxChoice:new(Frame, ?wxID_ANY, [{choices, Keys}]),
|
||||
ok = wxChoice:setSelection(KeyPicker, Selected),
|
||||
_ = wxStaticBoxSizer:add(KeySz, KeyPicker, zxw:flags(wide)),
|
||||
|
||||
{ArgSz, Args} = call_arg_sizer(Frame, J, FunSpec),
|
||||
{ParamSz, TTL_T, GasPriceT, GasT, AmountT} = call_param_sizer(Frame, J),
|
||||
{ArgSz, Args, Dimensions} = call_arg_sizer(Frame, J, FunSpec),
|
||||
{ParamSz, Params} = call_param_sizer(Frame, J),
|
||||
|
||||
Action = #w{wx = ActionBn} = gd_lib:button(Frame, ActionLabel),
|
||||
|
||||
TX_Sz = wxStaticBoxSizer:new(?wxVERTICAL, Frame, [{label, J("Transaction Info")}]),
|
||||
TX_Hash = #w{wx = HashTxt} = gd_lib:mono_text(TX_Sz, tx_hash),
|
||||
TX_Data = #w{wx = HashT} = gd_lib:mono_text(TX_Sz, tx_hash),
|
||||
TX_Hash = #w{wx = HashT} = gd_lib:mono_text(TX_Sz, tx_hash),
|
||||
TX_Info = #w{wx = HashT} = gd_lib:mono_text(TX_Sz, tx_hash),
|
||||
Line = wxStaticLine:new(TX_Sz, [{style, ?wxLI_HORIZONTAL}]),
|
||||
Out = #w{wx = OutTxt} = gd_lib:mono_text(TX_Sz, out),
|
||||
Copy = #w{wx = CopyBn} = gd_lib:button(Frame, J("Copy")),
|
||||
|
||||
_ = wxStaticBoxSizer:add(TX_Sz, HashTxt, zxw:flags({wide, 5})),
|
||||
_ = wxStaticBoxSizer:add(TX_Sz, Line, zxw:flags({wide, 5})),
|
||||
_ = wxStaticBoxSizer:add(TX_Sz, OutTxt, zxw:flags({wide, 5})),
|
||||
_ = wxStaticBoxSizer:add(TX_Sz, CopyBn, zxw:flags({wide, 5})),
|
||||
_ = wxStaticBoxSizer:add(TX_Sz, HashT, zxw:flags({wide, 5})),
|
||||
_ = wxStaticBoxSizer:add(TX_Sz, Line, zxw:flags({wide, 5})),
|
||||
_ = wxStaticBoxSizer:add(TX_Sz, OutTxt, zxw:flags({wide, 5})),
|
||||
_ = wxStaticBoxSizer:add(TX_Sz, CopyBn, zxw:flags({wide, 5})),
|
||||
|
||||
_ = wxSizer:add(MainSz, ArgSz, zxw:flags({wide, 5})),
|
||||
_ = wxSizer:add(MainSz, KeySz, zxw:flags({wide, 5})),
|
||||
@@ -122,11 +125,12 @@ init({Prefs, {FunName, FunType}, 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,
|
||||
con_id = ConID, build = Build,
|
||||
args = Args, kp = KeyPicker,
|
||||
ttl = TTL_T, gasprice = GasPriceT, gas = GasT, amount = AmountT,
|
||||
action = Action, copy = Copy},
|
||||
#s{wx = Wx, frame = Frame, j = J, prefs = Prefs,
|
||||
con_id = ConID, build = Build,
|
||||
args = Args, kp = KeyPicker, params = Params,
|
||||
action = Action,
|
||||
tx_data = TX_Data, tx_hash = TX_Hash, tx_info = TX_Info,
|
||||
out = Out, copy = Copy},
|
||||
{Frame, State}.
|
||||
|
||||
|
||||
@@ -182,28 +186,33 @@ call_param_sizer(Frame, J) ->
|
||||
GridSz = wxFlexGridSizer:new(2, 4, 4),
|
||||
ok = wxFlexGridSizer:setFlexibleDirection(GridSz, ?wxHORIZONTAL),
|
||||
ok = wxFlexGridSizer:addGrowableCol(GridSz, 1),
|
||||
TTL_L = wxStaticText:new(Frame, ?wxID_ANY, "TTL"),
|
||||
TTL_Tx = wxTextCtrl:new(Frame, ?wxID_ANY),
|
||||
ok = wxTextCtrl:setValue(TTL_Tx, integer_to_list(DefTTL)),
|
||||
GasP_L = wxStaticText:new(Frame, ?wxID_ANY, J("Gas Price")),
|
||||
GasP_Tx = wxTextCtrl:new(Frame, ?wxID_ANY),
|
||||
ok = wxTextCtrl:setValue(GasP_Tx, integer_to_list(DefGasP)),
|
||||
Gas_L = wxStaticText:new(Frame, ?wxID_ANY, J("Gas")),
|
||||
Gas_Tx = wxTextCtrl:new(Frame, ?wxID_ANY),
|
||||
ok = wxTextCtrl:setValue(Gas_Tx, integer_to_list(DefGas)),
|
||||
Amount_L = wxStaticText:new(Frame, ?wxID_ANY, J("TX Amount")),
|
||||
Amount_Tx = wxTextCtrl:new(Frame, ?wxID_ANY),
|
||||
ok = wxTextCtrl:setValue(Amount_Tx, integer_to_list(DefAmount)),
|
||||
_ = wxFlexGridSizer:add(GridSz, TTL_L, zxw:flags({base, 5})),
|
||||
_ = wxFlexGridSizer:add(GridSz, TTL_Tx, zxw:flags({wide, 5})),
|
||||
_ = wxFlexGridSizer:add(GridSz, GasP_L, zxw:flags({base, 5})),
|
||||
_ = wxFlexGridSizer:add(GridSz, GasP_Tx, zxw:flags({wide, 5})),
|
||||
_ = wxFlexGridSizer:add(GridSz, Gas_L, zxw:flags({base, 5})),
|
||||
_ = wxFlexGridSizer:add(GridSz, Gas_Tx, zxw:flags({wide, 5})),
|
||||
_ = wxFlexGridSizer:add(GridSz, Amount_L, zxw:flags({base, 5})),
|
||||
_ = wxFlexGridSizer:add(GridSz, Amount_Tx, zxw:flags({wide, 5})),
|
||||
TTL_L = wxStaticText:new(Frame, ?wxID_ANY, "TTL"),
|
||||
TTL_T = wxTextCtrl:new(Frame, ?wxID_ANY),
|
||||
ok = wxTextCtrl:setValue(TTL_T, integer_to_list(DefTTL)),
|
||||
GasP_L = wxStaticText:new(Frame, ?wxID_ANY, J("Gas Price")),
|
||||
GasP_T = wxTextCtrl:new(Frame, ?wxID_ANY),
|
||||
ok = wxTextCtrl:setValue(GasP_T, integer_to_list(DefGasP)),
|
||||
Gas_L = wxStaticText:new(Frame, ?wxID_ANY, J("Gas")),
|
||||
Gas_T = wxTextCtrl:new(Frame, ?wxID_ANY),
|
||||
ok = wxTextCtrl:setValue(Gas_T, integer_to_list(DefGas)),
|
||||
Amount_L = wxStaticText:new(Frame, ?wxID_ANY, J("TX Amount")),
|
||||
Amount_T = wxTextCtrl:new(Frame, ?wxID_ANY),
|
||||
ok = wxTextCtrl:setValue(Amount_T, integer_to_list(DefAmount)),
|
||||
_ = wxFlexGridSizer:add(GridSz, TTL_L, zxw:flags({base, 5})),
|
||||
_ = wxFlexGridSizer:add(GridSz, TTL_T, zxw:flags({wide, 5})),
|
||||
_ = wxFlexGridSizer:add(GridSz, GasP_L, zxw:flags({base, 5})),
|
||||
_ = wxFlexGridSizer:add(GridSz, GasP_T, zxw:flags({wide, 5})),
|
||||
_ = wxFlexGridSizer:add(GridSz, Gas_L, zxw:flags({base, 5})),
|
||||
_ = wxFlexGridSizer:add(GridSz, Gas_T, zxw:flags({wide, 5})),
|
||||
_ = wxFlexGridSizer:add(GridSz, Amount_L, zxw:flags({base, 5})),
|
||||
_ = wxFlexGridSizer:add(GridSz, Amount_T, zxw:flags({wide, 5})),
|
||||
_ = wxSizer:add(ParamSz, GridSz, zxw:flags(wide)),
|
||||
{ParamSz, TTL_Tx, GasP_Tx, Gas_Tx, Amount_Tx}.
|
||||
Params =
|
||||
[{ "TTL", fun gte_0/1, TTL_T},
|
||||
{J("Gas Price"), fun gt_0/1, GasP_T},
|
||||
{J("Gas") , fun gt_0/1, Gas_T},
|
||||
{J("TX Amount"), fun gt_0/1, Amount_T}],
|
||||
{ParamSz, Params}.
|
||||
|
||||
|
||||
handle_call(Unexpected, From, State) ->
|
||||
@@ -251,34 +260,41 @@ handle_troubling(State = #s{frame = Frame}, Info) ->
|
||||
|
||||
engage(State) ->
|
||||
case gd_con:chain_id() of
|
||||
{ok, ChainID} -> engage(State, ChainID);
|
||||
{ok, ChainID} -> engage2(State, ChainID);
|
||||
Error -> handle_troubling(State, Error)
|
||||
end.
|
||||
|
||||
engage(State = #s{fundef = {"init", init}, build = Build}, ChainID) ->
|
||||
{CallerID, Nonce, TTL, GasPrice, Gas, Amount} = params(State),
|
||||
Args = args(State),
|
||||
case hz:contract_create_built(CallerID,
|
||||
Nonce, Amount, TTL, Gas, GasPrice,
|
||||
Build, InitArgs) of
|
||||
engage2(State, ChainID) ->
|
||||
case params(State) of
|
||||
{ok, Params} -> engage3(State, ChainID, Params);
|
||||
Error -> handle_troubling(State, Error)
|
||||
end.
|
||||
|
||||
engage3(State, ChainID, Params) ->
|
||||
case args(State) of
|
||||
{ok, Args} -> engage4(State, ChainID, Params, {sophia, Args});
|
||||
Error -> handle_troubling(State, Error)
|
||||
end.
|
||||
|
||||
engage4(State = #s{fundef = {"init", init}, build = Build}, ChainID, Params, Args) ->
|
||||
{CallerID, Nonce, TTL, GP, Gas, Amount} = Params,
|
||||
case hz:contract_create_built(CallerID, Nonce, Gas, GP, Amount, TTL, Build, Args) of
|
||||
{ok, CreateTX} -> deploy(State, ChainID, CallerID, CreateTX);
|
||||
Error -> handle_troubling(State, Error)
|
||||
end;
|
||||
engage(State = #s{con_id = ConID, build = Build, fundef = {Name, Type}}, ChainID) ->
|
||||
AACI = maps:get(aaci, AACI),
|
||||
{PK, Nonce, TTL, GasP, Gas, Amount} = params(State),
|
||||
Args = args(State),
|
||||
case hz:contract_call(PK, Nonce, Gas, GasP, Amount, TTL, AACI, ConID, Name, Args) of
|
||||
engage4(State = #s{con_id = ConID, build = Build, fundef = {Name, Type}}, ChainID, Params, Args) ->
|
||||
{CallerID, Nonce, TTL, GP, Gas, Amount} = Params,
|
||||
AACI = maps:get(aaci, Build),
|
||||
case hz:contract_call(CallerID, Nonce, Gas, GP, Amount, TTL, AACI, ConID, Name, Args) of
|
||||
{ok, UnsignedTX} ->
|
||||
case Type of
|
||||
call -> do_call(State, ChainID, PK, UnsignedTX);
|
||||
call -> do_call(State, ChainID, CallerID, UnsignedTX);
|
||||
dryr -> do_dry_run(State, ConID, UnsignedTX)
|
||||
end;
|
||||
Error ->
|
||||
handle_troubling(State, Error)
|
||||
end.
|
||||
|
||||
|
||||
params(State = #s{kp = #w{wx = KeyPicker}}) ->
|
||||
ID = wxChoice:getString(KeyPicker, wxChoice:getSelection(KeyPicker)),
|
||||
PK = unicode:characters_to_binary(ID),
|
||||
@@ -287,20 +303,10 @@ params(State = #s{kp = #w{wx = KeyPicker}}) ->
|
||||
Error -> handle_troubling(State, Error)
|
||||
end.
|
||||
|
||||
params2(#s{ttl = #w{wx = TTL_T},
|
||||
gasprice = #w{wx = GasPriceT},
|
||||
gas = #w{wx = GasT},
|
||||
amount = #w{wx = AmountT}},
|
||||
PK,
|
||||
Nonce) ->
|
||||
ToExtract =
|
||||
[{ttl, fun gte_0/1, TTL_T},
|
||||
{gasprice, fun gt_0/1, GasPriceT},
|
||||
{gas, fun gt_0/1, GasT},
|
||||
{amount, fun gt_0/1, AmountT}],
|
||||
case lists:foldl(fun extract/2, {ok, []}, ToCheck) of
|
||||
{ok, [TTL, GasPrice, Gas, Amount]} ->
|
||||
{error, Out} ->
|
||||
params2(#s{params = Params}, PK, Nonce) ->
|
||||
case lists:foldl(fun extract/2, {ok, []}, Params) of
|
||||
{ok, [TTL, GP, Gas, Amount]} -> {ok, {PK, Nonce, TTL, GP, Gas, Amount}};
|
||||
Error -> Error
|
||||
end.
|
||||
|
||||
extract({Name, Check, Widget}, {Status, Out}) ->
|
||||
@@ -310,7 +316,12 @@ extract({Name, Check, Widget}, {Status, Out}) ->
|
||||
end.
|
||||
|
||||
|
||||
% TODO: Put some basic checking in here, like for blank strings, at least.
|
||||
% It should be possible to perform type/parse checks over this since we have
|
||||
% access to the AACI. But not today.
|
||||
args(#s{args = ArgFields}) ->
|
||||
Values = [wxTextCtrl:getValue(W) || #w{wx = W} <- ArgFields],
|
||||
{ok, Values}.
|
||||
|
||||
|
||||
deploy(State, ChainID, CallerID, CreateTX) ->
|
||||
|
||||
Reference in New Issue
Block a user