diff --git a/src/gd_con.erl b/src/gd_con.erl index 996b3fe..e00c5e7 100644 --- a/src/gd_con.erl +++ b/src/gd_con.erl @@ -632,23 +632,26 @@ do_refresh(State = #s{wallet = #wallet{endpoint = Node}}) -> do_refresh2(ChainID, State = #s{wallet = W = #wallet{poas = POAs}}) -> - CheckBalance = check_balance(ChainID), + CheckBalance = check_balance(ChainID, "gaju"), NewPOAs = lists:map(CheckBalance, POAs), ok = gd_gui:show(NewPOAs), NewW = W#wallet{chain_id = ChainID, poas = NewPOAs}, State#s{wallet = NewW}. -check_balance(ChainID) -> - fun(This = #poa{id = ID, balances = [#balance{coin = "gaju", total = OldBalance}]}) -> +check_balance(ChainID, Coin) -> + fun(This = #poa{id = ID, balances = Balances}) -> + #balance{dist = Dist} = lists:keyfind(Coin, #balance.coin, Balances), + Old = proplists:get_value(ChainID, Dist, 0), Pucks = case hz:acc(ID) of - {ok, #{"balance" := P}} -> P; - {error, "Account not found"} -> 0; - {error, timeout} -> OldBalance + {ok, #{"balance" := Old}} -> Old; + {ok, #{"balance" := New}} -> New; + {error, "Account not found"} -> 0; + {error, timeout} -> Old end, - Dist = [{ChainID, Pucks}], - Gaju = #balance{coin = "gaju", total = Pucks, dist = Dist}, + NewDist = [{ChainID, Pucks}], + Gaju = #balance{coin = "gaju", total = Pucks, dist = NewDist}, This#poa{balances = [Gaju]} end. @@ -1291,13 +1294,17 @@ do_close_wallet(State = #s{wallet = Current, wallets = Wallets, pass = Pass}) -> State#s{selected = 0, pass = none, wallet = none}. -save_wallet(#wr{path = Path, pass = false}, none, Wallet) -> +save_wallet(#wr{path = Path, pass = false}, none, Wallet = #wallet{name = Name}) -> ok = filelib:ensure_dir(Path), - file:write_file(Path, term_to_binary(Wallet)); -save_wallet(#wr{path = Path, pass = true}, Pass, Wallet) -> + ok = log(info, "Saving plain wallet ~ts file to disk...", [Name]), + ok = file:write_file(Path, term_to_binary(Wallet)), + log(info, "Wallet ~ts file written.", [Name]); +save_wallet(#wr{path = Path, pass = true}, Pass, Wallet = #wallet{name = Name}) -> ok = filelib:ensure_dir(Path), Cipher = encrypt(Pass, term_to_binary(Wallet)), - file:write_file(Path, Cipher). + ok = log(info, "Saving cipher wallet ~ts file to disk.", [Name]), + ok = file:write_file(Path, Cipher), + log(info, "Wallet ~ts file written.", [Name]). read(Path, none) -> @@ -1357,15 +1364,17 @@ handle_tic(State = #s{wallet = This = #wallet{poas = POAs, endpoint = Node}, timer = {T, MS}, selected = Selected}) when Selected > 0 -> + % TODO: The closure below is kind of silly. The internal hz:acc/1 account will + % Expand to become hz:acc/2, accepting a chain ID later. NewState = case ensure_hz_set(Node) of {ok, ChainID} -> POA = #poa{id = ID} = lists:nth(Selected, POAs), - CheckBalance = check_balance(ChainID), + CheckBalance = check_balance(ChainID, "gaju"), NewPOA = CheckBalance(POA), NewPOAs = lists:keystore(ID, #poa.id, POAs, NewPOA), ok = gd_gui:show(NewPOAs), - State#s{wallet = This#wallet{poas = POAs}}; + State#s{wallet = This#wallet{poas = NewPOAs}}; Error -> ok = log(info, "Balance update on tic failed with: ~p", [Error]), State @@ -1390,6 +1399,7 @@ cancel_timer(T) -> persist(Prefs) -> Path = prefs_path(), ok = filelib:ensure_dir(Path), + ok = log(info, "Writing prefs to disk."), zx_lib:write_terms(Path, proplists:from_map(Prefs)).