Skip to content

Commit

Permalink
Switch to derived JSON instance for PolicyID
Browse files Browse the repository at this point in the history
  • Loading branch information
lehins committed Mar 16, 2023
1 parent fa3721b commit bf681bb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
33 changes: 19 additions & 14 deletions eras/mary/impl/src/Cardano/Ledger/Mary/Value.hs
Expand Up @@ -135,23 +135,36 @@ instance DecCBOR AssetName where

-- | Policy ID
newtype PolicyID c = PolicyID {policyID :: ScriptHash c}
deriving (Show, Eq, EncCBOR, DecCBOR, Ord, NoThunks, NFData, Generic)
deriving
( Show
, Eq
, Ord
, Generic
, NoThunks
, NFData
, EncCBOR
, DecCBOR
, ToJSON
, FromJSON
, ToJSONKey
, FromJSONKey
)

-- | The MultiAssets map
newtype MultiAsset c = MultiAsset (Map (PolicyID c) (Map AssetName Integer))
deriving (Show, Generic, ToJSON)

instance Crypto c => Eq (MultiAsset c) where
(MultiAsset x) == (MultiAsset y) = pointWise (pointWise (==)) x y
MultiAsset x == MultiAsset y = pointWise (pointWise (==)) x y

instance NFData (MultiAsset cypto) where
rnf (MultiAsset m) = rnf m

instance NoThunks (MultiAsset c)

instance Semigroup (MultiAsset c) where
(MultiAsset m) <> (MultiAsset m1) =
MultiAsset (canonicalMapUnion (canonicalMapUnion (+)) m m1)
MultiAsset m1 <> MultiAsset m2 =
MultiAsset (canonicalMapUnion (canonicalMapUnion (+)) m1 m2)

instance Monoid (MultiAsset c) where
mempty = MultiAsset mempty
Expand Down Expand Up @@ -179,8 +192,8 @@ instance NFData (MaryValue c) where
instance NoThunks (MaryValue c)

instance Semigroup (MaryValue c) where
MaryValue c m <> MaryValue c1 m1 =
MaryValue (c + c1) (m <> m1)
MaryValue c1 m1 <> MaryValue c2 m2 =
MaryValue (c1 + c2) (m1 <> m2)

instance Monoid (MaryValue c) where
mempty = MaryValue 0 mempty
Expand Down Expand Up @@ -398,14 +411,6 @@ instance ToJSON AssetName where
instance ToJSONKey AssetName where
toJSONKey = toJSONKeyText assetNameToTextAsHex

instance ToJSON (PolicyID era) where
toJSON (PolicyID (ScriptHash h)) = Aeson.String (Hash.hashToTextAsHex h)

instance ToJSONKey (PolicyID era) where
toJSONKey = toJSONKeyText render
where
render (PolicyID (ScriptHash h)) = Hash.hashToTextAsHex h

-- ========================================================================
-- Compactible
-- This is used in the TxOut which stores the (CompactForm MaryValue).
Expand Down
1 change: 1 addition & 0 deletions eras/shelley/impl/CHANGELOG.md
Expand Up @@ -27,6 +27,7 @@
* `AccountState`, `EpochState`, `UTxOState`, `IncrementalStake` and `LedgerState`
* `Likelihood` and `NonMyopic`
* `RewardUpdate` and `PulsingRewUpdate`
* Added of `ToJSON`/`FromJSON` instances for `LogWeight`

### `testlib`

Expand Down

0 comments on commit bf681bb

Please sign in to comment.