rvv001 - Display total Balance in FIAT

ShowFIATBalance New form added to CLI.hs
This commit is contained in:
Rene V. Vergara A. 2024-12-14 19:56:10 -05:00
parent c0520bcbc7
commit a290f9c912
2 changed files with 98 additions and 6 deletions

View file

@ -126,6 +126,8 @@ import Zenith.Utils
, jsonNumber , jsonNumber
, showAddress , showAddress
, validBarValue , validBarValue
, getZcashPrice
, displayValue
) )
data Name data Name
@ -150,6 +152,7 @@ data Name
| DeshieldField | DeshieldField
| TotalTranspField | TotalTranspField
| TotalShieldedField | TotalShieldedField
| SFBViewPort
deriving (Eq, Show, Ord) deriving (Eq, Show, Ord)
data DialogInput = DialogInput data DialogInput = DialogInput
@ -194,6 +197,7 @@ data DialogType
| AdrBookDelForm | AdrBookDelForm
| DeshieldForm | DeshieldForm
| ShieldForm | ShieldForm
| ShowFIATBalance
data DisplayType data DisplayType
= AddrDisplay = AddrDisplay
@ -211,7 +215,7 @@ data Tick
| TickMsg !String | TickMsg !String
| TickTx !HexString | TickTx !HexString
data DropDownItem = newtype DropDownItem =
DropdownItem String DropdownItem String
data State = State data State = State
@ -246,10 +250,14 @@ data State = State
, _tBalance :: !Integer , _tBalance :: !Integer
, _sBalance :: !Integer , _sBalance :: !Integer
, _currencyCode :: !T.Text , _currencyCode :: !T.Text
, _zprice :: !Double
} }
makeLenses ''State makeLenses ''State
zBalance :: State -> Double
zBalance st = (fromIntegral (st ^. balance) ) / 100000000
drawUI :: State -> [Widget Name] drawUI :: State -> [Widget Name]
drawUI s = [splashDialog s, helpDialog s, displayDialog s, inputDialog s, ui s] drawUI s = [splashDialog s, helpDialog s, displayDialog s, inputDialog s, ui s]
where where
@ -299,7 +307,8 @@ drawUI s = [splashDialog s, helpDialog s, displayDialog s, inputDialog s, ui s]
[ capCommand "W" "allets" [ capCommand "W" "allets"
, capCommand "A" "ccounts" , capCommand "A" "ccounts"
, capCommand "V" "iew address" , capCommand "V" "iew address"
, capCommand3 "" "S" "end Tx" , capCommand "S" "end Tx"
, capCommand3 "ba" "L" ("ance (" ++ ( T.unpack (st ^. currencyCode) )++ ")" )
]) ])
, C.hCenter , C.hCenter
(hBox (hBox
@ -376,7 +385,7 @@ drawUI s = [splashDialog s, helpDialog s, displayDialog s, inputDialog s, ui s]
else emptyWidget else emptyWidget
where where
keyList = keyList =
map (C.hCenter . str) ["?", "Esc", "w", "a", "v", "s", "b", "d", "q"] map (C.hCenter . str) ["?", "Esc", "w", "a", "v", "s", "b", "d", "l", "q"]
actionList = actionList =
map map
(hLimit 40 . str) (hLimit 40 . str)
@ -388,6 +397,7 @@ drawUI s = [splashDialog s, helpDialog s, displayDialog s, inputDialog s, ui s]
, "Send Tx" , "Send Tx"
, "Address Book" , "Address Book"
, "Shield/De-Shield" , "Shield/De-Shield"
, "Balance in Fiat"
, "Quit" , "Quit"
] ]
inputDialog :: State -> Widget Name inputDialog :: State -> Widget Name
@ -517,6 +527,36 @@ drawUI s = [splashDialog s, helpDialog s, displayDialog s, inputDialog s, ui s]
[ capCommand "C" "onfirm delete" [ capCommand "C" "onfirm delete"
, capCommand3 "" "<Esc>" " Cancel" , capCommand3 "" "<Esc>" " Cancel"
])) ]))
-- Show Balance in FIAT form
ShowFIATBalance ->
D.renderDialog
(D.dialog (Just $ str (" Account Balance (" ++ ( T.unpack (st ^. currencyCode)) ++ ") ") ) Nothing 60)
(withAttr abDefAttr $
setAvailableSize (50, 8) $
viewport SFBViewPort BT.Vertical $
vLimit 8 $
hLimit 50 $
vBox $
[
vLimit 4 $
hLimit 50 $
vBox $
[ C.hCenter (str $ " ")
, C.hCenter (str $ "1 ZEC = " ++ ( printf "%.2f" ( s ^. zprice ) ) ++ " " ++ (T.unpack ( s ^. currencyCode) ))
, C.hCenter (str $ " ")
, C.hCenter ( str $ " Balance: " ++ ( printf "%.8f" $ zBalance s ) ++ " ZEC ==> " ++ ( printf "%.2f" (( s ^. zprice ) * (zBalance s) ) ++ " " ++ (T.unpack ( s ^. currencyCode) )) )
]
, padTop Max $
vLimit 4 $
hLimit 50 $
withAttr abMBarAttr $
vBox $
[ C.hCenter (str " ")
, C.hCenter $
(capCommand "R" "efresh" <+> capCommand3 "E" "x" "it")
]
])
-- --
splashDialog :: State -> Widget Name splashDialog :: State -> Widget Name
splashDialog st = splashDialog st =
@ -997,6 +1037,7 @@ appEvent (BT.AppEvent t) = do
AdrBookDelForm -> return () AdrBookDelForm -> return ()
DeshieldForm -> return () DeshieldForm -> return ()
ShieldForm -> return () ShieldForm -> return ()
ShowFIATBalance -> return()
Blank -> do Blank -> do
if s ^. timer == 90 if s ^. timer == 90
then do then do
@ -1574,6 +1615,24 @@ appEvent (BT.VtyEvent e) = do
ev -> ev ->
BT.zoom deshieldForm $ do BT.zoom deshieldForm $ do
handleFormEvent (BT.VtyEvent ev) handleFormEvent (BT.VtyEvent ev)
-- Process ShowFIATBalance events
ShowFIATBalance -> do
case e of
V.EvKey (V.KChar 'x') [] ->
BT.modify $ set dialogBox Blank
V.EvKey (V.KChar 'r') [] -> do
BT.modify $ set dialogBox Blank
zpr <- liftIO $ getZcashPrice $ s ^. currencyCode
case zpr of
Just p -> do
BT.modify $ set zprice p
BT.modify $ set dialogBox ShowFIATBalance
Nothing -> do
BT.modify $ set msg ("CoinGecko is not responding!!!")
BT.modify $ set displayBox MsgDisplay
-- Process any other event
ev -> BT.zoom abAddresses $ L.handleListEvent ev
--
-- Process any other event -- Process any other event
Blank -> do Blank -> do
case e of case e of
@ -1600,6 +1659,22 @@ appEvent (BT.VtyEvent e) = do
BT.modify $ set dialogBox SendTx BT.modify $ set dialogBox SendTx
V.EvKey (V.KChar 'b') [] -> V.EvKey (V.KChar 'b') [] ->
BT.modify $ set dialogBox AdrBook BT.modify $ set dialogBox AdrBook
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
V.EvKey (V.KChar 'l') [] -> do
if s ^. network == MainNet
then do
zpr <- liftIO $ getZcashPrice $ s ^. currencyCode
case zpr of
Just p -> do
BT.modify $ set zprice p
BT.modify $ set dialogBox ShowFIATBalance
Nothing -> do
BT.modify $ set msg ("Currency not supported (" ++ T.unpack (s ^. currencyCode ) ++ ")!!!")
BT.modify $ set displayBox MsgDisplay
else do
BT.modify $ set msg "Balance conversion not available for TestNet"
BT.modify $ set displayBox MsgDisplay
-- <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
V.EvKey (V.KChar 'd') [] -> do V.EvKey (V.KChar 'd') [] -> do
pool <- liftIO $ runNoLoggingT $ initPool $ s ^. dbPath pool <- liftIO $ runNoLoggingT $ initPool $ s ^. dbPath
selAcc <- selAcc <-
@ -1651,6 +1726,7 @@ appEvent (BT.VtyEvent e) = do
msg msg
"Not enough transparent funds in this account" "Not enough transparent funds in this account"
BT.modify $ set displayBox MsgDisplay BT.modify $ set displayBox MsgDisplay
ev -> ev ->
case r of case r of
Just AList -> Just AList ->
@ -1804,6 +1880,7 @@ runZenithTUI config = do
tBal tBal
sBal sBal
currencyCode currencyCode
0
Left _e -> do Left _e -> do
print $ print $
"No Zebra node available on port " <> "No Zebra node available on port " <>

View file

@ -20,6 +20,7 @@ import qualified Data.ByteString.Lazy.Char8 as BL
import System.Directory import System.Directory
import System.Process (createProcess_, shell) import System.Process (createProcess_, shell)
import Text.Regex.Posix import Text.Regex.Posix
import Text.Printf (printf)
import ZcashHaskell.Orchard import ZcashHaskell.Orchard
( encodeUnifiedAddress ( encodeUnifiedAddress
, isValidUnifiedAddress , isValidUnifiedAddress
@ -62,7 +63,7 @@ displayZec s
| abs s < 100000000 = show (fromIntegral s / 100000) ++ " mZEC" | abs s < 100000000 = show (fromIntegral s / 100000) ++ " mZEC"
| otherwise = show (fromIntegral s / 100000000) ++ " ZEC " | otherwise = show (fromIntegral s / 100000000) ++ " ZEC "
-- | Helper function to display small amounts of ZEC -- | Helper function to display small amounts of TAZ
displayTaz :: Integer -> String displayTaz :: Integer -> String
displayTaz s displayTaz s
| abs s < 100 = show s ++ " tazs" | abs s < 100 = show s ++ " tazs"
@ -276,3 +277,17 @@ getZcashPrice currency = do
_ -> return Nothing _ -> return Nothing
_ -> return Nothing _ -> return Nothing
-- Function to test if CoinGecko supports a currency code
chkCurrencyCode :: T.Text -> IO ( Bool )
chkCurrencyCode c = do
if T.length c == 3
then do
value <- getZcashPrice $ T.toLower c
case value of
Just v -> return True
Nothing -> return False
else return False
-- | Helper function to display small amounts of ZEC
displayValue :: Double -> Integer -> String
displayValue zp bal = printf "%.2f" $ zp * fromIntegral bal