{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
module Cardano.Ledger.Mary.Value
( PolicyID (..),
AssetName (..),
Value (..),
insert,
lookup,
policies,
prune,
showValue,
valueFromList,
)
where
import Cardano.Binary
( Decoder,
Encoding,
FromCBOR,
ToCBOR,
TokenType (..),
decodeInteger,
decodeWord64,
fromCBOR,
peekTokenType,
toCBOR,
)
import qualified Cardano.Crypto.Hash.Class as Hash
import Cardano.Ledger.Compactible (Compactible (..))
import qualified Cardano.Ledger.Crypto as CC (Crypto)
import Cardano.Ledger.Torsor (Torsor (..))
import Cardano.Ledger.Val
( DecodeMint (..),
DecodeNonNegative (..),
EncodeMint (..),
Val (..),
)
import Control.DeepSeq (NFData (..))
import Control.Monad (guard)
import Data.Array (Array)
import Data.Array.IArray (array)
import Data.ByteString (ByteString)
import Data.CannonicalMaps
( cannonicalMap,
cannonicalMapUnion,
pointWise,
)
import Data.Coders
( Decode (..),
Encode (..),
decode,
encode,
(!>),
(<!),
)
import Data.Group (Abelian, Group (..))
import Data.Map.Internal
( Map (..),
link,
link2,
splitLookup,
)
import Data.Map.Strict (assocs)
import qualified Data.Map.Strict as Map
import Data.Maybe (fromMaybe)
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Typeable (Typeable)
import Data.Word (Word64)
import GHC.Generics (Generic)
import NoThunks.Class (NoThunks (..))
import Shelley.Spec.Ledger.Coin (Coin (..), integerToWord64)
import Shelley.Spec.Ledger.Scripts (ScriptHash (..))
import Shelley.Spec.Ledger.Serialization (decodeMap, encodeMap)
import Prelude hiding (lookup)
newtype AssetName = AssetName {AssetName -> ByteString
assetName :: ByteString}
deriving newtype
( Int -> AssetName -> ShowS
[AssetName] -> ShowS
AssetName -> String
(Int -> AssetName -> ShowS)
-> (AssetName -> String)
-> ([AssetName] -> ShowS)
-> Show AssetName
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AssetName] -> ShowS
$cshowList :: [AssetName] -> ShowS
show :: AssetName -> String
$cshow :: AssetName -> String
showsPrec :: Int -> AssetName -> ShowS
$cshowsPrec :: Int -> AssetName -> ShowS
Show,
AssetName -> AssetName -> Bool
(AssetName -> AssetName -> Bool)
-> (AssetName -> AssetName -> Bool) -> Eq AssetName
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AssetName -> AssetName -> Bool
$c/= :: AssetName -> AssetName -> Bool
== :: AssetName -> AssetName -> Bool
$c== :: AssetName -> AssetName -> Bool
Eq,
Typeable AssetName
Typeable AssetName
-> (AssetName -> Encoding)
-> ((forall t. ToCBOR t => Proxy t -> Size)
-> Proxy AssetName -> Size)
-> ((forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [AssetName] -> Size)
-> ToCBOR AssetName
AssetName -> Encoding
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [AssetName] -> Size
(forall t. ToCBOR t => Proxy t -> Size) -> Proxy AssetName -> Size
forall a.
Typeable a
-> (a -> Encoding)
-> ((forall t. ToCBOR t => Proxy t -> Size) -> Proxy a -> Size)
-> ((forall t. ToCBOR t => Proxy t -> Size) -> Proxy [a] -> Size)
-> ToCBOR a
encodedListSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [AssetName] -> Size
$cencodedListSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [AssetName] -> Size
encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size) -> Proxy AssetName -> Size
$cencodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size) -> Proxy AssetName -> Size
toCBOR :: AssetName -> Encoding
$ctoCBOR :: AssetName -> Encoding
$cp1ToCBOR :: Typeable AssetName
ToCBOR,
Typeable AssetName
Decoder s AssetName
Typeable AssetName
-> (forall s. Decoder s AssetName)
-> (Proxy AssetName -> Text)
-> FromCBOR AssetName
Proxy AssetName -> Text
forall s. Decoder s AssetName
forall a.
Typeable a
-> (forall s. Decoder s a) -> (Proxy a -> Text) -> FromCBOR a
label :: Proxy AssetName -> Text
$clabel :: Proxy AssetName -> Text
fromCBOR :: Decoder s AssetName
$cfromCBOR :: forall s. Decoder s AssetName
$cp1FromCBOR :: Typeable AssetName
FromCBOR,
Eq AssetName
Eq AssetName
-> (AssetName -> AssetName -> Ordering)
-> (AssetName -> AssetName -> Bool)
-> (AssetName -> AssetName -> Bool)
-> (AssetName -> AssetName -> Bool)
-> (AssetName -> AssetName -> Bool)
-> (AssetName -> AssetName -> AssetName)
-> (AssetName -> AssetName -> AssetName)
-> Ord AssetName
AssetName -> AssetName -> Bool
AssetName -> AssetName -> Ordering
AssetName -> AssetName -> AssetName
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 :: AssetName -> AssetName -> AssetName
$cmin :: AssetName -> AssetName -> AssetName
max :: AssetName -> AssetName -> AssetName
$cmax :: AssetName -> AssetName -> AssetName
>= :: AssetName -> AssetName -> Bool
$c>= :: AssetName -> AssetName -> Bool
> :: AssetName -> AssetName -> Bool
$c> :: AssetName -> AssetName -> Bool
<= :: AssetName -> AssetName -> Bool
$c<= :: AssetName -> AssetName -> Bool
< :: AssetName -> AssetName -> Bool
$c< :: AssetName -> AssetName -> Bool
compare :: AssetName -> AssetName -> Ordering
$ccompare :: AssetName -> AssetName -> Ordering
$cp1Ord :: Eq AssetName
Ord,
Context -> AssetName -> IO (Maybe ThunkInfo)
Proxy AssetName -> String
(Context -> AssetName -> IO (Maybe ThunkInfo))
-> (Context -> AssetName -> IO (Maybe ThunkInfo))
-> (Proxy AssetName -> String)
-> NoThunks AssetName
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy AssetName -> String
$cshowTypeOf :: Proxy AssetName -> String
wNoThunks :: Context -> AssetName -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> AssetName -> IO (Maybe ThunkInfo)
noThunks :: Context -> AssetName -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> AssetName -> IO (Maybe ThunkInfo)
NoThunks,
AssetName -> ()
(AssetName -> ()) -> NFData AssetName
forall a. (a -> ()) -> NFData a
rnf :: AssetName -> ()
$crnf :: AssetName -> ()
NFData
)
newtype PolicyID crypto = PolicyID {PolicyID crypto -> ScriptHash crypto
policyID :: ScriptHash crypto}
deriving (Int -> PolicyID crypto -> ShowS
[PolicyID crypto] -> ShowS
PolicyID crypto -> String
(Int -> PolicyID crypto -> ShowS)
-> (PolicyID crypto -> String)
-> ([PolicyID crypto] -> ShowS)
-> Show (PolicyID crypto)
forall crypto. Int -> PolicyID crypto -> ShowS
forall crypto. [PolicyID crypto] -> ShowS
forall crypto. PolicyID crypto -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PolicyID crypto] -> ShowS
$cshowList :: forall crypto. [PolicyID crypto] -> ShowS
show :: PolicyID crypto -> String
$cshow :: forall crypto. PolicyID crypto -> String
showsPrec :: Int -> PolicyID crypto -> ShowS
$cshowsPrec :: forall crypto. Int -> PolicyID crypto -> ShowS
Show, PolicyID crypto -> PolicyID crypto -> Bool
(PolicyID crypto -> PolicyID crypto -> Bool)
-> (PolicyID crypto -> PolicyID crypto -> Bool)
-> Eq (PolicyID crypto)
forall crypto. PolicyID crypto -> PolicyID crypto -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PolicyID crypto -> PolicyID crypto -> Bool
$c/= :: forall crypto. PolicyID crypto -> PolicyID crypto -> Bool
== :: PolicyID crypto -> PolicyID crypto -> Bool
$c== :: forall crypto. PolicyID crypto -> PolicyID crypto -> Bool
Eq, Typeable (PolicyID crypto)
Typeable (PolicyID crypto)
-> (PolicyID crypto -> Encoding)
-> ((forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (PolicyID crypto) -> Size)
-> ((forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [PolicyID crypto] -> Size)
-> ToCBOR (PolicyID crypto)
PolicyID crypto -> Encoding
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [PolicyID crypto] -> Size
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (PolicyID crypto) -> Size
forall a.
Typeable a
-> (a -> Encoding)
-> ((forall t. ToCBOR t => Proxy t -> Size) -> Proxy a -> Size)
-> ((forall t. ToCBOR t => Proxy t -> Size) -> Proxy [a] -> Size)
-> ToCBOR a
forall crypto. Crypto crypto => Typeable (PolicyID crypto)
forall crypto. Crypto crypto => PolicyID crypto -> Encoding
forall crypto.
Crypto crypto =>
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [PolicyID crypto] -> Size
forall crypto.
Crypto crypto =>
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (PolicyID crypto) -> Size
encodedListSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [PolicyID crypto] -> Size
$cencodedListSizeExpr :: forall crypto.
Crypto crypto =>
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [PolicyID crypto] -> Size
encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (PolicyID crypto) -> Size
$cencodedSizeExpr :: forall crypto.
Crypto crypto =>
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (PolicyID crypto) -> Size
toCBOR :: PolicyID crypto -> Encoding
$ctoCBOR :: forall crypto. Crypto crypto => PolicyID crypto -> Encoding
$cp1ToCBOR :: forall crypto. Crypto crypto => Typeable (PolicyID crypto)
ToCBOR, Typeable (PolicyID crypto)
Decoder s (PolicyID crypto)
Typeable (PolicyID crypto)
-> (forall s. Decoder s (PolicyID crypto))
-> (Proxy (PolicyID crypto) -> Text)
-> FromCBOR (PolicyID crypto)
Proxy (PolicyID crypto) -> Text
forall s. Decoder s (PolicyID crypto)
forall a.
Typeable a
-> (forall s. Decoder s a) -> (Proxy a -> Text) -> FromCBOR a
forall crypto. Crypto crypto => Typeable (PolicyID crypto)
forall crypto. Crypto crypto => Proxy (PolicyID crypto) -> Text
forall crypto s. Crypto crypto => Decoder s (PolicyID crypto)
label :: Proxy (PolicyID crypto) -> Text
$clabel :: forall crypto. Crypto crypto => Proxy (PolicyID crypto) -> Text
fromCBOR :: Decoder s (PolicyID crypto)
$cfromCBOR :: forall crypto s. Crypto crypto => Decoder s (PolicyID crypto)
$cp1FromCBOR :: forall crypto. Crypto crypto => Typeable (PolicyID crypto)
FromCBOR, Eq (PolicyID crypto)
Eq (PolicyID crypto)
-> (PolicyID crypto -> PolicyID crypto -> Ordering)
-> (PolicyID crypto -> PolicyID crypto -> Bool)
-> (PolicyID crypto -> PolicyID crypto -> Bool)
-> (PolicyID crypto -> PolicyID crypto -> Bool)
-> (PolicyID crypto -> PolicyID crypto -> Bool)
-> (PolicyID crypto -> PolicyID crypto -> PolicyID crypto)
-> (PolicyID crypto -> PolicyID crypto -> PolicyID crypto)
-> Ord (PolicyID crypto)
PolicyID crypto -> PolicyID crypto -> Bool
PolicyID crypto -> PolicyID crypto -> Ordering
PolicyID crypto -> PolicyID crypto -> PolicyID crypto
forall crypto. Eq (PolicyID crypto)
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
forall crypto. PolicyID crypto -> PolicyID crypto -> Bool
forall crypto. PolicyID crypto -> PolicyID crypto -> Ordering
forall crypto.
PolicyID crypto -> PolicyID crypto -> PolicyID crypto
min :: PolicyID crypto -> PolicyID crypto -> PolicyID crypto
$cmin :: forall crypto.
PolicyID crypto -> PolicyID crypto -> PolicyID crypto
max :: PolicyID crypto -> PolicyID crypto -> PolicyID crypto
$cmax :: forall crypto.
PolicyID crypto -> PolicyID crypto -> PolicyID crypto
>= :: PolicyID crypto -> PolicyID crypto -> Bool
$c>= :: forall crypto. PolicyID crypto -> PolicyID crypto -> Bool
> :: PolicyID crypto -> PolicyID crypto -> Bool
$c> :: forall crypto. PolicyID crypto -> PolicyID crypto -> Bool
<= :: PolicyID crypto -> PolicyID crypto -> Bool
$c<= :: forall crypto. PolicyID crypto -> PolicyID crypto -> Bool
< :: PolicyID crypto -> PolicyID crypto -> Bool
$c< :: forall crypto. PolicyID crypto -> PolicyID crypto -> Bool
compare :: PolicyID crypto -> PolicyID crypto -> Ordering
$ccompare :: forall crypto. PolicyID crypto -> PolicyID crypto -> Ordering
$cp1Ord :: forall crypto. Eq (PolicyID crypto)
Ord, Context -> PolicyID crypto -> IO (Maybe ThunkInfo)
Proxy (PolicyID crypto) -> String
(Context -> PolicyID crypto -> IO (Maybe ThunkInfo))
-> (Context -> PolicyID crypto -> IO (Maybe ThunkInfo))
-> (Proxy (PolicyID crypto) -> String)
-> NoThunks (PolicyID crypto)
forall crypto. Context -> PolicyID crypto -> IO (Maybe ThunkInfo)
forall crypto. Proxy (PolicyID crypto) -> String
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy (PolicyID crypto) -> String
$cshowTypeOf :: forall crypto. Proxy (PolicyID crypto) -> String
wNoThunks :: Context -> PolicyID crypto -> IO (Maybe ThunkInfo)
$cwNoThunks :: forall crypto. Context -> PolicyID crypto -> IO (Maybe ThunkInfo)
noThunks :: Context -> PolicyID crypto -> IO (Maybe ThunkInfo)
$cnoThunks :: forall crypto. Context -> PolicyID crypto -> IO (Maybe ThunkInfo)
NoThunks, PolicyID crypto -> ()
(PolicyID crypto -> ()) -> NFData (PolicyID crypto)
forall crypto. PolicyID crypto -> ()
forall a. (a -> ()) -> NFData a
rnf :: PolicyID crypto -> ()
$crnf :: forall crypto. PolicyID crypto -> ()
NFData)
data Value crypto = Value !Integer !(Map (PolicyID crypto) (Map AssetName Integer))
deriving (Int -> Value crypto -> ShowS
[Value crypto] -> ShowS
Value crypto -> String
(Int -> Value crypto -> ShowS)
-> (Value crypto -> String)
-> ([Value crypto] -> ShowS)
-> Show (Value crypto)
forall crypto. Int -> Value crypto -> ShowS
forall crypto. [Value crypto] -> ShowS
forall crypto. Value crypto -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Value crypto] -> ShowS
$cshowList :: forall crypto. [Value crypto] -> ShowS
show :: Value crypto -> String
$cshow :: forall crypto. Value crypto -> String
showsPrec :: Int -> Value crypto -> ShowS
$cshowsPrec :: forall crypto. Int -> Value crypto -> ShowS
Show, (forall x. Value crypto -> Rep (Value crypto) x)
-> (forall x. Rep (Value crypto) x -> Value crypto)
-> Generic (Value crypto)
forall x. Rep (Value crypto) x -> Value crypto
forall x. Value crypto -> Rep (Value crypto) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall crypto x. Rep (Value crypto) x -> Value crypto
forall crypto x. Value crypto -> Rep (Value crypto) x
$cto :: forall crypto x. Rep (Value crypto) x -> Value crypto
$cfrom :: forall crypto x. Value crypto -> Rep (Value crypto) x
Generic)
instance CC.Crypto crypto => Eq (Value crypto) where
Value crypto
x == :: Value crypto -> Value crypto -> Bool
== Value crypto
y = (Integer -> Integer -> Bool)
-> Value crypto -> Value crypto -> Bool
forall t. Val t => (Integer -> Integer -> Bool) -> t -> t -> Bool
pointwise Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
(==) Value crypto
x Value crypto
y
instance NFData (Value crypto)
instance NoThunks (Value crypto)
instance Semigroup (Value crypto) where
Value Integer
c Map (PolicyID crypto) (Map AssetName Integer)
m <> :: Value crypto -> Value crypto -> Value crypto
<> Value Integer
c1 Map (PolicyID crypto) (Map AssetName Integer)
m1 =
Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
forall crypto.
Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
Value (Integer
c Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
c1) ((Map AssetName Integer
-> Map AssetName Integer -> Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
forall k a.
(Ord k, CannonicalZero a) =>
(a -> a -> a) -> Map k a -> Map k a -> Map k a
cannonicalMapUnion ((Integer -> Integer -> Integer)
-> Map AssetName Integer
-> Map AssetName Integer
-> Map AssetName Integer
forall k a.
(Ord k, CannonicalZero a) =>
(a -> a -> a) -> Map k a -> Map k a -> Map k a
cannonicalMapUnion Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
(+)) Map (PolicyID crypto) (Map AssetName Integer)
m Map (PolicyID crypto) (Map AssetName Integer)
m1)
instance Monoid (Value crypto) where
mempty :: Value crypto
mempty = Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
forall crypto.
Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
Value Integer
0 Map (PolicyID crypto) (Map AssetName Integer)
forall a. Monoid a => a
mempty
instance Group (Value crypto) where
invert :: Value crypto -> Value crypto
invert (Value Integer
c Map (PolicyID crypto) (Map AssetName Integer)
m) =
Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
forall crypto.
Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
Value
(- Integer
c)
((Map AssetName Integer -> Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
forall k a.
(Ord k, CannonicalZero a) =>
(a -> a) -> Map k a -> Map k a
cannonicalMap ((Integer -> Integer)
-> Map AssetName Integer -> Map AssetName Integer
forall k a.
(Ord k, CannonicalZero a) =>
(a -> a) -> Map k a -> Map k a
cannonicalMap ((-Integer
1 :: Integer) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
*)) Map (PolicyID crypto) (Map AssetName Integer)
m)
instance Abelian (Value crypto)
instance CC.Crypto crypto => Val (Value crypto) where
i
s <×> :: i -> Value crypto -> Value crypto
<×> (Value Integer
c Map (PolicyID crypto) (Map AssetName Integer)
v) =
Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
forall crypto.
Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
Value
(i -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral i
s Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
c)
((Map AssetName Integer -> Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
forall k a.
(Ord k, CannonicalZero a) =>
(a -> a) -> Map k a -> Map k a
cannonicalMap ((Integer -> Integer)
-> Map AssetName Integer -> Map AssetName Integer
forall k a.
(Ord k, CannonicalZero a) =>
(a -> a) -> Map k a -> Map k a
cannonicalMap ((i -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral i
s) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
*)) Map (PolicyID crypto) (Map AssetName Integer)
v)
isZero :: Value crypto -> Bool
isZero (Value Integer
c Map (PolicyID crypto) (Map AssetName Integer)
v) = Integer
c Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0 Bool -> Bool -> Bool
&& Map (PolicyID crypto) (Map AssetName Integer) -> Bool
forall k a. Map k a -> Bool
Map.null Map (PolicyID crypto) (Map AssetName Integer)
v
coin :: Value crypto -> Coin
coin (Value Integer
c Map (PolicyID crypto) (Map AssetName Integer)
_) = Integer -> Coin
Coin Integer
c
inject :: Coin -> Value crypto
inject (Coin Integer
c) = Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
forall crypto.
Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
Value Integer
c Map (PolicyID crypto) (Map AssetName Integer)
forall a. Monoid a => a
mempty
modifyCoin :: (Coin -> Coin) -> Value crypto -> Value crypto
modifyCoin Coin -> Coin
f (Value Integer
c Map (PolicyID crypto) (Map AssetName Integer)
m) = Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
forall crypto.
Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
Value Integer
n Map (PolicyID crypto) (Map AssetName Integer)
m where (Coin Integer
n) = Coin -> Coin
f (Integer -> Coin
Coin Integer
c)
pointwise :: (Integer -> Integer -> Bool)
-> Value crypto -> Value crypto -> Bool
pointwise Integer -> Integer -> Bool
p (Value Integer
c Map (PolicyID crypto) (Map AssetName Integer)
x) (Value Integer
d Map (PolicyID crypto) (Map AssetName Integer)
y) = (Integer -> Integer -> Bool
p Integer
c Integer
d) Bool -> Bool -> Bool
&& ((Map AssetName Integer -> Map AssetName Integer -> Bool)
-> Map (PolicyID crypto) (Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
-> Bool
forall k v.
(Ord k, CannonicalZero v) =>
(v -> v -> Bool) -> Map k v -> Map k v -> Bool
pointWise ((Integer -> Integer -> Bool)
-> Map AssetName Integer -> Map AssetName Integer -> Bool
forall k v.
(Ord k, CannonicalZero v) =>
(v -> v -> Bool) -> Map k v -> Map k v -> Bool
pointWise Integer -> Integer -> Bool
p) Map (PolicyID crypto) (Map AssetName Integer)
x Map (PolicyID crypto) (Map AssetName Integer)
y)
size :: Value crypto -> Integer
size (Value Integer
_ Map (PolicyID crypto) (Map AssetName Integer)
v) =
(Map AssetName Integer -> Integer -> Integer)
-> Integer
-> Map (PolicyID crypto) (Map AssetName Integer)
-> Integer
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Map AssetName Integer -> Integer -> Integer
forall (t :: * -> *) p. Foldable t => t p -> Integer -> Integer
accum Integer
uint Map (PolicyID crypto) (Map AssetName Integer)
v
where
accum :: t p -> Integer -> Integer
accum t p
u Integer
ans = (p -> Integer -> Integer) -> Integer -> t p -> Integer
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr p -> Integer -> Integer
forall p. p -> Integer -> Integer
accumIns (Integer
ans Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
policyIdLen) t p
u
where
accumIns :: p -> Integer -> Integer
accumIns p
_ Integer
ans1 = Integer
ans1 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
assetNameLen Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
uint
uint :: Integer
uint :: Integer
uint = Integer
9
assetNameLen :: Integer
assetNameLen :: Integer
assetNameLen = Integer
32
policyIdLen :: Integer
policyIdLen :: Integer
policyIdLen = Integer
28
decodeValue ::
CC.Crypto crypto =>
Decoder s (Value crypto)
decodeValue :: Decoder s (Value crypto)
decodeValue = do
TokenType
tt <- Decoder s TokenType
forall s. Decoder s TokenType
peekTokenType
case TokenType
tt of
TokenType
TypeUInt -> Coin -> Value crypto
forall t. Val t => Coin -> t
inject (Coin -> Value crypto)
-> (Integer -> Coin) -> Integer -> Value crypto
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Coin
Coin (Integer -> Value crypto)
-> Decoder s Integer -> Decoder s (Value crypto)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Integer
forall s. Decoder s Integer
decodeInteger
TokenType
TypeUInt64 -> Coin -> Value crypto
forall t. Val t => Coin -> t
inject (Coin -> Value crypto)
-> (Integer -> Coin) -> Integer -> Value crypto
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Coin
Coin (Integer -> Value crypto)
-> Decoder s Integer -> Decoder s (Value crypto)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Integer
forall s. Decoder s Integer
decodeInteger
TokenType
TypeNInt -> Coin -> Value crypto
forall t. Val t => Coin -> t
inject (Coin -> Value crypto)
-> (Integer -> Coin) -> Integer -> Value crypto
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Coin
Coin (Integer -> Value crypto)
-> Decoder s Integer -> Decoder s (Value crypto)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Integer
forall s. Decoder s Integer
decodeInteger
TokenType
TypeNInt64 -> Coin -> Value crypto
forall t. Val t => Coin -> t
inject (Coin -> Value crypto)
-> (Integer -> Coin) -> Integer -> Value crypto
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Coin
Coin (Integer -> Value crypto)
-> Decoder s Integer -> Decoder s (Value crypto)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Integer
forall s. Decoder s Integer
decodeInteger
TokenType
TypeListLen -> (forall s. Decoder s Integer) -> Decoder s (Value crypto)
forall crypto s.
Crypto crypto =>
(forall s. Decoder s Integer) -> Decoder s (Value crypto)
decodeValuePair forall s. Decoder s Integer
decodeInteger
TokenType
TypeListLen64 -> (forall s. Decoder s Integer) -> Decoder s (Value crypto)
forall crypto s.
Crypto crypto =>
(forall s. Decoder s Integer) -> Decoder s (Value crypto)
decodeValuePair forall s. Decoder s Integer
decodeInteger
TokenType
TypeListLenIndef -> (forall s. Decoder s Integer) -> Decoder s (Value crypto)
forall crypto s.
Crypto crypto =>
(forall s. Decoder s Integer) -> Decoder s (Value crypto)
decodeValuePair forall s. Decoder s Integer
decodeInteger
TokenType
_ -> String -> Decoder s (Value crypto)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s (Value crypto))
-> String -> Decoder s (Value crypto)
forall a b. (a -> b) -> a -> b
$ String
"Value: expected array or int, got " String -> ShowS
forall a. [a] -> [a] -> [a]
++ TokenType -> String
forall a. Show a => a -> String
show TokenType
tt
decodeValuePair ::
CC.Crypto crypto =>
(forall t. Decoder t Integer) ->
Decoder s (Value crypto)
decodeValuePair :: (forall s. Decoder s Integer) -> Decoder s (Value crypto)
decodeValuePair forall s. Decoder s Integer
decodeAmount =
Decode ('Closed 'Dense) (Value crypto) -> Decoder s (Value crypto)
forall (w :: Wrapped) t s. Decode w t -> Decoder s t
decode (Decode ('Closed 'Dense) (Value crypto)
-> Decoder s (Value crypto))
-> Decode ('Closed 'Dense) (Value crypto)
-> Decoder s (Value crypto)
forall a b. (a -> b) -> a -> b
$
(Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto)
-> Decode
('Closed 'Dense)
(Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto)
forall t. t -> Decode ('Closed 'Dense) t
RecD Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
forall crypto.
Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
Value
Decode
('Closed 'Dense)
(Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto)
-> Decode ('Closed 'Dense) Integer
-> Decode
('Closed 'Dense)
(Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto)
forall (w1 :: Wrapped) a t (w :: Density).
Decode w1 (a -> t) -> Decode ('Closed w) a -> Decode w1 t
<! (forall s. Decoder s Integer) -> Decode ('Closed 'Dense) Integer
forall t. (forall s. Decoder s t) -> Decode ('Closed 'Dense) t
D forall s. Decoder s Integer
decodeAmount
Decode
('Closed 'Dense)
(Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto)
-> Decode
('Closed 'Dense) (Map (PolicyID crypto) (Map AssetName Integer))
-> Decode ('Closed 'Dense) (Value crypto)
forall (w1 :: Wrapped) a t (w :: Density).
Decode w1 (a -> t) -> Decode ('Closed w) a -> Decode w1 t
<! (forall s.
Decoder s (Map (PolicyID crypto) (Map AssetName Integer)))
-> Decode
('Closed 'Dense) (Map (PolicyID crypto) (Map AssetName Integer))
forall t. (forall s. Decoder s t) -> Decode ('Closed 'Dense) t
D (Decoder s Integer
-> Decoder s (Map (PolicyID crypto) (Map AssetName Integer))
forall crypto s.
Crypto crypto =>
Decoder s Integer
-> Decoder s (Map (PolicyID crypto) (Map AssetName Integer))
decodeMultiAssetMaps Decoder s Integer
forall s. Decoder s Integer
decodeAmount)
encodeMultiAssetMaps ::
CC.Crypto crypto =>
Map (PolicyID crypto) (Map AssetName Integer) ->
Encoding
encodeMultiAssetMaps :: Map (PolicyID crypto) (Map AssetName Integer) -> Encoding
encodeMultiAssetMaps = (PolicyID crypto -> Encoding)
-> (Map AssetName Integer -> Encoding)
-> Map (PolicyID crypto) (Map AssetName Integer)
-> Encoding
forall a b.
(a -> Encoding) -> (b -> Encoding) -> Map a b -> Encoding
encodeMap PolicyID crypto -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR ((AssetName -> Encoding)
-> (Integer -> Encoding) -> Map AssetName Integer -> Encoding
forall a b.
(a -> Encoding) -> (b -> Encoding) -> Map a b -> Encoding
encodeMap AssetName -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR Integer -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR)
decodeMultiAssetMaps ::
CC.Crypto crypto =>
Decoder s Integer ->
Decoder s (Map (PolicyID crypto) (Map AssetName Integer))
decodeMultiAssetMaps :: Decoder s Integer
-> Decoder s (Map (PolicyID crypto) (Map AssetName Integer))
decodeMultiAssetMaps Decoder s Integer
decodeAmount =
Map (PolicyID crypto) (Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
forall crypto.
Map (PolicyID crypto) (Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
prune (Map (PolicyID crypto) (Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer))
-> Decoder s (Map (PolicyID crypto) (Map AssetName Integer))
-> Decoder s (Map (PolicyID crypto) (Map AssetName Integer))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (PolicyID crypto)
-> Decoder s (Map AssetName Integer)
-> Decoder s (Map (PolicyID crypto) (Map AssetName Integer))
forall a s b.
Ord a =>
Decoder s a -> Decoder s b -> Decoder s (Map a b)
decodeMap Decoder s (PolicyID crypto)
forall a s. FromCBOR a => Decoder s a
fromCBOR (Decoder s AssetName
-> Decoder s Integer -> Decoder s (Map AssetName Integer)
forall a s b.
Ord a =>
Decoder s a -> Decoder s b -> Decoder s (Map a b)
decodeMap Decoder s AssetName
forall a s. FromCBOR a => Decoder s a
fromCBOR Decoder s Integer
decodeAmount)
decodeNonNegativeInteger :: Decoder s Integer
decodeNonNegativeInteger :: Decoder s Integer
decodeNonNegativeInteger = Word64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Integer) -> Decoder s Word64 -> Decoder s Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Word64
forall s. Decoder s Word64
decodeWord64
decodeNonNegativeValue ::
CC.Crypto crypto =>
Decoder s (Value crypto)
decodeNonNegativeValue :: Decoder s (Value crypto)
decodeNonNegativeValue = do
TokenType
tt <- Decoder s TokenType
forall s. Decoder s TokenType
peekTokenType
case TokenType
tt of
TokenType
TypeUInt -> Coin -> Value crypto
forall t. Val t => Coin -> t
inject (Coin -> Value crypto)
-> (Integer -> Coin) -> Integer -> Value crypto
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Coin
Coin (Integer -> Value crypto)
-> Decoder s Integer -> Decoder s (Value crypto)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Integer
forall s. Decoder s Integer
decodeNonNegativeInteger
TokenType
TypeUInt64 -> Coin -> Value crypto
forall t. Val t => Coin -> t
inject (Coin -> Value crypto)
-> (Integer -> Coin) -> Integer -> Value crypto
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Coin
Coin (Integer -> Value crypto)
-> Decoder s Integer -> Decoder s (Value crypto)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Integer
forall s. Decoder s Integer
decodeNonNegativeInteger
TokenType
TypeListLen -> (forall s. Decoder s Integer) -> Decoder s (Value crypto)
forall crypto s.
Crypto crypto =>
(forall s. Decoder s Integer) -> Decoder s (Value crypto)
decodeValuePair forall s. Decoder s Integer
decodeNonNegativeInteger
TokenType
TypeListLen64 -> (forall s. Decoder s Integer) -> Decoder s (Value crypto)
forall crypto s.
Crypto crypto =>
(forall s. Decoder s Integer) -> Decoder s (Value crypto)
decodeValuePair forall s. Decoder s Integer
decodeNonNegativeInteger
TokenType
TypeListLenIndef -> (forall s. Decoder s Integer) -> Decoder s (Value crypto)
forall crypto s.
Crypto crypto =>
(forall s. Decoder s Integer) -> Decoder s (Value crypto)
decodeValuePair forall s. Decoder s Integer
decodeNonNegativeInteger
TokenType
_ -> String -> Decoder s (Value crypto)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s (Value crypto))
-> String -> Decoder s (Value crypto)
forall a b. (a -> b) -> a -> b
$ String
"Value: expected array or int, got " String -> ShowS
forall a. [a] -> [a] -> [a]
++ TokenType -> String
forall a. Show a => a -> String
show TokenType
tt
instance
CC.Crypto crypto =>
ToCBOR (Value crypto)
where
toCBOR :: Value crypto -> Encoding
toCBOR (Value Integer
c Map (PolicyID crypto) (Map AssetName Integer)
v) =
if Map (PolicyID crypto) (Map AssetName Integer) -> Bool
forall k a. Map k a -> Bool
Map.null Map (PolicyID crypto) (Map AssetName Integer)
v
then Integer -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR Integer
c
else
Encode ('Closed 'Dense) (Value crypto) -> Encoding
forall (w :: Wrapped) t. Encode w t -> Encoding
encode (Encode ('Closed 'Dense) (Value crypto) -> Encoding)
-> Encode ('Closed 'Dense) (Value crypto) -> Encoding
forall a b. (a -> b) -> a -> b
$
(Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto)
-> Encode
('Closed 'Dense)
(Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto)
forall t. t -> Encode ('Closed 'Dense) t
Rec Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
forall crypto.
Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
Value
Encode
('Closed 'Dense)
(Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto)
-> Encode ('Closed 'Dense) Integer
-> Encode
('Closed 'Dense)
(Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto)
forall (w :: Wrapped) a t (r :: Density).
Encode w (a -> t) -> Encode ('Closed r) a -> Encode w t
!> Integer -> Encode ('Closed 'Dense) Integer
forall t. ToCBOR t => t -> Encode ('Closed 'Dense) t
To Integer
c
Encode
('Closed 'Dense)
(Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto)
-> Encode
('Closed 'Dense) (Map (PolicyID crypto) (Map AssetName Integer))
-> Encode ('Closed 'Dense) (Value crypto)
forall (w :: Wrapped) a t (r :: Density).
Encode w (a -> t) -> Encode ('Closed r) a -> Encode w t
!> (Map (PolicyID crypto) (Map AssetName Integer) -> Encoding)
-> Map (PolicyID crypto) (Map AssetName Integer)
-> Encode
('Closed 'Dense) (Map (PolicyID crypto) (Map AssetName Integer))
forall t. (t -> Encoding) -> t -> Encode ('Closed 'Dense) t
E Map (PolicyID crypto) (Map AssetName Integer) -> Encoding
forall crypto.
Crypto crypto =>
Map (PolicyID crypto) (Map AssetName Integer) -> Encoding
encodeMultiAssetMaps Map (PolicyID crypto) (Map AssetName Integer)
v
instance
CC.Crypto crypto =>
FromCBOR (Value crypto)
where
fromCBOR :: Decoder s (Value crypto)
fromCBOR = Decoder s (Value crypto)
forall crypto s. Crypto crypto => Decoder s (Value crypto)
decodeValue
instance
CC.Crypto crypto =>
DecodeNonNegative (Value crypto)
where
decodeNonNegative :: Decoder s (Value crypto)
decodeNonNegative = Decoder s (Value crypto)
forall crypto s. Crypto crypto => Decoder s (Value crypto)
decodeNonNegativeValue
instance
CC.Crypto crypto =>
DecodeMint (Value crypto)
where
decodeMint :: Decoder s (Value crypto)
decodeMint = Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
forall crypto.
Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
Value Integer
0 (Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto)
-> Decoder s (Map (PolicyID crypto) (Map AssetName Integer))
-> Decoder s (Value crypto)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Integer
-> Decoder s (Map (PolicyID crypto) (Map AssetName Integer))
forall crypto s.
Crypto crypto =>
Decoder s Integer
-> Decoder s (Map (PolicyID crypto) (Map AssetName Integer))
decodeMultiAssetMaps Decoder s Integer
forall s. Decoder s Integer
decodeInteger
instance
CC.Crypto crypto =>
EncodeMint (Value crypto)
where
encodeMint :: Value crypto -> Encoding
encodeMint (Value Integer
_ Map (PolicyID crypto) (Map AssetName Integer)
multiasset) = Map (PolicyID crypto) (Map AssetName Integer) -> Encoding
forall crypto.
Crypto crypto =>
Map (PolicyID crypto) (Map AssetName Integer) -> Encoding
encodeMultiAssetMaps Map (PolicyID crypto) (Map AssetName Integer)
multiasset
instance CC.Crypto crypto => Compactible (Value crypto) where
newtype CompactForm (Value crypto) = CompactValue (CV crypto)
deriving (CompactForm (Value crypto) -> CompactForm (Value crypto) -> Bool
(CompactForm (Value crypto) -> CompactForm (Value crypto) -> Bool)
-> (CompactForm (Value crypto)
-> CompactForm (Value crypto) -> Bool)
-> Eq (CompactForm (Value crypto))
forall crypto.
CompactForm (Value crypto) -> CompactForm (Value crypto) -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CompactForm (Value crypto) -> CompactForm (Value crypto) -> Bool
$c/= :: forall crypto.
CompactForm (Value crypto) -> CompactForm (Value crypto) -> Bool
== :: CompactForm (Value crypto) -> CompactForm (Value crypto) -> Bool
$c== :: forall crypto.
CompactForm (Value crypto) -> CompactForm (Value crypto) -> Bool
Eq, Typeable, Int -> CompactForm (Value crypto) -> ShowS
[CompactForm (Value crypto)] -> ShowS
CompactForm (Value crypto) -> String
(Int -> CompactForm (Value crypto) -> ShowS)
-> (CompactForm (Value crypto) -> String)
-> ([CompactForm (Value crypto)] -> ShowS)
-> Show (CompactForm (Value crypto))
forall crypto. Int -> CompactForm (Value crypto) -> ShowS
forall crypto. [CompactForm (Value crypto)] -> ShowS
forall crypto. CompactForm (Value crypto) -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CompactForm (Value crypto)] -> ShowS
$cshowList :: forall crypto. [CompactForm (Value crypto)] -> ShowS
show :: CompactForm (Value crypto) -> String
$cshow :: forall crypto. CompactForm (Value crypto) -> String
showsPrec :: Int -> CompactForm (Value crypto) -> ShowS
$cshowsPrec :: forall crypto. Int -> CompactForm (Value crypto) -> ShowS
Show, Typeable (CompactForm (Value crypto))
Typeable (CompactForm (Value crypto))
-> (CompactForm (Value crypto) -> Encoding)
-> ((forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (CompactForm (Value crypto)) -> Size)
-> ((forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [CompactForm (Value crypto)] -> Size)
-> ToCBOR (CompactForm (Value crypto))
CompactForm (Value crypto) -> Encoding
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [CompactForm (Value crypto)] -> Size
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (CompactForm (Value crypto)) -> Size
forall a.
Typeable a
-> (a -> Encoding)
-> ((forall t. ToCBOR t => Proxy t -> Size) -> Proxy a -> Size)
-> ((forall t. ToCBOR t => Proxy t -> Size) -> Proxy [a] -> Size)
-> ToCBOR a
forall crypto.
Crypto crypto =>
Typeable (CompactForm (Value crypto))
forall crypto.
Crypto crypto =>
CompactForm (Value crypto) -> Encoding
forall crypto.
Crypto crypto =>
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [CompactForm (Value crypto)] -> Size
forall crypto.
Crypto crypto =>
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (CompactForm (Value crypto)) -> Size
encodedListSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [CompactForm (Value crypto)] -> Size
$cencodedListSizeExpr :: forall crypto.
Crypto crypto =>
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [CompactForm (Value crypto)] -> Size
encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (CompactForm (Value crypto)) -> Size
$cencodedSizeExpr :: forall crypto.
Crypto crypto =>
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (CompactForm (Value crypto)) -> Size
toCBOR :: CompactForm (Value crypto) -> Encoding
$ctoCBOR :: forall crypto.
Crypto crypto =>
CompactForm (Value crypto) -> Encoding
$cp1ToCBOR :: forall crypto.
Crypto crypto =>
Typeable (CompactForm (Value crypto))
ToCBOR, Typeable (CompactForm (Value crypto))
Decoder s (CompactForm (Value crypto))
Typeable (CompactForm (Value crypto))
-> (forall s. Decoder s (CompactForm (Value crypto)))
-> (Proxy (CompactForm (Value crypto)) -> Text)
-> FromCBOR (CompactForm (Value crypto))
Proxy (CompactForm (Value crypto)) -> Text
forall s. Decoder s (CompactForm (Value crypto))
forall a.
Typeable a
-> (forall s. Decoder s a) -> (Proxy a -> Text) -> FromCBOR a
forall crypto.
Crypto crypto =>
Typeable (CompactForm (Value crypto))
forall crypto.
Crypto crypto =>
Proxy (CompactForm (Value crypto)) -> Text
forall crypto s.
Crypto crypto =>
Decoder s (CompactForm (Value crypto))
label :: Proxy (CompactForm (Value crypto)) -> Text
$clabel :: forall crypto.
Crypto crypto =>
Proxy (CompactForm (Value crypto)) -> Text
fromCBOR :: Decoder s (CompactForm (Value crypto))
$cfromCBOR :: forall crypto s.
Crypto crypto =>
Decoder s (CompactForm (Value crypto))
$cp1FromCBOR :: forall crypto.
Crypto crypto =>
Typeable (CompactForm (Value crypto))
FromCBOR)
toCompact :: Value crypto -> Maybe (CompactForm (Value crypto))
toCompact Value crypto
x = CV crypto -> CompactForm (Value crypto)
forall crypto. CV crypto -> CompactForm (Value crypto)
CompactValue (CV crypto -> CompactForm (Value crypto))
-> Maybe (CV crypto) -> Maybe (CompactForm (Value crypto))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value crypto -> Maybe (CV crypto)
forall crypto. Value crypto -> Maybe (CV crypto)
toCV Value crypto
x
fromCompact :: CompactForm (Value crypto) -> Value crypto
fromCompact (CompactValue x) = CV crypto -> Value crypto
forall crypto. Crypto crypto => CV crypto -> Value crypto
fromCV CV crypto
x
instance CC.Crypto crypto => ToCBOR (CV crypto) where
toCBOR :: CV crypto -> Encoding
toCBOR = Value crypto -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (Value crypto -> Encoding)
-> (CV crypto -> Value crypto) -> CV crypto -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CV crypto -> Value crypto
forall crypto. Crypto crypto => CV crypto -> Value crypto
fromCV
instance CC.Crypto crypto => FromCBOR (CV crypto) where
fromCBOR :: Decoder s (CV crypto)
fromCBOR = do
Value crypto
v <- Decoder s (Value crypto)
forall crypto s. Crypto crypto => Decoder s (Value crypto)
decodeNonNegativeValue
case Value crypto -> Maybe (CV crypto)
forall crypto. Value crypto -> Maybe (CV crypto)
toCV Value crypto
v of
Maybe (CV crypto)
Nothing ->
String -> Decoder s (CV crypto)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail
String
"impossible failure: decoded nonnegative value that cannot be compacted"
Just CV crypto
x -> CV crypto -> Decoder s (CV crypto)
forall (f :: * -> *) a. Applicative f => a -> f a
pure CV crypto
x
data CV crypto
= CV
{-# UNPACK #-} !Word64
{-# UNPACK #-} !(Array Int (CVPart crypto))
deriving (CV crypto -> CV crypto -> Bool
(CV crypto -> CV crypto -> Bool)
-> (CV crypto -> CV crypto -> Bool) -> Eq (CV crypto)
forall crypto. CV crypto -> CV crypto -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CV crypto -> CV crypto -> Bool
$c/= :: forall crypto. CV crypto -> CV crypto -> Bool
== :: CV crypto -> CV crypto -> Bool
$c== :: forall crypto. CV crypto -> CV crypto -> Bool
Eq, Int -> CV crypto -> ShowS
[CV crypto] -> ShowS
CV crypto -> String
(Int -> CV crypto -> ShowS)
-> (CV crypto -> String)
-> ([CV crypto] -> ShowS)
-> Show (CV crypto)
forall crypto. Int -> CV crypto -> ShowS
forall crypto. [CV crypto] -> ShowS
forall crypto. CV crypto -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CV crypto] -> ShowS
$cshowList :: forall crypto. [CV crypto] -> ShowS
show :: CV crypto -> String
$cshow :: forall crypto. CV crypto -> String
showsPrec :: Int -> CV crypto -> ShowS
$cshowsPrec :: forall crypto. Int -> CV crypto -> ShowS
Show, Typeable)
data CVPart crypto
= CVPart
!(PolicyID crypto)
{-# UNPACK #-} !AssetName
{-# UNPACK #-} !Word64
deriving (CVPart crypto -> CVPart crypto -> Bool
(CVPart crypto -> CVPart crypto -> Bool)
-> (CVPart crypto -> CVPart crypto -> Bool) -> Eq (CVPart crypto)
forall crypto. CVPart crypto -> CVPart crypto -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CVPart crypto -> CVPart crypto -> Bool
$c/= :: forall crypto. CVPart crypto -> CVPart crypto -> Bool
== :: CVPart crypto -> CVPart crypto -> Bool
$c== :: forall crypto. CVPart crypto -> CVPart crypto -> Bool
Eq, Int -> CVPart crypto -> ShowS
[CVPart crypto] -> ShowS
CVPart crypto -> String
(Int -> CVPart crypto -> ShowS)
-> (CVPart crypto -> String)
-> ([CVPart crypto] -> ShowS)
-> Show (CVPart crypto)
forall crypto. Int -> CVPart crypto -> ShowS
forall crypto. [CVPart crypto] -> ShowS
forall crypto. CVPart crypto -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CVPart crypto] -> ShowS
$cshowList :: forall crypto. [CVPart crypto] -> ShowS
show :: CVPart crypto -> String
$cshow :: forall crypto. CVPart crypto -> String
showsPrec :: Int -> CVPart crypto -> ShowS
$cshowsPrec :: forall crypto. Int -> CVPart crypto -> ShowS
Show, Typeable)
toCV :: Value crypto -> Maybe (CV crypto)
toCV :: Value crypto -> Maybe (CV crypto)
toCV Value crypto
v = do
let (Integer
c, [(PolicyID crypto, AssetName, Integer)]
triples) = Value crypto -> (Integer, [(PolicyID crypto, AssetName, Integer)])
forall crypto.
Value crypto -> (Integer, [(PolicyID crypto, AssetName, Integer)])
gettriples Value crypto
v
policyIDs :: Set (PolicyID crypto)
policyIDs = [PolicyID crypto] -> Set (PolicyID crypto)
forall a. Ord a => [a] -> Set a
Set.fromList ([PolicyID crypto] -> Set (PolicyID crypto))
-> [PolicyID crypto] -> Set (PolicyID crypto)
forall a b. (a -> b) -> a -> b
$ (\(PolicyID crypto
x, AssetName
_, Integer
_) -> PolicyID crypto
x) ((PolicyID crypto, AssetName, Integer) -> PolicyID crypto)
-> [(PolicyID crypto, AssetName, Integer)] -> [PolicyID crypto]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(PolicyID crypto, AssetName, Integer)]
triples
n :: Int
n = [(PolicyID crypto, AssetName, Integer)] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(PolicyID crypto, AssetName, Integer)]
triples Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
[CVPart crypto]
cvParts <- ((PolicyID crypto, AssetName, Integer) -> Maybe (CVPart crypto))
-> [(PolicyID crypto, AssetName, Integer)] -> Maybe [CVPart crypto]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (Set (PolicyID crypto)
-> (PolicyID crypto, AssetName, Integer) -> Maybe (CVPart crypto)
forall crypto.
Set (PolicyID crypto)
-> (PolicyID crypto, AssetName, Integer) -> Maybe (CVPart crypto)
toCVPart Set (PolicyID crypto)
policyIDs) [(PolicyID crypto, AssetName, Integer)]
triples
let arr :: Array Int (CVPart crypto)
arr = (Int, Int) -> [(Int, CVPart crypto)] -> Array Int (CVPart crypto)
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
(i, i) -> [(i, e)] -> a i e
array (Int
0, Int
n) ([Int] -> [CVPart crypto] -> [(Int, CVPart crypto)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 .. Int
n] [CVPart crypto]
cvParts)
Word64
c' <- Integer -> Maybe Word64
integerToWord64 Integer
c
CV crypto -> Maybe (CV crypto)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CV crypto -> Maybe (CV crypto)) -> CV crypto -> Maybe (CV crypto)
forall a b. (a -> b) -> a -> b
$ Word64 -> Array Int (CVPart crypto) -> CV crypto
forall crypto. Word64 -> Array Int (CVPart crypto) -> CV crypto
CV Word64
c' Array Int (CVPart crypto)
arr
where
deduplicate :: Set a -> a -> a
deduplicate Set a
xs a
x = a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe a
x (Maybe a -> a) -> Maybe a -> a
forall a b. (a -> b) -> a -> b
$ do
a
r <- a -> Set a -> Maybe a
forall a. Ord a => a -> Set a -> Maybe a
Set.lookupLE a
x Set a
xs
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
r)
a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
r
toCVPart :: Set (PolicyID crypto)
-> (PolicyID crypto, AssetName, Integer) -> Maybe (CVPart crypto)
toCVPart Set (PolicyID crypto)
policyIdSet (PolicyID crypto
policyId, AssetName
aname, Integer
amount) =
PolicyID crypto -> AssetName -> Word64 -> CVPart crypto
forall crypto.
PolicyID crypto -> AssetName -> Word64 -> CVPart crypto
CVPart (Set (PolicyID crypto) -> PolicyID crypto -> PolicyID crypto
forall a. Ord a => Set a -> a -> a
deduplicate Set (PolicyID crypto)
policyIdSet PolicyID crypto
policyId) AssetName
aname
(Word64 -> CVPart crypto) -> Maybe Word64 -> Maybe (CVPart crypto)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Integer -> Maybe Word64
integerToWord64 Integer
amount
fromCV :: CC.Crypto crypto => CV crypto -> Value crypto
fromCV :: CV crypto -> Value crypto
fromCV (CV Word64
w Array Int (CVPart crypto)
vs) = (CVPart crypto -> Value crypto -> Value crypto)
-> Value crypto -> Array Int (CVPart crypto) -> Value crypto
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr CVPart crypto -> Value crypto -> Value crypto
forall crypto. CVPart crypto -> Value crypto -> Value crypto
f (Coin -> Value crypto
forall t. Val t => Coin -> t
inject (Coin -> Value crypto)
-> (Word64 -> Coin) -> Word64 -> Value crypto
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Coin
Coin (Integer -> Coin) -> (Word64 -> Integer) -> Word64 -> Coin
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Value crypto) -> Word64 -> Value crypto
forall a b. (a -> b) -> a -> b
$ Word64
w) Array Int (CVPart crypto)
vs
where
f :: CVPart crypto -> Value crypto -> Value crypto
f (CVPart PolicyID crypto
policyId AssetName
aname Word64
amount) Value crypto
acc =
(Integer -> Integer -> Integer)
-> PolicyID crypto
-> AssetName
-> Integer
-> Value crypto
-> Value crypto
forall crypto.
(Integer -> Integer -> Integer)
-> PolicyID crypto
-> AssetName
-> Integer
-> Value crypto
-> Value crypto
insert Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
(+) PolicyID crypto
policyId AssetName
aname (Word64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
amount) Value crypto
acc
instance CC.Crypto crypto => Torsor (Value crypto) where
type Delta (Value crypto) = (Value crypto)
addDelta :: Value crypto -> Delta (Value crypto) -> Value crypto
addDelta = Value crypto -> Delta (Value crypto) -> Value crypto
forall t. Val t => t -> t -> t
(<+>)
toDelta :: Value crypto -> Delta (Value crypto)
toDelta = Value crypto -> Delta (Value crypto)
forall a. a -> a
id
policies :: Value crypto -> Set (PolicyID crypto)
policies :: Value crypto -> Set (PolicyID crypto)
policies (Value Integer
_ Map (PolicyID crypto) (Map AssetName Integer)
m) = Map (PolicyID crypto) (Map AssetName Integer)
-> Set (PolicyID crypto)
forall k a. Map k a -> Set k
Map.keysSet Map (PolicyID crypto) (Map AssetName Integer)
m
lookup :: PolicyID crypto -> AssetName -> Value crypto -> Integer
lookup :: PolicyID crypto -> AssetName -> Value crypto -> Integer
lookup PolicyID crypto
pid AssetName
aid (Value Integer
_ Map (PolicyID crypto) (Map AssetName Integer)
m) =
case PolicyID crypto
-> Map (PolicyID crypto) (Map AssetName Integer)
-> Maybe (Map AssetName Integer)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup PolicyID crypto
pid Map (PolicyID crypto) (Map AssetName Integer)
m of
Maybe (Map AssetName Integer)
Nothing -> Integer
0
Just Map AssetName Integer
m2 -> Integer -> AssetName -> Map AssetName Integer -> Integer
forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault Integer
0 AssetName
aid Map AssetName Integer
m2
insert ::
(Integer -> Integer -> Integer) ->
PolicyID crypto ->
AssetName ->
Integer ->
Value crypto ->
Value crypto
insert :: (Integer -> Integer -> Integer)
-> PolicyID crypto
-> AssetName
-> Integer
-> Value crypto
-> Value crypto
insert Integer -> Integer -> Integer
combine PolicyID crypto
pid AssetName
aid Integer
new (Value Integer
cn Map (PolicyID crypto) (Map AssetName Integer)
m1) =
case PolicyID crypto
-> Map (PolicyID crypto) (Map AssetName Integer)
-> (Map (PolicyID crypto) (Map AssetName Integer),
Maybe (Map AssetName Integer),
Map (PolicyID crypto) (Map AssetName Integer))
forall k a. Ord k => k -> Map k a -> (Map k a, Maybe a, Map k a)
splitLookup PolicyID crypto
pid Map (PolicyID crypto) (Map AssetName Integer)
m1 of
(Map (PolicyID crypto) (Map AssetName Integer)
l1, Just Map AssetName Integer
m2, Map (PolicyID crypto) (Map AssetName Integer)
l2) ->
case AssetName
-> Map AssetName Integer
-> (Map AssetName Integer, Maybe Integer, Map AssetName Integer)
forall k a. Ord k => k -> Map k a -> (Map k a, Maybe a, Map k a)
splitLookup AssetName
aid Map AssetName Integer
m2 of
(Map AssetName Integer
v1, Just Integer
old, Map AssetName Integer
v2) ->
if Integer
n Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0
then
let m3 :: Map AssetName Integer
m3 = (Map AssetName Integer
-> Map AssetName Integer -> Map AssetName Integer
forall k a. Map k a -> Map k a -> Map k a
link2 Map AssetName Integer
v1 Map AssetName Integer
v2)
in if Map AssetName Integer -> Bool
forall k a. Map k a -> Bool
Map.null Map AssetName Integer
m3
then Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
forall crypto.
Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
Value Integer
cn (Map (PolicyID crypto) (Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
forall k a. Map k a -> Map k a -> Map k a
link2 Map (PolicyID crypto) (Map AssetName Integer)
l1 Map (PolicyID crypto) (Map AssetName Integer)
l2)
else Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
forall crypto.
Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
Value Integer
cn (PolicyID crypto
-> Map AssetName Integer
-> Map (PolicyID crypto) (Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link PolicyID crypto
pid Map AssetName Integer
m3 Map (PolicyID crypto) (Map AssetName Integer)
l1 Map (PolicyID crypto) (Map AssetName Integer)
l2)
else Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
forall crypto.
Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
Value Integer
cn (PolicyID crypto
-> Map AssetName Integer
-> Map (PolicyID crypto) (Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link PolicyID crypto
pid (AssetName
-> Integer
-> Map AssetName Integer
-> Map AssetName Integer
-> Map AssetName Integer
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link AssetName
aid Integer
n Map AssetName Integer
v1 Map AssetName Integer
v2) Map (PolicyID crypto) (Map AssetName Integer)
l1 Map (PolicyID crypto) (Map AssetName Integer)
l2)
where
n :: Integer
n = Integer -> Integer -> Integer
combine Integer
old Integer
new
(Map AssetName Integer
_, Maybe Integer
Nothing, Map AssetName Integer
_) ->
Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
forall crypto.
Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
Value
Integer
cn
( PolicyID crypto
-> Map AssetName Integer
-> Map (PolicyID crypto) (Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link
PolicyID crypto
pid
( if Integer
new Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0
then Map AssetName Integer
m2
else (AssetName
-> Integer -> Map AssetName Integer -> Map AssetName Integer
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert AssetName
aid Integer
new Map AssetName Integer
m2)
)
Map (PolicyID crypto) (Map AssetName Integer)
l1
Map (PolicyID crypto) (Map AssetName Integer)
l2
)
(Map (PolicyID crypto) (Map AssetName Integer)
l1, Maybe (Map AssetName Integer)
Nothing, Map (PolicyID crypto) (Map AssetName Integer)
l2) ->
Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
forall crypto.
Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
Value
Integer
cn
( if Integer
new Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0
then Map (PolicyID crypto) (Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
forall k a. Map k a -> Map k a -> Map k a
link2 Map (PolicyID crypto) (Map AssetName Integer)
l1 Map (PolicyID crypto) (Map AssetName Integer)
l2
else PolicyID crypto
-> Map AssetName Integer
-> Map (PolicyID crypto) (Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
forall k a. k -> a -> Map k a -> Map k a -> Map k a
link PolicyID crypto
pid (AssetName -> Integer -> Map AssetName Integer
forall k a. k -> a -> Map k a
Map.singleton AssetName
aid Integer
new) Map (PolicyID crypto) (Map AssetName Integer)
l1 Map (PolicyID crypto) (Map AssetName Integer)
l2
)
prune ::
Map (PolicyID crypto) (Map AssetName Integer) ->
Map (PolicyID crypto) (Map AssetName Integer)
prune :: Map (PolicyID crypto) (Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
prune Map (PolicyID crypto) (Map AssetName Integer)
assets =
(Map AssetName Integer -> Bool)
-> Map (PolicyID crypto) (Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter (Bool -> Bool
not (Bool -> Bool)
-> (Map AssetName Integer -> Bool) -> Map AssetName Integer -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map AssetName Integer -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null) (Map (PolicyID crypto) (Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer))
-> Map (PolicyID crypto) (Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
forall a b. (a -> b) -> a -> b
$ (Integer -> Bool) -> Map AssetName Integer -> Map AssetName Integer
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter (Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
/= Integer
0) (Map AssetName Integer -> Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
-> Map (PolicyID crypto) (Map AssetName Integer)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map (PolicyID crypto) (Map AssetName Integer)
assets
valueFromList :: Integer -> [(PolicyID era, AssetName, Integer)] -> Value era
valueFromList :: Integer -> [(PolicyID era, AssetName, Integer)] -> Value era
valueFromList Integer
ada =
((PolicyID era, AssetName, Integer) -> Value era -> Value era)
-> Value era -> [(PolicyID era, AssetName, Integer)] -> Value era
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr
(\(PolicyID era
p, AssetName
n, Integer
i) Value era
ans -> (Integer -> Integer -> Integer)
-> PolicyID era -> AssetName -> Integer -> Value era -> Value era
forall crypto.
(Integer -> Integer -> Integer)
-> PolicyID crypto
-> AssetName
-> Integer
-> Value crypto
-> Value crypto
insert Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
(+) PolicyID era
p AssetName
n Integer
i Value era
ans)
(Integer -> Map (PolicyID era) (Map AssetName Integer) -> Value era
forall crypto.
Integer
-> Map (PolicyID crypto) (Map AssetName Integer) -> Value crypto
Value Integer
ada Map (PolicyID era) (Map AssetName Integer)
forall k a. Map k a
Map.empty)
showValue :: Value crypto -> String
showValue :: Value crypto -> String
showValue Value crypto
v = Integer -> String
forall a. Show a => a -> String
show Integer
c String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"\n" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Context -> String
unlines (((PolicyID crypto, AssetName, Integer) -> String)
-> [(PolicyID crypto, AssetName, Integer)] -> Context
forall a b. (a -> b) -> [a] -> [b]
map (PolicyID crypto, AssetName, Integer) -> String
forall a a crypto.
(Show a, Show a) =>
(PolicyID crypto, a, a) -> String
trans [(PolicyID crypto, AssetName, Integer)]
ts)
where
(Integer
c, [(PolicyID crypto, AssetName, Integer)]
ts) = Value crypto -> (Integer, [(PolicyID crypto, AssetName, Integer)])
forall crypto.
Value crypto -> (Integer, [(PolicyID crypto, AssetName, Integer)])
gettriples Value crypto
v
trans :: (PolicyID crypto, a, a) -> String
trans (PolicyID ScriptHash crypto
x, a
hash, a
cnt) =
ScriptHash crypto -> String
forall a. Show a => a -> String
show ScriptHash crypto
x
String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
", "
String -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
hash
String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
", "
String -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
cnt
gettriples :: Value crypto -> (Integer, [(PolicyID crypto, AssetName, Integer)])
gettriples :: Value crypto -> (Integer, [(PolicyID crypto, AssetName, Integer)])
gettriples (Value Integer
c Map (PolicyID crypto) (Map AssetName Integer)
m1) = (Integer
c, [(PolicyID crypto, AssetName, Integer)]
triples)
where
triples :: [(PolicyID crypto, AssetName, Integer)]
triples =
[ (PolicyID crypto
policyId, AssetName
aname, Integer
amount)
| (PolicyID crypto
policyId, Map AssetName Integer
m2) <- Map (PolicyID crypto) (Map AssetName Integer)
-> [(PolicyID crypto, Map AssetName Integer)]
forall k a. Map k a -> [(k, a)]
assocs Map (PolicyID crypto) (Map AssetName Integer)
m1,
(AssetName
aname, Integer
amount) <- Map AssetName Integer -> [(AssetName, Integer)]
forall k a. Map k a -> [(k, a)]
assocs Map AssetName Integer
m2
]