Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Data.Value to PlutusLedgerAPI #6143

Merged
merged 14 commits into from
Jun 7, 2024
Prev Previous commit
Next Next commit
Fix issue with unsupported feature
Signed-off-by: Ana Pantilie <ana.pantilie95@gmail.com>
  • Loading branch information
ana-pantilie committed May 31, 2024
commit 5d78bfc3b3400621a66d0cd08fabb8b60d7b219e
28 changes: 21 additions & 7 deletions plutus-ledger-api/src/PlutusLedgerApi/V1/Data/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -480,14 +480,14 @@ unordEqWith is0 eqV = goBoth where
-- null l2 case
(\() -> True)
-- non-null l2 case
(\ _ _ -> Map.all is0 (Map.unsafeFromBuiltinList l2))
(\ _ _ -> Map.all is0 (Map.unsafeFromBuiltinList l2 :: Map.Map BuiltinData BuiltinData))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You decided not to preserve the comments?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I forgot I deleted it, thanks for pointing this out!

)
-- non-null l1 case
( \hd1 tl1 ->
B.matchList
l2
-- null l2 case
(\() -> Map.all is0 (Map.unsafeFromBuiltinList l1))
(\() -> Map.all is0 (Map.unsafeFromBuiltinList l1 :: Map.Map BuiltinData BuiltinData))
-- non-null l2 case
( \hd2 tl2 ->
let
Expand Down Expand Up @@ -555,25 +555,39 @@ unordEqWith is0 eqV = goBoth where
{-# INLINABLE eqMapWith #-}
-- | Check equality of two 'Map's given a function checking whether a value is zero and a function
-- checking equality of values.
eqMapWith ::
forall k v
. (PlutusTx.UnsafeFromData v)
=> (v -> Bool) -> (v -> v -> Bool) -> Map.Map k v -> Map.Map k v -> Bool
eqMapWith
:: (Map.Map TokenName Integer -> Bool)
-> (Map.Map TokenName Integer -> Map.Map TokenName Integer -> Bool)
-> Map.Map CurrencySymbol (Map.Map TokenName Integer)
-> Map.Map CurrencySymbol (Map.Map TokenName Integer)
-> Bool
eqMapWith is0 eqV map1 map2 =
let xs1 = Map.toBuiltinList map1
xs2 = Map.toBuiltinList map2
is0' v = is0 (unsafeFromBuiltinData v)
eqV' v1 v2 = eqV (unsafeFromBuiltinData v1) (unsafeFromBuiltinData v2)
in unordEqWith is0' eqV' xs1 xs2

{-# INLINABLE eqMapWith' #-}
-- | Check equality of two 'Map's given a function checking whether a value is zero and a function
-- checking equality of values.
eqMapWith'
:: (Integer -> Bool) -> (Integer -> Integer -> Bool) -> Map.Map TokenName Integer -> Map.Map TokenName Integer -> Bool
eqMapWith' is0 eqV map1 map2 =
let xs1 = Map.toBuiltinList map1
xs2 = Map.toBuiltinList map2
is0' v = is0 (unsafeFromBuiltinData v)
eqV' v1 v2 = eqV (unsafeFromBuiltinData v1) (unsafeFromBuiltinData v2)
in unordEqWith is0' eqV' xs1 xs2

{-# INLINABLE eq #-}
-- | Check equality of two 'Value's. Does not assume orderness of lists within a 'Value' or a lack
-- of empty values (such as a token whose quantity is zero or a currency that has a bunch of such
-- tokens or no tokens at all), but does assume that no currencies or tokens within a single
-- currency have multiple entries.
eq :: Value -> Value -> Bool
eq (Value currs1) (Value currs2) =
eqMapWith (Map.all (0 ==)) (eqMapWith (0 ==) (==)) currs1 currs2
eqMapWith (Map.all (0 ==)) (eqMapWith' (0 ==) (==)) currs1 currs2

valueToList :: Value -> [(CurrencySymbol, [(TokenName, Integer)])]
valueToList (Map.toBuiltinList . getValue -> bList) =
Expand Down