module Cometa.Value.Raw
( Snapshot,
mkSnapshot,
coin,
assets,
fromValue,
toValue,
)
where
import Cometa.AssetId (AssetId (Ada, Native))
import Cometa.AssetName (AssetName)
import qualified Cometa.AssetName as Name
import Cometa.Error (Error (InvalidValueQuantity))
import Cometa.PolicyId (PolicyId)
import qualified Cometa.Value as Value
import Control.Monad (unless)
import qualified Data.ByteString as B
import Data.Int (Int64)
import Data.List (sortOn)
import qualified Data.Map.Strict as Map
data Snapshot = Snapshot !Integer ![(PolicyId, [(AssetName, Integer)])]
deriving (Snapshot -> Snapshot -> Bool
(Snapshot -> Snapshot -> Bool)
-> (Snapshot -> Snapshot -> Bool) -> Eq Snapshot
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Snapshot -> Snapshot -> Bool
== :: Snapshot -> Snapshot -> Bool
$c/= :: Snapshot -> Snapshot -> Bool
/= :: Snapshot -> Snapshot -> Bool
Eq, Int -> Snapshot -> ShowS
[Snapshot] -> ShowS
Snapshot -> String
(Int -> Snapshot -> ShowS)
-> (Snapshot -> String) -> ([Snapshot] -> ShowS) -> Show Snapshot
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Snapshot -> ShowS
showsPrec :: Int -> Snapshot -> ShowS
$cshow :: Snapshot -> String
show :: Snapshot -> String
$cshowList :: [Snapshot] -> ShowS
showList :: [Snapshot] -> ShowS
Show)
mkSnapshot :: Integer -> [(PolicyId, [(AssetName, Integer)])] -> Either Error Snapshot
mkSnapshot :: Integer
-> [(PolicyId, [(AssetName, Integer)])] -> Either Error Snapshot
mkSnapshot Integer
lovelace [(PolicyId, [(AssetName, Integer)])]
groups = do
Integer -> Either Error ()
bounded Integer
lovelace
((PolicyId, [(AssetName, Integer)]) -> Either Error ())
-> [(PolicyId, [(AssetName, Integer)])] -> Either Error ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (((AssetName, Integer) -> Either Error ())
-> [(AssetName, Integer)] -> Either Error ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Integer -> Either Error ()
bounded (Integer -> Either Error ())
-> ((AssetName, Integer) -> Integer)
-> (AssetName, Integer)
-> Either Error ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (AssetName, Integer) -> Integer
forall a b. (a, b) -> b
snd) ([(AssetName, Integer)] -> Either Error ())
-> ((PolicyId, [(AssetName, Integer)]) -> [(AssetName, Integer)])
-> (PolicyId, [(AssetName, Integer)])
-> Either Error ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PolicyId, [(AssetName, Integer)]) -> [(AssetName, Integer)]
forall a b. (a, b) -> b
snd) [(PolicyId, [(AssetName, Integer)])]
groups
Snapshot -> Either Error Snapshot
forall a. a -> Either Error a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Integer -> [(PolicyId, [(AssetName, Integer)])] -> Snapshot
Snapshot Integer
lovelace [(PolicyId
policy, ((AssetName, Integer) -> (Int, ByteString))
-> [(AssetName, Integer)] -> [(AssetName, Integer)]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn (AssetName, Integer) -> (Int, ByteString)
forall {b}. (AssetName, b) -> (Int, ByteString)
nameKey [(AssetName, Integer)]
names) | (PolicyId
policy, [(AssetName, Integer)]
names) <- Map PolicyId [(AssetName, Integer)]
-> [(PolicyId, [(AssetName, Integer)])]
forall k a. Map k a -> [(k, a)]
Map.toAscList ([(PolicyId, [(AssetName, Integer)])]
-> Map PolicyId [(AssetName, Integer)]
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(PolicyId, [(AssetName, Integer)])]
groups)])
where
nameKey :: (AssetName, b) -> (Int, ByteString)
nameKey (AssetName
name, b
_) = let bytes :: ByteString
bytes = AssetName -> ByteString
Name.toBytes AssetName
name in (ByteString -> Int
B.length ByteString
bytes, ByteString
bytes)
coin :: Snapshot -> Integer
coin :: Snapshot -> Integer
coin (Snapshot Integer
result [(PolicyId, [(AssetName, Integer)])]
_) = Integer
result
assets :: Snapshot -> [(PolicyId, [(AssetName, Integer)])]
assets :: Snapshot -> [(PolicyId, [(AssetName, Integer)])]
assets (Snapshot Integer
_ [(PolicyId, [(AssetName, Integer)])]
result) = [(PolicyId, [(AssetName, Integer)])]
result
fromValue :: Value.Value -> Either Error Snapshot
fromValue :: Value -> Either Error Snapshot
fromValue Value
value = Integer
-> [(PolicyId, [(AssetName, Integer)])] -> Either Error Snapshot
mkSnapshot (AssetId -> Value -> Integer
Value.quantity AssetId
Ada Value
value) (Map PolicyId [(AssetName, Integer)]
-> [(PolicyId, [(AssetName, Integer)])]
forall k a. Map k a -> [(k, a)]
Map.toAscList Map PolicyId [(AssetName, Integer)]
groups)
where
groups :: Map PolicyId [(AssetName, Integer)]
groups = ([(AssetName, Integer)]
-> [(AssetName, Integer)] -> [(AssetName, Integer)])
-> [(PolicyId, [(AssetName, Integer)])]
-> Map PolicyId [(AssetName, Integer)]
forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
Map.fromListWith [(AssetName, Integer)]
-> [(AssetName, Integer)] -> [(AssetName, Integer)]
forall a. Semigroup a => a -> a -> a
(<>) [(PolicyId
policy, [(AssetName
name, Integer
quantity)]) | (Native PolicyId
policy AssetName
name, Integer
quantity) <- Value -> [(AssetId, Integer)]
Value.toList Value
value]
toValue :: Snapshot -> Value.Value
toValue :: Snapshot -> Value
toValue (Snapshot Integer
lovelace [(PolicyId, [(AssetName, Integer)])]
groups) = [(AssetId, Integer)] -> Value
Value.fromList ((AssetId
Ada, Integer
lovelace) (AssetId, Integer) -> [(AssetId, Integer)] -> [(AssetId, Integer)]
forall a. a -> [a] -> [a]
: [(PolicyId -> AssetName -> AssetId
Native PolicyId
policy AssetName
name, Integer
quantity) | (PolicyId
policy, [(AssetName, Integer)]
names) <- [(PolicyId, [(AssetName, Integer)])]
groups, (AssetName
name, Integer
quantity) <- [(AssetName, Integer)]
names])
bounded :: Integer -> Either Error ()
bounded :: Integer -> Either Error ()
bounded Integer
quantity = Bool -> Either Error () -> Either Error ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Integer
quantity Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int64
forall a. Bounded a => a
minBound :: Int64) Bool -> Bool -> Bool
&& Integer
quantity Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int64
forall a. Bounded a => a
maxBound :: Int64)) (Error -> Either Error ()
forall a b. a -> Either a b
Left (Integer -> Error
InvalidValueQuantity Integer
quantity))