{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}

{-# OPTIONS_GHC -Wno-orphans #-}
{-# OPTIONS_GHC -Wno-unticked-promoted-constructors #-}

module Cardano.Api.Orphans () where

import           Prelude

import           Data.Aeson (FromJSON (..), ToJSON (..), object, (.!=), (.:), (.:?), (.=))
import qualified Data.Aeson as Aeson
import           Data.Aeson.Types (FromJSONKey (..), ToJSONKey (..), toJSONKeyText)
import qualified Data.ByteString.Base16 as B16
import qualified Data.Map.Strict as Map
import           Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text

import           Control.Applicative
import           Control.Iterate.SetAlgebra (BiMap (..), Bimap)

import           Cardano.Api.Json
import           Cardano.Ledger.BaseTypes (StrictMaybe (..), strictMaybeToMaybe)
import qualified Cardano.Ledger.BaseTypes as Ledger
import           Cardano.Ledger.Crypto (StandardCrypto)
import           Cardano.Slotting.Slot (SlotNo (..))

import qualified Cardano.Crypto.Hash.Class as Crypto
import qualified Cardano.Ledger.Alonzo as Alonzo
import qualified Cardano.Ledger.Alonzo.Genesis as Alonzo
import qualified Cardano.Ledger.Alonzo.Language as Alonzo
import qualified Cardano.Ledger.Alonzo.PParams as Alonzo
import qualified Cardano.Ledger.Alonzo.Scripts as Alonzo
import qualified Cardano.Ledger.Alonzo.TxBody as Alonzo
import qualified Cardano.Ledger.Coin as Shelley
import qualified Cardano.Ledger.Core as Core
import qualified Cardano.Ledger.Core as Ledger
import qualified Cardano.Ledger.Crypto as Crypto
import qualified Cardano.Ledger.Era as Ledger
import qualified Cardano.Ledger.Mary.Value as Mary
import qualified Cardano.Ledger.SafeHash as SafeHash
import qualified Cardano.Ledger.Shelley.Constraints as Shelley
import qualified Ouroboros.Consensus.Shelley.Eras as Consensus
import qualified Shelley.Spec.Ledger.API as Shelley
import qualified Shelley.Spec.Ledger.Delegation.Certificates as Shelley
import qualified Shelley.Spec.Ledger.EpochBoundary as ShelleyEpoch
import qualified Shelley.Spec.Ledger.LedgerState as ShelleyLedger
import           Shelley.Spec.Ledger.PParams (PParamsUpdate)
import qualified Shelley.Spec.Ledger.RewardUpdate as Shelley
import qualified Shelley.Spec.Ledger.Rewards as Shelley

import           Plutus.V1.Ledger.Api (defaultCostModelParams)

-- Orphan instances involved in the JSON output of the API queries.
-- We will remove/replace these as we provide more API wrapper types

instance ToJSON (Mary.Value era) where
  toJSON :: Value era -> Value
toJSON (Mary.Value Integer
l Map (PolicyID era) (Map AssetName Integer)
ps) =
    [Pair] -> Value
object
      [ Text
"lovelace" Text -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Integer -> Value
forall a. ToJSON a => a -> Value
toJSON Integer
l
      , Text
"policies" Text -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Map (PolicyID era) (Map AssetName Integer) -> Value
forall a. ToJSON a => a -> Value
toJSON Map (PolicyID era) (Map AssetName Integer)
ps
      ]

instance ToJSONKey Mary.AssetName where
  toJSONKey :: ToJSONKeyFunction AssetName
toJSONKey = (AssetName -> Text) -> ToJSONKeyFunction AssetName
forall a. (a -> Text) -> ToJSONKeyFunction a
toJSONKeyText AssetName -> Text
render
    where
      render :: AssetName -> Text
render = ByteString -> Text
Text.decodeLatin1 (ByteString -> Text)
-> (AssetName -> ByteString) -> AssetName -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
B16.encode (ByteString -> ByteString)
-> (AssetName -> ByteString) -> AssetName -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AssetName -> ByteString
Mary.assetName

instance ToJSON (Mary.PolicyID era) where
  toJSON :: PolicyID era -> Value
toJSON (Mary.PolicyID (Shelley.ScriptHash Hash (ADDRHASH era) EraIndependentScript
h)) = Text -> Value
Aeson.String (Hash (ADDRHASH era) EraIndependentScript -> Text
forall crypto a. Hash crypto a -> Text
hashToText Hash (ADDRHASH era) EraIndependentScript
h)

instance ToJSONKey (Mary.PolicyID era) where
  toJSONKey :: ToJSONKeyFunction (PolicyID era)
toJSONKey = (PolicyID era -> Text) -> ToJSONKeyFunction (PolicyID era)
forall a. (a -> Text) -> ToJSONKeyFunction a
toJSONKeyText PolicyID era -> Text
forall crypto. PolicyID crypto -> Text
render
    where
      render :: PolicyID crypto -> Text
render (Mary.PolicyID (Shelley.ScriptHash Hash (ADDRHASH crypto) EraIndependentScript
h)) = Hash (ADDRHASH crypto) EraIndependentScript -> Text
forall crypto a. Hash crypto a -> Text
hashToText Hash (ADDRHASH crypto) EraIndependentScript
h

instance ToJSON Mary.AssetName where
  toJSON :: AssetName -> Value
toJSON = Text -> Value
Aeson.String (Text -> Value) -> (AssetName -> Text) -> AssetName -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
Text.decodeLatin1 (ByteString -> Text)
-> (AssetName -> ByteString) -> AssetName -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
B16.encode (ByteString -> ByteString)
-> (AssetName -> ByteString) -> AssetName -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AssetName -> ByteString
Mary.assetName

instance ToJSON Shelley.AccountState where
  toJSON :: AccountState -> Value
toJSON (Shelley.AccountState Coin
tr Coin
rs) = [Pair] -> Value
object [ Text
"treasury" Text -> Coin -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Coin
tr
                                               , Text
"reserves" Text -> Coin -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Coin
rs
                                               ]

instance ( Consensus.ShelleyBasedEra era
         , ToJSON (Core.TxOut era)
         , ToJSON (Core.PParams era)
         , ToJSON (Core.PParamsDelta era)
         ) => ToJSON (Shelley.EpochState era) where
  toJSON :: EpochState era -> Value
toJSON EpochState era
eState = [Pair] -> Value
object [ Text
"esAccountState" Text -> AccountState -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= EpochState era -> AccountState
forall era. EpochState era -> AccountState
Shelley.esAccountState EpochState era
eState
                         , Text
"esSnapshots" Text -> SnapShots (Crypto era) -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= EpochState era -> SnapShots (Crypto era)
forall era. EpochState era -> SnapShots (Crypto era)
Shelley.esSnapshots EpochState era
eState
                         , Text
"esLState" Text -> LedgerState era -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= EpochState era -> LedgerState era
forall era. EpochState era -> LedgerState era
Shelley.esLState EpochState era
eState
                         , Text
"esPrevPp" Text -> PParams era -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= EpochState era -> PParams era
forall era. EpochState era -> PParams era
Shelley.esPrevPp EpochState era
eState
                         , Text
"esPp" Text -> PParams era -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= EpochState era -> PParams era
forall era. EpochState era -> PParams era
Shelley.esPp EpochState era
eState
                         , Text
"esNonMyopic" Text -> NonMyopic (Crypto era) -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= EpochState era -> NonMyopic (Crypto era)
forall era. EpochState era -> NonMyopic (Crypto era)
Shelley.esNonMyopic EpochState era
eState
                         ]

instance ( Consensus.ShelleyBasedEra era
         , ToJSON (Core.TxOut era)
         , ToJSON (Core.PParamsDelta era)
         ) => ToJSON (Shelley.LedgerState era) where
  toJSON :: LedgerState era -> Value
toJSON LedgerState era
lState = [Pair] -> Value
object [ Text
"utxoState" Text -> UTxOState era -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= LedgerState era -> UTxOState era
forall era. LedgerState era -> UTxOState era
Shelley._utxoState LedgerState era
lState
                         , Text
"delegationState" Text -> DPState (Crypto era) -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= LedgerState era -> DPState (Crypto era)
forall era. LedgerState era -> DPState (Crypto era)
Shelley._delegationState LedgerState era
lState
                         ]

instance ( Consensus.ShelleyBasedEra era
         , ToJSON (Core.TxOut era)
         , ToJSON (Core.PParamsDelta era)
         ) => ToJSON (Shelley.UTxOState era) where
  toJSON :: UTxOState era -> Value
toJSON UTxOState era
utxoState = [Pair] -> Value
object [ Text
"utxo" Text -> UTxO era -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= UTxOState era -> UTxO era
forall era. UTxOState era -> UTxO era
Shelley._utxo UTxOState era
utxoState
                            , Text
"deposited" Text -> Coin -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= UTxOState era -> Coin
forall era. UTxOState era -> Coin
Shelley._deposited UTxOState era
utxoState
                            , Text
"fees" Text -> Coin -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= UTxOState era -> Coin
forall era. UTxOState era -> Coin
Shelley._fees UTxOState era
utxoState
                            , Text
"ppups" Text -> PPUPState era -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= UTxOState era -> State (EraRule "PPUP" era)
forall era. UTxOState era -> State (EraRule "PPUP" era)
Shelley._ppups UTxOState era
utxoState
                            ]

instance ( ToJSON (Core.PParamsDelta era)
         , Shelley.UsesPParams era
         ) => ToJSON (Shelley.PPUPState era) where
  toJSON :: PPUPState era -> Value
toJSON PPUPState era
ppUpState = [Pair] -> Value
object [ Text
"proposals" Text -> ProposedPPUpdates era -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PPUPState era -> ProposedPPUpdates era
forall era. PPUPState era -> ProposedPPUpdates era
Shelley.proposals PPUPState era
ppUpState
                            , Text
"futureProposals" Text -> ProposedPPUpdates era -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PPUPState era -> ProposedPPUpdates era
forall era. PPUPState era -> ProposedPPUpdates era
Shelley.futureProposals PPUPState era
ppUpState
                            ]

instance ( ToJSON (Core.PParamsDelta era)
         , Shelley.UsesPParams era
         ) => ToJSON (Shelley.ProposedPPUpdates era) where
  toJSON :: ProposedPPUpdates era -> Value
toJSON (Shelley.ProposedPPUpdates Map (KeyHash 'Genesis (Crypto era)) (PParamsDelta era)
ppUpdates) = [(KeyHash 'Genesis (Crypto era), PParamsDelta era)] -> Value
forall a. ToJSON a => a -> Value
toJSON ([(KeyHash 'Genesis (Crypto era), PParamsDelta era)] -> Value)
-> [(KeyHash 'Genesis (Crypto era), PParamsDelta era)] -> Value
forall a b. (a -> b) -> a -> b
$ Map (KeyHash 'Genesis (Crypto era)) (PParamsDelta era)
-> [(KeyHash 'Genesis (Crypto era), PParamsDelta era)]
forall k a. Map k a -> [(k, a)]
Map.toList Map (KeyHash 'Genesis (Crypto era)) (PParamsDelta era)
ppUpdates

instance ToJSON (PParamsUpdate era) where
  toJSON :: PParamsUpdate era -> Value
toJSON PParamsUpdate era
pp =
    [Pair] -> Value
Aeson.object ([Pair] -> Value) -> [Pair] -> Value
forall a b. (a -> b) -> a -> b
$
        [ Text
"minFeeA"               Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Natural
x | Natural
x <- StrictMaybe Natural -> [Natural]
forall a. StrictMaybe a -> [a]
mbfield (PParamsUpdate era -> HKD StrictMaybe Natural
forall (f :: * -> *) era. PParams' f era -> HKD f Natural
Shelley._minfeeA PParamsUpdate era
pp) ]
     [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++ [ Text
"minFeeB"               Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Natural
x | Natural
x <- StrictMaybe Natural -> [Natural]
forall a. StrictMaybe a -> [a]
mbfield (PParamsUpdate era -> HKD StrictMaybe Natural
forall (f :: * -> *) era. PParams' f era -> HKD f Natural
Shelley._minfeeB PParamsUpdate era
pp) ]
     [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++ [ Text
"maxBlockBodySize"      Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Natural
x | Natural
x <- StrictMaybe Natural -> [Natural]
forall a. StrictMaybe a -> [a]
mbfield (PParamsUpdate era -> HKD StrictMaybe Natural
forall (f :: * -> *) era. PParams' f era -> HKD f Natural
Shelley._maxBBSize PParamsUpdate era
pp) ]
     [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++ [ Text
"maxTxSize"             Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Natural
x | Natural
x <- StrictMaybe Natural -> [Natural]
forall a. StrictMaybe a -> [a]
mbfield (PParamsUpdate era -> HKD StrictMaybe Natural
forall (f :: * -> *) era. PParams' f era -> HKD f Natural
Shelley._maxTxSize PParamsUpdate era
pp) ]
     [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++ [ Text
"maxBlockHeaderSize"    Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Natural
x | Natural
x <- StrictMaybe Natural -> [Natural]
forall a. StrictMaybe a -> [a]
mbfield (PParamsUpdate era -> HKD StrictMaybe Natural
forall (f :: * -> *) era. PParams' f era -> HKD f Natural
Shelley._maxBHSize PParamsUpdate era
pp) ]
     [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++ [ Text
"keyDeposit"            Text -> Coin -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Coin
x | Coin
x <- StrictMaybe Coin -> [Coin]
forall a. StrictMaybe a -> [a]
mbfield (PParamsUpdate era -> HKD StrictMaybe Coin
forall (f :: * -> *) era. PParams' f era -> HKD f Coin
Shelley._keyDeposit PParamsUpdate era
pp) ]
     [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++ [ Text
"poolDeposit"           Text -> Coin -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Coin
x | Coin
x <- StrictMaybe Coin -> [Coin]
forall a. StrictMaybe a -> [a]
mbfield (PParamsUpdate era -> HKD StrictMaybe Coin
forall (f :: * -> *) era. PParams' f era -> HKD f Coin
Shelley._poolDeposit PParamsUpdate era
pp) ]
     [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++ [ Text
"eMax"                  Text -> EpochNo -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= EpochNo
x | EpochNo
x <- StrictMaybe EpochNo -> [EpochNo]
forall a. StrictMaybe a -> [a]
mbfield (PParamsUpdate era -> HKD StrictMaybe EpochNo
forall (f :: * -> *) era. PParams' f era -> HKD f EpochNo
Shelley._eMax PParamsUpdate era
pp) ]
     [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++ [ Text
"nOpt"                  Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Natural
x | Natural
x <- StrictMaybe Natural -> [Natural]
forall a. StrictMaybe a -> [a]
mbfield (PParamsUpdate era -> HKD StrictMaybe Natural
forall (f :: * -> *) era. PParams' f era -> HKD f Natural
Shelley._nOpt PParamsUpdate era
pp) ]
     [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++ [ Text
"a0"                    Text -> NonNegativeInterval -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= NonNegativeInterval
x | NonNegativeInterval
x <- StrictMaybe NonNegativeInterval -> [NonNegativeInterval]
forall a. StrictMaybe a -> [a]
mbfield (PParamsUpdate era -> HKD StrictMaybe NonNegativeInterval
forall (f :: * -> *) era.
PParams' f era -> HKD f NonNegativeInterval
Shelley._a0 PParamsUpdate era
pp) ]
     [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++ [ Text
"rho"                   Text -> UnitInterval -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= UnitInterval
x | UnitInterval
x <- StrictMaybe UnitInterval -> [UnitInterval]
forall a. StrictMaybe a -> [a]
mbfield (PParamsUpdate era -> HKD StrictMaybe UnitInterval
forall (f :: * -> *) era. PParams' f era -> HKD f UnitInterval
Shelley._rho PParamsUpdate era
pp) ]
     [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++ [ Text
"tau"                   Text -> UnitInterval -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= UnitInterval
x | UnitInterval
x <- StrictMaybe UnitInterval -> [UnitInterval]
forall a. StrictMaybe a -> [a]
mbfield (PParamsUpdate era -> HKD StrictMaybe UnitInterval
forall (f :: * -> *) era. PParams' f era -> HKD f UnitInterval
Shelley._tau PParamsUpdate era
pp) ]
     [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++ [ Text
"decentralisationParam" Text -> UnitInterval -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= UnitInterval
x | UnitInterval
x <- StrictMaybe UnitInterval -> [UnitInterval]
forall a. StrictMaybe a -> [a]
mbfield (PParamsUpdate era -> HKD StrictMaybe UnitInterval
forall (f :: * -> *) era. PParams' f era -> HKD f UnitInterval
Shelley._d PParamsUpdate era
pp) ]
     [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++ [ Text
"extraEntropy"          Text -> Nonce -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Nonce
x | Nonce
x <- StrictMaybe Nonce -> [Nonce]
forall a. StrictMaybe a -> [a]
mbfield (PParamsUpdate era -> HKD StrictMaybe Nonce
forall (f :: * -> *) era. PParams' f era -> HKD f Nonce
Shelley._extraEntropy PParamsUpdate era
pp) ]
     [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++ [ Text
"protocolVersion"       Text -> ProtVer -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= ProtVer
x | ProtVer
x <- StrictMaybe ProtVer -> [ProtVer]
forall a. StrictMaybe a -> [a]
mbfield (PParamsUpdate era -> HKD StrictMaybe ProtVer
forall (f :: * -> *) era. PParams' f era -> HKD f ProtVer
Shelley._protocolVersion PParamsUpdate era
pp) ]
     [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++ [ Text
"minUTxOValue"          Text -> Coin -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Coin
x | Coin
x <- StrictMaybe Coin -> [Coin]
forall a. StrictMaybe a -> [a]
mbfield (PParamsUpdate era -> HKD StrictMaybe Coin
forall (f :: * -> *) era. PParams' f era -> HKD f Coin
Shelley._minUTxOValue PParamsUpdate era
pp) ]
     [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++ [ Text
"minPoolCost"           Text -> Coin -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Coin
x | Coin
x <- StrictMaybe Coin -> [Coin]
forall a. StrictMaybe a -> [a]
mbfield (PParamsUpdate era -> HKD StrictMaybe Coin
forall (f :: * -> *) era. PParams' f era -> HKD f Coin
Shelley._minPoolCost PParamsUpdate era
pp) ]
    where
      mbfield :: StrictMaybe a -> [a]
mbfield StrictMaybe a
SNothing  = []
      mbfield (SJust a
x) = [a
x]

instance Crypto.Crypto crypto => ToJSON (Shelley.DPState crypto) where
  toJSON :: DPState crypto -> Value
toJSON DPState crypto
dpState = [Pair] -> Value
object [ Text
"dstate" Text -> DState crypto -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= DPState crypto -> DState crypto
forall crypto. DPState crypto -> DState crypto
Shelley._dstate DPState crypto
dpState
                          , Text
"pstate" Text -> PState crypto -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= DPState crypto -> PState crypto
forall crypto. DPState crypto -> PState crypto
Shelley._pstate DPState crypto
dpState
                          ]

instance Crypto.Crypto crypto => ToJSON (Shelley.DState crypto) where
  toJSON :: DState crypto -> Value
toJSON DState crypto
dState = [Pair] -> Value
object [ Text
"rewards" Text -> RewardAccounts crypto -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= DState crypto -> RewardAccounts crypto
forall crypto. DState crypto -> RewardAccounts crypto
Shelley._rewards DState crypto
dState
                         , Text
"delegations" Text
-> Map (Credential 'Staking crypto) (KeyHash 'StakePool crypto)
-> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= DState crypto
-> Map (Credential 'Staking crypto) (KeyHash 'StakePool crypto)
forall crypto.
DState crypto
-> Map (Credential 'Staking crypto) (KeyHash 'StakePool crypto)
ShelleyLedger._delegations DState crypto
dState
                         , Text
"ptrs" Text -> Bimap Ptr (Credential 'Staking crypto) -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= DState crypto -> Bimap Ptr (Credential 'Staking crypto)
forall crypto.
DState crypto -> Bimap Ptr (Credential 'Staking crypto)
Shelley._ptrs DState crypto
dState
                         , Text
"fGenDelegs" Text -> [(FutureGenDeleg crypto, GenDelegPair crypto)] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Map (FutureGenDeleg crypto) (GenDelegPair crypto)
-> [(FutureGenDeleg crypto, GenDelegPair crypto)]
forall k a. Map k a -> [(k, a)]
Map.toList (DState crypto -> Map (FutureGenDeleg crypto) (GenDelegPair crypto)
forall crypto.
DState crypto -> Map (FutureGenDeleg crypto) (GenDelegPair crypto)
Shelley._fGenDelegs DState crypto
dState)
                         , Text
"genDelegs" Text -> GenDelegs crypto -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= DState crypto -> GenDelegs crypto
forall crypto. DState crypto -> GenDelegs crypto
Shelley._genDelegs DState crypto
dState
                         , Text
"irwd" Text -> InstantaneousRewards crypto -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= DState crypto -> InstantaneousRewards crypto
forall crypto. DState crypto -> InstantaneousRewards crypto
Shelley._irwd DState crypto
dState
                         ]

instance ToJSON (ShelleyLedger.FutureGenDeleg crypto) where
  toJSON :: FutureGenDeleg crypto -> Value
toJSON FutureGenDeleg crypto
fGenDeleg =
    [Pair] -> Value
object [ Text
"fGenDelegSlot" Text -> SlotNo -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= FutureGenDeleg crypto -> SlotNo
forall crypto. FutureGenDeleg crypto -> SlotNo
ShelleyLedger.fGenDelegSlot FutureGenDeleg crypto
fGenDeleg
           , Text
"fGenDelegGenKeyHash" Text -> KeyHash 'Genesis crypto -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= FutureGenDeleg crypto -> KeyHash 'Genesis crypto
forall crypto. FutureGenDeleg crypto -> KeyHash 'Genesis crypto
ShelleyLedger.fGenDelegGenKeyHash FutureGenDeleg crypto
fGenDeleg
           ]

instance Crypto.Crypto crypto => ToJSON (Shelley.GenDelegs crypto) where
  toJSON :: GenDelegs crypto -> Value
toJSON (Shelley.GenDelegs Map (KeyHash 'Genesis crypto) (GenDelegPair crypto)
delegs) = Map (KeyHash 'Genesis crypto) (GenDelegPair crypto) -> Value
forall a. ToJSON a => a -> Value
toJSON Map (KeyHash 'Genesis crypto) (GenDelegPair crypto)
delegs

instance ToJSON (Shelley.InstantaneousRewards crypto) where
  toJSON :: InstantaneousRewards crypto -> Value
toJSON InstantaneousRewards crypto
iRwds = [Pair] -> Value
object [ Text
"iRReserves" Text -> Map (Credential 'Staking crypto) Coin -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= InstantaneousRewards crypto
-> Map (Credential 'Staking crypto) Coin
forall crypto.
InstantaneousRewards crypto
-> Map (Credential 'Staking crypto) Coin
Shelley.iRReserves InstantaneousRewards crypto
iRwds
                        , Text
"iRTreasury" Text -> Map (Credential 'Staking crypto) Coin -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= InstantaneousRewards crypto
-> Map (Credential 'Staking crypto) Coin
forall crypto.
InstantaneousRewards crypto
-> Map (Credential 'Staking crypto) Coin
Shelley.iRTreasury InstantaneousRewards crypto
iRwds
                        ]

instance ToJSON (Bimap Shelley.Ptr (Shelley.Credential Shelley.Staking crypto)) where
  toJSON :: Bimap Ptr (Credential 'Staking crypto) -> Value
toJSON (MkBiMap Map Ptr (Credential 'Staking crypto)
ptsStakeM Map (Credential 'Staking crypto) (Set Ptr)
stakePtrSetM) =
    [Pair] -> Value
object [ Text
"stakedCreds" Text -> [(Ptr, Credential 'Staking crypto)] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Map Ptr (Credential 'Staking crypto)
-> [(Ptr, Credential 'Staking crypto)]
forall k a. Map k a -> [(k, a)]
Map.toList Map Ptr (Credential 'Staking crypto)
ptsStakeM
           , Text
"credPtrR" Text -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Map (Credential 'Staking crypto) (Set Ptr) -> Value
forall a. ToJSON a => a -> Value
toJSON Map (Credential 'Staking crypto) (Set Ptr)
stakePtrSetM
           ]
instance ToJSON Shelley.Ptr where
  toJSON :: Ptr -> Value
toJSON (Shelley.Ptr SlotNo
slotNo Ix
txIndex Ix
certIndex) =
    [Pair] -> Value
object [ Text
"slot" Text -> Ix -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= SlotNo -> Ix
unSlotNo SlotNo
slotNo
           , Text
"txIndex" Text -> Ix -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Ix
txIndex
           , Text
"certIndex" Text -> Ix -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Ix
certIndex
           ]


instance Crypto.Crypto crypto => ToJSON (Shelley.PState crypto) where
  toJSON :: PState crypto -> Value
toJSON PState crypto
pState = [Pair] -> Value
object [ Text
"pParams pState" Text -> Map (KeyHash 'StakePool crypto) (PoolParams crypto) -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PState crypto
-> Map (KeyHash 'StakePool crypto) (PoolParams crypto)
forall crypto.
PState crypto
-> Map (KeyHash 'StakePool crypto) (PoolParams crypto)
Shelley._pParams PState crypto
pState
                         , Text
"fPParams pState" Text -> Map (KeyHash 'StakePool crypto) (PoolParams crypto) -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PState crypto
-> Map (KeyHash 'StakePool crypto) (PoolParams crypto)
forall crypto.
PState crypto
-> Map (KeyHash 'StakePool crypto) (PoolParams crypto)
Shelley._fPParams PState crypto
pState
                         , Text
"retiring pState" Text -> Map (KeyHash 'StakePool crypto) EpochNo -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PState crypto -> Map (KeyHash 'StakePool crypto) EpochNo
forall crypto.
PState crypto -> Map (KeyHash 'StakePool crypto) EpochNo
Shelley._retiring PState crypto
pState
                         ]

instance ( Consensus.ShelleyBasedEra era
         , ToJSON (Core.TxOut era)
         ) => ToJSON (Shelley.UTxO era) where
  toJSON :: UTxO era -> Value
toJSON (Shelley.UTxO Map (TxIn (Crypto era)) (TxOut era)
utxo) = Map (TxIn (Crypto era)) (TxOut era) -> Value
forall a. ToJSON a => a -> Value
toJSON Map (TxIn (Crypto era)) (TxOut era)
utxo

instance ( Consensus.ShelleyBasedEra era
         , ToJSON (Core.Value era)
         ) => ToJSON (Shelley.TxOut era) where
  toJSON :: TxOut era -> Value
toJSON (Shelley.TxOut Addr (Crypto era)
addr Value era
amount) =
    [Pair] -> Value
object
      [ Text
"address" Text -> Addr (Crypto era) -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Addr (Crypto era)
addr
      , Text
"amount" Text -> Value era -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Value era
amount
      ]

instance Crypto.Crypto crypto => ToJSON (Shelley.TxIn crypto) where
  toJSON :: TxIn crypto -> Value
toJSON = Text -> Value
forall a. ToJSON a => a -> Value
toJSON (Text -> Value) -> (TxIn crypto -> Text) -> TxIn crypto -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxIn crypto -> Text
forall crypto. Crypto crypto => TxIn crypto -> Text
txInToText

instance Crypto.Crypto crypto => ToJSONKey (Shelley.TxIn crypto) where
  toJSONKey :: ToJSONKeyFunction (TxIn crypto)
toJSONKey = (TxIn crypto -> Text) -> ToJSONKeyFunction (TxIn crypto)
forall a. (a -> Text) -> ToJSONKeyFunction a
toJSONKeyText TxIn crypto -> Text
forall crypto. Crypto crypto => TxIn crypto -> Text
txInToText

txInToText :: Crypto.Crypto crypto => Shelley.TxIn crypto -> Text
txInToText :: TxIn crypto -> Text
txInToText (Shelley.TxIn (Shelley.TxId SafeHash crypto EraIndependentTxBody
txidHash) Natural
ix) =
  Hash (HASH crypto) EraIndependentTxBody -> Text
forall crypto a. Hash crypto a -> Text
hashToText (SafeHash crypto EraIndependentTxBody
-> Hash (HASH crypto) EraIndependentTxBody
forall crypto i. SafeHash crypto i -> Hash (HASH crypto) i
SafeHash.extractHash SafeHash crypto EraIndependentTxBody
txidHash)
    Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack String
"#"
    Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack (Natural -> String
forall a. Show a => a -> String
show Natural
ix)

hashToText :: Crypto.Hash crypto a -> Text
hashToText :: Hash crypto a -> Text
hashToText = ByteString -> Text
Text.decodeLatin1 (ByteString -> Text)
-> (Hash crypto a -> ByteString) -> Hash crypto a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash crypto a -> ByteString
forall h a. Hash h a -> ByteString
Crypto.hashToBytesAsHex

instance Crypto.Crypto crypto => ToJSON (Shelley.NonMyopic crypto) where
  toJSON :: NonMyopic crypto -> Value
toJSON NonMyopic crypto
nonMy = [Pair] -> Value
object [ Text
"likelihoodsNM" Text -> Map (KeyHash 'StakePool crypto) Likelihood -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= NonMyopic crypto -> Map (KeyHash 'StakePool crypto) Likelihood
forall crypto.
NonMyopic crypto -> Map (KeyHash 'StakePool crypto) Likelihood
Shelley.likelihoodsNM NonMyopic crypto
nonMy
                        , Text
"rewardPotNM" Text -> Coin -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= NonMyopic crypto -> Coin
forall crypto. NonMyopic crypto -> Coin
Shelley.rewardPotNM NonMyopic crypto
nonMy
                        ]

instance ToJSON Shelley.Likelihood where
  toJSON :: Likelihood -> Value
toJSON (Shelley.Likelihood StrictSeq LogWeight
llhd) =
    StrictSeq Double -> Value
forall a. ToJSON a => a -> Value
toJSON (StrictSeq Double -> Value) -> StrictSeq Double -> Value
forall a b. (a -> b) -> a -> b
$ (LogWeight -> Double) -> StrictSeq LogWeight -> StrictSeq Double
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(Shelley.LogWeight Float
f) -> Double -> Double
forall a. Floating a => a -> a
exp (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ Float -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac Float
f :: Double) StrictSeq LogWeight
llhd

instance Crypto.Crypto crypto => ToJSON (Shelley.SnapShots crypto) where
  toJSON :: SnapShots crypto -> Value
toJSON SnapShots crypto
ss = [Pair] -> Value
object [ Text
"pstakeMark" Text -> SnapShot crypto -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= SnapShots crypto -> SnapShot crypto
forall crypto. SnapShots crypto -> SnapShot crypto
Shelley._pstakeMark SnapShots crypto
ss
                     , Text
"pstakeSet" Text -> SnapShot crypto -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= SnapShots crypto -> SnapShot crypto
forall crypto. SnapShots crypto -> SnapShot crypto
Shelley._pstakeSet SnapShots crypto
ss
                     , Text
"pstakeGo" Text -> SnapShot crypto -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= SnapShots crypto -> SnapShot crypto
forall crypto. SnapShots crypto -> SnapShot crypto
Shelley._pstakeGo SnapShots crypto
ss
                     , Text
"feeSS" Text -> Coin -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= SnapShots crypto -> Coin
forall crypto. SnapShots crypto -> Coin
Shelley._feeSS SnapShots crypto
ss
                     ]

instance Crypto.Crypto crypto => ToJSON (Shelley.SnapShot crypto) where
  toJSON :: SnapShot crypto -> Value
toJSON SnapShot crypto
ss = [Pair] -> Value
object [ Text
"stake" Text -> Stake crypto -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= SnapShot crypto -> Stake crypto
forall crypto. SnapShot crypto -> Stake crypto
Shelley._stake SnapShot crypto
ss
                     , Text
"delegations" Text
-> Map (Credential 'Staking crypto) (KeyHash 'StakePool crypto)
-> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= SnapShot crypto
-> Map (Credential 'Staking crypto) (KeyHash 'StakePool crypto)
forall crypto.
SnapShot crypto
-> Map (Credential 'Staking crypto) (KeyHash 'StakePool crypto)
ShelleyEpoch._delegations SnapShot crypto
ss
                     , Text
"poolParams" Text -> Map (KeyHash 'StakePool crypto) (PoolParams crypto) -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= SnapShot crypto
-> Map (KeyHash 'StakePool crypto) (PoolParams crypto)
forall crypto.
SnapShot crypto
-> Map (KeyHash 'StakePool crypto) (PoolParams crypto)
Shelley._poolParams SnapShot crypto
ss
                     ]

instance ToJSON (Shelley.Stake crypto) where
  toJSON :: Stake crypto -> Value
toJSON (Shelley.Stake Map (Credential 'Staking crypto) Coin
s) = Map (Credential 'Staking crypto) Coin -> Value
forall a. ToJSON a => a -> Value
toJSON Map (Credential 'Staking crypto) Coin
s

instance Crypto.Crypto crypto => ToJSON (Shelley.RewardUpdate crypto) where
  toJSON :: RewardUpdate crypto -> Value
toJSON RewardUpdate crypto
rUpdate = [Pair] -> Value
object [ Text
"deltaT" Text -> DeltaCoin -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= RewardUpdate crypto -> DeltaCoin
forall crypto. RewardUpdate crypto -> DeltaCoin
Shelley.deltaT RewardUpdate crypto
rUpdate
                          , Text
"deltaR" Text -> DeltaCoin -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= RewardUpdate crypto -> DeltaCoin
forall crypto. RewardUpdate crypto -> DeltaCoin
Shelley.deltaR RewardUpdate crypto
rUpdate
                          , Text
"rs" Text
-> Map (Credential 'Staking crypto) (Set (Reward crypto)) -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= RewardUpdate crypto
-> Map (Credential 'Staking crypto) (Set (Reward crypto))
forall crypto.
RewardUpdate crypto
-> Map (Credential 'Staking crypto) (Set (Reward crypto))
Shelley.rs RewardUpdate crypto
rUpdate
                          , Text
"deltaF" Text -> DeltaCoin -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= RewardUpdate crypto -> DeltaCoin
forall crypto. RewardUpdate crypto -> DeltaCoin
Shelley.deltaF RewardUpdate crypto
rUpdate
                          , Text
"nonMyopic" Text -> NonMyopic crypto -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= RewardUpdate crypto -> NonMyopic crypto
forall crypto. RewardUpdate crypto -> NonMyopic crypto
Shelley.nonMyopic RewardUpdate crypto
rUpdate
                          ]

instance Crypto.Crypto crypto => ToJSON (Shelley.PulsingRewUpdate crypto) where
  toJSON :: PulsingRewUpdate crypto -> Value
toJSON (Shelley.Pulsing RewardSnapShot crypto
_ Pulser crypto
_) = Value
Aeson.Null
  toJSON (Shelley.Complete RewardUpdate crypto
ru) = RewardUpdate crypto -> Value
forall a. ToJSON a => a -> Value
toJSON RewardUpdate crypto
ru

instance ToJSON Shelley.DeltaCoin where
  toJSON :: DeltaCoin -> Value
toJSON (Shelley.DeltaCoin Integer
i) = Integer -> Value
forall a. ToJSON a => a -> Value
toJSON Integer
i

instance Crypto.Crypto crypto => ToJSON (Shelley.PoolDistr crypto) where
  toJSON :: PoolDistr crypto -> Value
toJSON (Shelley.PoolDistr Map (KeyHash 'StakePool crypto) (IndividualPoolStake crypto)
m) = Map (KeyHash 'StakePool crypto) (IndividualPoolStake crypto)
-> Value
forall a. ToJSON a => a -> Value
toJSON Map (KeyHash 'StakePool crypto) (IndividualPoolStake crypto)
m

instance ToJSON (Shelley.IndividualPoolStake crypto) where
  toJSON :: IndividualPoolStake crypto -> Value
toJSON IndividualPoolStake crypto
indivPoolStake =
    [Pair] -> Value
object [ Text
"individualPoolStake" Text -> Rational -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= IndividualPoolStake crypto -> Rational
forall crypto. IndividualPoolStake crypto -> Rational
Shelley.individualPoolStake IndividualPoolStake crypto
indivPoolStake
           , Text
"individualPoolStakeVrf" Text -> Hash (HASH crypto) (VerKeyVRF (VRF crypto)) -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= IndividualPoolStake crypto
-> Hash (HASH crypto) (VerKeyVRF (VRF crypto))
forall crypto.
IndividualPoolStake crypto -> Hash crypto (VerKeyVRF crypto)
Shelley.individualPoolStakeVrf IndividualPoolStake crypto
indivPoolStake
           ]

instance ToJSON (Shelley.Reward crypto) where
  toJSON :: Reward crypto -> Value
toJSON Reward crypto
reward =
     [Pair] -> Value
object [ Text
"rewardType" Text -> RewardType -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Reward crypto -> RewardType
forall crypto. Reward crypto -> RewardType
Shelley.rewardType Reward crypto
reward
            , Text
"rewardPool" Text -> KeyHash 'StakePool crypto -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Reward crypto -> KeyHash 'StakePool crypto
forall crypto. Reward crypto -> KeyHash 'StakePool crypto
Shelley.rewardPool Reward crypto
reward
            , Text
"rewardAmount" Text -> Coin -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Reward crypto -> Coin
forall crypto. Reward crypto -> Coin
Shelley.rewardAmount Reward crypto
reward
            ]

instance ToJSON Shelley.RewardType where
  toJSON :: RewardType -> Value
toJSON RewardType
Shelley.MemberReward = Value
"MemberReward"
  toJSON RewardType
Shelley.LeaderReward = Value
"LeaderReward"

instance ToJSON (SafeHash.SafeHash c a) where
  toJSON :: SafeHash c a -> Value
toJSON = Hash (HASH c) a -> Value
forall a. ToJSON a => a -> Value
toJSON (Hash (HASH c) a -> Value)
-> (SafeHash c a -> Hash (HASH c) a) -> SafeHash c a -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SafeHash c a -> Hash (HASH c) a
forall crypto i. SafeHash crypto i -> Hash (HASH crypto) i
SafeHash.extractHash

-----

instance ToJSON Alonzo.ExUnits
deriving instance FromJSON Alonzo.ExUnits

instance ToJSON Alonzo.Prices where
  toJSON :: Prices -> Value
toJSON Alonzo.Prices { NonNegativeInterval
prSteps :: Prices -> NonNegativeInterval
prSteps :: NonNegativeInterval
Alonzo.prSteps, NonNegativeInterval
prMem :: Prices -> NonNegativeInterval
prMem :: NonNegativeInterval
Alonzo.prMem } =
    -- We cannot round-trip via NonNegativeInterval, so we go via Rational
    [Pair] -> Value
object [ Text
"prSteps" Text -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Rational -> Value
toRationalJSON (NonNegativeInterval -> Rational
forall r. BoundedRational r => r -> Rational
Ledger.unboundRational NonNegativeInterval
prSteps)
           , Text
"prMem"   Text -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Rational -> Value
toRationalJSON (NonNegativeInterval -> Rational
forall r. BoundedRational r => r -> Rational
Ledger.unboundRational NonNegativeInterval
prMem)
           ]

instance FromJSON Alonzo.Prices where
  parseJSON :: Value -> Parser Prices
parseJSON =
    String -> (Object -> Parser Prices) -> Value -> Parser Prices
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Aeson.withObject String
"prices" ((Object -> Parser Prices) -> Value -> Parser Prices)
-> (Object -> Parser Prices) -> Value -> Parser Prices
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
      Rational
steps <- Object
o Object -> Text -> Parser Rational
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"prSteps"
      Rational
mem   <- Object
o Object -> Text -> Parser Rational
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"prMem"
      NonNegativeInterval
prSteps <- Rational -> Parser NonNegativeInterval
forall a (m :: * -> *).
(BoundedRational a, MonadFail m) =>
Rational -> m a
checkBoundedRational Rational
steps
      NonNegativeInterval
prMem   <- Rational -> Parser NonNegativeInterval
forall a (m :: * -> *).
(BoundedRational a, MonadFail m) =>
Rational -> m a
checkBoundedRational Rational
mem
      Prices -> Parser Prices
forall (m :: * -> *) a. Monad m => a -> m a
return Prices :: NonNegativeInterval -> NonNegativeInterval -> Prices
Alonzo.Prices { NonNegativeInterval
prSteps :: NonNegativeInterval
prSteps :: NonNegativeInterval
Alonzo.prSteps, NonNegativeInterval
prMem :: NonNegativeInterval
prMem :: NonNegativeInterval
Alonzo.prMem }
    where
      -- We cannot round-trip via NonNegativeInterval, so we go via Rational
      checkBoundedRational :: Rational -> m a
checkBoundedRational Rational
r =
        case Rational -> Maybe a
forall r. BoundedRational r => Rational -> Maybe r
Ledger.boundRational Rational
r of
          Maybe a
Nothing -> String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
"too much precision for bounded rational: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Rational -> String
forall a. Show a => a -> String
show Rational
r)
          Just a
s  -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
s

deriving newtype instance FromJSON Alonzo.CostModel
deriving newtype instance ToJSON Alonzo.CostModel


languageToText :: Alonzo.Language -> Text
languageToText :: Language -> Text
languageToText Language
Alonzo.PlutusV1 = Text
"PlutusV1"

languageFromText :: MonadFail m => Text -> m Alonzo.Language
languageFromText :: Text -> m Language
languageFromText Text
"PlutusV1" = Language -> m Language
forall (f :: * -> *) a. Applicative f => a -> f a
pure Language
Alonzo.PlutusV1
languageFromText Text
lang = String -> m Language
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m Language) -> String -> m Language
forall a b. (a -> b) -> a -> b
$ String
"Error decoding Language: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Text -> String
forall a. Show a => a -> String
show Text
lang

instance FromJSON Alonzo.Language where
  parseJSON :: Value -> Parser Language
parseJSON = String -> (Text -> Parser Language) -> Value -> Parser Language
forall a. String -> (Text -> Parser a) -> Value -> Parser a
Aeson.withText String
"Language" Text -> Parser Language
forall (m :: * -> *). MonadFail m => Text -> m Language
languageFromText

instance ToJSON Alonzo.Language where
  toJSON :: Language -> Value
toJSON = Text -> Value
Aeson.String (Text -> Value) -> (Language -> Text) -> Language -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Language -> Text
languageToText

instance ToJSONKey Alonzo.Language where
  toJSONKey :: ToJSONKeyFunction Language
toJSONKey = (Language -> Text) -> ToJSONKeyFunction Language
forall a. (a -> Text) -> ToJSONKeyFunction a
toJSONKeyText Language -> Text
languageToText

instance FromJSONKey Alonzo.Language where
  fromJSONKey :: FromJSONKeyFunction Language
fromJSONKey = (Text -> Parser Language) -> FromJSONKeyFunction Language
forall a. (Text -> Parser a) -> FromJSONKeyFunction a
Aeson.FromJSONKeyTextParser Text -> Parser Language
forall (m :: * -> *). MonadFail m => Text -> m Language
languageFromText

instance FromJSON Alonzo.AlonzoGenesis where
  parseJSON :: Value -> Parser AlonzoGenesis
parseJSON = String
-> (Object -> Parser AlonzoGenesis)
-> Value
-> Parser AlonzoGenesis
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Aeson.withObject String
"Alonzo Genesis" ((Object -> Parser AlonzoGenesis) -> Value -> Parser AlonzoGenesis)
-> (Object -> Parser AlonzoGenesis)
-> Value
-> Parser AlonzoGenesis
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
    Coin
coinsPerUTxOWord     <- Object
o Object -> Text -> Parser Coin
forall a. FromJSON a => Object -> Text -> Parser a
.:  Text
"lovelacePerUTxOWord"
                        Parser Coin -> Parser Coin -> Parser Coin
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Object
o Object -> Text -> Parser Coin
forall a. FromJSON a => Object -> Text -> Parser a
.:  Text
"adaPerUTxOWord" --TODO: deprecate
    Maybe (Map Language CostModel)
cModels              <- Object
o Object -> Text -> Parser (Maybe (Map Language CostModel))
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
.:? Text
"costModels"
    Prices
prices               <- Object
o Object -> Text -> Parser Prices
forall a. FromJSON a => Object -> Text -> Parser a
.:  Text
"executionPrices"
    ExUnits
maxTxExUnits         <- Object
o Object -> Text -> Parser ExUnits
forall a. FromJSON a => Object -> Text -> Parser a
.:  Text
"maxTxExUnits"
    ExUnits
maxBlockExUnits      <- Object
o Object -> Text -> Parser ExUnits
forall a. FromJSON a => Object -> Text -> Parser a
.:  Text
"maxBlockExUnits"
    Natural
maxValSize           <- Object
o Object -> Text -> Parser Natural
forall a. FromJSON a => Object -> Text -> Parser a
.:  Text
"maxValueSize"
    Natural
collateralPercentage <- Object
o Object -> Text -> Parser Natural
forall a. FromJSON a => Object -> Text -> Parser a
.:  Text
"collateralPercentage"
    Natural
maxCollateralInputs  <- Object
o Object -> Text -> Parser Natural
forall a. FromJSON a => Object -> Text -> Parser a
.:  Text
"maxCollateralInputs"
    case Maybe (Map Language CostModel)
cModels of
      Maybe (Map Language CostModel)
Nothing -> case Map Text Integer -> CostModel
Alonzo.CostModel (Map Text Integer -> CostModel)
-> Maybe (Map Text Integer) -> Maybe CostModel
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Map Text Integer)
defaultCostModelParams of
        Just CostModel
m -> AlonzoGenesis -> Parser AlonzoGenesis
forall (m :: * -> *) a. Monad m => a -> m a
return AlonzoGenesis :: Coin
-> Map Language CostModel
-> Prices
-> ExUnits
-> ExUnits
-> Natural
-> Natural
-> Natural
-> AlonzoGenesis
Alonzo.AlonzoGenesis
          { Coin
coinsPerUTxOWord :: Coin
coinsPerUTxOWord :: Coin
Alonzo.coinsPerUTxOWord
          , costmdls :: Map Language CostModel
Alonzo.costmdls = Language -> CostModel -> Map Language CostModel
forall k a. k -> a -> Map k a
Map.singleton Language
Alonzo.PlutusV1 CostModel
m
          , Prices
prices :: Prices
prices :: Prices
Alonzo.prices
          , ExUnits
maxTxExUnits :: ExUnits
maxTxExUnits :: ExUnits
Alonzo.maxTxExUnits
          , ExUnits
maxBlockExUnits :: ExUnits
maxBlockExUnits :: ExUnits
Alonzo.maxBlockExUnits
          , Natural
maxValSize :: Natural
maxValSize :: Natural
Alonzo.maxValSize
          , Natural
collateralPercentage :: Natural
collateralPercentage :: Natural
Alonzo.collateralPercentage
          , Natural
maxCollateralInputs :: Natural
maxCollateralInputs :: Natural
Alonzo.maxCollateralInputs
          }
        Maybe CostModel
Nothing -> String -> Parser AlonzoGenesis
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Failed to extract the cost model params from defaultCostModel"
      Just Map Language CostModel
costmdls -> AlonzoGenesis -> Parser AlonzoGenesis
forall (m :: * -> *) a. Monad m => a -> m a
return AlonzoGenesis :: Coin
-> Map Language CostModel
-> Prices
-> ExUnits
-> ExUnits
-> Natural
-> Natural
-> Natural
-> AlonzoGenesis
Alonzo.AlonzoGenesis
        { Coin
coinsPerUTxOWord :: Coin
coinsPerUTxOWord :: Coin
Alonzo.coinsPerUTxOWord
        , Map Language CostModel
costmdls :: Map Language CostModel
costmdls :: Map Language CostModel
Alonzo.costmdls
        , Prices
prices :: Prices
prices :: Prices
Alonzo.prices
        , ExUnits
maxTxExUnits :: ExUnits
maxTxExUnits :: ExUnits
Alonzo.maxTxExUnits
        , ExUnits
maxBlockExUnits :: ExUnits
maxBlockExUnits :: ExUnits
Alonzo.maxBlockExUnits
        , Natural
maxValSize :: Natural
maxValSize :: Natural
Alonzo.maxValSize
        , Natural
collateralPercentage :: Natural
collateralPercentage :: Natural
Alonzo.collateralPercentage
        , Natural
maxCollateralInputs :: Natural
maxCollateralInputs :: Natural
Alonzo.maxCollateralInputs
        }

-- We don't render the cost model so that we can
-- render it later in 'AlonzoGenWrapper' as a filepath
-- and keep the cost model (which is chunky) as a separate file.
instance ToJSON Alonzo.AlonzoGenesis where
  toJSON :: AlonzoGenesis -> Value
toJSON AlonzoGenesis
v = [Pair] -> Value
object
      [ Text
"lovelacePerUTxOWord" Text -> Coin -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= AlonzoGenesis -> Coin
Alonzo.coinsPerUTxOWord AlonzoGenesis
v
      , Text
"costModels" Text -> Map Language CostModel -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= AlonzoGenesis -> Map Language CostModel
Alonzo.costmdls AlonzoGenesis
v
      , Text
"executionPrices" Text -> Prices -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= AlonzoGenesis -> Prices
Alonzo.prices AlonzoGenesis
v
      , Text
"maxTxExUnits" Text -> ExUnits -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= AlonzoGenesis -> ExUnits
Alonzo.maxTxExUnits AlonzoGenesis
v
      , Text
"maxBlockExUnits" Text -> ExUnits -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= AlonzoGenesis -> ExUnits
Alonzo.maxBlockExUnits AlonzoGenesis
v
      , Text
"maxValueSize" Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= AlonzoGenesis -> Natural
Alonzo.maxValSize AlonzoGenesis
v
      , Text
"collateralPercentage" Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= AlonzoGenesis -> Natural
Alonzo.collateralPercentage AlonzoGenesis
v
      , Text
"maxCollateralInputs" Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= AlonzoGenesis -> Natural
Alonzo.maxCollateralInputs AlonzoGenesis
v
      ]

instance ToJSON (Alonzo.PParams era) where
  toJSON :: PParams era -> Value
toJSON PParams era
pp =
    [Pair] -> Value
Aeson.object
      [ Text
"minFeeA" Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PParams era -> HKD Identity Natural
forall (f :: * -> *) era. PParams' f era -> HKD f Natural
Alonzo._minfeeA PParams era
pp
      , Text
"minFeeB" Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PParams era -> HKD Identity Natural
forall (f :: * -> *) era. PParams' f era -> HKD f Natural
Alonzo._minfeeB PParams era
pp
      , Text
"maxBlockBodySize" Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PParams era -> HKD Identity Natural
forall (f :: * -> *) era. PParams' f era -> HKD f Natural
Alonzo._maxBBSize PParams era
pp
      , Text
"maxTxSize" Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PParams era -> HKD Identity Natural
forall (f :: * -> *) era. PParams' f era -> HKD f Natural
Alonzo._maxTxSize PParams era
pp
      , Text
"maxBlockHeaderSize" Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PParams era -> HKD Identity Natural
forall (f :: * -> *) era. PParams' f era -> HKD f Natural
Alonzo._maxBHSize PParams era
pp
      , Text
"keyDeposit" Text -> Coin -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PParams era -> HKD Identity Coin
forall (f :: * -> *) era. PParams' f era -> HKD f Coin
Alonzo._keyDeposit PParams era
pp
      , Text
"poolDeposit" Text -> Coin -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PParams era -> HKD Identity Coin
forall (f :: * -> *) era. PParams' f era -> HKD f Coin
Alonzo._poolDeposit PParams era
pp
      , Text
"eMax" Text -> EpochNo -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PParams era -> HKD Identity EpochNo
forall (f :: * -> *) era. PParams' f era -> HKD f EpochNo
Alonzo._eMax PParams era
pp
      , Text
"nOpt" Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PParams era -> HKD Identity Natural
forall (f :: * -> *) era. PParams' f era -> HKD f Natural
Alonzo._nOpt PParams era
pp
      , Text
"a0"  Text -> NonNegativeInterval -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PParams era -> HKD Identity NonNegativeInterval
forall (f :: * -> *) era.
PParams' f era -> HKD f NonNegativeInterval
Alonzo._a0 PParams era
pp
      , Text
"rho" Text -> UnitInterval -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PParams era -> HKD Identity UnitInterval
forall (f :: * -> *) era. PParams' f era -> HKD f UnitInterval
Alonzo._rho PParams era
pp
      , Text
"tau" Text -> UnitInterval -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PParams era -> HKD Identity UnitInterval
forall (f :: * -> *) era. PParams' f era -> HKD f UnitInterval
Alonzo._tau PParams era
pp
      , Text
"decentralisationParam" Text -> UnitInterval -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PParams era -> HKD Identity UnitInterval
forall (f :: * -> *) era. PParams' f era -> HKD f UnitInterval
Alonzo._d PParams era
pp
      , Text
"extraEntropy" Text -> Nonce -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PParams era -> HKD Identity Nonce
forall (f :: * -> *) era. PParams' f era -> HKD f Nonce
Alonzo._extraEntropy PParams era
pp
      , Text
"protocolVersion" Text -> ProtVer -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PParams era -> HKD Identity ProtVer
forall (f :: * -> *) era. PParams' f era -> HKD f ProtVer
Alonzo._protocolVersion PParams era
pp
      , Text
"minPoolCost" Text -> Coin -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PParams era -> HKD Identity Coin
forall (f :: * -> *) era. PParams' f era -> HKD f Coin
Alonzo._minPoolCost PParams era
pp
      , Text
"lovelacePerUTxOWord" Text -> Coin -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PParams era -> HKD Identity Coin
forall (f :: * -> *) era. PParams' f era -> HKD f Coin
Alonzo._coinsPerUTxOWord PParams era
pp
      , Text
"costmdls" Text -> Map Language CostModel -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PParams era -> HKD Identity (Map Language CostModel)
forall (f :: * -> *) era.
PParams' f era -> HKD f (Map Language CostModel)
Alonzo._costmdls PParams era
pp
      , Text
"prices" Text -> Prices -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PParams era -> HKD Identity Prices
forall (f :: * -> *) era. PParams' f era -> HKD f Prices
Alonzo._prices PParams era
pp
      , Text
"maxTxExUnits" Text -> ExUnits -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PParams era -> HKD Identity ExUnits
forall (f :: * -> *) era. PParams' f era -> HKD f ExUnits
Alonzo._maxTxExUnits PParams era
pp
      , Text
"maxBlockExUnits" Text -> ExUnits -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PParams era -> HKD Identity ExUnits
forall (f :: * -> *) era. PParams' f era -> HKD f ExUnits
Alonzo._maxBlockExUnits PParams era
pp
      , Text
"maxValSize" Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PParams era -> HKD Identity Natural
forall (f :: * -> *) era. PParams' f era -> HKD f Natural
Alonzo._maxValSize PParams era
pp
      , Text
"collateralPercentage" Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PParams era -> HKD Identity Natural
forall (f :: * -> *) era. PParams' f era -> HKD f Natural
Alonzo._collateralPercentage PParams era
pp
      , Text
"maxCollateralInputs " Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= PParams era -> HKD Identity Natural
forall (f :: * -> *) era. PParams' f era -> HKD f Natural
Alonzo._maxCollateralInputs PParams era
pp
      ]

instance FromJSON (Alonzo.PParams era) where
  parseJSON :: Value -> Parser (PParams era)
parseJSON =
    String
-> (Object -> Parser (PParams era))
-> Value
-> Parser (PParams era)
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Aeson.withObject String
"PParams" ((Object -> Parser (PParams era)) -> Value -> Parser (PParams era))
-> (Object -> Parser (PParams era))
-> Value
-> Parser (PParams era)
forall a b. (a -> b) -> a -> b
$ \Object
obj ->
      Natural
-> Natural
-> Natural
-> Natural
-> Natural
-> Coin
-> Coin
-> EpochNo
-> Natural
-> NonNegativeInterval
-> UnitInterval
-> UnitInterval
-> UnitInterval
-> Nonce
-> ProtVer
-> Coin
-> Coin
-> Map Language CostModel
-> Prices
-> ExUnits
-> ExUnits
-> Natural
-> Natural
-> Natural
-> PParams era
forall (f :: * -> *) era.
HKD f Natural
-> HKD f Natural
-> HKD f Natural
-> HKD f Natural
-> HKD f Natural
-> HKD f Coin
-> HKD f Coin
-> HKD f EpochNo
-> HKD f Natural
-> HKD f NonNegativeInterval
-> HKD f UnitInterval
-> HKD f UnitInterval
-> HKD f UnitInterval
-> HKD f Nonce
-> HKD f ProtVer
-> HKD f Coin
-> HKD f Coin
-> HKD f (Map Language CostModel)
-> HKD f Prices
-> HKD f ExUnits
-> HKD f ExUnits
-> HKD f Natural
-> HKD f Natural
-> HKD f Natural
-> PParams' f era
Alonzo.PParams
        (Natural
 -> Natural
 -> Natural
 -> Natural
 -> Natural
 -> Coin
 -> Coin
 -> EpochNo
 -> Natural
 -> NonNegativeInterval
 -> UnitInterval
 -> UnitInterval
 -> UnitInterval
 -> Nonce
 -> ProtVer
 -> Coin
 -> Coin
 -> Map Language CostModel
 -> Prices
 -> ExUnits
 -> ExUnits
 -> Natural
 -> Natural
 -> Natural
 -> PParams era)
-> Parser Natural
-> Parser
     (Natural
      -> Natural
      -> Natural
      -> Natural
      -> Coin
      -> Coin
      -> EpochNo
      -> Natural
      -> NonNegativeInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> Nonce
      -> ProtVer
      -> Coin
      -> Coin
      -> Map Language CostModel
      -> Prices
      -> ExUnits
      -> ExUnits
      -> Natural
      -> Natural
      -> Natural
      -> PParams era)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
obj Object -> Text -> Parser Natural
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"minFeeA"
        Parser
  (Natural
   -> Natural
   -> Natural
   -> Natural
   -> Coin
   -> Coin
   -> EpochNo
   -> Natural
   -> NonNegativeInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> Nonce
   -> ProtVer
   -> Coin
   -> Coin
   -> Map Language CostModel
   -> Prices
   -> ExUnits
   -> ExUnits
   -> Natural
   -> Natural
   -> Natural
   -> PParams era)
-> Parser Natural
-> Parser
     (Natural
      -> Natural
      -> Natural
      -> Coin
      -> Coin
      -> EpochNo
      -> Natural
      -> NonNegativeInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> Nonce
      -> ProtVer
      -> Coin
      -> Coin
      -> Map Language CostModel
      -> Prices
      -> ExUnits
      -> ExUnits
      -> Natural
      -> Natural
      -> Natural
      -> PParams era)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Text -> Parser Natural
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"minFeeB"
        Parser
  (Natural
   -> Natural
   -> Natural
   -> Coin
   -> Coin
   -> EpochNo
   -> Natural
   -> NonNegativeInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> Nonce
   -> ProtVer
   -> Coin
   -> Coin
   -> Map Language CostModel
   -> Prices
   -> ExUnits
   -> ExUnits
   -> Natural
   -> Natural
   -> Natural
   -> PParams era)
-> Parser Natural
-> Parser
     (Natural
      -> Natural
      -> Coin
      -> Coin
      -> EpochNo
      -> Natural
      -> NonNegativeInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> Nonce
      -> ProtVer
      -> Coin
      -> Coin
      -> Map Language CostModel
      -> Prices
      -> ExUnits
      -> ExUnits
      -> Natural
      -> Natural
      -> Natural
      -> PParams era)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Text -> Parser Natural
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"maxBlockBodySize"
        Parser
  (Natural
   -> Natural
   -> Coin
   -> Coin
   -> EpochNo
   -> Natural
   -> NonNegativeInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> Nonce
   -> ProtVer
   -> Coin
   -> Coin
   -> Map Language CostModel
   -> Prices
   -> ExUnits
   -> ExUnits
   -> Natural
   -> Natural
   -> Natural
   -> PParams era)
-> Parser Natural
-> Parser
     (Natural
      -> Coin
      -> Coin
      -> EpochNo
      -> Natural
      -> NonNegativeInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> Nonce
      -> ProtVer
      -> Coin
      -> Coin
      -> Map Language CostModel
      -> Prices
      -> ExUnits
      -> ExUnits
      -> Natural
      -> Natural
      -> Natural
      -> PParams era)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Text -> Parser Natural
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"maxTxSize"
        Parser
  (Natural
   -> Coin
   -> Coin
   -> EpochNo
   -> Natural
   -> NonNegativeInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> Nonce
   -> ProtVer
   -> Coin
   -> Coin
   -> Map Language CostModel
   -> Prices
   -> ExUnits
   -> ExUnits
   -> Natural
   -> Natural
   -> Natural
   -> PParams era)
-> Parser Natural
-> Parser
     (Coin
      -> Coin
      -> EpochNo
      -> Natural
      -> NonNegativeInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> Nonce
      -> ProtVer
      -> Coin
      -> Coin
      -> Map Language CostModel
      -> Prices
      -> ExUnits
      -> ExUnits
      -> Natural
      -> Natural
      -> Natural
      -> PParams era)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Text -> Parser Natural
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"maxBlockHeaderSize"
        Parser
  (Coin
   -> Coin
   -> EpochNo
   -> Natural
   -> NonNegativeInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> Nonce
   -> ProtVer
   -> Coin
   -> Coin
   -> Map Language CostModel
   -> Prices
   -> ExUnits
   -> ExUnits
   -> Natural
   -> Natural
   -> Natural
   -> PParams era)
-> Parser Coin
-> Parser
     (Coin
      -> EpochNo
      -> Natural
      -> NonNegativeInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> Nonce
      -> ProtVer
      -> Coin
      -> Coin
      -> Map Language CostModel
      -> Prices
      -> ExUnits
      -> ExUnits
      -> Natural
      -> Natural
      -> Natural
      -> PParams era)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Text -> Parser Coin
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"keyDeposit"
        Parser
  (Coin
   -> EpochNo
   -> Natural
   -> NonNegativeInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> Nonce
   -> ProtVer
   -> Coin
   -> Coin
   -> Map Language CostModel
   -> Prices
   -> ExUnits
   -> ExUnits
   -> Natural
   -> Natural
   -> Natural
   -> PParams era)
-> Parser Coin
-> Parser
     (EpochNo
      -> Natural
      -> NonNegativeInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> Nonce
      -> ProtVer
      -> Coin
      -> Coin
      -> Map Language CostModel
      -> Prices
      -> ExUnits
      -> ExUnits
      -> Natural
      -> Natural
      -> Natural
      -> PParams era)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Text -> Parser Coin
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"poolDeposit"
        Parser
  (EpochNo
   -> Natural
   -> NonNegativeInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> Nonce
   -> ProtVer
   -> Coin
   -> Coin
   -> Map Language CostModel
   -> Prices
   -> ExUnits
   -> ExUnits
   -> Natural
   -> Natural
   -> Natural
   -> PParams era)
-> Parser EpochNo
-> Parser
     (Natural
      -> NonNegativeInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> Nonce
      -> ProtVer
      -> Coin
      -> Coin
      -> Map Language CostModel
      -> Prices
      -> ExUnits
      -> ExUnits
      -> Natural
      -> Natural
      -> Natural
      -> PParams era)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Text -> Parser EpochNo
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"eMax"
        Parser
  (Natural
   -> NonNegativeInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> Nonce
   -> ProtVer
   -> Coin
   -> Coin
   -> Map Language CostModel
   -> Prices
   -> ExUnits
   -> ExUnits
   -> Natural
   -> Natural
   -> Natural
   -> PParams era)
-> Parser Natural
-> Parser
     (NonNegativeInterval
      -> UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> Nonce
      -> ProtVer
      -> Coin
      -> Coin
      -> Map Language CostModel
      -> Prices
      -> ExUnits
      -> ExUnits
      -> Natural
      -> Natural
      -> Natural
      -> PParams era)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Text -> Parser Natural
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"nOpt"
        Parser
  (NonNegativeInterval
   -> UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> Nonce
   -> ProtVer
   -> Coin
   -> Coin
   -> Map Language CostModel
   -> Prices
   -> ExUnits
   -> ExUnits
   -> Natural
   -> Natural
   -> Natural
   -> PParams era)
-> Parser NonNegativeInterval
-> Parser
     (UnitInterval
      -> UnitInterval
      -> UnitInterval
      -> Nonce
      -> ProtVer
      -> Coin
      -> Coin
      -> Map Language CostModel
      -> Prices
      -> ExUnits
      -> ExUnits
      -> Natural
      -> Natural
      -> Natural
      -> PParams era)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Text -> Parser NonNegativeInterval
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"a0"
        Parser
  (UnitInterval
   -> UnitInterval
   -> UnitInterval
   -> Nonce
   -> ProtVer
   -> Coin
   -> Coin
   -> Map Language CostModel
   -> Prices
   -> ExUnits
   -> ExUnits
   -> Natural
   -> Natural
   -> Natural
   -> PParams era)
-> Parser UnitInterval
-> Parser
     (UnitInterval
      -> UnitInterval
      -> Nonce
      -> ProtVer
      -> Coin
      -> Coin
      -> Map Language CostModel
      -> Prices
      -> ExUnits
      -> ExUnits
      -> Natural
      -> Natural
      -> Natural
      -> PParams era)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Text -> Parser UnitInterval
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"rho"
        Parser
  (UnitInterval
   -> UnitInterval
   -> Nonce
   -> ProtVer
   -> Coin
   -> Coin
   -> Map Language CostModel
   -> Prices
   -> ExUnits
   -> ExUnits
   -> Natural
   -> Natural
   -> Natural
   -> PParams era)
-> Parser UnitInterval
-> Parser
     (UnitInterval
      -> Nonce
      -> ProtVer
      -> Coin
      -> Coin
      -> Map Language CostModel
      -> Prices
      -> ExUnits
      -> ExUnits
      -> Natural
      -> Natural
      -> Natural
      -> PParams era)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Text -> Parser UnitInterval
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"tau"
        Parser
  (UnitInterval
   -> Nonce
   -> ProtVer
   -> Coin
   -> Coin
   -> Map Language CostModel
   -> Prices
   -> ExUnits
   -> ExUnits
   -> Natural
   -> Natural
   -> Natural
   -> PParams era)
-> Parser UnitInterval
-> Parser
     (Nonce
      -> ProtVer
      -> Coin
      -> Coin
      -> Map Language CostModel
      -> Prices
      -> ExUnits
      -> ExUnits
      -> Natural
      -> Natural
      -> Natural
      -> PParams era)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Text -> Parser UnitInterval
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"decentralisationParam"
        Parser
  (Nonce
   -> ProtVer
   -> Coin
   -> Coin
   -> Map Language CostModel
   -> Prices
   -> ExUnits
   -> ExUnits
   -> Natural
   -> Natural
   -> Natural
   -> PParams era)
-> Parser Nonce
-> Parser
     (ProtVer
      -> Coin
      -> Coin
      -> Map Language CostModel
      -> Prices
      -> ExUnits
      -> ExUnits
      -> Natural
      -> Natural
      -> Natural
      -> PParams era)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Text -> Parser Nonce
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"extraEntropy"
        Parser
  (ProtVer
   -> Coin
   -> Coin
   -> Map Language CostModel
   -> Prices
   -> ExUnits
   -> ExUnits
   -> Natural
   -> Natural
   -> Natural
   -> PParams era)
-> Parser ProtVer
-> Parser
     (Coin
      -> Coin
      -> Map Language CostModel
      -> Prices
      -> ExUnits
      -> ExUnits
      -> Natural
      -> Natural
      -> Natural
      -> PParams era)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Text -> Parser ProtVer
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"protocolVersion"
        Parser
  (Coin
   -> Coin
   -> Map Language CostModel
   -> Prices
   -> ExUnits
   -> ExUnits
   -> Natural
   -> Natural
   -> Natural
   -> PParams era)
-> Parser Coin
-> Parser
     (Coin
      -> Map Language CostModel
      -> Prices
      -> ExUnits
      -> ExUnits
      -> Natural
      -> Natural
      -> Natural
      -> PParams era)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Text -> Parser (Maybe Coin)
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"minPoolCost" Parser (Maybe Coin) -> Coin -> Parser Coin
forall a. Parser (Maybe a) -> a -> Parser a
.!= Coin
forall a. Monoid a => a
mempty
        Parser
  (Coin
   -> Map Language CostModel
   -> Prices
   -> ExUnits
   -> ExUnits
   -> Natural
   -> Natural
   -> Natural
   -> PParams era)
-> Parser Coin
-> Parser
     (Map Language CostModel
      -> Prices
      -> ExUnits
      -> ExUnits
      -> Natural
      -> Natural
      -> Natural
      -> PParams era)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Text -> Parser Coin
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"lovelacePerUTxOWord"
        Parser
  (Map Language CostModel
   -> Prices
   -> ExUnits
   -> ExUnits
   -> Natural
   -> Natural
   -> Natural
   -> PParams era)
-> Parser (Map Language CostModel)
-> Parser
     (Prices
      -> ExUnits
      -> ExUnits
      -> Natural
      -> Natural
      -> Natural
      -> PParams era)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Text -> Parser (Map Language CostModel)
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"costmdls"
        Parser
  (Prices
   -> ExUnits
   -> ExUnits
   -> Natural
   -> Natural
   -> Natural
   -> PParams era)
-> Parser Prices
-> Parser
     (ExUnits
      -> ExUnits -> Natural -> Natural -> Natural -> PParams era)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Text -> Parser Prices
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"prices"
        Parser
  (ExUnits
   -> ExUnits -> Natural -> Natural -> Natural -> PParams era)
-> Parser ExUnits
-> Parser (ExUnits -> Natural -> Natural -> Natural -> PParams era)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Text -> Parser ExUnits
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"maxTxExUnits"
        Parser (ExUnits -> Natural -> Natural -> Natural -> PParams era)
-> Parser ExUnits
-> Parser (Natural -> Natural -> Natural -> PParams era)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Text -> Parser ExUnits
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"maxBlockExUnits"
        Parser (Natural -> Natural -> Natural -> PParams era)
-> Parser Natural -> Parser (Natural -> Natural -> PParams era)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Text -> Parser Natural
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"maxValSize"
        Parser (Natural -> Natural -> PParams era)
-> Parser Natural -> Parser (Natural -> PParams era)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Text -> Parser Natural
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"collateralPercentage"
        Parser (Natural -> PParams era)
-> Parser Natural -> Parser (PParams era)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Text -> Parser Natural
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"maxCollateralInputs"

deriving instance ToJSON (Alonzo.PParamsUpdate (Alonzo.AlonzoEra StandardCrypto))

instance (Ledger.Era era, Show (Ledger.Value era), ToJSON (Ledger.Value era))
    => ToJSON (Alonzo.TxOut era) where
  toJSON :: TxOut era -> Value
toJSON (Alonzo.TxOut Addr (Crypto era)
addr Value era
v StrictMaybe (DataHash (Crypto era))
dataHash) =
    [Pair] -> Value
object [ Text
"address" Text -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Addr (Crypto era) -> Value
forall a. ToJSON a => a -> Value
toJSON Addr (Crypto era)
addr
           , Text
"value" Text -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Value era -> Value
forall a. ToJSON a => a -> Value
toJSON Value era
v
           , Text
"datahash" Text -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= case StrictMaybe (DataHash (Crypto era))
-> Maybe (DataHash (Crypto era))
forall a. StrictMaybe a -> Maybe a
strictMaybeToMaybe StrictMaybe (DataHash (Crypto era))
dataHash of
                             Maybe (DataHash (Crypto era))
Nothing -> Value
Aeson.Null
                             Just DataHash (Crypto era)
dHash ->
                               Text -> Value
Aeson.String (Text -> Value)
-> (Hash (HASH (Crypto era)) EraIndependentData -> Text)
-> Hash (HASH (Crypto era)) EraIndependentData
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash (HASH (Crypto era)) EraIndependentData -> Text
forall crypto a. Hash crypto a -> Text
Crypto.hashToTextAsHex
                                 (Hash (HASH (Crypto era)) EraIndependentData -> Value)
-> Hash (HASH (Crypto era)) EraIndependentData -> Value
forall a b. (a -> b) -> a -> b
$ DataHash (Crypto era)
-> Hash (HASH (Crypto era)) EraIndependentData
forall crypto i. SafeHash crypto i -> Hash (HASH crypto) i
SafeHash.extractHash DataHash (Crypto era)
dHash
           ]

deriving instance Show Alonzo.AlonzoGenesis