diff --git a/sapling-output.params b/sapling-output.params deleted file mode 100644 index 01760fa..0000000 Binary files a/sapling-output.params and /dev/null differ diff --git a/sapling-spend.params b/sapling-spend.params deleted file mode 100644 index b91cd77..0000000 Binary files a/sapling-spend.params and /dev/null differ diff --git a/src/Zenith/Utils.hs b/src/Zenith/Utils.hs index c3b74ee..5fc2886 100644 --- a/src/Zenith/Utils.hs +++ b/src/Zenith/Utils.hs @@ -3,6 +3,9 @@ module Zenith.Utils where import Data.Aeson +import Data.Aeson.Types (parseMaybe) +import qualified Data.Aeson.Key as K +import qualified Data.Aeson.KeyMap as KM import Data.Char (isAlphaNum, isSpace) import Data.Functor (void) import Data.Maybe @@ -10,6 +13,10 @@ import Data.Ord (clamp) import Data.Scientific (Scientific(..), scientific) import qualified Data.Text as T import qualified Data.Text.Encoding as E +import Control.Exception (try, SomeException) +import Control.Monad (when) +import qualified Data.ByteString.Lazy as B +import qualified Data.ByteString.Lazy.Char8 as BL import System.Directory import System.Process (createProcess_, shell) import Text.Regex.Posix @@ -39,6 +46,9 @@ import Zenith.Types , ZcashAddress(..) , ZcashPool(..) ) +import Network.HTTP.Simple +import Data.Scientific (Scientific, toRealFloat) + -- | Helper function to convert numbers into JSON jsonNumber :: Int -> Value @@ -248,3 +258,21 @@ getChainTip zHost zPort = do case r of Left e1 -> pure 0 Right i -> pure i + +-- Function to fetch Zcash price from CoinGecko +getZcashPrice :: T.Text -> IO (Maybe Double) +getZcashPrice currency = do + let url = "https://api.coingecko.com/api/v3/simple/price?ids=zcash&vs_currencies=" <> T.unpack currency + response <- httpJSONEither (parseRequest_ url) + case getResponseBody response of + Right (Object obj) -> do + -- Extract "zcash" object + case KM.lookup "zcash" obj of + Just (Object zcashObj) -> + -- Extract the currency price + case KM.lookup (K.fromText (T.toLower currency)) zcashObj of + Just (Number price) -> return (Just (toRealFloat price)) + _ -> return Nothing + _ -> return Nothing + _ -> return Nothing + \ No newline at end of file diff --git a/test/Spec.hs b/test/Spec.hs index ca66599..dfabe0c 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -2,13 +2,14 @@ import Codec.Borsh import Control.Monad (when) -import Control.Monad.Logger (runFileLoggingT, runNoLoggingT) +import Control.Monad.Logger (runNoLoggingT, runNoLoggingT) import Data.Aeson import qualified Data.ByteString as BS import qualified Data.ByteString.Lazy as LBS import Data.HexString import Data.List (foldl') import Data.Maybe (fromJust) +import qualified Data.Text as T import qualified Data.Text.Encoding as E import Database.Persist import Database.Persist.Sqlite @@ -69,6 +70,7 @@ import Zenith.Core import Zenith.DB import Zenith.Tree import Zenith.Types +import Zenith.Utils main :: IO () main = do @@ -644,7 +646,7 @@ main = do Nothing -> assertFailure "couldn't find index at block" Just i -> do updatedTree <- - runFileLoggingT "test.log" $ truncateTree oTree i + runNoLoggingT $ truncateTree oTree i let finalAnchor = getOrchardTreeAnchor $ OrchardCommitmentTree $ ztiOrchard zebraTreesIn @@ -737,7 +739,7 @@ main = do Just ua -> do pool <- runNoLoggingT $ initPool "/home/rav/Zenith/zenith.db" tx <- - runFileLoggingT "zenith.log" $ + runNoLoggingT $ prepareTxV2 pool "localhost" @@ -763,7 +765,7 @@ main = do Just ua -> do pool <- runNoLoggingT $ initPool "/home/rav/Zenith/zenith.db" tx <- - runFileLoggingT "zenith.log" $ + runNoLoggingT $ prepareTxV2 pool "localhost" @@ -787,7 +789,7 @@ main = do Just ua -> do pool <- runNoLoggingT $ initPool "/home/rav/Zenith/zenith.db" tx <- - runFileLoggingT "zenith.log" $ + runNoLoggingT $ prepareTxV2 pool "localhost" @@ -815,7 +817,7 @@ main = do Just ua -> do pool <- runNoLoggingT $ initPool "/home/rav/Zenith/zenith.db" tx <- - runFileLoggingT "zenith.log" $ + runNoLoggingT $ prepareTxV2 pool "localhost" @@ -847,7 +849,7 @@ main = do Just ua -> do pool <- runNoLoggingT $ initPool "/home/rav/Zenith/zenith.db" tx <- - runFileLoggingT "zenith.log" $ + runNoLoggingT $ prepareTxV2 pool "localhost" @@ -873,7 +875,7 @@ main = do Just ua -> do pool <- runNoLoggingT $ initPool "/home/rav/Zenith/zenith.db" tx <- - runFileLoggingT "zenith.log" $ + runNoLoggingT $ prepareTxV2 pool "localhost" @@ -897,7 +899,7 @@ main = do Just ua -> do pool <- runNoLoggingT $ initPool "/home/rav/Zenith/zenith.db" tx <- - runFileLoggingT "zenith.log" $ + runNoLoggingT $ prepareTxV2 pool "localhost" @@ -926,7 +928,7 @@ main = do Just ua -> do pool <- runNoLoggingT $ initPool "/home/rav/Zenith/zenith.db" tx <- - runFileLoggingT "zenith.log" $ + runNoLoggingT $ prepareTxV2 pool "localhost" @@ -957,7 +959,7 @@ main = do Just ua -> do pool <- runNoLoggingT $ initPool "/home/rav/Zenith/zenith.db" tx <- - runFileLoggingT "zenith.log" $ + runNoLoggingT $ prepareTxV2 pool "localhost" @@ -983,7 +985,7 @@ main = do Just ua -> do pool <- runNoLoggingT $ initPool "/home/rav/Zenith/zenith.db" tx <- - runFileLoggingT "zenith.log" $ + runNoLoggingT $ prepareTxV2 pool "localhost" @@ -1007,7 +1009,7 @@ main = do Just ua -> do pool <- runNoLoggingT $ initPool "/home/rav/Zenith/zenith.db" tx <- - runFileLoggingT "zenith.log" $ + runNoLoggingT $ prepareTxV2 pool "localhost" @@ -1034,7 +1036,7 @@ main = do Just ua -> do pool <- runNoLoggingT $ initPool "/home/rav/Zenith/zenith.db" tx <- - runFileLoggingT "zenith.log" $ + runNoLoggingT $ prepareTxV2 pool "localhost" @@ -1061,7 +1063,7 @@ main = do Just ua -> do pool <- runNoLoggingT $ initPool "/home/rav/Zenith/zenith.db" tx <- - runFileLoggingT "zenith.log" $ + runNoLoggingT $ prepareTxV2 pool "localhost" @@ -1086,7 +1088,7 @@ main = do Just ua -> do pool <- runNoLoggingT $ initPool "/home/rav/Zenith/zenith.db" tx <- - runFileLoggingT "zenith.log" $ + runNoLoggingT $ prepareTxV2 pool "localhost" @@ -1103,3 +1105,9 @@ main = do case tx of Left e -> assertFailure $ show e Right h -> h `shouldNotBe` hexString "deadbeef" + describe "Call CoinGecko to get ZEC price" $ do + it "Testing for USD " $ do + price <- getZcashPrice $ T.pack "usd" + case price of + Just p -> p `shouldNotBe` 0.0 + Nothing -> assertFailure "Failed to get ZEC price" diff --git a/zenith.cabal b/zenith.cabal index 5ee487b..5f7be4b 100644 --- a/zenith.cabal +++ b/zenith.cabal @@ -96,6 +96,7 @@ library , vty-crossplatform , word-wrap , zcash-haskell + , unordered-containers --pkgconfig-depends: rustzcash_wrapper default-language: Haskell2010