-
Notifications
You must be signed in to change notification settings - Fork 729
JSON output for cardano-cli #3548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3346e76
222e16a
259c612
5d100c0
569445c
f9161c9
6501a12
37e5f7d
ac26b33
3973486
ecb9be0
ba37c5d
759a646
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,29 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE RankNTypes #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
{-# LANGUAGE UndecidableInstances #-} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering if there is a way to avoid using |
||
|
||
{-# OPTIONS_GHC -Wno-orphans #-} | ||
|
||
module Cardano.Api.Orphans () where | ||
|
||
import Data.Aeson (ToJSON (..), object, pairs, (.=)) | ||
import qualified Data.Aeson as Aeson | ||
import qualified Data.ByteString.Base16 as Base16 | ||
import Data.Text (Text) | ||
import qualified Data.Text.Encoding as Text | ||
|
||
import qualified Cardano.Ledger.Crypto as Crypto | ||
|
||
import Cardano.Ledger.Shelley.API (MIRPot (..)) | ||
import qualified Cardano.Ledger.Shelley.API as Ledger (KeyRole (..), WitVKey) | ||
|
||
import Cardano.Api.SerialiseCBOR (ToCBOR (..)) | ||
import Cardano.Binary (encodeListLen, serialize') | ||
import Codec.CBOR.Encoding (encodeWord) | ||
import qualified Ouroboros.Consensus.Shelley.Ledger.Query as Consensus | ||
|
||
-- Orphan instances involved in the JSON output of the API queries. | ||
|
@@ -47,3 +64,14 @@ stakeSnapshotToPair Consensus.StakeSnapshot | |
, "stakeSet" .= ssSetPool | ||
, "stakeGo" .= ssGoPool | ||
] | ||
|
||
instance ToJSON MIRPot where | ||
toJSON pot = toJSON @Text $ | ||
case pot of | ||
ReservesMIR -> "reserves" | ||
TreasuryMIR -> "treasury" | ||
|
||
instance Crypto.Crypto crypto => ToJSON (Ledger.WitVKey 'Ledger.Witness crypto) where | ||
toJSON = toJSON . Text.decodeLatin1 . Base16.encode . serialize' . prefixWithTag | ||
where | ||
prefixWithTag wit = encodeListLen 2 <> encodeWord 0 <> toCBOR wit |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
{-# LANGUAGE DerivingVia #-} | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE RecordWildCards #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
|
@@ -538,7 +537,7 @@ data ProtocolParametersUpdate = | |
-- /Introduced in Babbage. Supercedes 'protocolUpdateUTxOCostPerWord'/ | ||
protocolUpdateUTxOCostPerByte :: Maybe Lovelace | ||
} | ||
deriving (Eq, Show) | ||
deriving (Eq, Generic, Show) | ||
|
||
instance Semigroup ProtocolParametersUpdate where | ||
ppu1 <> ppu2 = | ||
|
@@ -673,6 +672,37 @@ instance FromCBOR ProtocolParametersUpdate where | |
<*> fromCBOR | ||
<*> fromCBOR | ||
|
||
instance ToJSON ProtocolParametersUpdate where | ||
toJSON ref = | ||
object $ | ||
( ("protocolVersion" ..=? protocolUpdateProtocolVersion ref) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we have |
||
. ("decentralization" ..=? protocolUpdateDecentralization ref) | ||
. ("extraPraosEntropy" ..=? protocolUpdateExtraPraosEntropy ref) | ||
. ("maxBlockHeaderSize" ..=? protocolUpdateMaxBlockHeaderSize ref) | ||
. ("maxBlockBodySize" ..=? protocolUpdateMaxBlockBodySize ref) | ||
. ("maxTxSize" ..=? protocolUpdateMaxTxSize ref) | ||
. ("txFeeFixed" ..=? protocolUpdateTxFeeFixed ref) | ||
. ("txFeePerByte" ..=? protocolUpdateTxFeePerByte ref) | ||
. ("minUTxOValue" ..=? protocolUpdateMinUTxOValue ref) | ||
. ("stakeAddressDeposit" ..=? protocolUpdateStakeAddressDeposit ref) | ||
. ("stakePoolDeposit" ..=? protocolUpdateStakePoolDeposit ref) | ||
. ("minPoolCost" ..=? protocolUpdateMinPoolCost ref) | ||
. ("poolRetireMaxEpoch" ..=? protocolUpdatePoolRetireMaxEpoch ref) | ||
. ("stakePoolTargetNum" ..=? protocolUpdateStakePoolTargetNum ref) | ||
. ("poolPledgeInfluence" ..=? protocolUpdatePoolPledgeInfluence ref) | ||
. ("monetaryExpansion" ..=? protocolUpdateMonetaryExpansion ref) | ||
. ("treasuryCut" ..=? protocolUpdateTreasuryCut ref) | ||
. ("utxoCostPerWord" ..=? protocolUpdateUTxOCostPerWord ref) | ||
. ("costModels" ..= protocolUpdateCostModels ref) | ||
. ("prices" ..=? protocolUpdatePrices ref) | ||
. ("maxTxExUnits" ..=? protocolUpdateMaxTxExUnits ref) | ||
. ("maxBlockExUnits" ..=? protocolUpdateMaxBlockExUnits ref) | ||
. ("maxValueSize" ..=? protocolUpdateMaxValueSize ref) | ||
. ("collateralPercent" ..=? protocolUpdateCollateralPercent ref) | ||
. ("maxCollateralInputs" ..=? protocolUpdateMaxCollateralInputs ref) | ||
. ("utxoCostPerByte" ..=? protocolUpdateUTxOCostPerByte ref) | ||
) [] | ||
|
||
|
||
-- ---------------------------------------------------------------------------- | ||
-- Praos nonce | ||
|
@@ -778,7 +808,7 @@ fromAlonzoPrices Alonzo.Prices{Alonzo.prSteps, Alonzo.prMem} = | |
|
||
newtype CostModel = CostModel [Integer] | ||
deriving (Eq, Show) | ||
deriving newtype (ToCBOR, FromCBOR) | ||
deriving newtype (ToCBOR, FromCBOR, ToJSON) | ||
|
||
newtype CostModels = CostModels { unCostModels :: Map AnyPlutusScriptVersion CostModel } | ||
deriving (Eq, Show) | ||
|
@@ -845,7 +875,7 @@ data UpdateProposal = | |
UpdateProposal | ||
!(Map (Hash GenesisKey) ProtocolParametersUpdate) | ||
!EpochNo | ||
deriving stock (Eq, Show) | ||
deriving stock (Eq, Generic, Show) | ||
deriving anyclass SerialiseAsCBOR | ||
|
||
instance HasTypeProxy UpdateProposal where | ||
|
@@ -868,6 +898,13 @@ instance FromCBOR UpdateProposal where | |
<$> fromCBOR | ||
<*> fromCBOR | ||
|
||
instance ToJSON UpdateProposal where | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not convert |
||
toJSON (UpdateProposal updates epoch) = | ||
object | ||
[ "updates" .= Map.mapKeys serialiseToRawBytesHexText updates | ||
, "epoch" .= epoch | ||
] | ||
|
||
makeShelleyUpdateProposal :: ProtocolParametersUpdate | ||
-> [Hash GenesisKey] | ||
-> EpochNo | ||
|
@@ -1247,7 +1284,7 @@ unbundleLedgerShelleyBasedProtocolParams | |
:: ShelleyBasedEra era | ||
-> BundledProtocolParameters era | ||
-> Ledger.PParams (ShelleyLedgerEra era) | ||
unbundleLedgerShelleyBasedProtocolParams = \case | ||
unbundleLedgerShelleyBasedProtocolParams era = case era of | ||
ShelleyBasedEraShelley -> \(BundleAsShelleyBasedProtocolParameters _ _ lpp) -> lpp | ||
ShelleyBasedEraAllegra -> \(BundleAsShelleyBasedProtocolParameters _ _ lpp) -> lpp | ||
ShelleyBasedEraMary -> \(BundleAsShelleyBasedProtocolParameters _ _ lpp) -> lpp | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if it would be possible here to change constructors to records and then use genericToJSON with
constructorTagModifier
instead of manual instances?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we prefer explicit instances. @Jimbo4350 do you have an opinion on
genericToJSON
?