Move common functionality to the Hakuzaru library

This commit is contained in:
2025-03-05 22:35:18 +09:00
parent 970eb1680f
commit 4fd4ab3f54
2 changed files with 45 additions and 106 deletions
+13 -28
View File
@@ -773,9 +773,8 @@ spend2(#poa{id = ID, name = Name}, Nonce, Height, State = #s{frame = Frame, j =
ok =
case wxDialog:showModal(Dialog) of
?wxID_OK ->
{ok, PK} = decode_account_id(ID),
TX =
#spend_tx{sender_id = gmser_id:create(account, PK),
#spend_tx{sender_id = ID,
recipient_id = wxTextCtrl:getValue(ToTx),
amount = wxTextCtrl:getValue(AmtTx),
gas_price = wxSlider:getValue(GasSl),
@@ -783,49 +782,35 @@ spend2(#poa{id = ID, name = Name}, Nonce, Height, State = #s{frame = Frame, j =
ttl = Height + wxSlider:getValue(TTL_Sl),
nonce = Nonce,
payload = wxTextCtrl:getValue(DataTx)},
clean_spend(ID, TX);
clean_spend(TX);
?wxID_CANCEL ->
ok
end,
ok = wxDialog:destroy(Dialog),
State.
clean_spend(_, #spend_tx{recipient_id = ""}) ->
clean_spend(#spend_tx{recipient_id = ""}) ->
ok;
clean_spend(ID, TX = #spend_tx{recipient_id = S}) when is_list(S) ->
case decode_account_id(S) of
{ok, PK} -> clean_spend(ID, TX#spend_tx{recipient_id = gmser_id:create(account, PK)});
Error -> tell("Decode recipient_id failed with: ~tp", [Error])
end;
clean_spend(ID, TX = #spend_tx{amount = S}) when is_list(S) ->
clean_spend( TX = #spend_tx{amount = S}) when is_list(S) ->
case string_to_price(S) of
{ok, Amount} -> clean_spend(ID, TX#spend_tx{amount = Amount});
{ok, Amount} -> clean_spend(TX#spend_tx{amount = Amount});
{error, _} -> ok
end;
clean_spend(ID, TX = #spend_tx{gas_price = S}) when is_list(S) ->
clean_spend(TX = #spend_tx{gas_price = S}) when is_list(S) ->
case is_int(S) of
true -> clean_spend(ID, TX#spend_tx{gas_price = list_to_integer(S)});
true -> clean_spend(TX#spend_tx{gas_price = list_to_integer(S)});
false -> ok
end;
clean_spend(ID, TX = #spend_tx{gas = S}) when is_list(S) ->
clean_spend(TX = #spend_tx{gas = S}) when is_list(S) ->
case is_int(S) of
true -> clean_spend(ID, TX#spend_tx{gas = list_to_integer(S)});
true -> clean_spend(TX#spend_tx{gas = list_to_integer(S)});
false -> ok
end;
clean_spend(ID, TX = #spend_tx{payload = S}) when is_list(S) ->
clean_spend(ID, TX#spend_tx{payload = list_to_binary(S)});
clean_spend(ID, TX) ->
gd_con:spend(ID, TX).
clean_spend(TX = #spend_tx{payload = S}) when is_list(S) ->
clean_spend(TX#spend_tx{payload = list_to_binary(S)});
clean_spend(TX) ->
gd_con:spend(TX).
decode_account_id(S) when is_list(S) ->
decode_account_id(list_to_binary(S));
decode_account_id(B) ->
try
{account_pubkey, PK} = gmser_api_encoder:decode(B),
{ok, PK}
catch
E:R -> {E, R}
end.
is_int(S) ->
lists:all(fun(C) -> $0 =< C andalso C =< $9 end, S).