-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move websocket parsing functions to their own module
- Loading branch information
Showing
3 changed files
with
53 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Frontend.WebsocketParse where | ||
|
||
import Control.Lens | ||
import qualified Data.Aeson as Aeson | ||
import Data.Aeson.Lens | ||
import qualified Data.HashMap.Lazy as HMap | ||
import qualified Data.Map as Map | ||
import Data.Maybe (fromMaybe, catMaybes) | ||
import Data.Text (Text) | ||
import qualified Data.Vector as V | ||
import Reflex.Dom.Core | ||
|
||
-- TODO: Create a websocket parsing module for this | ||
parseTokensToMap :: V.Vector Aeson.Value -> Map.Map Text (Integer, Text) | ||
parseTokensToMap cd = mconcat $ catMaybes $ V.toList $ ffor cd $ \case | ||
Aeson.Array xs -> case V.toList xs of | ||
symbol:(Aeson.Array tokens):_ -> | ||
let currencySymbol = symbol ^. key "unCurrencySymbol" . _String | ||
tokens' = ffor tokens $ \case | ||
Aeson.Array xs' -> case V.toList xs' of | ||
tokenName:tokenAmount:_ -> Just | ||
( tokenName ^. key "unTokenName" . _String | ||
, (fromMaybe 0 $ tokenAmount ^? _Integer, currencySymbol) | ||
) | ||
_ -> Nothing | ||
_ -> Nothing | ||
in Just $ Map.fromList $ catMaybes $ V.toList tokens' | ||
_ -> Nothing | ||
_ -> Just Map.empty | ||
|
||
-- returns Map of TokenName (LiquidityAmount, ((CoinA, CoinAPoolAmount), (CoinB, CoinBPoolAmount))) | ||
parseLiquidityTokensToMap :: V.Vector Aeson.Value -> Map.Map Text (Integer, ((Text,Integer), (Text, Integer))) | ||
parseLiquidityTokensToMap cd = mconcat $ catMaybes $ V.toList $ ffor cd $ \case | ||
Aeson.Array xs -> case V.toList xs of | ||
coinA:coinB:(Aeson.Array liquidityCoin):_-> case V.toList liquidityCoin of | ||
(Aeson.Object obj):(Aeson.Number lqAmount):_ -> case HMap.toList obj of | ||
(_, Aeson.String lqTokenName):_ -> do | ||
let tna = coinA ^. nth 0 . key "unAssetClass" . values . key "unTokenName" . _String | ||
tnaPool = fromMaybe 0 $ coinA ^? nth 1 . _Integer | ||
tnb = coinB ^. nth 0 . key "unAssetClass" . values . key "unTokenName" . _String | ||
tnbPool = fromMaybe 0 $ coinB ^? nth 1 . _Integer | ||
Just $ Map.singleton lqTokenName ((fromMaybe 0 $ lqAmount ^? _Integer), ((tna, tnaPool),(tnb, tnbPool))) | ||
_ -> Nothing | ||
_ -> Nothing | ||
_ -> Nothing | ||
_ -> Just Map.empty | ||
|