1
+ {-# LANGUAGE LambdaCase #-}
1
2
{-# LANGUAGE OverloadedStrings #-}
2
3
3
4
import Cardano.Db (PoolMetaHash (.. ), PoolUrl (.. ), VoteMetaHash (.. ), VoteUrl (.. ))
5
+ import qualified Cardano.Db as DB
4
6
import Cardano.DbSync.Error (bsBase16Encode , runOrThrowIO )
5
7
import Cardano.DbSync.OffChain.Http
6
8
import Cardano.DbSync.Types
@@ -30,9 +32,10 @@ import System.Exit (exitFailure)
30
32
main :: IO ()
31
33
main = do
32
34
xs <- getArgs
35
+ let voteType = getVoteType xs
33
36
case cleanOpt xs of
34
- [url] -> runGet (isItVote xs) (isItGa xs) (isItLink xs) (Text. pack url) Nothing
35
- [url, hash] -> runGet (isItVote xs) (isItGa xs) (isItLink xs) (Text. pack url) (Just $ parseHash hash)
37
+ [url] -> runGet voteType (isItLink xs) (Text. pack url) Nothing
38
+ [url, hash] -> runGet voteType (isItLink xs) (Text. pack url) (Just $ parseHash hash)
36
39
_otherwise -> usageExit
37
40
where
38
41
parseHash :: String -> BS. ByteString
@@ -44,17 +47,30 @@ main = do
44
47
cleanOpt :: [String ] -> [String ]
45
48
cleanOpt ls = List. delete " ga" $ List. delete " vote" $ List. delete " pool" ls
46
49
47
- isItVote ls = List. elem " vote" ls || isItGa ls
48
- isItGa = List. elem " ga"
50
+ getVoteType :: [String ] -> Maybe OffChainVoteType
51
+ getVoteType ls
52
+ | " ga" `List.elem` ls = Just GovAction
53
+ | " drep" `List.elem` ls = Just DrepReg
54
+ | " vote" `List.elem` ls = Just Other
55
+ | otherwise = Nothing
49
56
isItLink = List. elem " url"
50
57
51
- runGet isVote isGa isLink url mhsh
52
- | isVote && isLink =
53
- runHttpGetVote (VoteUrl url) (VoteMetaHash <$> mhsh) isGa
54
- | isVote && not isLink =
55
- runGetVote url (VoteMetaHash <$> mhsh) isGa
56
- | otherwise =
57
- runHttpGetPool (PoolUrl url) (PoolMetaHash <$> mhsh)
58
+ runGet mvtype isLink url mhsh = case mvtype of
59
+ Just vtype
60
+ | isLink ->
61
+ runHttpGetVote (VoteUrl url) (VoteMetaHash <$> mhsh) (toDBOffChainVoteType vtype)
62
+ Just vtype ->
63
+ runGetVote url (VoteMetaHash <$> mhsh) (toDBOffChainVoteType vtype)
64
+ _ ->
65
+ runHttpGetPool (PoolUrl url) (PoolMetaHash <$> mhsh)
66
+
67
+ data OffChainVoteType = GovAction | DrepReg | Other
68
+
69
+ toDBOffChainVoteType :: OffChainVoteType -> DB. AnchorType
70
+ toDBOffChainVoteType = \ case
71
+ GovAction -> DB. GovActionAnchor
72
+ DrepReg -> DB. DrepAnchor
73
+ Other -> DB. OtherAnchor
58
74
59
75
-- -------------------------------------------------------------------------------------------------
60
76
@@ -93,15 +109,15 @@ runHttpGetPool poolUrl mHash =
93
109
else putStrLn $ orangeText (" Warning: This should be 'application/json'\n Content-type: " ++ BSC. unpack ct)
94
110
Text. putStrLn $ spodJson spod
95
111
96
- runHttpGetVote :: VoteUrl -> Maybe VoteMetaHash -> Bool -> IO ()
97
- runHttpGetVote voteUrl mHash isGa =
112
+ runHttpGetVote :: VoteUrl -> Maybe VoteMetaHash -> DB. AnchorType -> IO ()
113
+ runHttpGetVote voteUrl mHash vtype =
98
114
reportSuccess =<< runOrThrowIO (runExceptT httpGet)
99
115
where
100
116
httpGet :: ExceptT OffChainFetchError IO SimplifiedOffChainVoteData
101
117
httpGet = do
102
118
request <- parseOffChainUrl $ OffChainVoteUrl voteUrl
103
119
manager <- liftIO $ Http. newManager tlsManagerSettings
104
- httpGetOffChainVoteData manager request voteUrl mHash isGa
120
+ httpGetOffChainVoteData manager request voteUrl mHash vtype
105
121
106
122
reportSuccess :: SimplifiedOffChainVoteData -> IO ()
107
123
reportSuccess spod = do
@@ -113,11 +129,11 @@ runHttpGetVote voteUrl mHash isGa =
113
129
else putStrLn $ orangeText (" Warning: This should be 'application/json'\n Content-type: " ++ BSC. unpack ct)
114
130
Text. putStrLn $ sovaJson spod
115
131
116
- runGetVote :: Text. Text -> Maybe VoteMetaHash -> Bool -> IO ()
117
- runGetVote file mExpectedHash isGa = do
132
+ runGetVote :: Text. Text -> Maybe VoteMetaHash -> DB. AnchorType -> IO ()
133
+ runGetVote file mExpectedHash vtype = do
118
134
respBs <- BS. readFile (Text. unpack file)
119
135
let respLBs = fromStrict respBs
120
- (ocvd, val, hsh, mWarning) <- runOrThrowIO $ runExceptT $ parseAndValidateVoteData respBs respLBs mExpectedHash isGa Nothing
136
+ (ocvd, val, hsh, mWarning) <- runOrThrowIO $ runExceptT $ parseAndValidateVoteData respBs respLBs mExpectedHash vtype Nothing
121
137
print ocvd
122
138
print val
123
139
print $ bsBase16Encode hsh
0 commit comments