|
|
|
@ -22,8 +22,8 @@ import ATrade.Types |
|
|
|
import Control.Applicative ((<|>)) |
|
|
|
import Control.Applicative ((<|>)) |
|
|
|
import Control.Error.Util |
|
|
|
import Control.Error.Util |
|
|
|
import Data.Aeson |
|
|
|
import Data.Aeson |
|
|
|
|
|
|
|
import Data.Aeson.KeyMap as KM |
|
|
|
import Data.Aeson.Types hiding (parse) |
|
|
|
import Data.Aeson.Types hiding (parse) |
|
|
|
import qualified Data.HashMap.Strict as HM |
|
|
|
|
|
|
|
import Data.Int |
|
|
|
import Data.Int |
|
|
|
import qualified Data.Text as T |
|
|
|
import qualified Data.Text as T |
|
|
|
import Data.Text.Encoding |
|
|
|
import Data.Text.Encoding |
|
|
|
@ -56,16 +56,16 @@ instance FromJSON Notification where |
|
|
|
parseJSON = withObject "notification" $ \obj -> parseNotification obj |
|
|
|
parseJSON = withObject "notification" $ \obj -> parseNotification obj |
|
|
|
where |
|
|
|
where |
|
|
|
parseNotification obj = |
|
|
|
parseNotification obj = |
|
|
|
case HM.lookup "notification-sqnum" obj of |
|
|
|
case KM.lookup "notification-sqnum" obj of |
|
|
|
Just (Number sqnum) -> parseTrade (NotificationSqnum $ truncate sqnum) obj <|> |
|
|
|
Just (Number sqnum) -> parseTrade (NotificationSqnum $ truncate sqnum) obj <|> |
|
|
|
parseOrder (NotificationSqnum $ truncate sqnum) obj <|> |
|
|
|
parseOrder (NotificationSqnum $ truncate sqnum) obj <|> |
|
|
|
fail "Can't parse notification" |
|
|
|
fail "Can't parse notification" |
|
|
|
Just _ -> fail "Invalid sqnum" |
|
|
|
Just _ -> fail "Invalid sqnum" |
|
|
|
Nothing -> fail "Unable to lookup notification sqnum" |
|
|
|
Nothing -> fail "Unable to lookup notification sqnum" |
|
|
|
parseTrade sqnum obj = case HM.lookup "trade" obj of |
|
|
|
parseTrade sqnum obj = case KM.lookup "trade" obj of |
|
|
|
Just val -> TradeNotification sqnum <$> (parseJSON val) |
|
|
|
Just val -> TradeNotification sqnum <$> (parseJSON val) |
|
|
|
Nothing -> fail "Can't parse trade" |
|
|
|
Nothing -> fail "Can't parse trade" |
|
|
|
parseOrder sqnum obj = case HM.lookup "order-state" obj of |
|
|
|
parseOrder sqnum obj = case KM.lookup "order-state" obj of |
|
|
|
Just v -> withObject "object" (\os -> do |
|
|
|
Just v -> withObject "object" (\os -> do |
|
|
|
oid <- os .: "order-id" |
|
|
|
oid <- os .: "order-id" |
|
|
|
ns <- os .: "new-state" |
|
|
|
ns <- os .: "new-state" |
|
|
|
@ -97,16 +97,16 @@ instance FromJSON BrokerServerRequest where |
|
|
|
where |
|
|
|
where |
|
|
|
parseRequest :: RequestSqnum -> ClientIdentity -> Object -> Parser BrokerServerRequest |
|
|
|
parseRequest :: RequestSqnum -> ClientIdentity -> Object -> Parser BrokerServerRequest |
|
|
|
parseRequest sqnum clientIdentity obj |
|
|
|
parseRequest sqnum clientIdentity obj |
|
|
|
| HM.member "order" obj = do |
|
|
|
| KM.member "order" obj = do |
|
|
|
order <- obj .: "order" |
|
|
|
order <- obj .: "order" |
|
|
|
RequestSubmitOrder sqnum clientIdentity <$> parseJSON order |
|
|
|
RequestSubmitOrder sqnum clientIdentity <$> parseJSON order |
|
|
|
| HM.member "cancel-order" obj = do |
|
|
|
| KM.member "cancel-order" obj = do |
|
|
|
orderId <- obj .: "cancel-order" |
|
|
|
orderId <- obj .: "cancel-order" |
|
|
|
RequestCancelOrder sqnum clientIdentity <$> parseJSON orderId |
|
|
|
RequestCancelOrder sqnum clientIdentity <$> parseJSON orderId |
|
|
|
| HM.member "request-notifications" obj = do |
|
|
|
| KM.member "request-notifications" obj = do |
|
|
|
initialSqnum <- obj .: "initial-sqnum" |
|
|
|
initialSqnum <- obj .: "initial-sqnum" |
|
|
|
return (RequestNotifications sqnum clientIdentity (NotificationSqnum initialSqnum)) |
|
|
|
return (RequestNotifications sqnum clientIdentity (NotificationSqnum initialSqnum)) |
|
|
|
| HM.member "request-current-sqnum" obj = |
|
|
|
| KM.member "request-current-sqnum" obj = |
|
|
|
return (RequestCurrentSqnum sqnum clientIdentity) |
|
|
|
return (RequestCurrentSqnum sqnum clientIdentity) |
|
|
|
parseRequest _ _ _ = fail "Invalid request object" |
|
|
|
parseRequest _ _ _ = fail "Invalid request object" |
|
|
|
|
|
|
|
|
|
|
|
@ -134,17 +134,17 @@ data BrokerServerResponse = ResponseOk |
|
|
|
|
|
|
|
|
|
|
|
instance FromJSON BrokerServerResponse where |
|
|
|
instance FromJSON BrokerServerResponse where |
|
|
|
parseJSON = withObject "object" (\obj -> |
|
|
|
parseJSON = withObject "object" (\obj -> |
|
|
|
if | HM.member "result" obj -> do |
|
|
|
if | KM.member "result" obj -> do |
|
|
|
result <- obj .: "result" |
|
|
|
result <- obj .: "result" |
|
|
|
if (result :: T.Text) == "success" |
|
|
|
if (result :: T.Text) == "success" |
|
|
|
then return ResponseOk |
|
|
|
then return ResponseOk |
|
|
|
else do |
|
|
|
else do |
|
|
|
msg <- obj .:? "message" .!= "" |
|
|
|
msg <- obj .:? "message" .!= "" |
|
|
|
return (ResponseError msg) |
|
|
|
return (ResponseError msg) |
|
|
|
| HM.member "notifications" obj -> do |
|
|
|
| KM.member "notifications" obj -> do |
|
|
|
notifications <- obj .: "notifications" |
|
|
|
notifications <- obj .: "notifications" |
|
|
|
ResponseNotifications <$> parseJSON notifications |
|
|
|
ResponseNotifications <$> parseJSON notifications |
|
|
|
| HM.member "current-sqnum" obj -> do |
|
|
|
| KM.member "current-sqnum" obj -> do |
|
|
|
rawSqnum <- obj .: "current-sqnum" |
|
|
|
rawSqnum <- obj .: "current-sqnum" |
|
|
|
return $ ResponseCurrentSqnum (NotificationSqnum rawSqnum) |
|
|
|
return $ ResponseCurrentSqnum (NotificationSqnum rawSqnum) |
|
|
|
| otherwise -> fail "Unable to parse BrokerServerResponse") |
|
|
|
| otherwise -> fail "Unable to parse BrokerServerResponse") |
|
|
|
@ -234,11 +234,11 @@ instance ToJSON TradeSinkMessage where |
|
|
|
|
|
|
|
|
|
|
|
instance FromJSON TradeSinkMessage where |
|
|
|
instance FromJSON TradeSinkMessage where |
|
|
|
parseJSON = withObject "object" (\obj -> |
|
|
|
parseJSON = withObject "object" (\obj -> |
|
|
|
case HM.lookup "command" obj of |
|
|
|
case KM.lookup "command" obj of |
|
|
|
Nothing -> parseTrade obj |
|
|
|
Nothing -> parseTrade obj |
|
|
|
Just cmd -> return TradeSinkHeartBeat) |
|
|
|
Just cmd -> return TradeSinkHeartBeat) |
|
|
|
where |
|
|
|
where |
|
|
|
parseTrade obj = case HM.lookup "trade" obj of |
|
|
|
parseTrade obj = case KM.lookup "trade" obj of |
|
|
|
Just (Object v) -> do |
|
|
|
Just (Object v) -> do |
|
|
|
acc <- v .: "account" |
|
|
|
acc <- v .: "account" |
|
|
|
sec <- v .: "security" |
|
|
|
sec <- v .: "security" |
|
|
|
|