{-# LANGUAGE DataKinds             #-}
{-# LANGUAGE DeriveAnyClass        #-}
{-# LANGUAGE DeriveDataTypeable    #-}
{-# LANGUAGE DeriveGeneric         #-}
{-# LANGUAGE DerivingVia           #-}
{-# LANGUAGE LambdaCase            #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NoImplicitPrelude     #-}
{-# LANGUAGE OverloadedLists       #-}
{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE TemplateHaskell       #-}
{-# LANGUAGE TypeApplications      #-}
{-# OPTIONS_GHC -Wno-orphans #-}
-- Prevent unboxing, which the plugin can't deal with
{-# OPTIONS_GHC -fno-omit-interface-pragmas #-}
{-# OPTIONS_GHC -fno-spec-constr #-}
{-# OPTIONS_GHC -fno-specialise #-}

-- | Functions for working with 'Value'.
module Plutus.V1.Ledger.Value(
    -- ** Currency symbols
      CurrencySymbol(..)
    , currencySymbol
    , mpsSymbol
    , currencyMPSHash
    -- ** Token names
    , TokenName(..)
    , tokenName
    , toString
    -- * Asset classes
    , AssetClass(..)
    , assetClass
    , assetClassValue
    , assetClassValueOf
    -- ** Value
    , Value(..)
    , singleton
    , valueOf
    , scale
    , symbols
      -- * Partial order operations
    , geq
    , gt
    , leq
    , lt
      -- * Etc.
    , isZero
    , split
    , unionWith
    , flattenValue
    ) where

import Prelude qualified as Haskell

import Codec.Serialise.Class (Serialise)
import Control.DeepSeq (NFData)
import Data.Aeson (FromJSON, FromJSONKey, ToJSON, ToJSONKey, (.:))
import Data.Aeson qualified as JSON
import Data.Aeson.Extras qualified as JSON
import Data.ByteString qualified as BS
import Data.Data
import Data.Hashable (Hashable)
import Data.List qualified (sortBy)
import Data.String (IsString (fromString))
import Data.Text (Text)
import Data.Text qualified as Text
import Data.Text.Encoding qualified as E
import GHC.Generics (Generic)
import GHC.Show (showList__)
import Plutus.V1.Ledger.Bytes (LedgerBytes (LedgerBytes))
import Plutus.V1.Ledger.Orphans ()
import Plutus.V1.Ledger.Scripts
import PlutusTx qualified as PlutusTx
import PlutusTx.AssocMap qualified as Map
import PlutusTx.Lift (makeLift)
import PlutusTx.Ord qualified as Ord
import PlutusTx.Prelude as PlutusTx hiding (sort)
import PlutusTx.These
import Prettyprinter
import Prettyprinter.Extras

newtype CurrencySymbol = CurrencySymbol { CurrencySymbol -> BuiltinByteString
unCurrencySymbol :: PlutusTx.BuiltinByteString }
    deriving (String -> CurrencySymbol
(String -> CurrencySymbol) -> IsString CurrencySymbol
forall a. (String -> a) -> IsString a
fromString :: String -> CurrencySymbol
$cfromString :: String -> CurrencySymbol
IsString, Int -> CurrencySymbol -> ShowS
[CurrencySymbol] -> ShowS
CurrencySymbol -> String
(Int -> CurrencySymbol -> ShowS)
-> (CurrencySymbol -> String)
-> ([CurrencySymbol] -> ShowS)
-> Show CurrencySymbol
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CurrencySymbol] -> ShowS
$cshowList :: [CurrencySymbol] -> ShowS
show :: CurrencySymbol -> String
$cshow :: CurrencySymbol -> String
showsPrec :: Int -> CurrencySymbol -> ShowS
$cshowsPrec :: Int -> CurrencySymbol -> ShowS
Haskell.Show, Decoder s CurrencySymbol
Decoder s [CurrencySymbol]
[CurrencySymbol] -> Encoding
CurrencySymbol -> Encoding
(CurrencySymbol -> Encoding)
-> (forall s. Decoder s CurrencySymbol)
-> ([CurrencySymbol] -> Encoding)
-> (forall s. Decoder s [CurrencySymbol])
-> Serialise CurrencySymbol
forall s. Decoder s [CurrencySymbol]
forall s. Decoder s CurrencySymbol
forall a.
(a -> Encoding)
-> (forall s. Decoder s a)
-> ([a] -> Encoding)
-> (forall s. Decoder s [a])
-> Serialise a
decodeList :: Decoder s [CurrencySymbol]
$cdecodeList :: forall s. Decoder s [CurrencySymbol]
encodeList :: [CurrencySymbol] -> Encoding
$cencodeList :: [CurrencySymbol] -> Encoding
decode :: Decoder s CurrencySymbol
$cdecode :: forall s. Decoder s CurrencySymbol
encode :: CurrencySymbol -> Encoding
$cencode :: CurrencySymbol -> Encoding
Serialise, [CurrencySymbol] -> Doc ann
CurrencySymbol -> Doc ann
(forall ann. CurrencySymbol -> Doc ann)
-> (forall ann. [CurrencySymbol] -> Doc ann)
-> Pretty CurrencySymbol
forall ann. [CurrencySymbol] -> Doc ann
forall ann. CurrencySymbol -> Doc ann
forall a.
(forall ann. a -> Doc ann)
-> (forall ann. [a] -> Doc ann) -> Pretty a
prettyList :: [CurrencySymbol] -> Doc ann
$cprettyList :: forall ann. [CurrencySymbol] -> Doc ann
pretty :: CurrencySymbol -> Doc ann
$cpretty :: forall ann. CurrencySymbol -> Doc ann
Pretty) via LedgerBytes
    deriving stock ((forall x. CurrencySymbol -> Rep CurrencySymbol x)
-> (forall x. Rep CurrencySymbol x -> CurrencySymbol)
-> Generic CurrencySymbol
forall x. Rep CurrencySymbol x -> CurrencySymbol
forall x. CurrencySymbol -> Rep CurrencySymbol x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep CurrencySymbol x -> CurrencySymbol
$cfrom :: forall x. CurrencySymbol -> Rep CurrencySymbol x
Generic, Typeable CurrencySymbol
DataType
Constr
Typeable CurrencySymbol
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> CurrencySymbol -> c CurrencySymbol)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c CurrencySymbol)
-> (CurrencySymbol -> Constr)
-> (CurrencySymbol -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c CurrencySymbol))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c CurrencySymbol))
-> ((forall b. Data b => b -> b)
    -> CurrencySymbol -> CurrencySymbol)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> CurrencySymbol -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> CurrencySymbol -> r)
-> (forall u.
    (forall d. Data d => d -> u) -> CurrencySymbol -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> CurrencySymbol -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> CurrencySymbol -> m CurrencySymbol)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> CurrencySymbol -> m CurrencySymbol)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> CurrencySymbol -> m CurrencySymbol)
-> Data CurrencySymbol
CurrencySymbol -> DataType
CurrencySymbol -> Constr
(forall b. Data b => b -> b) -> CurrencySymbol -> CurrencySymbol
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CurrencySymbol -> c CurrencySymbol
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CurrencySymbol
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u.
Int -> (forall d. Data d => d -> u) -> CurrencySymbol -> u
forall u. (forall d. Data d => d -> u) -> CurrencySymbol -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> CurrencySymbol -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> CurrencySymbol -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> CurrencySymbol -> m CurrencySymbol
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> CurrencySymbol -> m CurrencySymbol
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CurrencySymbol
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CurrencySymbol -> c CurrencySymbol
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c CurrencySymbol)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c CurrencySymbol)
$cCurrencySymbol :: Constr
$tCurrencySymbol :: DataType
gmapMo :: (forall d. Data d => d -> m d)
-> CurrencySymbol -> m CurrencySymbol
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> CurrencySymbol -> m CurrencySymbol
gmapMp :: (forall d. Data d => d -> m d)
-> CurrencySymbol -> m CurrencySymbol
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> CurrencySymbol -> m CurrencySymbol
gmapM :: (forall d. Data d => d -> m d)
-> CurrencySymbol -> m CurrencySymbol
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> CurrencySymbol -> m CurrencySymbol
gmapQi :: Int -> (forall d. Data d => d -> u) -> CurrencySymbol -> u
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> CurrencySymbol -> u
gmapQ :: (forall d. Data d => d -> u) -> CurrencySymbol -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> CurrencySymbol -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> CurrencySymbol -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> CurrencySymbol -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> CurrencySymbol -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> CurrencySymbol -> r
gmapT :: (forall b. Data b => b -> b) -> CurrencySymbol -> CurrencySymbol
$cgmapT :: (forall b. Data b => b -> b) -> CurrencySymbol -> CurrencySymbol
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c CurrencySymbol)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c CurrencySymbol)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c CurrencySymbol)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c CurrencySymbol)
dataTypeOf :: CurrencySymbol -> DataType
$cdataTypeOf :: CurrencySymbol -> DataType
toConstr :: CurrencySymbol -> Constr
$ctoConstr :: CurrencySymbol -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CurrencySymbol
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CurrencySymbol
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CurrencySymbol -> c CurrencySymbol
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CurrencySymbol -> c CurrencySymbol
$cp1Data :: Typeable CurrencySymbol
Data)
    deriving newtype (CurrencySymbol -> CurrencySymbol -> Bool
(CurrencySymbol -> CurrencySymbol -> Bool)
-> (CurrencySymbol -> CurrencySymbol -> Bool) -> Eq CurrencySymbol
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CurrencySymbol -> CurrencySymbol -> Bool
$c/= :: CurrencySymbol -> CurrencySymbol -> Bool
== :: CurrencySymbol -> CurrencySymbol -> Bool
$c== :: CurrencySymbol -> CurrencySymbol -> Bool
Haskell.Eq, Eq CurrencySymbol
Eq CurrencySymbol
-> (CurrencySymbol -> CurrencySymbol -> Ordering)
-> (CurrencySymbol -> CurrencySymbol -> Bool)
-> (CurrencySymbol -> CurrencySymbol -> Bool)
-> (CurrencySymbol -> CurrencySymbol -> Bool)
-> (CurrencySymbol -> CurrencySymbol -> Bool)
-> (CurrencySymbol -> CurrencySymbol -> CurrencySymbol)
-> (CurrencySymbol -> CurrencySymbol -> CurrencySymbol)
-> Ord CurrencySymbol
CurrencySymbol -> CurrencySymbol -> Bool
CurrencySymbol -> CurrencySymbol -> Ordering
CurrencySymbol -> CurrencySymbol -> CurrencySymbol
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: CurrencySymbol -> CurrencySymbol -> CurrencySymbol
$cmin :: CurrencySymbol -> CurrencySymbol -> CurrencySymbol
max :: CurrencySymbol -> CurrencySymbol -> CurrencySymbol
$cmax :: CurrencySymbol -> CurrencySymbol -> CurrencySymbol
>= :: CurrencySymbol -> CurrencySymbol -> Bool
$c>= :: CurrencySymbol -> CurrencySymbol -> Bool
> :: CurrencySymbol -> CurrencySymbol -> Bool
$c> :: CurrencySymbol -> CurrencySymbol -> Bool
<= :: CurrencySymbol -> CurrencySymbol -> Bool
$c<= :: CurrencySymbol -> CurrencySymbol -> Bool
< :: CurrencySymbol -> CurrencySymbol -> Bool
$c< :: CurrencySymbol -> CurrencySymbol -> Bool
compare :: CurrencySymbol -> CurrencySymbol -> Ordering
$ccompare :: CurrencySymbol -> CurrencySymbol -> Ordering
$cp1Ord :: Eq CurrencySymbol
Haskell.Ord, CurrencySymbol -> CurrencySymbol -> Bool
(CurrencySymbol -> CurrencySymbol -> Bool) -> Eq CurrencySymbol
forall a. (a -> a -> Bool) -> Eq a
== :: CurrencySymbol -> CurrencySymbol -> Bool
$c== :: CurrencySymbol -> CurrencySymbol -> Bool
Eq, Eq CurrencySymbol
Eq CurrencySymbol
-> (CurrencySymbol -> CurrencySymbol -> Ordering)
-> (CurrencySymbol -> CurrencySymbol -> Bool)
-> (CurrencySymbol -> CurrencySymbol -> Bool)
-> (CurrencySymbol -> CurrencySymbol -> Bool)
-> (CurrencySymbol -> CurrencySymbol -> Bool)
-> (CurrencySymbol -> CurrencySymbol -> CurrencySymbol)
-> (CurrencySymbol -> CurrencySymbol -> CurrencySymbol)
-> Ord CurrencySymbol
CurrencySymbol -> CurrencySymbol -> Bool
CurrencySymbol -> CurrencySymbol -> Ordering
CurrencySymbol -> CurrencySymbol -> CurrencySymbol
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: CurrencySymbol -> CurrencySymbol -> CurrencySymbol
$cmin :: CurrencySymbol -> CurrencySymbol -> CurrencySymbol
max :: CurrencySymbol -> CurrencySymbol -> CurrencySymbol
$cmax :: CurrencySymbol -> CurrencySymbol -> CurrencySymbol
>= :: CurrencySymbol -> CurrencySymbol -> Bool
$c>= :: CurrencySymbol -> CurrencySymbol -> Bool
> :: CurrencySymbol -> CurrencySymbol -> Bool
$c> :: CurrencySymbol -> CurrencySymbol -> Bool
<= :: CurrencySymbol -> CurrencySymbol -> Bool
$c<= :: CurrencySymbol -> CurrencySymbol -> Bool
< :: CurrencySymbol -> CurrencySymbol -> Bool
$c< :: CurrencySymbol -> CurrencySymbol -> Bool
compare :: CurrencySymbol -> CurrencySymbol -> Ordering
$ccompare :: CurrencySymbol -> CurrencySymbol -> Ordering
$cp1Ord :: Eq CurrencySymbol
Ord, CurrencySymbol -> BuiltinData
(CurrencySymbol -> BuiltinData) -> ToData CurrencySymbol
forall a. (a -> BuiltinData) -> ToData a
toBuiltinData :: CurrencySymbol -> BuiltinData
$ctoBuiltinData :: CurrencySymbol -> BuiltinData
PlutusTx.ToData, BuiltinData -> Maybe CurrencySymbol
(BuiltinData -> Maybe CurrencySymbol) -> FromData CurrencySymbol
forall a. (BuiltinData -> Maybe a) -> FromData a
fromBuiltinData :: BuiltinData -> Maybe CurrencySymbol
$cfromBuiltinData :: BuiltinData -> Maybe CurrencySymbol
PlutusTx.FromData, BuiltinData -> CurrencySymbol
(BuiltinData -> CurrencySymbol) -> UnsafeFromData CurrencySymbol
forall a. (BuiltinData -> a) -> UnsafeFromData a
unsafeFromBuiltinData :: BuiltinData -> CurrencySymbol
$cunsafeFromBuiltinData :: BuiltinData -> CurrencySymbol
PlutusTx.UnsafeFromData)
    deriving anyclass (Int -> CurrencySymbol -> Int
CurrencySymbol -> Int
(Int -> CurrencySymbol -> Int)
-> (CurrencySymbol -> Int) -> Hashable CurrencySymbol
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: CurrencySymbol -> Int
$chash :: CurrencySymbol -> Int
hashWithSalt :: Int -> CurrencySymbol -> Int
$chashWithSalt :: Int -> CurrencySymbol -> Int
Hashable, ToJSONKeyFunction [CurrencySymbol]
ToJSONKeyFunction CurrencySymbol
ToJSONKeyFunction CurrencySymbol
-> ToJSONKeyFunction [CurrencySymbol] -> ToJSONKey CurrencySymbol
forall a.
ToJSONKeyFunction a -> ToJSONKeyFunction [a] -> ToJSONKey a
toJSONKeyList :: ToJSONKeyFunction [CurrencySymbol]
$ctoJSONKeyList :: ToJSONKeyFunction [CurrencySymbol]
toJSONKey :: ToJSONKeyFunction CurrencySymbol
$ctoJSONKey :: ToJSONKeyFunction CurrencySymbol
ToJSONKey, FromJSONKeyFunction [CurrencySymbol]
FromJSONKeyFunction CurrencySymbol
FromJSONKeyFunction CurrencySymbol
-> FromJSONKeyFunction [CurrencySymbol]
-> FromJSONKey CurrencySymbol
forall a.
FromJSONKeyFunction a -> FromJSONKeyFunction [a] -> FromJSONKey a
fromJSONKeyList :: FromJSONKeyFunction [CurrencySymbol]
$cfromJSONKeyList :: FromJSONKeyFunction [CurrencySymbol]
fromJSONKey :: FromJSONKeyFunction CurrencySymbol
$cfromJSONKey :: FromJSONKeyFunction CurrencySymbol
FromJSONKey,  CurrencySymbol -> ()
(CurrencySymbol -> ()) -> NFData CurrencySymbol
forall a. (a -> ()) -> NFData a
rnf :: CurrencySymbol -> ()
$crnf :: CurrencySymbol -> ()
NFData)

instance ToJSON CurrencySymbol where
  toJSON :: CurrencySymbol -> Value
toJSON CurrencySymbol
c =
    [Pair] -> Value
JSON.object
      [ ( Text
"unCurrencySymbol"
        , Text -> Value
JSON.String (Text -> Value)
-> (CurrencySymbol -> Text) -> CurrencySymbol -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
          ByteString -> Text
JSON.encodeByteString (ByteString -> Text)
-> (CurrencySymbol -> ByteString) -> CurrencySymbol -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
          BuiltinByteString -> ByteString
forall arep a. FromBuiltin arep a => arep -> a
PlutusTx.fromBuiltin (BuiltinByteString -> ByteString)
-> (CurrencySymbol -> BuiltinByteString)
-> CurrencySymbol
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
          CurrencySymbol -> BuiltinByteString
unCurrencySymbol (CurrencySymbol -> Value) -> CurrencySymbol -> Value
forall a b. (a -> b) -> a -> b
$
          CurrencySymbol
c)
      ]

instance FromJSON CurrencySymbol where
  parseJSON :: Value -> Parser CurrencySymbol
parseJSON =
    String
-> (Object -> Parser CurrencySymbol)
-> Value
-> Parser CurrencySymbol
forall a. String -> (Object -> Parser a) -> Value -> Parser a
JSON.withObject String
"CurrencySymbol" ((Object -> Parser CurrencySymbol)
 -> Value -> Parser CurrencySymbol)
-> (Object -> Parser CurrencySymbol)
-> Value
-> Parser CurrencySymbol
forall a b. (a -> b) -> a -> b
$ \Object
object -> do
      Value
raw <- Object
object Object -> Text -> Parser Value
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"unCurrencySymbol"
      ByteString
bytes <- Value -> Parser ByteString
JSON.decodeByteString Value
raw
      CurrencySymbol -> Parser CurrencySymbol
forall (f :: * -> *) a. Applicative f => a -> f a
Haskell.pure (CurrencySymbol -> Parser CurrencySymbol)
-> CurrencySymbol -> Parser CurrencySymbol
forall a b. (a -> b) -> a -> b
$ BuiltinByteString -> CurrencySymbol
CurrencySymbol (BuiltinByteString -> CurrencySymbol)
-> BuiltinByteString -> CurrencySymbol
forall a b. (a -> b) -> a -> b
$ ByteString -> BuiltinByteString
forall a arep. ToBuiltin a arep => a -> arep
PlutusTx.toBuiltin ByteString
bytes

{-# INLINABLE mpsSymbol #-}
-- | The currency symbol of a monetay policy hash
mpsSymbol :: MintingPolicyHash -> CurrencySymbol
mpsSymbol :: MintingPolicyHash -> CurrencySymbol
mpsSymbol (MintingPolicyHash BuiltinByteString
h) = BuiltinByteString -> CurrencySymbol
CurrencySymbol BuiltinByteString
h

{-# INLINABLE currencyMPSHash #-}
-- | The minting policy hash of a currency symbol
currencyMPSHash :: CurrencySymbol -> MintingPolicyHash
currencyMPSHash :: CurrencySymbol -> MintingPolicyHash
currencyMPSHash (CurrencySymbol BuiltinByteString
h) = BuiltinByteString -> MintingPolicyHash
MintingPolicyHash BuiltinByteString
h

{-# INLINABLE currencySymbol #-}
-- | Creates `CurrencySymbol` from raw `ByteString`.
currencySymbol :: BS.ByteString -> CurrencySymbol
currencySymbol :: ByteString -> CurrencySymbol
currencySymbol = BuiltinByteString -> CurrencySymbol
CurrencySymbol (BuiltinByteString -> CurrencySymbol)
-> (ByteString -> BuiltinByteString)
-> ByteString
-> CurrencySymbol
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> BuiltinByteString
forall a arep. ToBuiltin a arep => a -> arep
PlutusTx.toBuiltin

-- | ByteString of a name of a token, shown as UTF-8 string when possible
newtype TokenName = TokenName { TokenName -> BuiltinByteString
unTokenName :: PlutusTx.BuiltinByteString }
    deriving (Decoder s TokenName
Decoder s [TokenName]
[TokenName] -> Encoding
TokenName -> Encoding
(TokenName -> Encoding)
-> (forall s. Decoder s TokenName)
-> ([TokenName] -> Encoding)
-> (forall s. Decoder s [TokenName])
-> Serialise TokenName
forall s. Decoder s [TokenName]
forall s. Decoder s TokenName
forall a.
(a -> Encoding)
-> (forall s. Decoder s a)
-> ([a] -> Encoding)
-> (forall s. Decoder s [a])
-> Serialise a
decodeList :: Decoder s [TokenName]
$cdecodeList :: forall s. Decoder s [TokenName]
encodeList :: [TokenName] -> Encoding
$cencodeList :: [TokenName] -> Encoding
decode :: Decoder s TokenName
$cdecode :: forall s. Decoder s TokenName
encode :: TokenName -> Encoding
$cencode :: TokenName -> Encoding
Serialise) via LedgerBytes
    deriving stock ((forall x. TokenName -> Rep TokenName x)
-> (forall x. Rep TokenName x -> TokenName) -> Generic TokenName
forall x. Rep TokenName x -> TokenName
forall x. TokenName -> Rep TokenName x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep TokenName x -> TokenName
$cfrom :: forall x. TokenName -> Rep TokenName x
Generic, Typeable TokenName
DataType
Constr
Typeable TokenName
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> TokenName -> c TokenName)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c TokenName)
-> (TokenName -> Constr)
-> (TokenName -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c TokenName))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c TokenName))
-> ((forall b. Data b => b -> b) -> TokenName -> TokenName)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> TokenName -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> TokenName -> r)
-> (forall u. (forall d. Data d => d -> u) -> TokenName -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> TokenName -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> TokenName -> m TokenName)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> TokenName -> m TokenName)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> TokenName -> m TokenName)
-> Data TokenName
TokenName -> DataType
TokenName -> Constr
(forall b. Data b => b -> b) -> TokenName -> TokenName
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> TokenName -> c TokenName
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c TokenName
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> TokenName -> u
forall u. (forall d. Data d => d -> u) -> TokenName -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> TokenName -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> TokenName -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> TokenName -> m TokenName
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> TokenName -> m TokenName
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c TokenName
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> TokenName -> c TokenName
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c TokenName)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c TokenName)
$cTokenName :: Constr
$tTokenName :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> TokenName -> m TokenName
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> TokenName -> m TokenName
gmapMp :: (forall d. Data d => d -> m d) -> TokenName -> m TokenName
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> TokenName -> m TokenName
gmapM :: (forall d. Data d => d -> m d) -> TokenName -> m TokenName
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> TokenName -> m TokenName
gmapQi :: Int -> (forall d. Data d => d -> u) -> TokenName -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> TokenName -> u
gmapQ :: (forall d. Data d => d -> u) -> TokenName -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> TokenName -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> TokenName -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> TokenName -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> TokenName -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> TokenName -> r
gmapT :: (forall b. Data b => b -> b) -> TokenName -> TokenName
$cgmapT :: (forall b. Data b => b -> b) -> TokenName -> TokenName
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c TokenName)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c TokenName)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c TokenName)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c TokenName)
dataTypeOf :: TokenName -> DataType
$cdataTypeOf :: TokenName -> DataType
toConstr :: TokenName -> Constr
$ctoConstr :: TokenName -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c TokenName
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c TokenName
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> TokenName -> c TokenName
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> TokenName -> c TokenName
$cp1Data :: Typeable TokenName
Data)
    deriving newtype (TokenName -> TokenName -> Bool
(TokenName -> TokenName -> Bool)
-> (TokenName -> TokenName -> Bool) -> Eq TokenName
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TokenName -> TokenName -> Bool
$c/= :: TokenName -> TokenName -> Bool
== :: TokenName -> TokenName -> Bool
$c== :: TokenName -> TokenName -> Bool
Haskell.Eq, Eq TokenName
Eq TokenName
-> (TokenName -> TokenName -> Ordering)
-> (TokenName -> TokenName -> Bool)
-> (TokenName -> TokenName -> Bool)
-> (TokenName -> TokenName -> Bool)
-> (TokenName -> TokenName -> Bool)
-> (TokenName -> TokenName -> TokenName)
-> (TokenName -> TokenName -> TokenName)
-> Ord TokenName
TokenName -> TokenName -> Bool
TokenName -> TokenName -> Ordering
TokenName -> TokenName -> TokenName
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: TokenName -> TokenName -> TokenName
$cmin :: TokenName -> TokenName -> TokenName
max :: TokenName -> TokenName -> TokenName
$cmax :: TokenName -> TokenName -> TokenName
>= :: TokenName -> TokenName -> Bool
$c>= :: TokenName -> TokenName -> Bool
> :: TokenName -> TokenName -> Bool
$c> :: TokenName -> TokenName -> Bool
<= :: TokenName -> TokenName -> Bool
$c<= :: TokenName -> TokenName -> Bool
< :: TokenName -> TokenName -> Bool
$c< :: TokenName -> TokenName -> Bool
compare :: TokenName -> TokenName -> Ordering
$ccompare :: TokenName -> TokenName -> Ordering
$cp1Ord :: Eq TokenName
Haskell.Ord, TokenName -> TokenName -> Bool
(TokenName -> TokenName -> Bool) -> Eq TokenName
forall a. (a -> a -> Bool) -> Eq a
== :: TokenName -> TokenName -> Bool
$c== :: TokenName -> TokenName -> Bool
Eq, Eq TokenName
Eq TokenName
-> (TokenName -> TokenName -> Ordering)
-> (TokenName -> TokenName -> Bool)
-> (TokenName -> TokenName -> Bool)
-> (TokenName -> TokenName -> Bool)
-> (TokenName -> TokenName -> Bool)
-> (TokenName -> TokenName -> TokenName)
-> (TokenName -> TokenName -> TokenName)
-> Ord TokenName
TokenName -> TokenName -> Bool
TokenName -> TokenName -> Ordering
TokenName -> TokenName -> TokenName
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: TokenName -> TokenName -> TokenName
$cmin :: TokenName -> TokenName -> TokenName
max :: TokenName -> TokenName -> TokenName
$cmax :: TokenName -> TokenName -> TokenName
>= :: TokenName -> TokenName -> Bool
$c>= :: TokenName -> TokenName -> Bool
> :: TokenName -> TokenName -> Bool
$c> :: TokenName -> TokenName -> Bool
<= :: TokenName -> TokenName -> Bool
$c<= :: TokenName -> TokenName -> Bool
< :: TokenName -> TokenName -> Bool
$c< :: TokenName -> TokenName -> Bool
compare :: TokenName -> TokenName -> Ordering
$ccompare :: TokenName -> TokenName -> Ordering
$cp1Ord :: Eq TokenName
Ord, TokenName -> BuiltinData
(TokenName -> BuiltinData) -> ToData TokenName
forall a. (a -> BuiltinData) -> ToData a
toBuiltinData :: TokenName -> BuiltinData
$ctoBuiltinData :: TokenName -> BuiltinData
PlutusTx.ToData, BuiltinData -> Maybe TokenName
(BuiltinData -> Maybe TokenName) -> FromData TokenName
forall a. (BuiltinData -> Maybe a) -> FromData a
fromBuiltinData :: BuiltinData -> Maybe TokenName
$cfromBuiltinData :: BuiltinData -> Maybe TokenName
PlutusTx.FromData, BuiltinData -> TokenName
(BuiltinData -> TokenName) -> UnsafeFromData TokenName
forall a. (BuiltinData -> a) -> UnsafeFromData a
unsafeFromBuiltinData :: BuiltinData -> TokenName
$cunsafeFromBuiltinData :: BuiltinData -> TokenName
PlutusTx.UnsafeFromData)
    deriving anyclass (Int -> TokenName -> Int
TokenName -> Int
(Int -> TokenName -> Int)
-> (TokenName -> Int) -> Hashable TokenName
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: TokenName -> Int
$chash :: TokenName -> Int
hashWithSalt :: Int -> TokenName -> Int
$chashWithSalt :: Int -> TokenName -> Int
Hashable, TokenName -> ()
(TokenName -> ()) -> NFData TokenName
forall a. (a -> ()) -> NFData a
rnf :: TokenName -> ()
$crnf :: TokenName -> ()
NFData)
    deriving [TokenName] -> Doc ann
TokenName -> Doc ann
(forall ann. TokenName -> Doc ann)
-> (forall ann. [TokenName] -> Doc ann) -> Pretty TokenName
forall ann. [TokenName] -> Doc ann
forall ann. TokenName -> Doc ann
forall a.
(forall ann. a -> Doc ann)
-> (forall ann. [a] -> Doc ann) -> Pretty a
prettyList :: [TokenName] -> Doc ann
$cprettyList :: forall ann. [TokenName] -> Doc ann
pretty :: TokenName -> Doc ann
$cpretty :: forall ann. TokenName -> Doc ann
Pretty via (PrettyShow TokenName)

instance IsString TokenName where
    fromString :: String -> TokenName
fromString = Text -> TokenName
fromText (Text -> TokenName) -> (String -> Text) -> String -> TokenName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
Text.pack

{-# INLINABLE tokenName #-}
-- | Creates `TokenName` from raw `ByteString`.
tokenName :: BS.ByteString -> TokenName
tokenName :: ByteString -> TokenName
tokenName = BuiltinByteString -> TokenName
TokenName (BuiltinByteString -> TokenName)
-> (ByteString -> BuiltinByteString) -> ByteString -> TokenName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> BuiltinByteString
forall a arep. ToBuiltin a arep => a -> arep
PlutusTx.toBuiltin

fromText :: Text -> TokenName
fromText :: Text -> TokenName
fromText = ByteString -> TokenName
tokenName (ByteString -> TokenName)
-> (Text -> ByteString) -> Text -> TokenName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
E.encodeUtf8

fromTokenName :: (BS.ByteString -> r) -> (Text -> r) -> TokenName -> r
fromTokenName :: (ByteString -> r) -> (Text -> r) -> TokenName -> r
fromTokenName ByteString -> r
handleBytestring Text -> r
handleText (TokenName BuiltinByteString
bs) = (UnicodeException -> r)
-> (Text -> r) -> Either UnicodeException Text -> r
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (\UnicodeException
_ -> ByteString -> r
handleBytestring (ByteString -> r) -> ByteString -> r
forall a b. (a -> b) -> a -> b
$ BuiltinByteString -> ByteString
forall arep a. FromBuiltin arep a => arep -> a
PlutusTx.fromBuiltin BuiltinByteString
bs) Text -> r
handleText (Either UnicodeException Text -> r)
-> Either UnicodeException Text -> r
forall a b. (a -> b) -> a -> b
$ ByteString -> Either UnicodeException Text
E.decodeUtf8' (BuiltinByteString -> ByteString
forall arep a. FromBuiltin arep a => arep -> a
PlutusTx.fromBuiltin BuiltinByteString
bs)

asBase16 :: BS.ByteString -> Text
asBase16 :: ByteString -> Text
asBase16 ByteString
bs = [Text] -> Text
Text.concat [Item [Text]
"0x", ByteString -> Text
JSON.encodeByteString ByteString
bs]

quoted :: Text -> Text
quoted :: Text -> Text
quoted Text
s = [Text] -> Text
Text.concat [Item [Text]
"\"", Text
Item [Text]
s, Item [Text]
"\""]

toString :: TokenName -> Haskell.String
toString :: TokenName -> String
toString = Text -> String
Text.unpack (Text -> String) -> (TokenName -> Text) -> TokenName -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> Text) -> (Text -> Text) -> TokenName -> Text
forall r. (ByteString -> r) -> (Text -> r) -> TokenName -> r
fromTokenName ByteString -> Text
asBase16 Text -> Text
forall a. a -> a
id

instance Haskell.Show TokenName where
    show :: TokenName -> String
show = Text -> String
Text.unpack (Text -> String) -> (TokenName -> Text) -> TokenName -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> Text) -> (Text -> Text) -> TokenName -> Text
forall r. (ByteString -> r) -> (Text -> r) -> TokenName -> r
fromTokenName ByteString -> Text
asBase16 Text -> Text
quoted

{- note [Roundtripping token names]

How to properly roundtrip a token name that is not valid UTF-8 through PureScript
without a big rewrite of the API?
We prefix it with a zero byte so we can recognize it when we get a bytestring value back,
and we serialize it base16 encoded, with 0x in front so it will look as a hex string.
(Browsers don't render the zero byte.)
-}

instance ToJSON TokenName where
    toJSON :: TokenName -> Value
toJSON = [Pair] -> Value
JSON.object ([Pair] -> Value) -> (TokenName -> [Pair]) -> TokenName -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pair -> [Pair]
forall (f :: * -> *) a. Applicative f => a -> f a
Haskell.pure (Pair -> [Pair]) -> (TokenName -> Pair) -> TokenName -> [Pair]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (,) Text
"unTokenName" (Value -> Pair) -> (TokenName -> Value) -> TokenName -> Pair
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Value
forall a. ToJSON a => a -> Value
JSON.toJSON (Text -> Value) -> (TokenName -> Text) -> TokenName -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
        (ByteString -> Text) -> (Text -> Text) -> TokenName -> Text
forall r. (ByteString -> r) -> (Text -> r) -> TokenName -> r
fromTokenName
            (\ByteString
bs -> Char -> Text -> Text
Text.cons Char
'\NUL' (ByteString -> Text
asBase16 ByteString
bs))
            (\Text
t -> case Int -> Text -> Text
Text.take Int
1 Text
t of Text
"\NUL" -> [Text] -> Text
Text.concat [Item [Text]
"\NUL\NUL", Text
Item [Text]
t]; Text
_ -> Text
t)

instance FromJSON TokenName where
    parseJSON :: Value -> Parser TokenName
parseJSON =
        String -> (Object -> Parser TokenName) -> Value -> Parser TokenName
forall a. String -> (Object -> Parser a) -> Value -> Parser a
JSON.withObject String
"TokenName" ((Object -> Parser TokenName) -> Value -> Parser TokenName)
-> (Object -> Parser TokenName) -> Value -> Parser TokenName
forall a b. (a -> b) -> a -> b
$ \Object
object -> do
        Text
raw <- Object
object Object -> Text -> Parser Text
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"unTokenName"
        Text -> Parser TokenName
forall (m :: * -> *). MonadFail m => Text -> m TokenName
fromJSONText Text
raw
        where
            fromJSONText :: Text -> m TokenName
fromJSONText Text
t = case Int -> Text -> Text
Text.take Int
3 Text
t of
                Text
"\NUL0x"       -> (String -> m TokenName)
-> (ByteString -> m TokenName)
-> Either String ByteString
-> m TokenName
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either String -> m TokenName
forall (m :: * -> *) a. MonadFail m => String -> m a
Haskell.fail (TokenName -> m TokenName
forall (f :: * -> *) a. Applicative f => a -> f a
Haskell.pure (TokenName -> m TokenName)
-> (ByteString -> TokenName) -> ByteString -> m TokenName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> TokenName
tokenName) (Either String ByteString -> m TokenName)
-> (Text -> Either String ByteString) -> Text -> m TokenName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either String ByteString
JSON.tryDecode (Text -> Either String ByteString)
-> (Text -> Text) -> Text -> Either String ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Text -> Text
Text.drop Int
3 (Text -> m TokenName) -> Text -> m TokenName
forall a b. (a -> b) -> a -> b
$ Text
t
                Text
"\NUL\NUL\NUL" -> TokenName -> m TokenName
forall (f :: * -> *) a. Applicative f => a -> f a
Haskell.pure (TokenName -> m TokenName)
-> (Text -> TokenName) -> Text -> m TokenName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> TokenName
fromText (Text -> TokenName) -> (Text -> Text) -> Text -> TokenName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Text -> Text
Text.drop Int
2 (Text -> m TokenName) -> Text -> m TokenName
forall a b. (a -> b) -> a -> b
$ Text
t
                Text
_              -> TokenName -> m TokenName
forall (f :: * -> *) a. Applicative f => a -> f a
Haskell.pure (TokenName -> m TokenName)
-> (Text -> TokenName) -> Text -> m TokenName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> TokenName
fromText (Text -> m TokenName) -> Text -> m TokenName
forall a b. (a -> b) -> a -> b
$ Text
t

-- | An asset class, identified by currency symbol and token name.
newtype AssetClass = AssetClass { AssetClass -> (CurrencySymbol, TokenName)
unAssetClass :: (CurrencySymbol, TokenName) }
    deriving stock ((forall x. AssetClass -> Rep AssetClass x)
-> (forall x. Rep AssetClass x -> AssetClass) -> Generic AssetClass
forall x. Rep AssetClass x -> AssetClass
forall x. AssetClass -> Rep AssetClass x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep AssetClass x -> AssetClass
$cfrom :: forall x. AssetClass -> Rep AssetClass x
Generic, Typeable AssetClass
DataType
Constr
Typeable AssetClass
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> AssetClass -> c AssetClass)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c AssetClass)
-> (AssetClass -> Constr)
-> (AssetClass -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c AssetClass))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c AssetClass))
-> ((forall b. Data b => b -> b) -> AssetClass -> AssetClass)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> AssetClass -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> AssetClass -> r)
-> (forall u. (forall d. Data d => d -> u) -> AssetClass -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> AssetClass -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> AssetClass -> m AssetClass)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> AssetClass -> m AssetClass)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> AssetClass -> m AssetClass)
-> Data AssetClass
AssetClass -> DataType
AssetClass -> Constr
(forall b. Data b => b -> b) -> AssetClass -> AssetClass
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> AssetClass -> c AssetClass
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c AssetClass
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> AssetClass -> u
forall u. (forall d. Data d => d -> u) -> AssetClass -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> AssetClass -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> AssetClass -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> AssetClass -> m AssetClass
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> AssetClass -> m AssetClass
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c AssetClass
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> AssetClass -> c AssetClass
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c AssetClass)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c AssetClass)
$cAssetClass :: Constr
$tAssetClass :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> AssetClass -> m AssetClass
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> AssetClass -> m AssetClass
gmapMp :: (forall d. Data d => d -> m d) -> AssetClass -> m AssetClass
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> AssetClass -> m AssetClass
gmapM :: (forall d. Data d => d -> m d) -> AssetClass -> m AssetClass
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> AssetClass -> m AssetClass
gmapQi :: Int -> (forall d. Data d => d -> u) -> AssetClass -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> AssetClass -> u
gmapQ :: (forall d. Data d => d -> u) -> AssetClass -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> AssetClass -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> AssetClass -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> AssetClass -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> AssetClass -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> AssetClass -> r
gmapT :: (forall b. Data b => b -> b) -> AssetClass -> AssetClass
$cgmapT :: (forall b. Data b => b -> b) -> AssetClass -> AssetClass
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c AssetClass)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c AssetClass)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c AssetClass)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c AssetClass)
dataTypeOf :: AssetClass -> DataType
$cdataTypeOf :: AssetClass -> DataType
toConstr :: AssetClass -> Constr
$ctoConstr :: AssetClass -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c AssetClass
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c AssetClass
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> AssetClass -> c AssetClass
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> AssetClass -> c AssetClass
$cp1Data :: Typeable AssetClass
Data)
    deriving newtype (AssetClass -> AssetClass -> Bool
(AssetClass -> AssetClass -> Bool)
-> (AssetClass -> AssetClass -> Bool) -> Eq AssetClass
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AssetClass -> AssetClass -> Bool
$c/= :: AssetClass -> AssetClass -> Bool
== :: AssetClass -> AssetClass -> Bool
$c== :: AssetClass -> AssetClass -> Bool
Haskell.Eq, Eq AssetClass
Eq AssetClass
-> (AssetClass -> AssetClass -> Ordering)
-> (AssetClass -> AssetClass -> Bool)
-> (AssetClass -> AssetClass -> Bool)
-> (AssetClass -> AssetClass -> Bool)
-> (AssetClass -> AssetClass -> Bool)
-> (AssetClass -> AssetClass -> AssetClass)
-> (AssetClass -> AssetClass -> AssetClass)
-> Ord AssetClass
AssetClass -> AssetClass -> Bool
AssetClass -> AssetClass -> Ordering
AssetClass -> AssetClass -> AssetClass
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: AssetClass -> AssetClass -> AssetClass
$cmin :: AssetClass -> AssetClass -> AssetClass
max :: AssetClass -> AssetClass -> AssetClass
$cmax :: AssetClass -> AssetClass -> AssetClass
>= :: AssetClass -> AssetClass -> Bool
$c>= :: AssetClass -> AssetClass -> Bool
> :: AssetClass -> AssetClass -> Bool
$c> :: AssetClass -> AssetClass -> Bool
<= :: AssetClass -> AssetClass -> Bool
$c<= :: AssetClass -> AssetClass -> Bool
< :: AssetClass -> AssetClass -> Bool
$c< :: AssetClass -> AssetClass -> Bool
compare :: AssetClass -> AssetClass -> Ordering
$ccompare :: AssetClass -> AssetClass -> Ordering
$cp1Ord :: Eq AssetClass
Haskell.Ord, Int -> AssetClass -> ShowS
[AssetClass] -> ShowS
AssetClass -> String
(Int -> AssetClass -> ShowS)
-> (AssetClass -> String)
-> ([AssetClass] -> ShowS)
-> Show AssetClass
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AssetClass] -> ShowS
$cshowList :: [AssetClass] -> ShowS
show :: AssetClass -> String
$cshow :: AssetClass -> String
showsPrec :: Int -> AssetClass -> ShowS
$cshowsPrec :: Int -> AssetClass -> ShowS
Haskell.Show, AssetClass -> AssetClass -> Bool
(AssetClass -> AssetClass -> Bool) -> Eq AssetClass
forall a. (a -> a -> Bool) -> Eq a
== :: AssetClass -> AssetClass -> Bool
$c== :: AssetClass -> AssetClass -> Bool
Eq, Eq AssetClass
Eq AssetClass
-> (AssetClass -> AssetClass -> Ordering)
-> (AssetClass -> AssetClass -> Bool)
-> (AssetClass -> AssetClass -> Bool)
-> (AssetClass -> AssetClass -> Bool)
-> (AssetClass -> AssetClass -> Bool)
-> (AssetClass -> AssetClass -> AssetClass)
-> (AssetClass -> AssetClass -> AssetClass)
-> Ord AssetClass
AssetClass -> AssetClass -> Bool
AssetClass -> AssetClass -> Ordering
AssetClass -> AssetClass -> AssetClass
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: AssetClass -> AssetClass -> AssetClass
$cmin :: AssetClass -> AssetClass -> AssetClass
max :: AssetClass -> AssetClass -> AssetClass
$cmax :: AssetClass -> AssetClass -> AssetClass
>= :: AssetClass -> AssetClass -> Bool
$c>= :: AssetClass -> AssetClass -> Bool
> :: AssetClass -> AssetClass -> Bool
$c> :: AssetClass -> AssetClass -> Bool
<= :: AssetClass -> AssetClass -> Bool
$c<= :: AssetClass -> AssetClass -> Bool
< :: AssetClass -> AssetClass -> Bool
$c< :: AssetClass -> AssetClass -> Bool
compare :: AssetClass -> AssetClass -> Ordering
$ccompare :: AssetClass -> AssetClass -> Ordering
$cp1Ord :: Eq AssetClass
Ord, Decoder s AssetClass
Decoder s [AssetClass]
[AssetClass] -> Encoding
AssetClass -> Encoding
(AssetClass -> Encoding)
-> (forall s. Decoder s AssetClass)
-> ([AssetClass] -> Encoding)
-> (forall s. Decoder s [AssetClass])
-> Serialise AssetClass
forall s. Decoder s [AssetClass]
forall s. Decoder s AssetClass
forall a.
(a -> Encoding)
-> (forall s. Decoder s a)
-> ([a] -> Encoding)
-> (forall s. Decoder s [a])
-> Serialise a
decodeList :: Decoder s [AssetClass]
$cdecodeList :: forall s. Decoder s [AssetClass]
encodeList :: [AssetClass] -> Encoding
$cencodeList :: [AssetClass] -> Encoding
decode :: Decoder s AssetClass
$cdecode :: forall s. Decoder s AssetClass
encode :: AssetClass -> Encoding
$cencode :: AssetClass -> Encoding
Serialise, AssetClass -> BuiltinData
(AssetClass -> BuiltinData) -> ToData AssetClass
forall a. (a -> BuiltinData) -> ToData a
toBuiltinData :: AssetClass -> BuiltinData
$ctoBuiltinData :: AssetClass -> BuiltinData
PlutusTx.ToData, BuiltinData -> Maybe AssetClass
(BuiltinData -> Maybe AssetClass) -> FromData AssetClass
forall a. (BuiltinData -> Maybe a) -> FromData a
fromBuiltinData :: BuiltinData -> Maybe AssetClass
$cfromBuiltinData :: BuiltinData -> Maybe AssetClass
PlutusTx.FromData, BuiltinData -> AssetClass
(BuiltinData -> AssetClass) -> UnsafeFromData AssetClass
forall a. (BuiltinData -> a) -> UnsafeFromData a
unsafeFromBuiltinData :: BuiltinData -> AssetClass
$cunsafeFromBuiltinData :: BuiltinData -> AssetClass
PlutusTx.UnsafeFromData)
    deriving anyclass (Int -> AssetClass -> Int
AssetClass -> Int
(Int -> AssetClass -> Int)
-> (AssetClass -> Int) -> Hashable AssetClass
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: AssetClass -> Int
$chash :: AssetClass -> Int
hashWithSalt :: Int -> AssetClass -> Int
$chashWithSalt :: Int -> AssetClass -> Int
Hashable, AssetClass -> ()
(AssetClass -> ()) -> NFData AssetClass
forall a. (a -> ()) -> NFData a
rnf :: AssetClass -> ()
$crnf :: AssetClass -> ()
NFData, [AssetClass] -> Encoding
[AssetClass] -> Value
AssetClass -> Encoding
AssetClass -> Value
(AssetClass -> Value)
-> (AssetClass -> Encoding)
-> ([AssetClass] -> Value)
-> ([AssetClass] -> Encoding)
-> ToJSON AssetClass
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [AssetClass] -> Encoding
$ctoEncodingList :: [AssetClass] -> Encoding
toJSONList :: [AssetClass] -> Value
$ctoJSONList :: [AssetClass] -> Value
toEncoding :: AssetClass -> Encoding
$ctoEncoding :: AssetClass -> Encoding
toJSON :: AssetClass -> Value
$ctoJSON :: AssetClass -> Value
ToJSON, Value -> Parser [AssetClass]
Value -> Parser AssetClass
(Value -> Parser AssetClass)
-> (Value -> Parser [AssetClass]) -> FromJSON AssetClass
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [AssetClass]
$cparseJSONList :: Value -> Parser [AssetClass]
parseJSON :: Value -> Parser AssetClass
$cparseJSON :: Value -> Parser AssetClass
FromJSON)
    deriving [AssetClass] -> Doc ann
AssetClass -> Doc ann
(forall ann. AssetClass -> Doc ann)
-> (forall ann. [AssetClass] -> Doc ann) -> Pretty AssetClass
forall ann. [AssetClass] -> Doc ann
forall ann. AssetClass -> Doc ann
forall a.
(forall ann. a -> Doc ann)
-> (forall ann. [a] -> Doc ann) -> Pretty a
prettyList :: [AssetClass] -> Doc ann
$cprettyList :: forall ann. [AssetClass] -> Doc ann
pretty :: AssetClass -> Doc ann
$cpretty :: forall ann. AssetClass -> Doc ann
Pretty via (PrettyShow (CurrencySymbol, TokenName))

{-# INLINABLE assetClass #-}
assetClass :: CurrencySymbol -> TokenName -> AssetClass
assetClass :: CurrencySymbol -> TokenName -> AssetClass
assetClass CurrencySymbol
s TokenName
t = (CurrencySymbol, TokenName) -> AssetClass
AssetClass (CurrencySymbol
s, TokenName
t)

-- | A cryptocurrency value. This is a map from 'CurrencySymbol's to a
-- quantity of that currency.
--
-- Operations on currencies are usually implemented /pointwise/. That is,
-- we apply the operation to the quantities for each currency in turn. So
-- when we add two 'Value's the resulting 'Value' has, for each currency,
-- the sum of the quantities of /that particular/ currency in the argument
-- 'Value'. The effect of this is that the currencies in the 'Value' are "independent",
-- and are operated on separately.
--
-- Whenever we need to get the quantity of a currency in a 'Value' where there
-- is no explicit quantity of that currency in the 'Value', then the quantity is
-- taken to be zero.
--
-- See note [Currencies] for more details.
newtype Value = Value { Value -> Map CurrencySymbol (Map TokenName Integer)
getValue :: Map.Map CurrencySymbol (Map.Map TokenName Integer) }
    deriving stock ((forall x. Value -> Rep Value x)
-> (forall x. Rep Value x -> Value) -> Generic Value
forall x. Rep Value x -> Value
forall x. Value -> Rep Value x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Value x -> Value
$cfrom :: forall x. Value -> Rep Value x
Generic, Typeable Value
DataType
Constr
Typeable Value
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> Value -> c Value)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c Value)
-> (Value -> Constr)
-> (Value -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c Value))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Value))
-> ((forall b. Data b => b -> b) -> Value -> Value)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Value -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Value -> r)
-> (forall u. (forall d. Data d => d -> u) -> Value -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> Value -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> Value -> m Value)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Value -> m Value)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Value -> m Value)
-> Data Value
Value -> DataType
Value -> Constr
(forall b. Data b => b -> b) -> Value -> Value
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Value -> c Value
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Value
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Value -> u
forall u. (forall d. Data d => d -> u) -> Value -> [u]
forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Value -> r
forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Value -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Value -> m Value
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Value -> m Value
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Value
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Value -> c Value
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Value)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Value)
$cValue :: Constr
$tValue :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> Value -> m Value
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Value -> m Value
gmapMp :: (forall d. Data d => d -> m d) -> Value -> m Value
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Value -> m Value
gmapM :: (forall d. Data d => d -> m d) -> Value -> m Value
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Value -> m Value
gmapQi :: Int -> (forall d. Data d => d -> u) -> Value -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Value -> u
gmapQ :: (forall d. Data d => d -> u) -> Value -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Value -> [u]
gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Value -> r
$cgmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Value -> r
gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Value -> r
$cgmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Value -> r
gmapT :: (forall b. Data b => b -> b) -> Value -> Value
$cgmapT :: (forall b. Data b => b -> b) -> Value -> Value
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Value)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Value)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c Value)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Value)
dataTypeOf :: Value -> DataType
$cdataTypeOf :: Value -> DataType
toConstr :: Value -> Constr
$ctoConstr :: Value -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Value
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Value
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Value -> c Value
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Value -> c Value
$cp1Data :: Typeable Value
Data)
    deriving anyclass ([Value] -> Encoding
[Value] -> Value
Value -> Encoding
Value -> Value
(Value -> Value)
-> (Value -> Encoding)
-> ([Value] -> Value)
-> ([Value] -> Encoding)
-> ToJSON Value
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [Value] -> Encoding
$ctoEncodingList :: [Value] -> Encoding
toJSONList :: [Value] -> Value
$ctoJSONList :: [Value] -> Value
toEncoding :: Value -> Encoding
$ctoEncoding :: Value -> Encoding
toJSON :: Value -> Value
$ctoJSON :: Value -> Value
ToJSON, Value -> Parser [Value]
Value -> Parser Value
(Value -> Parser Value)
-> (Value -> Parser [Value]) -> FromJSON Value
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [Value]
$cparseJSONList :: Value -> Parser [Value]
parseJSON :: Value -> Parser Value
$cparseJSON :: Value -> Parser Value
FromJSON, Int -> Value -> Int
Value -> Int
(Int -> Value -> Int) -> (Value -> Int) -> Hashable Value
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: Value -> Int
$chash :: Value -> Int
hashWithSalt :: Int -> Value -> Int
$chashWithSalt :: Int -> Value -> Int
Hashable, Value -> ()
(Value -> ()) -> NFData Value
forall a. (a -> ()) -> NFData a
rnf :: Value -> ()
$crnf :: Value -> ()
NFData)
    deriving newtype (Decoder s Value
Decoder s [Value]
[Value] -> Encoding
Value -> Encoding
(Value -> Encoding)
-> (forall s. Decoder s Value)
-> ([Value] -> Encoding)
-> (forall s. Decoder s [Value])
-> Serialise Value
forall s. Decoder s [Value]
forall s. Decoder s Value
forall a.
(a -> Encoding)
-> (forall s. Decoder s a)
-> ([a] -> Encoding)
-> (forall s. Decoder s [a])
-> Serialise a
decodeList :: Decoder s [Value]
$cdecodeList :: forall s. Decoder s [Value]
encodeList :: [Value] -> Encoding
$cencodeList :: [Value] -> Encoding
decode :: Decoder s Value
$cdecode :: forall s. Decoder s Value
encode :: Value -> Encoding
$cencode :: Value -> Encoding
Serialise, Value -> BuiltinData
(Value -> BuiltinData) -> ToData Value
forall a. (a -> BuiltinData) -> ToData a
toBuiltinData :: Value -> BuiltinData
$ctoBuiltinData :: Value -> BuiltinData
PlutusTx.ToData, BuiltinData -> Maybe Value
(BuiltinData -> Maybe Value) -> FromData Value
forall a. (BuiltinData -> Maybe a) -> FromData a
fromBuiltinData :: BuiltinData -> Maybe Value
$cfromBuiltinData :: BuiltinData -> Maybe Value
PlutusTx.FromData, BuiltinData -> Value
(BuiltinData -> Value) -> UnsafeFromData Value
forall a. (BuiltinData -> a) -> UnsafeFromData a
unsafeFromBuiltinData :: BuiltinData -> Value
$cunsafeFromBuiltinData :: BuiltinData -> Value
PlutusTx.UnsafeFromData)
    deriving [Value] -> Doc ann
Value -> Doc ann
(forall ann. Value -> Doc ann)
-> (forall ann. [Value] -> Doc ann) -> Pretty Value
forall ann. [Value] -> Doc ann
forall ann. Value -> Doc ann
forall a.
(forall ann. a -> Doc ann)
-> (forall ann. [a] -> Doc ann) -> Pretty a
prettyList :: [Value] -> Doc ann
$cprettyList :: forall ann. [Value] -> Doc ann
pretty :: Value -> Doc ann
$cpretty :: forall ann. Value -> Doc ann
Pretty via (PrettyShow Value)

instance Haskell.Show Value where
    showsPrec :: Int -> Value -> ShowS
showsPrec Int
d Value
v =
        Bool -> ShowS -> ShowS
Haskell.showParen (Int
d Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
Haskell.== Int
11) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
            String -> ShowS
Haskell.showString String
"Value " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Bool -> ShowS -> ShowS
Haskell.showParen Bool
True (((CurrencySymbol, Map TokenName Integer) -> ShowS)
-> Map CurrencySymbol (Map TokenName Integer) -> ShowS
forall k v. ((k, v) -> ShowS) -> Map k v -> ShowS
showsMap ((Map TokenName Integer -> ShowS)
-> (CurrencySymbol, Map TokenName Integer) -> ShowS
forall a t. Show a => (t -> ShowS) -> (a, t) -> ShowS
showPair (((TokenName, Integer) -> ShowS) -> Map TokenName Integer -> ShowS
forall k v. ((k, v) -> ShowS) -> Map k v -> ShowS
showsMap (TokenName, Integer) -> ShowS
forall a. Show a => a -> ShowS
Haskell.shows)) Map CurrencySymbol (Map TokenName Integer)
rep))
        where Value Map CurrencySymbol (Map TokenName Integer)
rep = Value -> Value
normalizeValue Value
v
              showsMap :: ((k, v) -> ShowS) -> Map k v -> ShowS
showsMap (k, v) -> ShowS
sh Map k v
m = String -> ShowS
Haskell.showString String
"Map " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((k, v) -> ShowS) -> [(k, v)] -> ShowS
forall a. (a -> ShowS) -> [a] -> ShowS
showList__ (k, v) -> ShowS
sh (Map k v -> [(k, v)]
forall k v. Map k v -> [(k, v)]
Map.toList Map k v
m)
              showPair :: (t -> ShowS) -> (a, t) -> ShowS
showPair t -> ShowS
s (a
x,t
y) = Bool -> ShowS -> ShowS
Haskell.showParen Bool
True (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ a -> ShowS
forall a. Show a => a -> ShowS
Haskell.shows a
x ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
Haskell.showString String
"," ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t -> ShowS
s t
y

normalizeValue :: Value -> Value
normalizeValue :: Value -> Value
normalizeValue = Map CurrencySymbol (Map TokenName Integer) -> Value
Value (Map CurrencySymbol (Map TokenName Integer) -> Value)
-> (Value -> Map CurrencySymbol (Map TokenName Integer))
-> Value
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(CurrencySymbol, Map TokenName Integer)]
-> Map CurrencySymbol (Map TokenName Integer)
forall k v. [(k, v)] -> Map k v
Map.fromList ([(CurrencySymbol, Map TokenName Integer)]
 -> Map CurrencySymbol (Map TokenName Integer))
-> (Value -> [(CurrencySymbol, Map TokenName Integer)])
-> Value
-> Map CurrencySymbol (Map TokenName Integer)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(CurrencySymbol, Map TokenName Integer)]
-> [(CurrencySymbol, Map TokenName Integer)]
forall a. Ord a => [a] -> [a]
sort ([(CurrencySymbol, Map TokenName Integer)]
 -> [(CurrencySymbol, Map TokenName Integer)])
-> (Value -> [(CurrencySymbol, Map TokenName Integer)])
-> Value
-> [(CurrencySymbol, Map TokenName Integer)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Map TokenName Integer -> Bool)
-> [(CurrencySymbol, Map TokenName Integer)]
-> [(CurrencySymbol, Map TokenName Integer)]
forall b a. (b -> Bool) -> [(a, b)] -> [(a, b)]
filterRange (Map TokenName Integer -> Map TokenName Integer -> Bool
forall a. Eq a => a -> a -> Bool
/=Map TokenName Integer
forall k v. Map k v
Map.empty)
               ([(CurrencySymbol, Map TokenName Integer)]
 -> [(CurrencySymbol, Map TokenName Integer)])
-> (Value -> [(CurrencySymbol, Map TokenName Integer)])
-> Value
-> [(CurrencySymbol, Map TokenName Integer)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Map TokenName Integer -> Map TokenName Integer)
-> [(CurrencySymbol, Map TokenName Integer)]
-> [(CurrencySymbol, Map TokenName Integer)]
forall t b a. (t -> b) -> [(a, t)] -> [(a, b)]
mapRange Map TokenName Integer -> Map TokenName Integer
normalizeTokenMap ([(CurrencySymbol, Map TokenName Integer)]
 -> [(CurrencySymbol, Map TokenName Integer)])
-> (Value -> [(CurrencySymbol, Map TokenName Integer)])
-> Value
-> [(CurrencySymbol, Map TokenName Integer)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map CurrencySymbol (Map TokenName Integer)
-> [(CurrencySymbol, Map TokenName Integer)]
forall k v. Map k v -> [(k, v)]
Map.toList (Map CurrencySymbol (Map TokenName Integer)
 -> [(CurrencySymbol, Map TokenName Integer)])
-> (Value -> Map CurrencySymbol (Map TokenName Integer))
-> Value
-> [(CurrencySymbol, Map TokenName Integer)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value -> Map CurrencySymbol (Map TokenName Integer)
getValue
  where normalizeTokenMap :: Map TokenName Integer -> Map TokenName Integer
normalizeTokenMap = [(TokenName, Integer)] -> Map TokenName Integer
forall k v. [(k, v)] -> Map k v
Map.fromList ([(TokenName, Integer)] -> Map TokenName Integer)
-> (Map TokenName Integer -> [(TokenName, Integer)])
-> Map TokenName Integer
-> Map TokenName Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(TokenName, Integer)] -> [(TokenName, Integer)]
forall a. Ord a => [a] -> [a]
sort ([(TokenName, Integer)] -> [(TokenName, Integer)])
-> (Map TokenName Integer -> [(TokenName, Integer)])
-> Map TokenName Integer
-> [(TokenName, Integer)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Integer -> Bool)
-> [(TokenName, Integer)] -> [(TokenName, Integer)]
forall b a. (b -> Bool) -> [(a, b)] -> [(a, b)]
filterRange (Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
/=Integer
0) ([(TokenName, Integer)] -> [(TokenName, Integer)])
-> (Map TokenName Integer -> [(TokenName, Integer)])
-> Map TokenName Integer
-> [(TokenName, Integer)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map TokenName Integer -> [(TokenName, Integer)]
forall k v. Map k v -> [(k, v)]
Map.toList
        filterRange :: (b -> Bool) -> [(a, b)] -> [(a, b)]
filterRange b -> Bool
p [(a, b)]
kvs = [(a
k,b
v) | (a
k,b
v) <- [(a, b)]
kvs, b -> Bool
p b
v]
        mapRange :: (t -> b) -> [(a, t)] -> [(a, b)]
mapRange t -> b
f [(a, t)]
xys = [(a
x,t -> b
f t
y) | (a
x,t
y) <- [(a, t)]
xys]
        sort :: [a] -> [a]
sort [a]
xs = (a -> a -> Ordering) -> [a] -> [a]
forall a. (a -> a -> Ordering) -> [a] -> [a]
Data.List.sortBy a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare [a]
xs

-- Orphan instances for 'Map' to make this work
instance (ToJSON v, ToJSON k) => ToJSON (Map.Map k v) where
    toJSON :: Map k v -> Value
toJSON = [(k, v)] -> Value
forall a. ToJSON a => a -> Value
JSON.toJSON ([(k, v)] -> Value) -> (Map k v -> [(k, v)]) -> Map k v -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map k v -> [(k, v)]
forall k v. Map k v -> [(k, v)]
Map.toList

instance (FromJSON v, FromJSON k) => FromJSON (Map.Map k v) where
    parseJSON :: Value -> Parser (Map k v)
parseJSON Value
v = [(k, v)] -> Map k v
forall k v. [(k, v)] -> Map k v
Map.fromList ([(k, v)] -> Map k v) -> Parser [(k, v)] -> Parser (Map k v)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Haskell.<$> Value -> Parser [(k, v)]
forall a. FromJSON a => Value -> Parser a
JSON.parseJSON Value
v

deriving anyclass instance (Hashable k, Hashable v) => Hashable (Map.Map k v)
deriving anyclass instance (Serialise k, Serialise v) => Serialise (Map.Map k v)

instance Haskell.Eq Value where
    == :: Value -> Value -> Bool
(==) = Value -> Value -> Bool
eq

instance Eq Value where
    {-# INLINABLE (==) #-}
    == :: Value -> Value -> Bool
(==) = Value -> Value -> Bool
eq

-- No 'Ord Value' instance since 'Value' is only a partial order, so 'compare' can't
-- do the right thing in some cases.

instance Haskell.Semigroup Value where
    <> :: Value -> Value -> Value
(<>) = (Integer -> Integer -> Integer) -> Value -> Value -> Value
unionWith Integer -> Integer -> Integer
forall a. AdditiveSemigroup a => a -> a -> a
(+)

instance Semigroup Value where
    {-# INLINABLE (<>) #-}
    <> :: Value -> Value -> Value
(<>) = (Integer -> Integer -> Integer) -> Value -> Value -> Value
unionWith Integer -> Integer -> Integer
forall a. AdditiveSemigroup a => a -> a -> a
(+)

instance Haskell.Monoid Value where
    mempty :: Value
mempty = Map CurrencySymbol (Map TokenName Integer) -> Value
Value Map CurrencySymbol (Map TokenName Integer)
forall k v. Map k v
Map.empty

instance Monoid Value where
    {-# INLINABLE mempty #-}
    mempty :: Value
mempty = Map CurrencySymbol (Map TokenName Integer) -> Value
Value Map CurrencySymbol (Map TokenName Integer)
forall k v. Map k v
Map.empty

instance Group Value where
    {-# INLINABLE inv #-}
    inv :: Value -> Value
inv = Integer -> Value -> Value
forall s v. Module s v => s -> v -> v
scale @Integer @Value (-Integer
1)

deriving via (Additive Value) instance AdditiveSemigroup Value
deriving via (Additive Value) instance AdditiveMonoid Value
deriving via (Additive Value) instance AdditiveGroup Value

instance Module Integer Value where
    {-# INLINABLE scale #-}
    scale :: Integer -> Value -> Value
scale Integer
i (Value Map CurrencySymbol (Map TokenName Integer)
xs) = Map CurrencySymbol (Map TokenName Integer) -> Value
Value ((Map TokenName Integer -> Map TokenName Integer)
-> Map CurrencySymbol (Map TokenName Integer)
-> Map CurrencySymbol (Map TokenName Integer)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Integer -> Integer)
-> Map TokenName Integer -> Map TokenName Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Integer
i' -> Integer
i Integer -> Integer -> Integer
forall a. MultiplicativeSemigroup a => a -> a -> a
* Integer
i')) Map CurrencySymbol (Map TokenName Integer)
xs)

instance JoinSemiLattice Value where
    {-# INLINABLE (\/) #-}
    \/ :: Value -> Value -> Value
(\/) = (Integer -> Integer -> Integer) -> Value -> Value -> Value
unionWith Integer -> Integer -> Integer
forall a. Ord a => a -> a -> a
Ord.max

instance MeetSemiLattice Value where
    {-# INLINABLE (/\) #-}
    /\ :: Value -> Value -> Value
(/\) = (Integer -> Integer -> Integer) -> Value -> Value -> Value
unionWith Integer -> Integer -> Integer
forall a. Ord a => a -> a -> a
Ord.min

{- note [Currencies]

The 'Value' type represents a collection of amounts of different currencies.

We can think of 'Value' as a vector space whose dimensions are
currencies. At the moment there is only a single currency (Ada), so 'Value'
contains one-dimensional vectors. When currency-creating transactions are
implemented, this will change and the definition of 'Value' will change to a
'Map Currency Int', effectively a vector with infinitely many dimensions whose
non-zero values are recorded in the map.

To create a value of 'Value', we need to specifiy a currency. This can be done
using 'Ledger.Ada.adaValueOf'. To get the ada dimension of 'Value' we use
'Ledger.Ada.fromValue'. Plutus contract authors will be able to define modules
similar to 'Ledger.Ada' for their own currencies.

-}

{-# INLINABLE valueOf #-}
-- | Get the quantity of the given currency in the 'Value'.
valueOf :: Value -> CurrencySymbol -> TokenName -> Integer
valueOf :: Value -> CurrencySymbol -> TokenName -> Integer
valueOf (Value Map CurrencySymbol (Map TokenName Integer)
mp) CurrencySymbol
cur TokenName
tn =
    case CurrencySymbol
-> Map CurrencySymbol (Map TokenName Integer)
-> Maybe (Map TokenName Integer)
forall k v. Eq k => k -> Map k v -> Maybe v
Map.lookup CurrencySymbol
cur Map CurrencySymbol (Map TokenName Integer)
mp of
        Maybe (Map TokenName Integer)
Nothing -> Integer
0 :: Integer
        Just Map TokenName Integer
i  -> case TokenName -> Map TokenName Integer -> Maybe Integer
forall k v. Eq k => k -> Map k v -> Maybe v
Map.lookup TokenName
tn Map TokenName Integer
i of
            Maybe Integer
Nothing -> Integer
0
            Just Integer
v  -> Integer
v

{-# INLINABLE symbols #-}
-- | The list of 'CurrencySymbol's of a 'Value'.
symbols :: Value -> [CurrencySymbol]
symbols :: Value -> [CurrencySymbol]
symbols (Value Map CurrencySymbol (Map TokenName Integer)
mp) = Map CurrencySymbol (Map TokenName Integer) -> [CurrencySymbol]
forall k v. Map k v -> [k]
Map.keys Map CurrencySymbol (Map TokenName Integer)
mp

{-# INLINABLE singleton #-}
-- | Make a 'Value' containing only the given quantity of the given currency.
singleton :: CurrencySymbol -> TokenName -> Integer -> Value
singleton :: CurrencySymbol -> TokenName -> Integer -> Value
singleton CurrencySymbol
c TokenName
tn Integer
i = Map CurrencySymbol (Map TokenName Integer) -> Value
Value (CurrencySymbol
-> Map TokenName Integer
-> Map CurrencySymbol (Map TokenName Integer)
forall k v. k -> v -> Map k v
Map.singleton CurrencySymbol
c (TokenName -> Integer -> Map TokenName Integer
forall k v. k -> v -> Map k v
Map.singleton TokenName
tn Integer
i))

{-# INLINABLE assetClassValue #-}
-- | A 'Value' containing the given amount of the asset class.
assetClassValue :: AssetClass -> Integer -> Value
assetClassValue :: AssetClass -> Integer -> Value
assetClassValue (AssetClass (CurrencySymbol
c, TokenName
t)) Integer
i = CurrencySymbol -> TokenName -> Integer -> Value
singleton CurrencySymbol
c TokenName
t Integer
i

{-# INLINABLE assetClassValueOf #-}
-- | Get the quantity of the given 'AssetClass' class in the 'Value'.
assetClassValueOf :: Value -> AssetClass -> Integer
assetClassValueOf :: Value -> AssetClass -> Integer
assetClassValueOf Value
v (AssetClass (CurrencySymbol
c, TokenName
t)) = Value -> CurrencySymbol -> TokenName -> Integer
valueOf Value
v CurrencySymbol
c TokenName
t

{-# INLINABLE unionVal #-}
-- | Combine two 'Value' maps
unionVal :: Value -> Value -> Map.Map CurrencySymbol (Map.Map TokenName (These Integer Integer))
unionVal :: Value
-> Value
-> Map CurrencySymbol (Map TokenName (These Integer Integer))
unionVal (Value Map CurrencySymbol (Map TokenName Integer)
l) (Value Map CurrencySymbol (Map TokenName Integer)
r) =
    let
        combined :: Map
  CurrencySymbol
  (These (Map TokenName Integer) (Map TokenName Integer))
combined = Map CurrencySymbol (Map TokenName Integer)
-> Map CurrencySymbol (Map TokenName Integer)
-> Map
     CurrencySymbol
     (These (Map TokenName Integer) (Map TokenName Integer))
forall k v r. Eq k => Map k v -> Map k r -> Map k (These v r)
Map.union Map CurrencySymbol (Map TokenName Integer)
l Map CurrencySymbol (Map TokenName Integer)
r
        unThese :: These (Map k v) (Map k r) -> Map k (These v r)
unThese These (Map k v) (Map k r)
k = case These (Map k v) (Map k r)
k of
            This Map k v
a    -> v -> These v r
forall a b. a -> These a b
This (v -> These v r) -> Map k v -> Map k (These v r)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map k v
a
            That Map k r
b    -> r -> These v r
forall a b. b -> These a b
That (r -> These v r) -> Map k r -> Map k (These v r)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map k r
b
            These Map k v
a Map k r
b -> Map k v -> Map k r -> Map k (These v r)
forall k v r. Eq k => Map k v -> Map k r -> Map k (These v r)
Map.union Map k v
a Map k r
b
    in These (Map TokenName Integer) (Map TokenName Integer)
-> Map TokenName (These Integer Integer)
forall k v r.
Eq k =>
These (Map k v) (Map k r) -> Map k (These v r)
unThese (These (Map TokenName Integer) (Map TokenName Integer)
 -> Map TokenName (These Integer Integer))
-> Map
     CurrencySymbol
     (These (Map TokenName Integer) (Map TokenName Integer))
-> Map CurrencySymbol (Map TokenName (These Integer Integer))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map
  CurrencySymbol
  (These (Map TokenName Integer) (Map TokenName Integer))
combined

{-# INLINABLE unionWith #-}
unionWith :: (Integer -> Integer -> Integer) -> Value -> Value -> Value
unionWith :: (Integer -> Integer -> Integer) -> Value -> Value -> Value
unionWith Integer -> Integer -> Integer
f Value
ls Value
rs =
    let
        combined :: Map CurrencySymbol (Map TokenName (These Integer Integer))
combined = Value
-> Value
-> Map CurrencySymbol (Map TokenName (These Integer Integer))
unionVal Value
ls Value
rs
        unThese :: These Integer Integer -> Integer
unThese These Integer Integer
k' = case These Integer Integer
k' of
            This Integer
a    -> Integer -> Integer -> Integer
f Integer
a Integer
0
            That Integer
b    -> Integer -> Integer -> Integer
f Integer
0 Integer
b
            These Integer
a Integer
b -> Integer -> Integer -> Integer
f Integer
a Integer
b
    in Map CurrencySymbol (Map TokenName Integer) -> Value
Value ((Map TokenName (These Integer Integer) -> Map TokenName Integer)
-> Map CurrencySymbol (Map TokenName (These Integer Integer))
-> Map CurrencySymbol (Map TokenName Integer)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((These Integer Integer -> Integer)
-> Map TokenName (These Integer Integer) -> Map TokenName Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap These Integer Integer -> Integer
unThese) Map CurrencySymbol (Map TokenName (These Integer Integer))
combined)

{-# INLINABLE flattenValue #-}
-- | Convert a value to a simple list, keeping only the non-zero amounts.
flattenValue :: Value -> [(CurrencySymbol, TokenName, Integer)]
flattenValue :: Value -> [(CurrencySymbol, TokenName, Integer)]
flattenValue Value
v = [(CurrencySymbol, TokenName, Integer)]
-> [(CurrencySymbol, Map TokenName Integer)]
-> [(CurrencySymbol, TokenName, Integer)]
forall c a b.
(Eq c, Num c) =>
[(a, b, c)] -> [(a, Map b c)] -> [(a, b, c)]
goOuter [] (Map CurrencySymbol (Map TokenName Integer)
-> [(CurrencySymbol, Map TokenName Integer)]
forall k v. Map k v -> [(k, v)]
Map.toList (Map CurrencySymbol (Map TokenName Integer)
 -> [(CurrencySymbol, Map TokenName Integer)])
-> Map CurrencySymbol (Map TokenName Integer)
-> [(CurrencySymbol, Map TokenName Integer)]
forall a b. (a -> b) -> a -> b
$ Value -> Map CurrencySymbol (Map TokenName Integer)
getValue Value
v)
  where
    goOuter :: [(a, b, c)] -> [(a, Map b c)] -> [(a, b, c)]
goOuter [(a, b, c)]
acc []             = [(a, b, c)]
acc
    goOuter [(a, b, c)]
acc ((a
cs, Map b c
m) : [(a, Map b c)]
tl) = [(a, b, c)] -> [(a, Map b c)] -> [(a, b, c)]
goOuter (a -> [(a, b, c)] -> [(b, c)] -> [(a, b, c)]
forall c a b.
(Eq c, Num c) =>
a -> [(a, b, c)] -> [(b, c)] -> [(a, b, c)]
goInner a
cs [(a, b, c)]
acc (Map b c -> [(b, c)]
forall k v. Map k v -> [(k, v)]
Map.toList Map b c
m)) [(a, Map b c)]
tl

    goInner :: a -> [(a, b, c)] -> [(b, c)] -> [(a, b, c)]
goInner a
_ [(a, b, c)]
acc [] = [(a, b, c)]
acc
    goInner a
cs [(a, b, c)]
acc ((b
tn, c
a) : [(b, c)]
tl)
        | c
a c -> c -> Bool
forall a. Eq a => a -> a -> Bool
/= c
0    = a -> [(a, b, c)] -> [(b, c)] -> [(a, b, c)]
goInner a
cs ((a
cs, b
tn, c
a) (a, b, c) -> [(a, b, c)] -> [(a, b, c)]
forall a. a -> [a] -> [a]
: [(a, b, c)]
acc) [(b, c)]
tl
        | Bool
otherwise = a -> [(a, b, c)] -> [(b, c)] -> [(a, b, c)]
goInner a
cs [(a, b, c)]
acc [(b, c)]
tl

-- Num operations

{-# INLINABLE isZero #-}
-- | Check whether a 'Value' is zero.
isZero :: Value -> Bool
isZero :: Value -> Bool
isZero (Value Map CurrencySymbol (Map TokenName Integer)
xs) = (Map TokenName Integer -> Bool)
-> Map CurrencySymbol (Map TokenName Integer) -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
Map.all ((Integer -> Bool) -> Map TokenName Integer -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
Map.all (\Integer
i -> Integer
0 Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
i)) Map CurrencySymbol (Map TokenName Integer)
xs

{-# INLINABLE checkPred #-}
checkPred :: (These Integer Integer -> Bool) -> Value -> Value -> Bool
checkPred :: (These Integer Integer -> Bool) -> Value -> Value -> Bool
checkPred These Integer Integer -> Bool
f Value
l Value
r =
    let
      inner :: Map.Map TokenName (These Integer Integer) -> Bool
      inner :: Map TokenName (These Integer Integer) -> Bool
inner = (These Integer Integer -> Bool)
-> Map TokenName (These Integer Integer) -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
Map.all These Integer Integer -> Bool
f
    in
      (Map TokenName (These Integer Integer) -> Bool)
-> Map CurrencySymbol (Map TokenName (These Integer Integer))
-> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
Map.all Map TokenName (These Integer Integer) -> Bool
inner (Value
-> Value
-> Map CurrencySymbol (Map TokenName (These Integer Integer))
unionVal Value
l Value
r)

{-# INLINABLE checkBinRel #-}
-- | Check whether a binary relation holds for value pairs of two 'Value' maps,
--   supplying 0 where a key is only present in one of them.
checkBinRel :: (Integer -> Integer -> Bool) -> Value -> Value -> Bool
checkBinRel :: (Integer -> Integer -> Bool) -> Value -> Value -> Bool
checkBinRel Integer -> Integer -> Bool
f Value
l Value
r =
    let
        unThese :: These Integer Integer -> Bool
unThese These Integer Integer
k' = case These Integer Integer
k' of
            This Integer
a    -> Integer -> Integer -> Bool
f Integer
a Integer
0
            That Integer
b    -> Integer -> Integer -> Bool
f Integer
0 Integer
b
            These Integer
a Integer
b -> Integer -> Integer -> Bool
f Integer
a Integer
b
    in (These Integer Integer -> Bool) -> Value -> Value -> Bool
checkPred These Integer Integer -> Bool
unThese Value
l Value
r

{-# INLINABLE geq #-}
-- | Check whether one 'Value' is greater than or equal to another. See 'Value' for an explanation of how operations on 'Value's work.
geq :: Value -> Value -> Bool
-- If both are zero then checkBinRel will be vacuously true, but this is fine.
geq :: Value -> Value -> Bool
geq = (Integer -> Integer -> Bool) -> Value -> Value -> Bool
checkBinRel Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
(>=)

{-# INLINABLE gt #-}
-- | Check whether one 'Value' is strictly greater than another. See 'Value' for an explanation of how operations on 'Value's work.
gt :: Value -> Value -> Bool
-- If both are zero then checkBinRel will be vacuously true. So we have a special case.
gt :: Value -> Value -> Bool
gt Value
l Value
r = Bool -> Bool
not (Value -> Bool
isZero Value
l Bool -> Bool -> Bool
&& Value -> Bool
isZero Value
r) Bool -> Bool -> Bool
&& (Integer -> Integer -> Bool) -> Value -> Value -> Bool
checkBinRel Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
(>) Value
l Value
r

{-# INLINABLE leq #-}
-- | Check whether one 'Value' is less than or equal to another. See 'Value' for an explanation of how operations on 'Value's work.
leq :: Value -> Value -> Bool
-- If both are zero then checkBinRel will be vacuously true, but this is fine.
leq :: Value -> Value -> Bool
leq = (Integer -> Integer -> Bool) -> Value -> Value -> Bool
checkBinRel Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
(<=)

{-# INLINABLE lt #-}
-- | Check whether one 'Value' is strictly less than another. See 'Value' for an explanation of how operations on 'Value's work.
lt :: Value -> Value -> Bool
-- If both are zero then checkBinRel will be vacuously true. So we have a special case.
lt :: Value -> Value -> Bool
lt Value
l Value
r = Bool -> Bool
not (Value -> Bool
isZero Value
l Bool -> Bool -> Bool
&& Value -> Bool
isZero Value
r) Bool -> Bool -> Bool
&& (Integer -> Integer -> Bool) -> Value -> Value -> Bool
checkBinRel Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
(<) Value
l Value
r

{-# INLINABLE eq #-}
-- | Check whether one 'Value' is equal to another. See 'Value' for an explanation of how operations on 'Value's work.
eq :: Value -> Value -> Bool
-- If both are zero then checkBinRel will be vacuously true, but this is fine.
eq :: Value -> Value -> Bool
eq = (Integer -> Integer -> Bool) -> Value -> Value -> Bool
checkBinRel Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
(==)

-- | Split a value into its positive and negative parts. The first element of
--   the tuple contains the negative parts of the value, the second element
--   contains the positive parts.
--
--   @negate (fst (split a)) `plus` (snd (split a)) == a@
--
{-# INLINABLE split #-}
split :: Value -> (Value, Value)
split :: Value -> (Value, Value)
split (Value Map CurrencySymbol (Map TokenName Integer)
mp) = (Value -> Value
forall a. AdditiveGroup a => a -> a
negate (Map CurrencySymbol (Map TokenName Integer) -> Value
Value Map CurrencySymbol (Map TokenName Integer)
neg), Map CurrencySymbol (Map TokenName Integer) -> Value
Value Map CurrencySymbol (Map TokenName Integer)
pos) where
  (Map CurrencySymbol (Map TokenName Integer)
neg, Map CurrencySymbol (Map TokenName Integer)
pos) = (Map TokenName Integer
 -> These (Map TokenName Integer) (Map TokenName Integer))
-> Map CurrencySymbol (Map TokenName Integer)
-> (Map CurrencySymbol (Map TokenName Integer),
    Map CurrencySymbol (Map TokenName Integer))
forall v a b k. (v -> These a b) -> Map k v -> (Map k a, Map k b)
Map.mapThese Map TokenName Integer
-> These (Map TokenName Integer) (Map TokenName Integer)
splitIntl Map CurrencySymbol (Map TokenName Integer)
mp

  splitIntl :: Map.Map TokenName Integer -> These (Map.Map TokenName Integer) (Map.Map TokenName Integer)
  splitIntl :: Map TokenName Integer
-> These (Map TokenName Integer) (Map TokenName Integer)
splitIntl Map TokenName Integer
mp' = Map TokenName Integer
-> Map TokenName Integer
-> These (Map TokenName Integer) (Map TokenName Integer)
forall a b. a -> b -> These a b
These Map TokenName Integer
l Map TokenName Integer
r where
    (Map TokenName Integer
l, Map TokenName Integer
r) = (Integer -> These Integer Integer)
-> Map TokenName Integer
-> (Map TokenName Integer, Map TokenName Integer)
forall v a b k. (v -> These a b) -> Map k v -> (Map k a, Map k b)
Map.mapThese (\Integer
i -> if Integer
i Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Integer
0 then Integer -> These Integer Integer
forall a b. a -> These a b
This Integer
i else Integer -> These Integer Integer
forall a b. b -> These a b
That Integer
i) Map TokenName Integer
mp'

makeLift ''CurrencySymbol
makeLift ''TokenName
makeLift ''AssetClass
makeLift ''Value