Skip to content

Commit

Permalink
Flatten the serialized representations of Flat TokenMap.
Browse files Browse the repository at this point in the history
This change further flattens the serialized `JSON` and `Buildable`
representations of `Flat TokenMap`, removing the `AssetId` structure
from both representations.
  • Loading branch information
jonathanknowles committed Dec 2, 2020
1 parent 584139a commit cd068cc
Show file tree
Hide file tree
Showing 5 changed files with 823 additions and 960 deletions.
36 changes: 11 additions & 25 deletions lib/core/src/Cardano/Wallet/Primitive/Types/TokenBundle/TokenMap.hs
Expand Up @@ -186,17 +186,13 @@ instance Buildable (Flat TokenMap) where
[ ("tokens",
buildList buildAssetQuantity $ toFlatList b)
]
buildAssetQuantity (_asset, _quantity) = buildMap
[ ("asset",
buildAssetId _asset)
, ("quantity",
build _quantity)
]
buildAssetId (AssetId policy token) = buildMap
buildAssetQuantity (AssetId policy token, quantity) = buildMap
[ ("policy",
build policy)
, ("token",
build token)
, ("quantity",
build quantity)
]

instance Buildable (Nested TokenMap) where
Expand All @@ -212,11 +208,11 @@ instance Buildable (Nested TokenMap) where
, ("tokens",
buildList buildTokenQuantity (NEMap.toList assetMap))
]
buildTokenQuantity (_tokenName, _tokenQuantity) = buildMap
[ ("name",
build _tokenName)
buildTokenQuantity (token, quantity) = buildMap
[ ("token",
build token)
, ("quantity",
build _tokenQuantity)
build quantity)
]

buildList :: Foldable f => (a -> Builder) -> f a -> Builder
Expand Down Expand Up @@ -258,39 +254,29 @@ jsonFailWithZeroValueTokenQuantity policy token = jsonFailWith $ unwords
instance ToJSON (Flat TokenMap) where
toJSON = toJSON . fmap fromTuple . toFlatList . getFlat
where
fromTuple (AssetId p t, q) = FlatAssetQuantity (FlatAssetId p t) q
fromTuple (AssetId p t, q) = FlatAssetQuantity p t q

instance FromJSON (Flat TokenMap) where
parseJSON =
fmap (Flat . fromFlatList) . sequence . fmap parseTuple <=< parseJSON
where
parseTuple :: FlatAssetQuantity -> Parser (AssetId, TokenQuantity)
parseTuple (FlatAssetQuantity (FlatAssetId p t) q) = do
parseTuple (FlatAssetQuantity p t q) = do
when (TQ.isZero q) $ jsonFailWithZeroValueTokenQuantity p t
pure (AssetId p t, q)

-- Used for JSON serialization only: not exported.
data FlatAssetQuantity = FlatAssetQuantity
{ _asset :: !FlatAssetId
, _quantity :: !TokenQuantity
} deriving Generic

-- Used for JSON serialization only: not exported.
data FlatAssetId = FlatAssetId
{ _policy :: !TokenPolicyId
, _token :: !TokenName
, _quantity :: !TokenQuantity
} deriving Generic

instance FromJSON FlatAssetQuantity where
parseJSON = genericParseJSON jsonOptions
instance ToJSON FlatAssetQuantity where
toJSON = genericToJSON jsonOptions

instance FromJSON FlatAssetId where
parseJSON = genericParseJSON jsonOptions
instance ToJSON FlatAssetId where
toJSON = genericToJSON jsonOptions

--------------------------------------------------------------------------------
-- JSON serialization (nested)
--------------------------------------------------------------------------------
Expand Down Expand Up @@ -332,7 +318,7 @@ data NestedMapEntry = NestedMapEntry

-- Used for JSON serialization only: not exported.
data NestedTokenQuantity = NestedTokenQuantity
{ _name :: !TokenName
{ _token :: !TokenName
, _quantity :: !TokenQuantity
} deriving Generic

Expand Down

0 comments on commit cd068cc

Please sign in to comment.