{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DisambiguateRecordFields #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

-- | User-friendly pretty-printing for textual user interfaces (TUI)
module Cardano.CLI.Run.Friendly (friendlyTxBodyBS) where

import           Cardano.Prelude

import           Data.Aeson (Object, Value (..), object, toJSON, (.=))
import qualified Data.HashMap.Strict as HashMap
import           Data.Yaml.Pretty (defConfig, encodePretty, setConfCompare)

import           Cardano.Api as Api (AddressInEra (..),
                   AddressTypeInEra (ByronAddressInAnyEra, ShelleyAddressInEra), CardanoEra,
                   ShelleyBasedEra (ShelleyBasedEraAllegra, ShelleyBasedEraAlonzo, ShelleyBasedEraMary, ShelleyBasedEraShelley),
                   ShelleyEra, TxBody, serialiseAddress)
import           Cardano.Api.Byron (TxBody (ByronTxBody))
import           Cardano.Api.Shelley (TxBody (ShelleyTxBody), fromShelleyAddr)
import           Cardano.Binary (Annotated)
import qualified Cardano.Chain.UTxO as Byron
import           Cardano.Ledger.Shelley as Ledger (ShelleyEra)
import           Cardano.Ledger.ShelleyMA (MaryOrAllegra (Allegra, Mary), ShelleyMAEra)
import qualified Cardano.Ledger.ShelleyMA.TxBody as ShelleyMA
import           Cardano.Ledger.Crypto (StandardCrypto)
import           Shelley.Spec.Ledger.API (Addr (..), TxOut (TxOut))
import qualified Shelley.Spec.Ledger.API as Shelley

import           Cardano.CLI.Helpers (textShow)

friendlyTxBodyBS :: CardanoEra era -> Api.TxBody era -> ByteString
friendlyTxBodyBS :: CardanoEra era -> TxBody era -> ByteString
friendlyTxBodyBS CardanoEra era
era =
  Config -> Value -> ByteString
forall a. ToJSON a => Config -> a -> ByteString
encodePretty ((Text -> Text -> Ordering) -> Config -> Config
setConfCompare Text -> Text -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Config
defConfig) (Value -> ByteString)
-> (TxBody era -> Value) -> TxBody era -> ByteString
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. CardanoEra era -> TxBody era -> Value
forall era. CardanoEra era -> TxBody era -> Value
friendlyTxBody CardanoEra era
era

friendlyTxBody :: CardanoEra era -> Api.TxBody era -> Value
friendlyTxBody :: CardanoEra era -> TxBody era -> Value
friendlyTxBody CardanoEra era
era TxBody era
txbody =
  Object -> Value
Object (Object -> Value) -> Object -> Value
forall a b. (a -> b) -> a -> b
$
    [(Text, Value)] -> Object
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList [Text
"era" Text -> Value -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= CardanoEra era -> Value
forall a. ToJSON a => a -> Value
toJSON CardanoEra era
era]
    Object -> Object -> Object
forall a. Semigroup a => a -> a -> a
<>
    case TxBody era
txbody of
      ByronTxBody Annotated Tx ByteString
body -> Annotated Tx ByteString -> Object
friendlyTxBodyByron Annotated Tx ByteString
body
      ShelleyTxBody ShelleyBasedEra era
ShelleyBasedEraShelley TxBody (ShelleyLedgerEra era)
body [Script (ShelleyLedgerEra era)]
_scripts TxBodyScriptData era
_ Maybe (AuxiliaryData (ShelleyLedgerEra era))
aux TxScriptValidity era
_ ->
        Maybe (Metadata (ShelleyEra StandardCrypto)) -> Object -> Object
forall a. Show a => Maybe a -> Object -> Object
addAuxData Maybe (AuxiliaryData (ShelleyLedgerEra era))
Maybe (Metadata (ShelleyEra StandardCrypto))
aux (Object -> Object) -> Object -> Object
forall a b. (a -> b) -> a -> b
$ TxBody (ShelleyEra StandardCrypto) -> Object
friendlyTxBodyShelley TxBody (ShelleyLedgerEra era)
TxBody (ShelleyEra StandardCrypto)
body
      ShelleyTxBody ShelleyBasedEra era
ShelleyBasedEraAllegra TxBody (ShelleyLedgerEra era)
body [Script (ShelleyLedgerEra era)]
_scripts TxBodyScriptData era
_ Maybe (AuxiliaryData (ShelleyLedgerEra era))
aux TxScriptValidity era
_ ->
        Maybe (AuxiliaryData (ShelleyMAEra 'Allegra StandardCrypto))
-> Object -> Object
forall a. Show a => Maybe a -> Object -> Object
addAuxData Maybe (AuxiliaryData (ShelleyMAEra 'Allegra StandardCrypto))
Maybe (AuxiliaryData (ShelleyLedgerEra era))
aux (Object -> Object) -> Object -> Object
forall a b. (a -> b) -> a -> b
$ TxBody (ShelleyMAEra 'Allegra StandardCrypto) -> Object
friendlyTxBodyAllegra TxBody (ShelleyLedgerEra era)
TxBody (ShelleyMAEra 'Allegra StandardCrypto)
body
      ShelleyTxBody ShelleyBasedEra era
ShelleyBasedEraMary TxBody (ShelleyLedgerEra era)
body [Script (ShelleyLedgerEra era)]
_scripts TxBodyScriptData era
_ Maybe (AuxiliaryData (ShelleyLedgerEra era))
aux TxScriptValidity era
_ ->
        Maybe (AuxiliaryData (ShelleyMAEra 'Mary StandardCrypto))
-> Object -> Object
forall a. Show a => Maybe a -> Object -> Object
addAuxData Maybe (AuxiliaryData (ShelleyMAEra 'Mary StandardCrypto))
Maybe (AuxiliaryData (ShelleyLedgerEra era))
aux (Object -> Object) -> Object -> Object
forall a b. (a -> b) -> a -> b
$ TxBody (ShelleyMAEra 'Mary StandardCrypto) -> Object
friendlyTxBodyMary TxBody (ShelleyLedgerEra era)
TxBody (ShelleyMAEra 'Mary StandardCrypto)
body
      ShelleyTxBody ShelleyBasedEra era
ShelleyBasedEraAlonzo TxBody (ShelleyLedgerEra era)
_ [Script (ShelleyLedgerEra era)]
_ TxBodyScriptData era
_ Maybe (AuxiliaryData (ShelleyLedgerEra era))
_ TxScriptValidity era
_ ->
        Text -> Object
forall a. HasCallStack => Text -> a
panic Text
"friendlyTxBody: Alonzo not implemented yet" -- TODO alonzo

addAuxData :: Show a => Maybe a -> Object -> Object
addAuxData :: Maybe a -> Object -> Object
addAuxData = Text -> Value -> Object -> Object
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
HashMap.insert Text
"auxiliary data" (Value -> Object -> Object)
-> (Maybe a -> Value) -> Maybe a -> Object -> Object
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Value -> (a -> Value) -> Maybe a -> Value
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Value
Null (Text -> Value
forall a. ToJSON a => a -> Value
toJSON (Text -> Value) -> (a -> Text) -> a -> Value
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> Text
forall a. Show a => a -> Text
textShow)

friendlyTxBodyByron :: Annotated Byron.Tx ByteString -> Object
friendlyTxBodyByron :: Annotated Tx ByteString -> Object
friendlyTxBodyByron = HasCallStack => Value -> Object
Value -> Object
assertObject (Value -> Object)
-> (Annotated Tx ByteString -> Value)
-> Annotated Tx ByteString
-> Object
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Annotated Tx ByteString -> Value
forall a. ToJSON a => a -> Value
toJSON

friendlyTxBodyShelley
  :: Shelley.TxBody (Ledger.ShelleyEra StandardCrypto) -> Object
friendlyTxBodyShelley :: TxBody (ShelleyEra StandardCrypto) -> Object
friendlyTxBodyShelley TxBody (ShelleyEra StandardCrypto)
body =
  [(Text, Value)] -> Object
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
    [ Text
"inputs" Text -> Set (TxIn StandardCrypto) -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= TxBody (ShelleyEra StandardCrypto)
-> (Era (ShelleyEra StandardCrypto),
    FromCBOR (PParamsDelta (ShelleyEra StandardCrypto)),
    TransTxBody ToCBOR (ShelleyEra StandardCrypto)) =>
   Set (TxIn (Crypto (ShelleyEra StandardCrypto)))
forall era.
TxBody era
-> (Era era, FromCBOR (PParamsDelta era),
    TransTxBody ToCBOR era) =>
   Set (TxIn (Crypto era))
Shelley._inputs TxBody (ShelleyEra StandardCrypto)
body
    , Text
"outputs" Text -> StrictSeq Value -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= (TxOut (ShelleyEra StandardCrypto) -> Value)
-> StrictSeq (TxOut (ShelleyEra StandardCrypto)) -> StrictSeq Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap TxOut (ShelleyEra StandardCrypto) -> Value
friendlyTxOutShelley (TxBody (ShelleyEra StandardCrypto)
-> (Era (ShelleyEra StandardCrypto),
    FromCBOR (PParamsDelta (ShelleyEra StandardCrypto)),
    TransTxBody ToCBOR (ShelleyEra StandardCrypto)) =>
   StrictSeq (TxOut (ShelleyEra StandardCrypto))
forall era.
TxBody era
-> (Era era, FromCBOR (PParamsDelta era),
    TransTxBody ToCBOR era) =>
   StrictSeq (TxOut era)
Shelley._outputs TxBody (ShelleyEra StandardCrypto)
body)
    , Text
"certificates" Text -> StrictSeq Text -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= (DCert StandardCrypto -> Text)
-> StrictSeq (DCert StandardCrypto) -> StrictSeq Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap DCert StandardCrypto -> Text
forall a. Show a => a -> Text
textShow (TxBody (ShelleyEra StandardCrypto)
-> (Era (ShelleyEra StandardCrypto),
    FromCBOR (PParamsDelta (ShelleyEra StandardCrypto)),
    TransTxBody ToCBOR (ShelleyEra StandardCrypto)) =>
   StrictSeq (DCert (Crypto (ShelleyEra StandardCrypto)))
forall era.
TxBody era
-> (Era era, FromCBOR (PParamsDelta era),
    TransTxBody ToCBOR era) =>
   StrictSeq (DCert (Crypto era))
Shelley._certs TxBody (ShelleyEra StandardCrypto)
body)
    , Text
"withdrawals" Text -> Map (RewardAcnt StandardCrypto) Coin -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Wdrl StandardCrypto -> Map (RewardAcnt StandardCrypto) Coin
forall crypto. Wdrl crypto -> Map (RewardAcnt crypto) Coin
Shelley.unWdrl (TxBody (ShelleyEra StandardCrypto)
-> (Era (ShelleyEra StandardCrypto),
    FromCBOR (PParamsDelta (ShelleyEra StandardCrypto)),
    TransTxBody ToCBOR (ShelleyEra StandardCrypto)) =>
   Wdrl (Crypto (ShelleyEra StandardCrypto))
forall era.
TxBody era
-> (Era era, FromCBOR (PParamsDelta era),
    TransTxBody ToCBOR era) =>
   Wdrl (Crypto era)
Shelley._wdrls TxBody (ShelleyEra StandardCrypto)
body)
    , Text
"fee" Text -> Coin -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= TxBody (ShelleyEra StandardCrypto)
-> (Era (ShelleyEra StandardCrypto),
    FromCBOR (PParamsDelta (ShelleyEra StandardCrypto)),
    TransTxBody ToCBOR (ShelleyEra StandardCrypto)) =>
   Coin
forall era.
TxBody era
-> (Era era, FromCBOR (PParamsDelta era),
    TransTxBody ToCBOR era) =>
   Coin
Shelley._txfee TxBody (ShelleyEra StandardCrypto)
body
    , Text
"time to live" Text -> SlotNo -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= TxBody (ShelleyEra StandardCrypto)
-> (Era (ShelleyEra StandardCrypto),
    FromCBOR (PParamsDelta (ShelleyEra StandardCrypto)),
    TransTxBody ToCBOR (ShelleyEra StandardCrypto)) =>
   SlotNo
forall era.
TxBody era
-> (Era era, FromCBOR (PParamsDelta era),
    TransTxBody ToCBOR era) =>
   SlotNo
Shelley._ttl TxBody (ShelleyEra StandardCrypto)
body
    , Text
"update" Text -> StrictMaybe Text -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= (Update (ShelleyEra StandardCrypto) -> Text)
-> StrictMaybe (Update (ShelleyEra StandardCrypto))
-> StrictMaybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Update (ShelleyEra StandardCrypto) -> Text
forall a. Show a => a -> Text
textShow (TxBody (ShelleyEra StandardCrypto)
-> (Era (ShelleyEra StandardCrypto),
    FromCBOR (PParamsDelta (ShelleyEra StandardCrypto)),
    TransTxBody ToCBOR (ShelleyEra StandardCrypto)) =>
   StrictMaybe (Update (ShelleyEra StandardCrypto))
forall era.
TxBody era
-> (Era era, FromCBOR (PParamsDelta era),
    TransTxBody ToCBOR era) =>
   StrictMaybe (Update era)
Shelley._txUpdate TxBody (ShelleyEra StandardCrypto)
body)
    , Text
"metadata hash" Text -> StrictMaybe Text -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= (AuxiliaryDataHash StandardCrypto -> Text)
-> StrictMaybe (AuxiliaryDataHash StandardCrypto)
-> StrictMaybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap AuxiliaryDataHash StandardCrypto -> Text
forall a. Show a => a -> Text
textShow (TxBody (ShelleyEra StandardCrypto)
-> (Era (ShelleyEra StandardCrypto),
    FromCBOR (PParamsDelta (ShelleyEra StandardCrypto)),
    TransTxBody ToCBOR (ShelleyEra StandardCrypto)) =>
   StrictMaybe
     (AuxiliaryDataHash (Crypto (ShelleyEra StandardCrypto)))
forall era.
TxBody era
-> (Era era, FromCBOR (PParamsDelta era),
    TransTxBody ToCBOR era) =>
   StrictMaybe (AuxiliaryDataHash (Crypto era))
Shelley._mdHash TxBody (ShelleyEra StandardCrypto)
body)
    ]

friendlyTxBodyAllegra
  :: ShelleyMA.TxBody (ShelleyMAEra 'Allegra StandardCrypto) -> Object
friendlyTxBodyAllegra :: TxBody (ShelleyMAEra 'Allegra StandardCrypto) -> Object
friendlyTxBodyAllegra
  (ShelleyMA.TxBody
    Set (TxIn (Crypto (ShelleyMAEra 'Allegra StandardCrypto)))
inputs
    StrictSeq (TxOut (ShelleyMAEra 'Allegra StandardCrypto))
outputs
    StrictSeq (DCert (Crypto (ShelleyMAEra 'Allegra StandardCrypto)))
certificates
    (Shelley.Wdrl Map
  (RewardAcnt (Crypto (ShelleyMAEra 'Allegra StandardCrypto))) Coin
withdrawals)
    Coin
txfee
    ValidityInterval
validity
    StrictMaybe (Update (ShelleyMAEra 'Allegra StandardCrypto))
update
    StrictMaybe
  (AuxiliaryDataHash (Crypto (ShelleyMAEra 'Allegra StandardCrypto)))
adHash
    Value (ShelleyMAEra 'Allegra StandardCrypto)
_mint -- mint is not used in Allegra, only in Mary+
    ) =
  [(Text, Value)] -> Object
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
    [ Text
"inputs" Text -> Set (TxIn StandardCrypto) -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Set (TxIn StandardCrypto)
Set (TxIn (Crypto (ShelleyMAEra 'Allegra StandardCrypto)))
inputs
    , Text
"outputs" Text -> StrictSeq Value -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= (TxOut (ShelleyMAEra 'Allegra StandardCrypto) -> Value)
-> StrictSeq (TxOut (ShelleyMAEra 'Allegra StandardCrypto))
-> StrictSeq Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap TxOut (ShelleyMAEra 'Allegra StandardCrypto) -> Value
friendlyTxOutAllegra StrictSeq (TxOut (ShelleyMAEra 'Allegra StandardCrypto))
outputs
    , Text
"certificates" Text -> StrictSeq Text -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= (DCert StandardCrypto -> Text)
-> StrictSeq (DCert StandardCrypto) -> StrictSeq Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap DCert StandardCrypto -> Text
forall a. Show a => a -> Text
textShow StrictSeq (DCert StandardCrypto)
StrictSeq (DCert (Crypto (ShelleyMAEra 'Allegra StandardCrypto)))
certificates
    , Text
"withdrawals" Text -> Map (RewardAcnt StandardCrypto) Coin -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Map (RewardAcnt StandardCrypto) Coin
Map
  (RewardAcnt (Crypto (ShelleyMAEra 'Allegra StandardCrypto))) Coin
withdrawals
    , Text
"fee" Text -> Coin -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Coin
txfee
    , Text
"validity interval" Text -> Value -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= ValidityInterval -> Value
friendlyValidityInterval ValidityInterval
validity
    , Text
"update" Text -> StrictMaybe Text -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= (Update (ShelleyMAEra 'Allegra StandardCrypto) -> Text)
-> StrictMaybe (Update (ShelleyMAEra 'Allegra StandardCrypto))
-> StrictMaybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Update (ShelleyMAEra 'Allegra StandardCrypto) -> Text
forall a. Show a => a -> Text
textShow StrictMaybe (Update (ShelleyMAEra 'Allegra StandardCrypto))
update
    , Text
"auxiliary data hash" Text -> StrictMaybe Text -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= (AuxiliaryDataHash StandardCrypto -> Text)
-> StrictMaybe (AuxiliaryDataHash StandardCrypto)
-> StrictMaybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap AuxiliaryDataHash StandardCrypto -> Text
forall a. Show a => a -> Text
textShow StrictMaybe (AuxiliaryDataHash StandardCrypto)
StrictMaybe
  (AuxiliaryDataHash (Crypto (ShelleyMAEra 'Allegra StandardCrypto)))
adHash
    ]

friendlyTxBodyMary
  :: ShelleyMA.TxBody (ShelleyMAEra 'Mary StandardCrypto) -> Object
friendlyTxBodyMary :: TxBody (ShelleyMAEra 'Mary StandardCrypto) -> Object
friendlyTxBodyMary
  (ShelleyMA.TxBody
    Set (TxIn (Crypto (ShelleyMAEra 'Mary StandardCrypto)))
inputs
    StrictSeq (TxOut (ShelleyMAEra 'Mary StandardCrypto))
outputs
    StrictSeq (DCert (Crypto (ShelleyMAEra 'Mary StandardCrypto)))
certificates
    (Shelley.Wdrl Map (RewardAcnt (Crypto (ShelleyMAEra 'Mary StandardCrypto))) Coin
withdrawals)
    Coin
txfee
    ValidityInterval
validity
    StrictMaybe (Update (ShelleyMAEra 'Mary StandardCrypto))
update
    StrictMaybe
  (AuxiliaryDataHash (Crypto (ShelleyMAEra 'Mary StandardCrypto)))
adHash
    Value (ShelleyMAEra 'Mary StandardCrypto)
mint) =
  [(Text, Value)] -> Object
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
    [ Text
"inputs" Text -> Set (TxIn StandardCrypto) -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Set (TxIn StandardCrypto)
Set (TxIn (Crypto (ShelleyMAEra 'Mary StandardCrypto)))
inputs
    , Text
"outputs" Text -> StrictSeq Value -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= (TxOut (ShelleyMAEra 'Mary StandardCrypto) -> Value)
-> StrictSeq (TxOut (ShelleyMAEra 'Mary StandardCrypto))
-> StrictSeq Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap TxOut (ShelleyMAEra 'Mary StandardCrypto) -> Value
friendlyTxOutMary StrictSeq (TxOut (ShelleyMAEra 'Mary StandardCrypto))
outputs
    , Text
"certificates" Text -> StrictSeq Text -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= (DCert StandardCrypto -> Text)
-> StrictSeq (DCert StandardCrypto) -> StrictSeq Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap DCert StandardCrypto -> Text
forall a. Show a => a -> Text
textShow StrictSeq (DCert StandardCrypto)
StrictSeq (DCert (Crypto (ShelleyMAEra 'Mary StandardCrypto)))
certificates
    , Text
"withdrawals" Text -> Map (RewardAcnt StandardCrypto) Coin -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Map (RewardAcnt StandardCrypto) Coin
Map (RewardAcnt (Crypto (ShelleyMAEra 'Mary StandardCrypto))) Coin
withdrawals
    , Text
"fee" Text -> Coin -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Coin
txfee
    , Text
"validity interval" Text -> Value -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= ValidityInterval -> Value
friendlyValidityInterval ValidityInterval
validity
    , Text
"update" Text -> StrictMaybe Text -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= (Update (ShelleyMAEra 'Mary StandardCrypto) -> Text)
-> StrictMaybe (Update (ShelleyMAEra 'Mary StandardCrypto))
-> StrictMaybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Update (ShelleyMAEra 'Mary StandardCrypto) -> Text
forall a. Show a => a -> Text
textShow StrictMaybe (Update (ShelleyMAEra 'Mary StandardCrypto))
update
    , Text
"auxiliary data hash" Text -> StrictMaybe Text -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= (AuxiliaryDataHash StandardCrypto -> Text)
-> StrictMaybe (AuxiliaryDataHash StandardCrypto)
-> StrictMaybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap AuxiliaryDataHash StandardCrypto -> Text
forall a. Show a => a -> Text
textShow StrictMaybe (AuxiliaryDataHash StandardCrypto)
StrictMaybe
  (AuxiliaryDataHash (Crypto (ShelleyMAEra 'Mary StandardCrypto)))
adHash
    , Text
"mint" Text -> Value StandardCrypto -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Value (ShelleyMAEra 'Mary StandardCrypto)
Value StandardCrypto
mint
    ]

friendlyValidityInterval :: ShelleyMA.ValidityInterval -> Value
friendlyValidityInterval :: ValidityInterval -> Value
friendlyValidityInterval
  ShelleyMA.ValidityInterval{StrictMaybe SlotNo
invalidBefore :: ValidityInterval -> StrictMaybe SlotNo
invalidBefore :: StrictMaybe SlotNo
invalidBefore, StrictMaybe SlotNo
invalidHereafter :: ValidityInterval -> StrictMaybe SlotNo
invalidHereafter :: StrictMaybe SlotNo
invalidHereafter} =
    [(Text, Value)] -> Value
object
      [ Text
"invalid before" Text -> StrictMaybe SlotNo -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= StrictMaybe SlotNo
invalidBefore
      , Text
"invalid hereafter" Text -> StrictMaybe SlotNo -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= StrictMaybe SlotNo
invalidHereafter
      ]

friendlyTxOutShelley :: TxOut (Ledger.ShelleyEra StandardCrypto) -> Value
friendlyTxOutShelley :: TxOut (ShelleyEra StandardCrypto) -> Value
friendlyTxOutShelley (TxOut Addr (Crypto (ShelleyEra StandardCrypto))
addr Value (ShelleyEra StandardCrypto)
amount) =
  Object -> Value
Object (Object -> Value) -> Object -> Value
forall a b. (a -> b) -> a -> b
$ Text -> Value -> Object -> Object
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
HashMap.insert Text
"amount" (Coin -> Value
forall a. ToJSON a => a -> Value
toJSON Value (ShelleyEra StandardCrypto)
Coin
amount) (Object -> Object) -> Object -> Object
forall a b. (a -> b) -> a -> b
$ Addr StandardCrypto -> Object
friendlyAddress Addr StandardCrypto
Addr (Crypto (ShelleyEra StandardCrypto))
addr

friendlyTxOutAllegra :: TxOut (ShelleyMAEra 'Allegra StandardCrypto) -> Value
friendlyTxOutAllegra :: TxOut (ShelleyMAEra 'Allegra StandardCrypto) -> Value
friendlyTxOutAllegra (TxOut Addr (Crypto (ShelleyMAEra 'Allegra StandardCrypto))
addr Value (ShelleyMAEra 'Allegra StandardCrypto)
amount) =
  Object -> Value
Object (Object -> Value) -> Object -> Value
forall a b. (a -> b) -> a -> b
$ Text -> Value -> Object -> Object
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
HashMap.insert Text
"amount" (Coin -> Value
forall a. ToJSON a => a -> Value
toJSON Value (ShelleyMAEra 'Allegra StandardCrypto)
Coin
amount) (Object -> Object) -> Object -> Object
forall a b. (a -> b) -> a -> b
$ Addr StandardCrypto -> Object
friendlyAddress Addr StandardCrypto
Addr (Crypto (ShelleyMAEra 'Allegra StandardCrypto))
addr

friendlyTxOutMary :: TxOut (ShelleyMAEra 'Mary StandardCrypto) -> Value
friendlyTxOutMary :: TxOut (ShelleyMAEra 'Mary StandardCrypto) -> Value
friendlyTxOutMary (TxOut Addr (Crypto (ShelleyMAEra 'Mary StandardCrypto))
addr Value (ShelleyMAEra 'Mary StandardCrypto)
amount) =
  Object -> Value
Object (Object -> Value) -> Object -> Value
forall a b. (a -> b) -> a -> b
$ Text -> Value -> Object -> Object
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
HashMap.insert Text
"amount" (Value StandardCrypto -> Value
forall a. ToJSON a => a -> Value
toJSON Value (ShelleyMAEra 'Mary StandardCrypto)
Value StandardCrypto
amount) (Object -> Object) -> Object -> Object
forall a b. (a -> b) -> a -> b
$ Addr StandardCrypto -> Object
friendlyAddress Addr StandardCrypto
Addr (Crypto (ShelleyMAEra 'Mary StandardCrypto))
addr

friendlyAddress :: Addr StandardCrypto -> Object
friendlyAddress :: Addr StandardCrypto -> Object
friendlyAddress Addr StandardCrypto
addr =
  [(Text, Value)] -> Object
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList ([(Text, Value)] -> Object) -> [(Text, Value)] -> Object
forall a b. (a -> b) -> a -> b
$
    case Addr StandardCrypto
addr of
      Addr Network
net PaymentCredential StandardCrypto
cred StakeReference StandardCrypto
ref ->
        [ Text
"address" Text -> Value -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.=
            [(Text, Value)] -> Value
object
              [ Text
"network" Text -> Network -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Network
net
              , Text
"credential" Text -> PaymentCredential StandardCrypto -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PaymentCredential StandardCrypto
cred
              , Text
"stake reference" Text -> Text -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= StakeReference StandardCrypto -> Text
forall a. Show a => a -> Text
textShow StakeReference StandardCrypto
ref
              , Text
"Bech32" Text -> Text -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Text
addressBech32
              ]
        ]
      AddrBootstrap BootstrapAddress StandardCrypto
_ ->
        [Text
"bootstrap address" Text -> Value -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= [(Text, Value)] -> Value
object [Text
"Bech32" Text -> Value -> (Text, Value)
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Text -> Value
String Text
addressBech32]]
  where
    addressBech32 :: Text
addressBech32 =
      case Addr StandardCrypto -> AddressInEra ShelleyEra
forall era.
IsShelleyBasedEra era =>
Addr StandardCrypto -> AddressInEra era
fromShelleyAddr @Api.ShelleyEra Addr StandardCrypto
addr of
        AddressInEra (ShelleyAddressInEra ShelleyBasedEra ShelleyEra
_) Address addrtype
a -> Address addrtype -> Text
forall addr. SerialiseAddress addr => addr -> Text
serialiseAddress Address addrtype
a
        AddressInEra AddressTypeInEra addrtype ShelleyEra
ByronAddressInAnyEra Address addrtype
a -> Address addrtype -> Text
forall addr. SerialiseAddress addr => addr -> Text
serialiseAddress Address addrtype
a

assertObject :: HasCallStack => Value -> Object
assertObject :: Value -> Object
assertObject = \case
  Object Object
obj -> Object
obj
  Value
val -> Text -> Object
forall a. HasCallStack => Text -> a
panic (Text -> Object) -> Text -> Object
forall a b. (a -> b) -> a -> b
$ Text
"expected JSON Object, but got " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
typ
    where
      typ :: Text
typ =
        case Value
val of
          Array{}  -> Text
"an Array"
          Bool{}   -> Text
"a Boolean"
          Value
Null     -> Text
"Null"
          Number{} -> Text
"a Number"
          Object{} -> Text
"an Object"
          String{} -> Text
"a String"