|
1 | 1 | {-# LANGUAGE DerivingVia #-}
|
2 | 2 | {-# LANGUAGE FlexibleInstances #-}
|
| 3 | +{-# LANGUAGE NamedFieldPuns #-} |
3 | 4 | {-# LANGUAGE StandaloneDeriving #-}
|
4 | 5 | {-# LANGUAGE TypeApplications #-}
|
5 | 6 |
|
@@ -37,8 +38,6 @@ import Cardano.Wallet.Deposit.Pure
|
37 | 38 | import Cardano.Wallet.Deposit.Read
|
38 | 39 | ( Address
|
39 | 40 | , ChainPoint (..)
|
40 |
| - , fromSlot |
41 |
| - , toSlot |
42 | 41 | )
|
43 | 42 | import Control.Applicative
|
44 | 43 | ( (<|>)
|
@@ -80,17 +79,12 @@ import Data.Text.Class
|
80 | 79 | , ToText (..)
|
81 | 80 | , getTextDecodingError
|
82 | 81 | )
|
83 |
| -import Data.Word |
84 |
| - ( Word64 |
85 |
| - ) |
86 |
| -import Numeric.Natural |
87 |
| - ( Natural |
88 |
| - ) |
89 | 82 | import Servant
|
90 | 83 | ( FromHttpApiData (..)
|
91 | 84 | )
|
92 | 85 |
|
93 | 86 | import qualified Cardano.Wallet.Read as Read
|
| 87 | +import qualified Cardano.Wallet.Read.Hash as Hash |
94 | 88 | import qualified Data.Text as T
|
95 | 89 | import qualified Data.Text.Encoding as T
|
96 | 90 |
|
@@ -192,21 +186,29 @@ instance ToSchema (ApiT CustomerList) where
|
192 | 186 | customerListSchema
|
193 | 187 |
|
194 | 188 | instance ToJSON (ApiT ChainPoint) where
|
195 |
| - toJSON (ApiT Origin) = "genesis" |
196 |
| - toJSON (ApiT (At sl)) = object |
197 |
| - [ "slot_no" .= toJSON (fromIntegral @Natural @Word64 $ fromSlot sl) |
| 189 | + toJSON (ApiT Read.GenesisPoint) = "genesis" |
| 190 | + toJSON (ApiT (Read.BlockPoint{slotNo,headerHash})) = object |
| 191 | + [ "slot_no" .= |
| 192 | + Read.unSlotNo slotNo |
| 193 | + , "header_hash" .= |
| 194 | + Hash.hashToTextAsHex headerHash |
198 | 195 | ]
|
199 | 196 |
|
200 | 197 | instance FromJSON (ApiT ChainPoint) where
|
201 | 198 | parseJSON payload = parseOrigin payload <|> parseSlot payload
|
202 | 199 | where
|
203 |
| - parseOrigin = withText "genesis" $ \txt -> |
204 |
| - if txt == "genesis" then |
205 |
| - pure $ ApiT Origin |
206 |
| - else |
207 |
| - fail "'origin' is expected." |
208 |
| - parseSlot = withObject "slot no" $ \obj -> |
209 |
| - ApiT . At . toSlot <$> obj .: "slot_no" |
| 200 | + parseOrigin = withText "genesis" $ \txt -> |
| 201 | + if txt == "genesis" |
| 202 | + then pure $ ApiT Read.GenesisPoint |
| 203 | + else fail "'genesis' is expected." |
| 204 | + parseSlot = withObject "slot_no" $ \obj -> do |
| 205 | + slotNo <- Read.SlotNo <$> obj .: "slot_no" |
| 206 | + headerHashText <- obj .: "header_hash" |
| 207 | + headerHash <- |
| 208 | + case Hash.hashFromTextAsHex headerHashText of |
| 209 | + Nothing -> fail "invalid 'header_hash'" |
| 210 | + Just hash -> pure hash |
| 211 | + pure $ ApiT Read.BlockPoint{slotNo,headerHash} |
210 | 212 |
|
211 | 213 | instance ToSchema (ApiT ChainPoint) where
|
212 | 214 | declareNamedSchema _ = do
|
|
0 commit comments