Skip to content

Commit

Permalink
canonicalise function
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimbo4350 committed Nov 24, 2020
1 parent 9a6d588 commit 3b0c669
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
6 changes: 3 additions & 3 deletions cardano-api/src/Cardano/Api/Value.hs
Expand Up @@ -124,8 +124,8 @@ instance FromJSONKey AssetName where
fromJSONKey = FromJSONKeyText (AssetName . Text.encodeUtf8)


data AssetId = AssetId !PolicyId !AssetName
| AdaAssetId
data AssetId = AdaAssetId
| AssetId !PolicyId !AssetName
deriving (Eq, Ord, Show)

newtype Value = Value (Map AssetId Quantity)
Expand Down Expand Up @@ -252,7 +252,7 @@ instance FromJSON ValueNestedRep where
parsePid ("lovelace", q) = ValueNestedBundleAda <$> parseJSON q
parsePid (pid, q) =
case deserialiseFromRawBytesHex AsScriptHash (Text.encodeUtf8 pid) of
Just sHash -> ValueNestedBundle (PolicyId sHash) <$> (parseJSON q)
Just sHash -> ValueNestedBundle (PolicyId sHash) <$> parseJSON q
Nothing -> fail $ "Failure when deserialising PolicyId: "
<> Text.unpack pid

Expand Down
36 changes: 32 additions & 4 deletions cardano-api/test/Test/Cardano/Api/Typed/Value.hs
Expand Up @@ -6,6 +6,7 @@ module Test.Cardano.Api.Typed.Value

import Cardano.Prelude
import Data.Aeson
import qualified Data.Map.Strict as Map

import Cardano.Api.Typed

Expand All @@ -32,14 +33,41 @@ prop_roundtrip_Value_flatten_unflatten =
-- Remember that Maps cannot have duplicate keys and therefore
-- we will never go from Value -> ValueNestedRep (via toValueNestedRep) to a
-- ValueNestedRep with duplicate values.

prop_roundtrip_Value_unflatten_flatten :: Property
prop_roundtrip_Value_unflatten_flatten =
property $ do
v <- forAll genValueNestedRep
let v' = valueToNestedRep (valueFromNestedRep v)
v `equiv` v'
where
equiv (ValueNestedRep a) (ValueNestedRep b) = sort a === sort b
canonicalise v === valueToNestedRep (valueFromNestedRep v)

canonicalise :: ValueNestedRep -> ValueNestedRep
canonicalise (ValueNestedRep bundles) =
ValueNestedRep . filter (not . isQuantityZero) $ mergeDuplicates bundles
where
mergeDuplicates :: [ValueNestedBundle] -> [ValueNestedBundle]
mergeDuplicates l =
let allAssets :: [(PolicyId, Map AssetName Quantity)]
allAssets = [(pId, qMap) | (ValueNestedBundle pId qMap) <- l]

allAda :: [Quantity]
allAda = [ada | ValueNestedBundleAda ada <- l]

summedMinted :: Map PolicyId (Map AssetName Quantity)
summedMinted = Map.fromListWith (Map.unionWith (<>)) allAssets

summedAda :: Quantity
summedAda = sum allAda

flattenedMinted = map (uncurry ValueNestedBundle) $ Map.toList summedMinted
flattenedAda = ValueNestedBundleAda summedAda

in flattenedAda : flattenedMinted

isQuantityZero :: ValueNestedBundle -> Bool
isQuantityZero (ValueNestedBundleAda 0) = True
isQuantityZero (ValueNestedBundle _ aNameQmap) =
0 `elem` Map.elems aNameQmap
isQuantityZero _ = False

-- -----------------------------------------------------------------------------

Expand Down

0 comments on commit 3b0c669

Please sign in to comment.