@@ -11,41 +11,74 @@ import Control.Monad.Reader
1111
1212import Crypto.Hash
1313
14+ import Data.Aeson (Value )
1415import Data.ByteString (ByteString )
15- import qualified Data.ByteString.Base16 as Base16
16- import qualified Data.ByteString.Char8 as C
16+ import qualified Data.ByteString.Base16 as Base16
17+ import qualified Data.ByteString.Char8 as C
1718import Data.FileEmbed (embedFile )
1819import Data.Foldable (Foldable (sum ))
1920import Data.Has (Has )
20- import qualified Data.Map as M
21+ import qualified Data.Map as M
2122import Data.Maybe (fromMaybe , isJust , isNothing )
2223import Data.Scientific
2324import Data.String (fromString )
2425import Data.Text (Text , pack , unpack , intercalate )
25- import qualified Data.Text.Encoding as Text
26+ import qualified Data.Text.Encoding as Text
2627import Data.Time
2728
28- import qualified Database.PostgreSQL.Simple as SQL
29+ import qualified Database.PostgreSQL.Simple as SQL
2930import Database.PostgreSQL.Simple.Types (In (.. ))
31+ import Database.PostgreSQL.Simple.FromRow
3032
3133import VVA.Config
3234import VVA.Pool (ConnectionPool , withPool )
33- import qualified VVA.Proposal as Proposal
35+ import qualified VVA.Proposal as Proposal
3436import VVA.Types (AppError , DRepInfo (.. ), DRepRegistration (.. ), DRepStatus (.. ),
3537 DRepType (.. ), Proposal (.. ), Vote (.. ), DRepVotingPowerList (.. ))
3638
39+ data DRepQueryResult = DRepQueryResult
40+ { queryDrepHash :: Text
41+ , queryDrepView :: Text
42+ , queryIsScriptBased :: Bool
43+ , queryUrl :: Maybe Text
44+ , queryDataHash :: Maybe Text
45+ , queryDeposit :: Scientific
46+ , queryVotingPower :: Maybe Integer
47+ , queryIsActive :: Bool
48+ , queryTxHash :: Maybe Text
49+ , queryDate :: LocalTime
50+ , queryLatestDeposit :: Scientific
51+ , queryLatestNonDeregisterVotingAnchorWasNotNull :: Bool
52+ , queryMetadataError :: Maybe Text
53+ , queryPaymentAddress :: Maybe Text
54+ , queryGivenName :: Maybe Text
55+ , queryObjectives :: Maybe Text
56+ , queryMotivations :: Maybe Text
57+ , queryQualifications :: Maybe Text
58+ , queryImageUrl :: Maybe Text
59+ , queryImageHash :: Maybe Text
60+ , queryIdentityReferences :: Maybe Value
61+ , queryLinkReferences :: Maybe Value
62+ } deriving (Show )
63+
64+ instance FromRow DRepQueryResult where
65+ fromRow = DRepQueryResult
66+ <$> field <*> field <*> field <*> field <*> field <*> field
67+ <*> field <*> field <*> field <*> field <*> field <*> field
68+ <*> field <*> field <*> field <*> field <*> field <*> field
69+ <*> field <*> field <*> field <*> field
70+
3771sqlFrom :: ByteString -> SQL. Query
3872sqlFrom bs = fromString $ unpack $ Text. decodeUtf8 bs
3973
4074listDRepsSql :: SQL. Query
4175listDRepsSql = sqlFrom $ (embedFile " sql/list-dreps.sql" )
42-
4376listDReps ::
4477 (Has ConnectionPool r , Has VVAConfig r , MonadReader r m , MonadIO m ) =>
4578 Maybe Text -> m [DRepRegistration ]
4679listDReps mSearchQuery = withPool $ \ conn -> do
4780 let searchParam = fromMaybe " " mSearchQuery
48- results <- liftIO $ SQL. query conn listDRepsSql
81+ results <- liftIO ( SQL. query conn listDRepsSql
4982 ( searchParam -- COALESCE(?, '')
5083 , searchParam -- LENGTH(?)
5184 , searchParam -- AND ?
@@ -56,44 +89,45 @@ listDReps mSearchQuery = withPool $ \conn -> do
5689 , " %" <> searchParam <> " %" -- objectives
5790 , " %" <> searchParam <> " %" -- motivations
5891 , " %" <> searchParam <> " %" -- qualifications
59- )
92+ ) :: IO [ DRepQueryResult ])
6093
6194 timeZone <- liftIO getCurrentTimeZone
6295 return
63- [ DRepRegistration drepHash drepView isScriptBased url dataHash (floor @ Scientific deposit) votingPower status drepType txHash (localTimeToUTC timeZone date) metadataError paymentAddress givenName objectives motivations qualifications imageUrl imageHash
64- | ( drepHash
65- , drepView
66- , isScriptBased
67- , url
68- , dataHash
69- , deposit
70- , votingPower
71- , isActive
72- , txHash
73- , date
74- , latestDeposit
75- , latestNonDeregisterVotingAnchorWasNotNull
76- , metadataError
77- , paymentAddress
78- , givenName
79- , objectives
80- , motivations
81- , qualifications
82- , imageUrl
83- , imageHash
84- ) <- results
85- , let status = case (isActive, deposit) of
96+ [ DRepRegistration
97+ (queryDrepHash result)
98+ (queryDrepView result)
99+ (queryIsScriptBased result)
100+ (queryUrl result)
101+ (queryDataHash result)
102+ (floor @ Scientific $ queryDeposit result)
103+ (queryVotingPower result)
104+ status
105+ drepType
106+ (queryTxHash result)
107+ (localTimeToUTC timeZone $ queryDate result)
108+ (queryMetadataError result)
109+ (queryPaymentAddress result)
110+ (queryGivenName result)
111+ (queryObjectives result)
112+ (queryMotivations result)
113+ (queryQualifications result)
114+ (queryImageUrl result)
115+ (queryImageHash result)
116+ (queryIdentityReferences result)
117+ (queryLinkReferences result)
118+ | result <- results
119+ , let status = case (queryIsActive result, queryDeposit result) of
86120 (_, d) | d < 0 -> Retired
87121 (isActive, d) | d >= 0 && isActive -> Active
88122 | d >= 0 && not isActive -> Inactive
89- , let latestDeposit' = floor @ Scientific latestDeposit :: Integer
90- , let drepType | latestDeposit' >= 0 && isNothing url = SoleVoter
91- | latestDeposit' >= 0 && isJust url = DRep
92- | latestDeposit' < 0 && not latestNonDeregisterVotingAnchorWasNotNull = SoleVoter
93- | latestDeposit' < 0 && latestNonDeregisterVotingAnchorWasNotNull = DRep
94- | Data.Maybe. isJust url = DRep
123+ , let latestDeposit' = floor @ Scientific (queryLatestDeposit result) :: Integer
124+ , let drepType | latestDeposit' >= 0 && isNothing (queryUrl result) = SoleVoter
125+ | latestDeposit' >= 0 && isJust (queryUrl result) = DRep
126+ | latestDeposit' < 0 && not (queryLatestNonDeregisterVotingAnchorWasNotNull result) = SoleVoter
127+ | latestDeposit' < 0 && queryLatestNonDeregisterVotingAnchorWasNotNull result = DRep
128+ | Data.Maybe. isJust (queryUrl result) = DRep
95129 ]
96-
130+
97131getVotingPowerSql :: SQL. Query
98132getVotingPowerSql = sqlFrom $ (embedFile " sql/get-voting-power.sql" )
99133
0 commit comments