-- | Module for the on-chain representation of Utxo.
module Hydra.Data.Utxo where

import Hydra.Prelude

import PlutusTx (FromData (..), ToData (..), UnsafeFromData (..))
import PlutusTx.Builtins (BuiltinByteString)
import PlutusTx.Builtins.Internal (BuiltinByteString (..))

newtype Utxo = Utxo BuiltinByteString

instance FromData Utxo where
  fromBuiltinData :: BuiltinData -> Maybe Utxo
fromBuiltinData = (BuiltinByteString -> Utxo)
-> Maybe BuiltinByteString -> Maybe Utxo
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap BuiltinByteString -> Utxo
Utxo (Maybe BuiltinByteString -> Maybe Utxo)
-> (BuiltinData -> Maybe BuiltinByteString)
-> BuiltinData
-> Maybe Utxo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BuiltinData -> Maybe BuiltinByteString
forall a. FromData a => BuiltinData -> Maybe a
fromBuiltinData

instance UnsafeFromData Utxo where
  unsafeFromBuiltinData :: BuiltinData -> Utxo
unsafeFromBuiltinData = BuiltinByteString -> Utxo
Utxo (BuiltinByteString -> Utxo)
-> (BuiltinData -> BuiltinByteString) -> BuiltinData -> Utxo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BuiltinData -> BuiltinByteString
forall a. UnsafeFromData a => BuiltinData -> a
unsafeFromBuiltinData

instance ToData Utxo where
  toBuiltinData :: Utxo -> BuiltinData
toBuiltinData (Utxo BuiltinByteString
bs) = BuiltinByteString -> BuiltinData
forall a. ToData a => a -> BuiltinData
toBuiltinData BuiltinByteString
bs

fromByteString :: ByteString -> Utxo
fromByteString :: ByteString -> Utxo
fromByteString = BuiltinByteString -> Utxo
Utxo (BuiltinByteString -> Utxo)
-> (ByteString -> BuiltinByteString) -> ByteString -> Utxo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> BuiltinByteString
BuiltinByteString

toByteString :: Utxo -> ByteString
toByteString :: Utxo -> ByteString
toByteString (Utxo (BuiltinByteString ByteString
bytes)) = ByteString
bytes