Skip to content

Commit

Permalink
Delete TxFeesExplicitInEra and TxFeesImplicitInEra
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Sep 20, 2023
1 parent ee7c07a commit 37d672e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 95 deletions.
8 changes: 4 additions & 4 deletions cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -720,10 +720,10 @@ genTxTotalCollateral era =
TxTotalCollateral supp <$> genPositiveLovelace

genTxFee :: CardanoEra era -> Gen (TxFee era)
genTxFee era =
case txFeesExplicitInEra era of
Left supported -> pure (TxFeeImplicit supported)
Right supported -> TxFeeExplicit supported <$> genLovelace
genTxFee =
caseByronOrShelleyBasedEra
(pure . TxFeeImplicit)
(\w -> TxFeeExplicit w <$> genLovelace)

genTxBody :: IsCardanoEra era => CardanoEra era -> Gen (TxBody era)
genTxBody era = do
Expand Down
11 changes: 9 additions & 2 deletions cardano-api/internal/Cardano/Api/Eras/Case.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE EmptyCase #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
Expand All @@ -11,10 +12,13 @@ module Cardano.Api.Eras.Case
, caseShelleyToMaryOrAlonzoEraOnwards
, caseShelleyToAlonzoOrBabbageEraOnwards
, caseShelleyToBabbageOrConwayEraOnwards

, noByronEraInShelleyBasedEra
) where

import Cardano.Api.Eon.AlonzoEraOnwards
import Cardano.Api.Eon.BabbageEraOnwards
import Cardano.Api.Eon.ByronEraOnly
import Cardano.Api.Eon.ConwayEraOnwards
import Cardano.Api.Eon.ShelleyBasedEra
import Cardano.Api.Eon.ShelleyToAlonzoEra
Expand All @@ -24,12 +28,12 @@ import Cardano.Api.Eras.Constraints
import Cardano.Api.Eras.Core

caseByronOrShelleyBasedEra :: ()
=> (CardanoEra ByronEra -> a)
=> (ByronEraOnly era -> a)
-> (ShelleyBasedEraConstraints era => ShelleyBasedEra era -> a)
-> CardanoEra era
-> a
caseByronOrShelleyBasedEra l r = \case
ByronEra -> l ByronEra
ByronEra -> l ByronEraOnlyByron
ShelleyEra -> r ShelleyBasedEraShelley
AllegraEra -> r ShelleyBasedEraAllegra
MaryEra -> r ShelleyBasedEraMary
Expand Down Expand Up @@ -75,3 +79,6 @@ caseShelleyToBabbageOrConwayEraOnwards l r = \case
ShelleyBasedEraAlonzo -> l ShelleyToBabbageEraAlonzo
ShelleyBasedEraBabbage -> l ShelleyToBabbageEraBabbage
ShelleyBasedEraConway -> r ConwayEraOnwardsConway

noByronEraInShelleyBasedEra :: ShelleyBasedEra era -> ByronEraOnly era -> a
noByronEraInShelleyBasedEra sbe ByronEraOnlyByron = case sbe of {}
9 changes: 3 additions & 6 deletions cardano-api/internal/Cardano/Api/Fees.hs
Original file line number Diff line number Diff line change
Expand Up @@ -953,9 +953,6 @@ makeTransactionBodyAutoBalance systemstart history lpp@(LedgerProtocolParameters

txbodycontent1 <- substituteExecutionUnits sbe exUnitsMap' txbodycontent

explicitTxFees <- first (const TxBodyErrorByronEraNotSupported) $
txFeesExplicitInEra era'

-- Make a txbody that we will use for calculating the fees. For the purpose
-- of fees we just need to make a txbody of the right size in bytes. We do
-- not need the right values for the fee or change output. We use
Expand Down Expand Up @@ -985,7 +982,7 @@ makeTransactionBodyAutoBalance systemstart history lpp@(LedgerProtocolParameters
let (dummyCollRet, dummyTotColl) = maybeDummyTotalCollAndCollReturnOutput txbodycontent changeaddr
txbody1 <- first TxBodyError $ -- TODO: impossible to fail now
createAndValidateTransactionBody txbodycontent1 {
txFee = TxFeeExplicit explicitTxFees $ Lovelace (2^(32 :: Integer) - 1),
txFee = TxFeeExplicit sbe $ Lovelace (2^(32 :: Integer) - 1),
txOuts = TxOut changeaddr
changeTxOut
TxOutDatumNone ReferenceScriptNone
Expand Down Expand Up @@ -1014,7 +1011,7 @@ makeTransactionBodyAutoBalance systemstart history lpp@(LedgerProtocolParameters
-- we need to calculate.
txbody2 <- first TxBodyError $ -- TODO: impossible to fail now
createAndValidateTransactionBody txbodycontent1 {
txFee = TxFeeExplicit explicitTxFees fee,
txFee = TxFeeExplicit sbe fee,
txReturnCollateral = retColl,
txTotalCollateral = reqCol
}
Expand All @@ -1035,7 +1032,7 @@ makeTransactionBodyAutoBalance systemstart history lpp@(LedgerProtocolParameters
-- Yes this could be an over-estimate by a few bytes if the fee or change
-- would fit within 2^16-1. That's a possible optimisation.
let finalTxBodyContent = txbodycontent1 {
txFee = TxFeeExplicit explicitTxFees fee,
txFee = TxFeeExplicit sbe fee,
txOuts = accountForNoChange
(TxOut changeaddr balance TxOutDatumNone ReferenceScriptNone)
(txOuts txbodycontent),
Expand Down
136 changes: 56 additions & 80 deletions cardano-api/internal/Cardano/Api/TxBody.hs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ module Cardano.Api.TxBody (
CollateralSupportedInEra(..),
MultiAssetSupportedInEra(..),
OnlyAdaSupportedInEra(..),
TxFeesExplicitInEra(..),
TxFeesImplicitInEra(..),
ValidityUpperBoundSupportedInEra(..),
ValidityNoUpperBoundSupportedInEra(..),
ValidityLowerBoundSupportedInEra(..),
Expand All @@ -134,7 +132,6 @@ module Cardano.Api.TxBody (
-- ** Feature availability functions
collateralSupportedInEra,
multiAssetSupportedInEra,
txFeesExplicitInEra,
validityUpperBoundSupportedInEra,
validityNoUpperBoundSupportedInEra,
validityLowerBoundSupportedInEra,
Expand Down Expand Up @@ -186,10 +183,12 @@ module Cardano.Api.TxBody (
import Cardano.Api.Address
import Cardano.Api.Certificate
import Cardano.Api.Convenience.Constraints
import Cardano.Api.Eon.ByronEraOnly
import Cardano.Api.Eon.ConwayEraOnwards
import Cardano.Api.Eon.ShelleyBasedEra
import Cardano.Api.EraCast
import Cardano.Api.Eras
import Cardano.Api.Eras.Case
import Cardano.Api.Eras.Constraints
import Cardano.Api.Error
import Cardano.Api.Feature
Expand Down Expand Up @@ -989,46 +988,46 @@ multiAssetSupportedInEra BabbageEra = Right MultiAssetInBabbageEra
multiAssetSupportedInEra ConwayEra = Right MultiAssetInConwayEra


-- | A representation of whether the era requires explicitly specified fees in
-- transactions.
--
-- The Byron era tx fees are implicit (as the difference bettween the sum of
-- outputs and sum of inputs), but all later eras the fees are specified in the
-- transaction explicitly.
--
data TxFeesExplicitInEra era where

TxFeesExplicitInShelleyEra :: TxFeesExplicitInEra ShelleyEra
TxFeesExplicitInAllegraEra :: TxFeesExplicitInEra AllegraEra
TxFeesExplicitInMaryEra :: TxFeesExplicitInEra MaryEra
TxFeesExplicitInAlonzoEra :: TxFeesExplicitInEra AlonzoEra
TxFeesExplicitInBabbageEra :: TxFeesExplicitInEra BabbageEra
TxFeesExplicitInConwayEra :: TxFeesExplicitInEra ConwayEra

deriving instance Eq (TxFeesExplicitInEra era)
deriving instance Show (TxFeesExplicitInEra era)

-- | A representation of whether the era requires implicitly specified fees in
-- transactions.
--
-- This is the negation of 'TxFeesExplicitInEra'.
--
data TxFeesImplicitInEra era where
TxFeesImplicitInByronEra :: TxFeesImplicitInEra ByronEra

deriving instance Eq (TxFeesImplicitInEra era)
deriving instance Show (TxFeesImplicitInEra era)

txFeesExplicitInEra :: CardanoEra era
-> Either (TxFeesImplicitInEra era)
(TxFeesExplicitInEra era)
txFeesExplicitInEra ByronEra = Left TxFeesImplicitInByronEra
txFeesExplicitInEra ShelleyEra = Right TxFeesExplicitInShelleyEra
txFeesExplicitInEra AllegraEra = Right TxFeesExplicitInAllegraEra
txFeesExplicitInEra MaryEra = Right TxFeesExplicitInMaryEra
txFeesExplicitInEra AlonzoEra = Right TxFeesExplicitInAlonzoEra
txFeesExplicitInEra BabbageEra = Right TxFeesExplicitInBabbageEra
txFeesExplicitInEra ConwayEra = Right TxFeesExplicitInConwayEra
-- -- | A representation of whether the era requires explicitly specified fees in
-- -- transactions.
-- --
-- -- The Byron era tx fees are implicit (as the difference bettween the sum of
-- -- outputs and sum of inputs), but all later eras the fees are specified in the
-- -- transaction explicitly.
-- --
-- data TxFeesExplicitInEra era where

-- TxFeesExplicitInShelleyEra :: TxFeesExplicitInEra ShelleyEra
-- TxFeesExplicitInAllegraEra :: TxFeesExplicitInEra AllegraEra
-- TxFeesExplicitInMaryEra :: TxFeesExplicitInEra MaryEra
-- TxFeesExplicitInAlonzoEra :: TxFeesExplicitInEra AlonzoEra
-- TxFeesExplicitInBabbageEra :: TxFeesExplicitInEra BabbageEra
-- TxFeesExplicitInConwayEra :: TxFeesExplicitInEra ConwayEra

-- deriving instance Eq (TxFeesExplicitInEra era)
-- deriving instance Show (TxFeesExplicitInEra era)

-- -- | A representation of whether the era requires implicitly specified fees in
-- -- transactions.
-- --
-- -- This is the negation of 'TxFeesExplicitInEra'.
-- --
-- data TxFeesImplicitInEra era where
-- TxFeesImplicitInByronEra :: TxFeesImplicitInEra ByronEra

-- deriving instance Eq (TxFeesImplicitInEra era)
-- deriving instance Show (TxFeesImplicitInEra era)

-- txFeesExplicitInEra :: CardanoEra era
-- -> Either (TxFeesImplicitInEra era)
-- (TxFeesExplicitInEra era)
-- txFeesExplicitInEra ByronEra = Left TxFeesImplicitInByronEra
-- txFeesExplicitInEra ShelleyEra = Right TxFeesExplicitInShelleyEra
-- txFeesExplicitInEra AllegraEra = Right TxFeesExplicitInAllegraEra
-- txFeesExplicitInEra MaryEra = Right TxFeesExplicitInMaryEra
-- txFeesExplicitInEra AlonzoEra = Right TxFeesExplicitInAlonzoEra
-- txFeesExplicitInEra BabbageEra = Right TxFeesExplicitInBabbageEra
-- txFeesExplicitInEra ConwayEra = Right TxFeesExplicitInConwayEra


-- | A representation of whether the era supports transactions with an upper
Expand Down Expand Up @@ -1568,24 +1567,19 @@ parseHash asType = do
--

data TxFee era where
TxFeeImplicit :: ByronEraOnly era -> TxFee era

TxFeeImplicit :: TxFeesImplicitInEra era -> TxFee era

TxFeeExplicit :: TxFeesExplicitInEra era -> Lovelace -> TxFee era
TxFeeExplicit :: ShelleyBasedEra era -> Lovelace -> TxFee era

deriving instance Eq (TxFee era)
deriving instance Show (TxFee era)

defaultTxFee :: forall era. IsCardanoEra era => TxFee era
defaultTxFee = case cardanoEra @era of
ByronEra -> TxFeeImplicit TxFeesImplicitInByronEra
ShelleyEra -> TxFeeExplicit TxFeesExplicitInShelleyEra mempty
AllegraEra -> TxFeeExplicit TxFeesExplicitInAllegraEra mempty
MaryEra -> TxFeeExplicit TxFeesExplicitInMaryEra mempty
AlonzoEra -> TxFeeExplicit TxFeesExplicitInAlonzoEra mempty
BabbageEra -> TxFeeExplicit TxFeesExplicitInBabbageEra mempty
ConwayEra -> TxFeeExplicit TxFeesExplicitInConwayEra mempty

defaultTxFee =
caseByronOrShelleyBasedEra
TxFeeImplicit
(\w -> TxFeeExplicit w mempty)
(cardanoEra @era)

-- ----------------------------------------------------------------------------
-- Transaction validity range
Expand Down Expand Up @@ -2965,25 +2959,9 @@ fromLedgerTxReturnCollateral sbe txbody =
fromLedgerTxFee
:: ShelleyBasedEra era -> Ledger.TxBody (ShelleyLedgerEra era) -> TxFee era
fromLedgerTxFee sbe body =
case sbe of
ShelleyBasedEraShelley ->
TxFeeExplicit TxFeesExplicitInShelleyEra $
fromShelleyLovelace $ body ^. L.feeTxBodyL
ShelleyBasedEraAllegra ->
TxFeeExplicit TxFeesExplicitInAllegraEra $
fromShelleyLovelace $ body ^. L.feeTxBodyL
ShelleyBasedEraMary ->
TxFeeExplicit TxFeesExplicitInMaryEra $
fromShelleyLovelace $ body ^. L.feeTxBodyL
ShelleyBasedEraAlonzo ->
TxFeeExplicit TxFeesExplicitInAlonzoEra $
fromShelleyLovelace $ body ^. L.feeTxBodyL
ShelleyBasedEraBabbage ->
TxFeeExplicit TxFeesExplicitInBabbageEra $
fromShelleyLovelace $ body ^. L.feeTxBodyL
ShelleyBasedEraConway ->
TxFeeExplicit TxFeesExplicitInConwayEra $
fromShelleyLovelace $ body ^. L.feeTxBodyL
shelleyBasedEraConstraints sbe
$ TxFeeExplicit sbe
$ fromShelleyLovelace $ body ^. L.feeTxBodyL

fromLedgerTxValidityRange
:: ShelleyBasedEra era
Expand Down Expand Up @@ -3398,7 +3376,7 @@ getByronTxBodyContent (Annotated Byron.UnsafeTx{txInputs, txOutputs} _) =
, txOuts = fromByronTxOut <$> toList txOutputs
, txReturnCollateral = TxReturnCollateralNone
, txTotalCollateral = TxTotalCollateralNone
, txFee = TxFeeImplicit TxFeesImplicitInByronEra
, txFee = TxFeeImplicit ByronEraOnlyByron
, txValidityRange = (TxValidityNoLowerBound, TxValidityNoUpperBound ValidityNoUpperBoundInByronEra)
, txMetadata = TxMetadataNone
, txAuxScripts = TxAuxScriptsNone
Expand Down Expand Up @@ -3460,11 +3438,9 @@ convWithdrawals txWithdrawals =
TxWithdrawals _ ws -> toShelleyWithdrawal ws

convTransactionFee :: ShelleyBasedEra era -> TxFee era -> Ledger.Coin
convTransactionFee sbe txFee =
case txFee of
TxFeeImplicit TxFeesImplicitInByronEra -> case sbe of {}
TxFeeExplicit _ fee -> toShelleyLovelace fee

convTransactionFee sbe = \case
TxFeeImplicit w -> noByronEraInShelleyBasedEra sbe w
TxFeeExplicit _ fee -> toShelleyLovelace fee

convValidityInterval
:: (TxValidityLowerBound era, TxValidityUpperBound era)
Expand Down
3 changes: 0 additions & 3 deletions cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,6 @@ module Cardano.Api (
CollateralSupportedInEra(..),
MultiAssetSupportedInEra(..),
OnlyAdaSupportedInEra(..),
TxFeesExplicitInEra(..),
TxFeesImplicitInEra(..),
ValidityUpperBoundSupportedInEra(..),
ValidityNoUpperBoundSupportedInEra(..),
ValidityLowerBoundSupportedInEra(..),
Expand All @@ -364,7 +362,6 @@ module Cardano.Api (
-- ** Feature availability functions
collateralSupportedInEra,
multiAssetSupportedInEra,
txFeesExplicitInEra,
validityUpperBoundSupportedInEra,
validityNoUpperBoundSupportedInEra,
validityLowerBoundSupportedInEra,
Expand Down

0 comments on commit 37d672e

Please sign in to comment.