22{-# LANGUAGE GADTs #-}
33{-# LANGUAGE MultiParamTypeClasses #-}
44{-# LANGUAGE OverloadedLabels #-}
5+ {-# LANGUAGE OverloadedLists #-}
6+ {-# LANGUAGE RankNTypes #-}
57{-# LANGUAGE ScopedTypeVariables #-}
68{-# LANGUAGE TypeApplications #-}
79{-# OPTIONS_GHC -Wno-orphans #-}
@@ -20,10 +22,18 @@ import Cardano.Api.Tx
2022import Cardano.Api.Value
2123import Cardano.Rpc.Proto.Api.UtxoRpc.Query qualified as UtxoRpc
2224
25+ import Cardano.Ledger.Api qualified as L
26+ import Cardano.Ledger.BaseTypes qualified as L
27+ import Cardano.Ledger.Conway.PParams qualified as L
28+ import Cardano.Ledger.Plutus qualified as L
29+
2330import RIO hiding (toList )
2431
32+ import Data.Default
33+ import Data.Map.Strict qualified as M
2534import Data.ProtoLens (defMessage )
26- import Data.Ratio (Ratio , denominator , numerator , (%) )
35+ import Data.ProtoLens.Message (Message )
36+ import Data.Ratio (denominator , numerator , (%) )
2737import Data.Text.Encoding qualified as T
2838import GHC.IsList
2939import Network.GRPC.Spec
@@ -34,12 +44,11 @@ import Network.GRPC.Spec
3444
3545-- It's easier to use 'Proto a' wrappers for RPC types, because it makes lens automatically available.
3646
37- -- TODO: write property tests for bijections
38-
39- instance Inject (Proto UtxoRpc. RationalNumber ) (Ratio Integer ) where
47+ instance Inject (Proto UtxoRpc. RationalNumber ) Rational where
4048 inject r = r ^. # numerator . to fromIntegral % r ^. # denominator . to fromIntegral
4149
42- instance Inject (Ratio Integer ) (Proto UtxoRpc. RationalNumber ) where
50+ -- NB. this clips value in Integer -> Int64/Word64 conversion here
51+ instance Inject Rational (Proto UtxoRpc. RationalNumber ) where
4352 inject r =
4453 defMessage
4554 & # numerator .~ fromIntegral (numerator r)
@@ -121,6 +130,80 @@ instance IsCardanoEra era => Inject (UTxO era) [Proto UtxoRpc.AnyUtxoData] where
121130 & # txoRef .~ inject txIn
122131 & # cardano .~ protoTxOut
123132
133+ instance L. ConwayEraPParams lera => Inject (L. PParams lera ) (Proto UtxoRpc. PParams ) where
134+ inject pparams = do
135+ let pparamsCostModels :: Map L. Language [Int64 ] =
136+ L. getCostModelParams <$> pparams ^. L. ppCostModelsL . to L. costModelsValid
137+ poolVotingThresholds :: L. PoolVotingThresholds =
138+ pparams ^. L. ppPoolVotingThresholdsL
139+ drepVotingThresholds :: L. DRepVotingThresholds =
140+ pparams ^. L. ppDRepVotingThresholdsL
141+ def
142+ & # coinsPerUtxoByte .~ pparams ^. L. ppCoinsPerUTxOByteL . to L. unCoinPerByte . to fromIntegral
143+ & # maxTxSize .~ pparams ^. L. ppMaxTxSizeL . to fromIntegral
144+ & # minFeeCoefficient .~ pparams ^. L. ppMinFeeBL . to fromIntegral
145+ & # minFeeConstant .~ pparams ^. L. ppMinFeeAL . to fromIntegral
146+ & # maxBlockBodySize .~ pparams ^. L. ppMaxBBSizeL . to fromIntegral
147+ & # maxBlockHeaderSize .~ pparams ^. L. ppMaxBHSizeL . to fromIntegral
148+ & # stakeKeyDeposit .~ pparams ^. L. ppKeyDepositL . to fromIntegral
149+ & # poolDeposit .~ pparams ^. L. ppPoolDepositL . to fromIntegral
150+ & # poolRetirementEpochBound .~ pparams ^. L. ppEMaxL . to L. unEpochInterval . to fromIntegral
151+ & # desiredNumberOfPools .~ pparams ^. L. ppNOptL . to fromIntegral
152+ & # poolInfluence .~ pparams ^. L. ppA0L . to L. unboundRational . to inject
153+ & # monetaryExpansion .~ pparams ^. L. ppRhoL . to L. unboundRational . to inject
154+ & # treasuryExpansion .~ pparams ^. L. ppTauL . to L. unboundRational . to inject
155+ & # minPoolCost .~ pparams ^. L. ppMinPoolCostL . to fromIntegral
156+ & # protocolVersion . # major .~ pparams ^. L. ppProtocolVersionL . to L. pvMajor . to L. getVersion
157+ & # protocolVersion . # minor .~ pparams ^. L. ppProtocolVersionL . to L. pvMinor . to fromIntegral
158+ & # maxValueSize .~ pparams ^. L. ppMaxValSizeL . to fromIntegral
159+ & # collateralPercentage .~ pparams ^. L. ppCollateralPercentageL . to fromIntegral
160+ & # maxCollateralInputs .~ pparams ^. L. ppMaxCollateralInputsL . to fromIntegral
161+ & # costModels . # plutusV1 . # values .~ (join . maybeToList) (M. lookup L. PlutusV1 pparamsCostModels)
162+ & # costModels . # plutusV2 . # values .~ (join . maybeToList) (M. lookup L. PlutusV2 pparamsCostModels)
163+ & # costModels . # plutusV3 . # values .~ (join . maybeToList) (M. lookup L. PlutusV3 pparamsCostModels)
164+ & # prices . # steps .~ pparams ^. L. ppPricesL . to L. prSteps . to L. unboundRational . to inject
165+ & # prices . # memory .~ pparams ^. L. ppPricesL . to L. prMem . to L. unboundRational . to inject
166+ & # maxExecutionUnitsPerTransaction .~ pparams ^. L. ppMaxTxExUnitsL . to inject
167+ & # maxExecutionUnitsPerBlock .~ pparams ^. L. ppMaxBlockExUnitsL . to inject
168+ & # minFeeScriptRefCostPerByte
169+ .~ pparams ^. L. ppMinFeeRefScriptCostPerByteL . to L. unboundRational . to inject
170+ & # poolVotingThresholds . # thresholds
171+ .~ ( inject . L. unboundRational
172+ -- order taken from https://github.com/cardano-foundation/CIPs/blob/acb4b2348c968003dfc370cd3769615bfca1f159/CIP-1694/README.md#requirements
173+ <$> [ poolVotingThresholds ^. L. pvtMotionNoConfidenceL
174+ , poolVotingThresholds ^. L. pvtCommitteeNormalL
175+ , poolVotingThresholds ^. L. pvtCommitteeNoConfidenceL
176+ , poolVotingThresholds ^. L. pvtHardForkInitiationL
177+ , poolVotingThresholds ^. L. pvtPPSecurityGroupL
178+ ]
179+ )
180+ & # drepVotingThresholds . # thresholds
181+ .~ ( inject . L. unboundRational
182+ -- order taken from https://github.com/cardano-foundation/CIPs/blob/acb4b2348c968003dfc370cd3769615bfca1f159/CIP-1694/README.md#requirements
183+ <$> [ drepVotingThresholds ^. L. dvtMotionNoConfidenceL
184+ , drepVotingThresholds ^. L. dvtCommitteeNormalL
185+ , drepVotingThresholds ^. L. dvtCommitteeNoConfidenceL
186+ , drepVotingThresholds ^. L. dvtUpdateToConstitutionL
187+ , drepVotingThresholds ^. L. dvtHardForkInitiationL
188+ , drepVotingThresholds ^. L. dvtPPNetworkGroupL
189+ , drepVotingThresholds ^. L. dvtPPEconomicGroupL
190+ , drepVotingThresholds ^. L. dvtPPTechnicalGroupL
191+ , drepVotingThresholds ^. L. dvtPPGovGroupL
192+ , drepVotingThresholds ^. L. dvtTreasuryWithdrawalL
193+ ]
194+ )
195+ & # minCommitteeSize .~ pparams ^. L. ppCommitteeMinSizeL . to fromIntegral
196+ & # committeeTermLimit
197+ .~ pparams ^. L. ppCommitteeMaxTermLengthL . to L. unEpochInterval . to fromIntegral
198+ & # governanceActionValidityPeriod
199+ .~ pparams ^. L. ppGovActionLifetimeL . to L. unEpochInterval . to fromIntegral
200+ & # governanceActionDeposit .~ pparams ^. L. ppGovActionDepositL . to fromIntegral
201+ & # drepDeposit .~ pparams ^. L. ppDRepDepositL . to fromIntegral
202+ & # drepInactivityPeriod .~ pparams ^. L. ppDRepActivityL . to L. unEpochInterval . to fromIntegral
203+
204+ instance Message a => Default (Proto a ) where
205+ def = defMessage
206+
124207-----------
125208-- Errors
126209-----------
@@ -129,3 +212,6 @@ instance IsCardanoEra era => Inject (UTxO era) [Proto UtxoRpc.AnyUtxoData] where
129212
130213instance Error StringException where
131214 prettyError = pshow
215+
216+ instance IsString e => MonadFail (Either e ) where
217+ fail = Left . fromString
0 commit comments