diff --git a/src/Zenith/CLI.hs b/src/Zenith/CLI.hs index a89b439..ff8b6a7 100644 --- a/src/Zenith/CLI.hs +++ b/src/Zenith/CLI.hs @@ -126,6 +126,8 @@ import Zenith.Utils , jsonNumber , showAddress , validBarValue + , getZcashPrice + , displayValue ) data Name @@ -150,6 +152,7 @@ data Name | DeshieldField | TotalTranspField | TotalShieldedField + | SFBViewPort deriving (Eq, Show, Ord) data DialogInput = DialogInput @@ -194,6 +197,7 @@ data DialogType | AdrBookDelForm | DeshieldForm | ShieldForm + | ShowFIATBalance data DisplayType = AddrDisplay @@ -211,7 +215,7 @@ data Tick | TickMsg !String | TickTx !HexString -data DropDownItem = +newtype DropDownItem = DropdownItem String data State = State @@ -246,10 +250,14 @@ data State = State , _tBalance :: !Integer , _sBalance :: !Integer , _currencyCode :: !T.Text - } + , _zprice :: !Double +} makeLenses ''State +zBalance :: State -> Double +zBalance st = (fromIntegral (st ^. balance) ) / 100000000 + drawUI :: State -> [Widget Name] drawUI s = [splashDialog s, helpDialog s, displayDialog s, inputDialog s, ui s] where @@ -299,7 +307,8 @@ drawUI s = [splashDialog s, helpDialog s, displayDialog s, inputDialog s, ui s] [ capCommand "W" "allets" , capCommand "A" "ccounts" , capCommand "V" "iew address" - , capCommand3 "" "S" "end Tx" + , capCommand "S" "end Tx" + , capCommand3 "ba" "L" ("ance (" ++ ( T.unpack (st ^. currencyCode) )++ ")" ) ]) , C.hCenter (hBox @@ -376,7 +385,7 @@ drawUI s = [splashDialog s, helpDialog s, displayDialog s, inputDialog s, ui s] else emptyWidget where 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 = map (hLimit 40 . str) @@ -388,6 +397,7 @@ drawUI s = [splashDialog s, helpDialog s, displayDialog s, inputDialog s, ui s] , "Send Tx" , "Address Book" , "Shield/De-Shield" + , "Balance in Fiat" , "Quit" ] inputDialog :: State -> Widget Name @@ -517,6 +527,36 @@ drawUI s = [splashDialog s, helpDialog s, displayDialog s, inputDialog s, ui s] [ capCommand "C" "onfirm delete" , capCommand3 "" "" " 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 st = @@ -997,6 +1037,7 @@ appEvent (BT.AppEvent t) = do AdrBookDelForm -> return () DeshieldForm -> return () ShieldForm -> return () + ShowFIATBalance -> return() Blank -> do if s ^. timer == 90 then do @@ -1574,6 +1615,24 @@ appEvent (BT.VtyEvent e) = do ev -> BT.zoom deshieldForm $ do 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 Blank -> do case e of @@ -1600,6 +1659,22 @@ appEvent (BT.VtyEvent e) = do BT.modify $ set dialogBox SendTx V.EvKey (V.KChar 'b') [] -> 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 pool <- liftIO $ runNoLoggingT $ initPool $ s ^. dbPath selAcc <- @@ -1651,6 +1726,7 @@ appEvent (BT.VtyEvent e) = do msg "Not enough transparent funds in this account" BT.modify $ set displayBox MsgDisplay + ev -> case r of Just AList -> @@ -1804,6 +1880,7 @@ runZenithTUI config = do tBal sBal currencyCode + 0 Left _e -> do print $ "No Zebra node available on port " <> diff --git a/src/Zenith/Utils.hs b/src/Zenith/Utils.hs index 5fc2886..9c3d6a6 100644 --- a/src/Zenith/Utils.hs +++ b/src/Zenith/Utils.hs @@ -20,6 +20,7 @@ import qualified Data.ByteString.Lazy.Char8 as BL import System.Directory import System.Process (createProcess_, shell) import Text.Regex.Posix +import Text.Printf (printf) import ZcashHaskell.Orchard ( encodeUnifiedAddress , isValidUnifiedAddress @@ -62,7 +63,7 @@ displayZec s | abs s < 100000000 = show (fromIntegral s / 100000) ++ " mZEC" | 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 s | abs s < 100 = show s ++ " tazs" @@ -275,4 +276,18 @@ getZcashPrice currency = do _ -> return Nothing _ -> return Nothing _ -> return Nothing - \ No newline at end of file + +-- 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