@@ -12,46 +12,37 @@ import Data.Aeson.KeyMap qualified as KeyMap
12
12
import Data.Aeson.Lens (members , _Object )
13
13
import Data.FileEmbed (embedFile )
14
14
import Data.List qualified as List
15
- import Data.Text (splitOn )
15
+ import Data.Text (pack , splitOn , toLower , unpack )
16
16
import Data.Text.Encoding (encodeUtf8 )
17
- import Hydra.Cardano.Api (NetworkId ( .. ), NetworkMagic ( .. ), TxId , deserialiseFromRawBytesHex )
17
+ import Hydra.Cardano.Api (TxId , deserialiseFromRawBytesHex )
18
18
19
19
networkVersionsFile :: ByteString
20
20
networkVersionsFile = $ (embedFile " ./../networks.json" )
21
21
22
- data ParseError
23
- = ExpectedStringForTxId
22
+ data NetworkParseError
23
+ = ExpectedJSONStringForTxId
24
24
| FailedToParseTextToTxId Text
25
25
| UnknownNetwork Text
26
26
deriving stock (Eq , Show )
27
27
28
- instance Exception ParseError
29
-
30
- parseNetworkVersions :: ByteString -> IO [(NetworkId , [TxId ])]
31
- parseNetworkVersions bs = do
32
- let info = bs ^@.. members . _Object
33
- fmap catMaybes <$> forM info $ \ (n, t) -> do
34
- let textKey = Key. toText n
35
- case textKey of
36
- " mainnet" -> do
37
- txids <- getLastTxId t
38
- pure $ Just (Mainnet , txids)
39
- " preview" -> do
40
- txids <- getLastTxId t
41
- pure $ Just (Testnet $ NetworkMagic 2 , txids)
42
- " preprod" -> do
43
- txids <- getLastTxId t
44
- pure $ Just (Testnet $ NetworkMagic 1 , txids)
45
- _ -> pure Nothing
28
+ instance Exception NetworkParseError
29
+
30
+ parseNetworkTxIds :: String -> Either String [TxId ]
31
+ parseNetworkTxIds networkString = do
32
+ let networkTxt = toLower $ pack networkString
33
+ let info = networkVersionsFile ^@.. members . _Object
34
+ case find (\ (n, _) -> Key. toText n == networkTxt) info of
35
+ Nothing -> Left $ " Unknown network:" <> unpack networkTxt
36
+ Just (_, t) -> getLastTxId t
46
37
where
47
38
getLastTxId t = do
48
39
lastTxIds <-
49
40
case List. last $ KeyMap. elems t of
50
- String s -> pure s
51
- _ -> throwIO ExpectedStringForTxId
41
+ String s -> Right s
42
+ _ -> Left " Failed to find the last tx-id string in networks.json "
52
43
mapM parseToTxId (splitOn " ," lastTxIds)
53
44
54
45
parseToTxId textTxId = do
55
46
case deserialiseFromRawBytesHex $ encodeUtf8 textTxId of
56
- Left _ -> throwIO $ FailedToParseTextToTxId textTxId
57
- Right txid -> pure txid
47
+ Left _ -> Left $ " Failed to parse string to TxId: " <> unpack textTxId
48
+ Right txid -> Right txid
0 commit comments