Skip to content

Commit

Permalink
Fix parsing of SMASHPoolId, thanks to Piotr
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Ospald committed Nov 10, 2020
1 parent 6cdad95 commit 0a34f9a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
34 changes: 18 additions & 16 deletions lib/core/src/Cardano/Pool/Metadata.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NumericUnderscores #-}
Expand Down Expand Up @@ -34,13 +35,16 @@ import Cardano.BM.Data.Severity
( Severity (..) )
import Cardano.BM.Data.Tracer
( HasPrivacyAnnotation (..), HasSeverityAnnotation (..) )
import Cardano.Wallet.Api.Types
( defaultRecordTypeOptions )
import Cardano.Wallet.Primitive.AddressDerivation
( hex )
import Cardano.Wallet.Primitive.Types
( PoolId
, StakePoolMetadata (..)
, StakePoolMetadataHash (..)
, StakePoolMetadataUrl (..)
, decodePoolIdBech32
)
import Control.Exception
( IOException, handle )
Expand All @@ -58,10 +62,8 @@ import Data.Aeson
( FromJSON (..)
, ToJSON (..)
, eitherDecodeStrict
, object
, withObject
, (.:)
, (.=)
, genericParseJSON
, genericToJSON
)
import Data.Bifunctor
( first )
Expand All @@ -77,6 +79,8 @@ import Data.Text.Class
( TextDecodingError (..), ToText (..), fromText )
import Fmt
( pretty )
import GHC.Generics
( Generic )
import Network.HTTP.Client
( HttpException (..)
, Manager
Expand Down Expand Up @@ -112,22 +116,20 @@ metadaFetchEp pid (StakePoolMetadataHash bytes)
pidStr = T.unpack $ toText pid

-- | TODO: import SMASH types
newtype SMASHPoolId = SMASHPoolId T.Text
deriving stock (Eq, Show, Ord)

instance ToJSON SMASHPoolId where
toJSON (SMASHPoolId poolId) =
object
[ "poolId" .= poolId
]
newtype SMASHPoolId = SMASHPoolId
{ poolId :: T.Text
} deriving stock (Eq, Show, Ord)
deriving (Generic)

instance FromJSON SMASHPoolId where
parseJSON = withObject "SMASHPoolId" $ \o -> do
poolId <- o .: "poolId"
return $ SMASHPoolId poolId
parseJSON = genericParseJSON defaultRecordTypeOptions

instance ToJSON SMASHPoolId where
toJSON = genericToJSON defaultRecordTypeOptions

toPoolId :: SMASHPoolId -> Either TextDecodingError PoolId
toPoolId (SMASHPoolId pid) = fromText pid
toPoolId (SMASHPoolId pid) =
either (\_ -> decodePoolIdBech32 pid) Right (fromText @PoolId pid)

-- | Some default settings, overriding some of the library's default with
-- stricter values.
Expand Down
3 changes: 3 additions & 0 deletions lib/core/src/Cardano/Wallet/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ module Cardano.Wallet.Api.Types
, PostTransactionDataT
, PostTransactionFeeDataT
, ApiWalletMigrationPostDataT

-- * other
, defaultRecordTypeOptions
) where

import Prelude
Expand Down

0 comments on commit 0a34f9a

Please sign in to comment.