{-# LANGUAGE DerivingVia           #-}
{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns        #-}
{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE RecordWildCards       #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{-# LANGUAGE ViewPatterns          #-}

{-# OPTIONS_GHC -Wno-orphans #-}

module Wallet.Rollup.Render(
    showBlockchain
    , showBlockchainFold
    ) where

import Cardano.Api qualified as C
import Codec.Serialise.Class (Serialise, decode, encode)
import Control.Lens.Combinators (itraverse)
import Control.Monad.Except (MonadError, throwError)
import Control.Monad.Reader
import Crypto.Hash (Digest, SHA256, digestFromByteString)
import Data.Aeson (FromJSON (parseJSON), ToJSON (toJSON))
import Data.Aeson qualified as JSON
import Data.Aeson.Extras qualified as JSON
import Data.ByteArray qualified as BA
import Data.ByteString qualified as BSS
import Data.Foldable (fold)
import Data.List (intersperse)
import Data.Map (Map)
import Data.Map qualified as Map
import Data.Set (Set)
import Data.Set qualified as Set
import Data.Text (Text)
import Data.Text qualified as Text
import Ledger (Address, Blockchain, PaymentPubKey, PaymentPubKeyHash, TxOut,
               TxOutRef (TxOutRef, txOutRefId, txOutRefIdx), txOutValue)
import Ledger.Crypto (PubKey, PubKeyHash, Signature)
import Ledger.Scripts (Datum (getDatum), Language, Script, Validator, ValidatorHash (ValidatorHash),
                       Versioned (Versioned), unValidatorScript)
import Ledger.Tx qualified as Tx
import Plutus.Script.Utils.Ada (Ada (Lovelace))
import Plutus.Script.Utils.Ada qualified as Ada
import Plutus.Script.Utils.Value (CurrencySymbol (CurrencySymbol), TokenName (TokenName), Value)
import Plutus.Script.Utils.Value qualified as Value
import Plutus.V2.Ledger.Api (TxId)
import PlutusTx qualified
import PlutusTx.AssocMap qualified as AssocMap
import PlutusTx.Prelude qualified as PlutusTx
import Prettyprinter (Doc, Pretty, defaultLayoutOptions, fill, indent, layoutPretty, line, parens, pretty, viaShow,
                      vsep, (<+>))
import Prettyprinter.Render.Text (renderStrict)
import Wallet.Emulator.Folds (EmulatorEventFold)
import Wallet.Emulator.Folds qualified as Folds
import Wallet.Emulator.Types (Wallet (Wallet))
import Wallet.Rollup (doAnnotateBlockchain)
import Wallet.Rollup.Types (AnnotatedTx (AnnotatedTx), BeneficialOwner (OwnedByPaymentPubKey, OwnedByScript),
                            DereferencedInput (DereferencedInput, InputNotFound, originalInput, refersTo),
                            SequenceId (SequenceId, slotIndex, txIndex), balances, dereferencedInputs,
                            toBeneficialOwner, tx, txId, valid)

showBlockchainFold :: [(PaymentPubKeyHash, Wallet)] -> EmulatorEventFold (Either Text Text)
showBlockchainFold :: [(PaymentPubKeyHash, Wallet)]
-> EmulatorEventFold (Either Text Text)
showBlockchainFold [(PaymentPubKeyHash, Wallet)]
walletKeys =
    let r :: a -> Either Text Text
r a
txns =
            SimpleDocStream Any -> Text
forall ann. SimpleDocStream ann -> Text
renderStrict (SimpleDocStream Any -> Text)
-> (Doc Any -> SimpleDocStream Any) -> Doc Any -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LayoutOptions -> Doc Any -> SimpleDocStream Any
forall ann. LayoutOptions -> Doc ann -> SimpleDocStream ann
layoutPretty LayoutOptions
defaultLayoutOptions
            (Doc Any -> Text) -> Either Text (Doc Any) -> Either Text Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc Any)
-> Map PaymentPubKeyHash Wallet -> Either Text (Doc Any)
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT (a -> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc Any)
forall a ann. Render a => a -> RenderM (Doc ann)
render a
txns) ([(PaymentPubKeyHash, Wallet)] -> Map PaymentPubKeyHash Wallet
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(PaymentPubKeyHash, Wallet)]
walletKeys)
    in ([[AnnotatedTx]] -> Either Text Text)
-> Fold EmulatorEvent [[AnnotatedTx]]
-> EmulatorEventFold (Either Text Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [[AnnotatedTx]] -> Either Text Text
forall a. Render a => a -> Either Text Text
r Fold EmulatorEvent [[AnnotatedTx]]
Folds.annotatedBlockchain

showBlockchain :: [(PaymentPubKeyHash, Wallet)] -> Blockchain -> Either Text Text
showBlockchain :: [(PaymentPubKeyHash, Wallet)] -> Blockchain -> Either Text Text
showBlockchain [(PaymentPubKeyHash, Wallet)]
walletKeys Blockchain
blockchain =
    (ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) Text
 -> Map PaymentPubKeyHash Wallet -> Either Text Text)
-> Map PaymentPubKeyHash Wallet
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) Text
-> Either Text Text
forall a b c. (a -> b -> c) -> b -> a -> c
flip ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) Text
-> Map PaymentPubKeyHash Wallet -> Either Text Text
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT ([(PaymentPubKeyHash, Wallet)] -> Map PaymentPubKeyHash Wallet
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(PaymentPubKeyHash, Wallet)]
walletKeys) (ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) Text
 -> Either Text Text)
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) Text
-> Either Text Text
forall a b. (a -> b) -> a -> b
$ do
        [[AnnotatedTx]]
annotatedBlockchain <- Blockchain
-> ReaderT
     (Map PaymentPubKeyHash Wallet) (Either Text) [[AnnotatedTx]]
forall (m :: * -> *). Monad m => Blockchain -> m [[AnnotatedTx]]
doAnnotateBlockchain Blockchain
blockchain
        Doc Any
doc <- [[AnnotatedTx]]
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc Any)
forall a ann. Render a => a -> RenderM (Doc ann)
render ([[AnnotatedTx]]
 -> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc Any))
-> [[AnnotatedTx]]
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc Any)
forall a b. (a -> b) -> a -> b
$ [[AnnotatedTx]] -> [[AnnotatedTx]]
forall a. [a] -> [a]
reverse [[AnnotatedTx]]
annotatedBlockchain
        Text -> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) Text
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) Text)
-> (Doc Any -> Text)
-> Doc Any
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SimpleDocStream Any -> Text
forall ann. SimpleDocStream ann -> Text
renderStrict (SimpleDocStream Any -> Text)
-> (Doc Any -> SimpleDocStream Any) -> Doc Any -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LayoutOptions -> Doc Any -> SimpleDocStream Any
forall ann. LayoutOptions -> Doc ann -> SimpleDocStream ann
layoutPretty LayoutOptions
defaultLayoutOptions (Doc Any
 -> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) Text)
-> Doc Any
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) Text
forall a b. (a -> b) -> a -> b
$ Doc Any
doc

type RenderM = ReaderT (Map PaymentPubKeyHash Wallet) (Either Text)

class Render a where
    render :: a -> RenderM (Doc ann)

newtype RenderPretty a =
    RenderPretty a

instance Pretty a => Render (RenderPretty a) where
    render :: RenderPretty a -> RenderM (Doc ann)
render (RenderPretty a
a) = Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc ann -> RenderM (Doc ann)) -> Doc ann -> RenderM (Doc ann)
forall a b. (a -> b) -> a -> b
$ a -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty a
a

instance (Render a, Render b) => Render (Either a b) where
    render :: Either a b -> RenderM (Doc ann)
render (Left a
a)  = a -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render a
a
    render (Right b
b) = b -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render b
b

instance Render [[AnnotatedTx]] where
    render :: [[AnnotatedTx]] -> RenderM (Doc ann)
render [[AnnotatedTx]]
blockchain =
        [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vsep ([Doc ann] -> Doc ann)
-> ([[Doc ann]] -> [Doc ann]) -> [[Doc ann]] -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc ann -> [Doc ann] -> [Doc ann]
forall a. a -> [a] -> [a]
intersperse Doc ann
forall a. Monoid a => a
mempty ([Doc ann] -> [Doc ann])
-> ([[Doc ann]] -> [Doc ann]) -> [[Doc ann]] -> [Doc ann]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Doc ann]] -> [Doc ann]
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold ([[Doc ann]] -> Doc ann)
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) [[Doc ann]]
-> RenderM (Doc ann)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
        (Int
 -> [AnnotatedTx]
 -> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) [Doc ann])
-> [[AnnotatedTx]]
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) [[Doc ann]]
forall i (t :: * -> *) (f :: * -> *) a b.
(TraversableWithIndex i t, Applicative f) =>
(i -> a -> f b) -> t a -> f (t b)
itraverse
            (\Int
slotIndex ->
                 (Int -> AnnotatedTx -> RenderM (Doc ann))
-> [AnnotatedTx]
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) [Doc ann]
forall i (t :: * -> *) (f :: * -> *) a b.
(TraversableWithIndex i t, Applicative f) =>
(i -> a -> f b) -> t a -> f (t b)
itraverse
                     (\Int
txIndex AnnotatedTx
tx -> do
                          Doc ann
i <- SequenceId -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render SequenceId :: Int -> Int -> SequenceId
SequenceId {Int
txIndex :: Int
slotIndex :: Int
txIndex :: Int
slotIndex :: Int
..}
                          Doc ann
v <- AnnotatedTx -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render AnnotatedTx
tx
                          Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc ann -> RenderM (Doc ann)) -> Doc ann -> RenderM (Doc ann)
forall a b. (a -> b) -> a -> b
$ [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vsep [Doc ann
"====" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ann
i Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ann
"====", Doc ann
v]))
            [[AnnotatedTx]]
blockchain

instance Render AnnotatedTx where
    render :: AnnotatedTx -> RenderM (Doc ann)
render AnnotatedTx { TxId
txId :: TxId
txId :: AnnotatedTx -> TxId
txId
                       , CardanoTx
tx :: CardanoTx
tx :: AnnotatedTx -> CardanoTx
tx
                       , [DereferencedInput]
dereferencedInputs :: [DereferencedInput]
dereferencedInputs :: AnnotatedTx -> [DereferencedInput]
dereferencedInputs
                       , Map BeneficialOwner Value
balances :: Map BeneficialOwner Value
balances :: AnnotatedTx -> Map BeneficialOwner Value
balances
                       , valid :: AnnotatedTx -> Bool
valid = Bool
True
                       } =
        [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vsep ([Doc ann] -> Doc ann)
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) [Doc ann]
-> RenderM (Doc ann)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
        [RenderM (Doc ann)]
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) [Doc ann]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence
            [ Doc ann -> TxId -> RenderM (Doc ann)
forall a ann.
Render a =>
Doc ann
-> a
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc ann)
heading Doc ann
"TxId:" TxId
txId
            , Doc ann -> Lovelace -> RenderM (Doc ann)
forall a ann.
Render a =>
Doc ann
-> a
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc ann)
heading Doc ann
"Fee:" (CardanoTx -> Lovelace
Tx.getCardanoTxFee CardanoTx
tx)
            , Doc ann -> Value -> RenderM (Doc ann)
forall a ann.
Render a =>
Doc ann
-> a
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc ann)
heading Doc ann
"Mint:" (CardanoTx -> Value
Tx.getCardanoTxMint CardanoTx
tx)
            , Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Doc ann
"Inputs:"
            , Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
indent Int
2 (Doc ann -> Doc ann) -> RenderM (Doc ann) -> RenderM (Doc ann)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Doc ann -> Doc ann -> [DereferencedInput] -> RenderM (Doc ann)
forall a ann.
Render a =>
Doc ann -> Doc ann -> [a] -> RenderM (Doc ann)
numbered Doc ann
"----" Doc ann
"Input" [DereferencedInput]
dereferencedInputs
            , Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Doc ann
forall ann. Doc ann
line
            , Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Doc ann
"Outputs:"
            , Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
indent Int
2 (Doc ann -> Doc ann) -> RenderM (Doc ann) -> RenderM (Doc ann)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Doc ann -> Doc ann -> [TxOut] -> RenderM (Doc ann)
forall a ann.
Render a =>
Doc ann -> Doc ann -> [a] -> RenderM (Doc ann)
numbered Doc ann
"----" Doc ann
"Output" (CardanoTx -> [TxOut]
Tx.getCardanoTxOutputs CardanoTx
tx)
            , Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Doc ann
forall ann. Doc ann
line
            , Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Doc ann
"Balances Carried Forward:"
            , Map BeneficialOwner Value -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
indented Map BeneficialOwner Value
balances
            ]
    render AnnotatedTx { TxId
txId :: TxId
txId :: AnnotatedTx -> TxId
txId
                       , CardanoTx
tx :: CardanoTx
tx :: AnnotatedTx -> CardanoTx
tx
                       , valid :: AnnotatedTx -> Bool
valid = Bool
False
                       } =
        [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vsep ([Doc ann] -> Doc ann)
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) [Doc ann]
-> RenderM (Doc ann)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
        [RenderM (Doc ann)]
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) [Doc ann]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence
            [ Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Doc ann
"Invalid transaction"
            , Doc ann -> TxId -> RenderM (Doc ann)
forall a ann.
Render a =>
Doc ann
-> a
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc ann)
heading Doc ann
"TxId:" TxId
txId
            , Doc ann -> Lovelace -> RenderM (Doc ann)
forall a ann.
Render a =>
Doc ann
-> a
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc ann)
heading Doc ann
"Fee:" (CardanoTx -> Lovelace
Tx.getCardanoTxFee CardanoTx
tx)
            ]

heading :: Render a => Doc ann -> a -> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc ann)
heading :: Doc ann
-> a
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc ann)
heading Doc ann
t a
x = do
    Doc ann
r <- a -> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
indented a
x
    Doc ann
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc ann
 -> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc ann))
-> Doc ann
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc ann)
forall a b. (a -> b) -> a -> b
$ Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
fill Int
10 Doc ann
t Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
r

instance Render SequenceId where
    render :: SequenceId -> RenderM (Doc ann)
render SequenceId {Int
txIndex :: Int
slotIndex :: Int
txIndex :: SequenceId -> Int
slotIndex :: SequenceId -> Int
..} =
        Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc ann -> RenderM (Doc ann)) -> Doc ann -> RenderM (Doc ann)
forall a b. (a -> b) -> a -> b
$
        Doc ann
"Slot #" Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Int -> Doc ann
forall a ann. Show a => a -> Doc ann
viaShow Int
slotIndex Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
"," Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ann
"Tx #" Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Int -> Doc ann
forall a ann. Show a => a -> Doc ann
viaShow Int
txIndex

instance Render CurrencySymbol where
    render :: CurrencySymbol -> RenderM (Doc ann)
render (CurrencySymbol BuiltinByteString
"")    = Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Doc ann
"Ada"
    render (CurrencySymbol BuiltinByteString
other) = BuiltinByteString -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render BuiltinByteString
other

instance Render TokenName where
    render :: TokenName -> RenderM (Doc ann)
render (TokenName BuiltinByteString
"") = Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Doc ann
"Lovelace"
    render TokenName
t              = Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc ann -> RenderM (Doc ann)) -> Doc ann -> RenderM (Doc ann)
forall a b. (a -> b) -> a -> b
$ String -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty (String -> Doc ann) -> String -> Doc ann
forall a b. (a -> b) -> a -> b
$ TokenName -> String
Value.toString TokenName
t

instance Render PlutusTx.BuiltinByteString where
    render :: BuiltinByteString -> RenderM (Doc ann)
render = Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc ann -> RenderM (Doc ann))
-> (BuiltinByteString -> Doc ann)
-> BuiltinByteString
-> RenderM (Doc ann)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty (Text -> Doc ann)
-> (BuiltinByteString -> Text) -> BuiltinByteString -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
JSON.encodeByteString (ByteString -> Text)
-> (BuiltinByteString -> ByteString) -> BuiltinByteString -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BuiltinByteString -> ByteString
forall arep a. FromBuiltin arep a => arep -> a
PlutusTx.fromBuiltin

deriving via RenderPretty PlutusTx.Data instance
         Render PlutusTx.Data

instance Render PlutusTx.BuiltinData where
    render :: BuiltinData -> RenderM (Doc ann)
render BuiltinData
d = Data -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render (Data -> RenderM (Doc ann)) -> Data -> RenderM (Doc ann)
forall a b. (a -> b) -> a -> b
$ BuiltinData -> Data
PlutusTx.builtinDataToData BuiltinData
d

deriving newtype instance Render Value

instance (Render k, Render v) => Render (AssocMap.Map k v) where
    render :: Map k v -> RenderM (Doc ann)
render Map k v
m
        | [(k, v)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (Map k v -> [(k, v)]
forall k v. Map k v -> [(k, v)]
AssocMap.toList Map k v
m) = Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Doc ann
"-"
        | Bool
otherwise =
            [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vsep ([Doc ann] -> Doc ann)
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) [Doc ann]
-> RenderM (Doc ann)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
            ((k, v) -> RenderM (Doc ann))
-> [(k, v)]
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) [Doc ann]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse
                (\(k
k, v
v) -> do
                     Doc ann
rk <- k -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render k
k
                     Doc ann
rv <- v -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render v
v
                     Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc ann -> RenderM (Doc ann)) -> Doc ann -> RenderM (Doc ann)
forall a b. (a -> b) -> a -> b
$ Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
fill Int
8 (Doc ann
rk Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
":") Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
indent Int
2 Doc ann
rv)
                (Map k v -> [(k, v)]
forall k v. Map k v -> [(k, v)]
AssocMap.toList Map k v
m)

instance Render (Map BeneficialOwner Value) where
    render :: Map BeneficialOwner Value -> RenderM (Doc ann)
render Map BeneficialOwner Value
xs
        | Map BeneficialOwner Value -> Bool
forall k a. Map k a -> Bool
Map.null Map BeneficialOwner Value
xs = Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Doc ann
"-"
        | Bool
otherwise = do
            [Doc ann]
entries <-
                ((BeneficialOwner, Value) -> RenderM (Doc ann))
-> [(BeneficialOwner, Value)]
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) [Doc ann]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse
                    (\(BeneficialOwner
k, Value
v) -> do
                         Doc ann
rk <- BeneficialOwner -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render BeneficialOwner
k
                         Doc ann
rv <- Value -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render Value
v
                         Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc ann -> RenderM (Doc ann)) -> Doc ann -> RenderM (Doc ann)
forall a b. (a -> b) -> a -> b
$ [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vsep [Doc ann
rk, Doc ann
"Value:", Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
indent Int
2 Doc ann
rv])
                    (Map BeneficialOwner Value -> [(BeneficialOwner, Value)]
forall k a. Map k a -> [(k, a)]
Map.toList Map BeneficialOwner Value
xs)
            Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc ann -> RenderM (Doc ann)) -> Doc ann -> RenderM (Doc ann)
forall a b. (a -> b) -> a -> b
$ [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vsep ([Doc ann] -> Doc ann) -> [Doc ann] -> Doc ann
forall a b. (a -> b) -> a -> b
$ Doc ann -> [Doc ann] -> [Doc ann]
forall a. a -> [a] -> [a]
intersperse Doc ann
forall a. Monoid a => a
mempty [Doc ann]
entries

instance Render (Map PubKey Signature) where
    render :: Map PubKey Signature -> RenderM (Doc ann)
render Map PubKey Signature
xs
        | Map PubKey Signature -> Bool
forall k a. Map k a -> Bool
Map.null Map PubKey Signature
xs = Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Doc ann
"-"
        | Bool
otherwise = do
            [Doc ann]
entries <-
                ((PubKey, Signature) -> RenderM (Doc ann))
-> [(PubKey, Signature)]
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) [Doc ann]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse
                    (\(PubKey
k, Signature
v) -> do
                         Doc ann
rk <- PubKey -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render PubKey
k
                         Doc ann
rv <- Signature -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render Signature
v
                         Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc ann -> RenderM (Doc ann)) -> Doc ann -> RenderM (Doc ann)
forall a b. (a -> b) -> a -> b
$ [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vsep [Doc ann
rk, Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
indent Int
2 Doc ann
rv])
                    (Map PubKey Signature -> [(PubKey, Signature)]
forall k a. Map k a -> [(k, a)]
Map.toList Map PubKey Signature
xs)
            Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc ann -> RenderM (Doc ann)) -> Doc ann -> RenderM (Doc ann)
forall a b. (a -> b) -> a -> b
$ [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vsep ([Doc ann] -> Doc ann) -> [Doc ann] -> Doc ann
forall a b. (a -> b) -> a -> b
$ Doc ann -> [Doc ann] -> [Doc ann]
forall a. a -> [a] -> [a]
intersperse Doc ann
forall a. Monoid a => a
mempty [Doc ann]
entries

deriving via RenderPretty Text instance Render Text

deriving via RenderPretty String instance Render String

deriving via RenderPretty Integer instance Render Integer

deriving via RenderPretty Word instance Render Word

deriving via RenderPretty Address instance Render Address

deriving via RenderPretty C.Value instance Render C.Value

deriving via RenderPretty C.Lovelace instance Render C.Lovelace

instance Render Wallet where
    render :: Wallet -> RenderM (Doc ann)
render (Wallet Maybe String
_ WalletId
n) = Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc ann -> RenderM (Doc ann)) -> Doc ann -> RenderM (Doc ann)
forall a b. (a -> b) -> a -> b
$ Doc ann
"Wallet" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> WalletId -> Doc ann
forall a ann. Show a => a -> Doc ann
viaShow WalletId
n

instance Render BeneficialOwner where
    render :: BeneficialOwner -> RenderM (Doc ann)
render (OwnedByScript ValidatorHash
address) = (Doc ann
"Script:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+>) (Doc ann -> Doc ann) -> RenderM (Doc ann) -> RenderM (Doc ann)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ValidatorHash -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render ValidatorHash
address
    render (OwnedByPaymentPubKey PaymentPubKeyHash
pkh) = do
        Map PaymentPubKeyHash Wallet
walletKeys <- ReaderT
  (Map PaymentPubKeyHash Wallet)
  (Either Text)
  (Map PaymentPubKeyHash Wallet)
forall r (m :: * -> *). MonadReader r m => m r
ask
        Wallet
wallet <- PaymentPubKeyHash
-> Map PaymentPubKeyHash Wallet
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) Wallet
forall (m :: * -> *).
MonadError Text m =>
PaymentPubKeyHash -> Map PaymentPubKeyHash Wallet -> m Wallet
lookupWallet PaymentPubKeyHash
pkh Map PaymentPubKeyHash Wallet
walletKeys
        Doc ann
w <- Wallet -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render Wallet
wallet
        Doc ann
p <- PaymentPubKeyHash -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render PaymentPubKeyHash
pkh
        Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc ann -> RenderM (Doc ann)) -> Doc ann -> RenderM (Doc ann)
forall a b. (a -> b) -> a -> b
$ Doc ann
p Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
parens Doc ann
w

instance Render Ada where
    render :: Ada -> RenderM (Doc ann)
render ada :: Ada
ada@(Lovelace Integer
l)
        | Ada -> Bool
Ada.isZero Ada
ada = Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Doc ann
"-"
        | Bool
otherwise = Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Integer -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty Integer
l)

instance Render (Digest SHA256) where
    render :: Digest SHA256 -> RenderM (Doc ann)
render = Text -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render (Text -> RenderM (Doc ann))
-> (Digest SHA256 -> Text) -> Digest SHA256 -> RenderM (Doc ann)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Text -> Text
abbreviate Int
40 (Text -> Text) -> (Digest SHA256 -> Text) -> Digest SHA256 -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Digest SHA256 -> Text
forall a. Serialise a => a -> Text
JSON.encodeSerialise

deriving via RenderPretty TxId instance Render TxId

deriving via RenderPretty C.TxId instance Render C.TxId

instance Render PubKey where
    render :: PubKey -> RenderM (Doc ann)
render PubKey
pubKey =
        Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc ann -> RenderM (Doc ann)) -> Doc ann -> RenderM (Doc ann)
forall a b. (a -> b) -> a -> b
$
        let v :: Text
v = String -> Text
Text.pack (Doc Any -> String
forall a. Show a => a -> String
show (PubKey -> Doc Any
forall a ann. Pretty a => a -> Doc ann
pretty PubKey
pubKey))
         in Doc ann
"PubKey:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Text -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty (Int -> Text -> Text
abbreviate Int
40 Text
v)

instance Render PubKeyHash where
    render :: PubKeyHash -> RenderM (Doc ann)
render PubKeyHash
pkh =
        Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc ann -> RenderM (Doc ann)) -> Doc ann -> RenderM (Doc ann)
forall a b. (a -> b) -> a -> b
$
        let v :: Text
v = String -> Text
Text.pack (Doc Any -> String
forall a. Show a => a -> String
show (PubKeyHash -> Doc Any
forall a ann. Pretty a => a -> Doc ann
pretty PubKeyHash
pkh))
         in Doc ann
"PubKeyHash:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Text -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty (Int -> Text -> Text
abbreviate Int
40 Text
v)

instance Render PaymentPubKey where
    render :: PaymentPubKey -> RenderM (Doc ann)
render PaymentPubKey
pubKey =
        Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc ann -> RenderM (Doc ann)) -> Doc ann -> RenderM (Doc ann)
forall a b. (a -> b) -> a -> b
$
        let v :: Text
v = String -> Text
Text.pack (Doc Any -> String
forall a. Show a => a -> String
show (PaymentPubKey -> Doc Any
forall a ann. Pretty a => a -> Doc ann
pretty PaymentPubKey
pubKey))
         in Doc ann
"PaymentPubKey:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Text -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty (Int -> Text -> Text
abbreviate Int
40 Text
v)

instance Render PaymentPubKeyHash where
    render :: PaymentPubKeyHash -> RenderM (Doc ann)
render PaymentPubKeyHash
pkh =
        Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc ann -> RenderM (Doc ann)) -> Doc ann -> RenderM (Doc ann)
forall a b. (a -> b) -> a -> b
$
        let v :: Text
v = String -> Text
Text.pack (Doc Any -> String
forall a. Show a => a -> String
show (PaymentPubKeyHash -> Doc Any
forall a ann. Pretty a => a -> Doc ann
pretty PaymentPubKeyHash
pkh))
         in Doc ann
"PaymentPubKeyHash:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Text -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty (Int -> Text -> Text
abbreviate Int
40 Text
v)

instance Render Signature where
    render :: Signature -> RenderM (Doc ann)
render Signature
sig =
        Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc ann -> RenderM (Doc ann)) -> Doc ann -> RenderM (Doc ann)
forall a b. (a -> b) -> a -> b
$
        let v :: Text
v = Signature -> Text
forall a. Serialise a => a -> Text
JSON.encodeSerialise Signature
sig
         in Doc ann
"Signature:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Text -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty (Int -> Text -> Text
abbreviate Int
40 Text
v)

instance Render Script where
    render :: Script -> RenderM (Doc ann)
render Script
script =
        Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc ann -> RenderM (Doc ann)) -> Doc ann -> RenderM (Doc ann)
forall a b. (a -> b) -> a -> b
$
        let v :: Text
v = Script -> Text
forall a. Serialise a => a -> Text
JSON.encodeSerialise Script
script
         in Doc ann
"Script:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Text -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty (Int -> Text -> Text
abbreviate Int
40 Text
v)

instance Render Validator where
    render :: Validator -> RenderM (Doc ann)
render = Script -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render (Script -> RenderM (Doc ann))
-> (Validator -> Script) -> Validator -> RenderM (Doc ann)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Validator -> Script
unValidatorScript

deriving newtype instance Render ValidatorHash

instance Render Datum where
    render :: Datum -> RenderM (Doc ann)
render = BuiltinData -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render (BuiltinData -> RenderM (Doc ann))
-> (Datum -> BuiltinData) -> Datum -> RenderM (Doc ann)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Datum -> BuiltinData
getDatum

instance Render a => Render (Set a) where
    render :: Set a -> RenderM (Doc ann)
render Set a
xs = [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vsep ([Doc ann] -> Doc ann)
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) [Doc ann]
-> RenderM (Doc ann)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> RenderM (Doc ann))
-> [a]
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) [Doc ann]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render (Set a -> [a]
forall a. Set a -> [a]
Set.toList Set a
xs)

instance Render DereferencedInput where
    render :: DereferencedInput -> RenderM (Doc ann)
render (InputNotFound TxIn
txKey) = Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc ann -> RenderM (Doc ann)) -> Doc ann -> RenderM (Doc ann)
forall a b. (a -> b) -> a -> b
$ Doc ann
"Input not found:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> TxIn -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty TxIn
txKey
    render DereferencedInput {TxIn
originalInput :: TxIn
originalInput :: DereferencedInput -> TxIn
originalInput, TxOut
refersTo :: TxOut
refersTo :: DereferencedInput -> TxOut
refersTo} =
        [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vsep ([Doc ann] -> Doc ann)
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) [Doc ann]
-> RenderM (Doc ann)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
        [RenderM (Doc ann)]
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) [Doc ann]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence
            [TxOut -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render TxOut
refersTo, Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Doc ann
"Source:", Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
indent Int
2 (Doc ann -> Doc ann) -> RenderM (Doc ann) -> RenderM (Doc ann)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TxIn -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render TxIn
originalInput]

instance Render a => Render (Versioned a) where
    render :: Versioned a -> RenderM (Doc ann)
render (Versioned a
a Language
lang) = do
        Doc ann
rlang <- Language -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render Language
lang
        Doc ann
ra <- a -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render a
a
        Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc ann -> RenderM (Doc ann)) -> Doc ann -> RenderM (Doc ann)
forall a b. (a -> b) -> a -> b
$ Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
parens Doc ann
rlang Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ann
ra

instance Render Language where
    render :: Language -> RenderM (Doc ann)
render = Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc ann -> RenderM (Doc ann))
-> (Language -> Doc ann) -> Language -> RenderM (Doc ann)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Language -> Doc ann
forall a ann. Show a => a -> Doc ann
viaShow

instance Render C.TxIn where
    render :: TxIn -> RenderM (Doc ann)
render (C.TxIn TxId
txId (C.TxIx Word
txIx)) =
        [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vsep ([Doc ann] -> Doc ann)
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) [Doc ann]
-> RenderM (Doc ann)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
        [RenderM (Doc ann)]
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) [Doc ann]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence [Doc ann -> TxId -> RenderM (Doc ann)
forall a ann.
Render a =>
Doc ann
-> a
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc ann)
heading' Doc ann
"Tx:" TxId
txId, Doc ann -> Word -> RenderM (Doc ann)
forall a ann.
Render a =>
Doc ann
-> a
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc ann)
heading' Doc ann
"Output #" Word
txIx]
      where
        heading' :: Doc ann
-> a
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc ann)
heading' Doc ann
t a
x = do
            Doc ann
r <- a -> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render a
x
            Doc ann
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc ann
 -> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc ann))
-> Doc ann
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc ann)
forall a b. (a -> b) -> a -> b
$ Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
fill Int
8 Doc ann
t Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
r

instance Render TxOutRef where
    render :: TxOutRef -> RenderM (Doc ann)
render TxOutRef {TxId
txOutRefId :: TxId
txOutRefId :: TxOutRef -> TxId
txOutRefId, Integer
txOutRefIdx :: Integer
txOutRefIdx :: TxOutRef -> Integer
txOutRefIdx} =
        [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vsep ([Doc ann] -> Doc ann)
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) [Doc ann]
-> RenderM (Doc ann)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
        [RenderM (Doc ann)]
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) [Doc ann]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence [Doc ann -> TxId -> RenderM (Doc ann)
forall a ann.
Render a =>
Doc ann
-> a
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc ann)
heading' Doc ann
"Tx:" TxId
txOutRefId, Doc ann -> Integer -> RenderM (Doc ann)
forall a ann.
Render a =>
Doc ann
-> a
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc ann)
heading' Doc ann
"Output #" Integer
txOutRefIdx]
      where
        heading' :: Doc ann
-> a
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc ann)
heading' Doc ann
t a
x = do
            Doc ann
r <- a -> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render a
x
            Doc ann
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc ann
 -> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc ann))
-> Doc ann
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) (Doc ann)
forall a b. (a -> b) -> a -> b
$ Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
fill Int
8 Doc ann
t Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
r

instance Render TxOut where
    render :: TxOut -> RenderM (Doc ann)
render TxOut
txOut =
        [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vsep ([Doc ann] -> Doc ann)
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) [Doc ann]
-> RenderM (Doc ann)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
        [RenderM (Doc ann)]
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) [Doc ann]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence
            [ Doc ann -> Doc ann -> Doc ann
forall a. Monoid a => a -> a -> a
mappend Doc ann
"Destination:" (Doc ann -> Doc ann) -> (Doc ann -> Doc ann) -> Doc ann -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
indent Int
2 (Doc ann -> Doc ann) -> RenderM (Doc ann) -> RenderM (Doc ann)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
              BeneficialOwner -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render (TxOut -> BeneficialOwner
toBeneficialOwner TxOut
txOut)
            , Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Doc ann
"Value:"
            , Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
indent Int
2 (Doc ann -> Doc ann) -> RenderM (Doc ann) -> RenderM (Doc ann)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render (TxOut -> Value
txOutValue TxOut
txOut)
            ]

------------------------------------------------------------
indented :: Render a => a -> RenderM (Doc ann)
indented :: a -> RenderM (Doc ann)
indented a
x = Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
indent Int
2 (Doc ann -> Doc ann) -> RenderM (Doc ann) -> RenderM (Doc ann)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render a
x

numbered :: Render a => Doc ann -> Doc ann -> [a] -> RenderM (Doc ann)
numbered :: Doc ann -> Doc ann -> [a] -> RenderM (Doc ann)
numbered Doc ann
separator Doc ann
title [a]
xs =
    [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vsep ([Doc ann] -> Doc ann)
-> ([Doc ann] -> [Doc ann]) -> [Doc ann] -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc ann -> [Doc ann] -> [Doc ann]
forall a. a -> [a] -> [a]
intersperse Doc ann
forall a. Monoid a => a
mempty ([Doc ann] -> Doc ann)
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) [Doc ann]
-> RenderM (Doc ann)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Int -> a -> RenderM (Doc ann))
-> [a]
-> ReaderT (Map PaymentPubKeyHash Wallet) (Either Text) [Doc ann]
forall i (t :: * -> *) (f :: * -> *) a b.
(TraversableWithIndex i t, Applicative f) =>
(i -> a -> f b) -> t a -> f (t b)
itraverse Int -> a -> RenderM (Doc ann)
forall a a. (Render a, Show a) => a -> a -> RenderM (Doc ann)
numberedEntry [a]
xs
  where
    numberedEntry :: a -> a -> RenderM (Doc ann)
numberedEntry a
index a
x = do
        Doc ann
v <- a -> RenderM (Doc ann)
forall a ann. Render a => a -> RenderM (Doc ann)
render a
x
        Doc ann -> RenderM (Doc ann)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc ann -> RenderM (Doc ann)) -> Doc ann -> RenderM (Doc ann)
forall a b. (a -> b) -> a -> b
$ [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vsep [Doc ann
separator Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ann
title Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> a -> Doc ann
forall a ann. Show a => a -> Doc ann
viaShow a
index Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ann
separator, Doc ann
v]

------------------------------------------------------------
lookupWallet ::
       MonadError Text m => PaymentPubKeyHash -> Map PaymentPubKeyHash Wallet -> m Wallet
lookupWallet :: PaymentPubKeyHash -> Map PaymentPubKeyHash Wallet -> m Wallet
lookupWallet PaymentPubKeyHash
pkh (PaymentPubKeyHash -> Map PaymentPubKeyHash Wallet -> Maybe Wallet
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup PaymentPubKeyHash
pkh -> Just Wallet
wallet) = Wallet -> m Wallet
forall (f :: * -> *) a. Applicative f => a -> f a
pure Wallet
wallet
lookupWallet PaymentPubKeyHash
pkh Map PaymentPubKeyHash Wallet
_ =
    Text -> m Wallet
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Text -> m Wallet) -> Text -> m Wallet
forall a b. (a -> b) -> a -> b
$
    Text
"Could not find referenced PubKeyHash: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack (PaymentPubKeyHash -> String
forall a. Show a => a -> String
show PaymentPubKeyHash
pkh)

abbreviate :: Int -> Text -> Text
abbreviate :: Int -> Text -> Text
abbreviate Int
n Text
t =
    let prefix :: Text
prefix = Int -> Text -> Text
Text.take Int
n Text
t
     in if Text
prefix Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
t
            then Text
t
            else Text
prefix Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"..."

{- Note [Serialising Digests from Crypto.Hash]
This is more complicated than you might expect.  If you say
`encode = encode . BA.unpack` then the contents of the digest are
unpacked into a `Word8` list with 32 entries.  However, when cborg
serialises a list, every element in the output is preceded by a type
tag (in this case, 24), and this means that the serialised version is
about 64 bytes long, twice the length of the original data.  Packing
the `Word8` list into a `ByteString` first fixes this because cborg
just serialises it as a sequence of contiguous bytes. -}

instance Serialise (Digest SHA256) where
    encode :: Digest SHA256 -> Encoding
encode = ByteString -> Encoding
forall a. Serialise a => a -> Encoding
encode (ByteString -> Encoding)
-> (Digest SHA256 -> ByteString) -> Digest SHA256 -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Word8] -> ByteString
BSS.pack ([Word8] -> ByteString)
-> (Digest SHA256 -> [Word8]) -> Digest SHA256 -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Digest SHA256 -> [Word8]
forall a. ByteArrayAccess a => a -> [Word8]
BA.unpack
    decode :: Decoder s (Digest SHA256)
decode = do
      ByteString
d :: BSS.ByteString <- Decoder s ByteString
forall a s. Serialise a => Decoder s a
decode
      let Bytes
bs :: BA.Bytes = [Word8] -> Bytes
forall a. ByteArray a => [Word8] -> a
BA.pack ([Word8] -> Bytes)
-> (ByteString -> [Word8]) -> ByteString -> Bytes
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [Word8]
BSS.unpack (ByteString -> Bytes) -> ByteString -> Bytes
forall a b. (a -> b) -> a -> b
$ ByteString
d
      case Bytes -> Maybe (Digest SHA256)
forall a ba.
(HashAlgorithm a, ByteArrayAccess ba) =>
ba -> Maybe (Digest a)
digestFromByteString Bytes
bs of
        Maybe (Digest SHA256)
Nothing -> String -> Decoder s (Digest SHA256)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s (Digest SHA256))
-> String -> Decoder s (Digest SHA256)
forall a b. (a -> b) -> a -> b
$ String
"Couldn't decode SHA256 Digest: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ ByteString -> String
forall a. Show a => a -> String
show ByteString
d
        Just Digest SHA256
v  -> Digest SHA256 -> Decoder s (Digest SHA256)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Digest SHA256
v

instance ToJSON (Digest SHA256) where
    toJSON :: Digest SHA256 -> Value
toJSON = Text -> Value
JSON.String (Text -> Value)
-> (Digest SHA256 -> Text) -> Digest SHA256 -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Digest SHA256 -> Text
forall a. Serialise a => a -> Text
JSON.encodeSerialise

instance FromJSON (Digest SHA256) where
    parseJSON :: Value -> Parser (Digest SHA256)
parseJSON = Value -> Parser (Digest SHA256)
forall a. Serialise a => Value -> Parser a
JSON.decodeSerialise