1
- {-# LANGUAGE ConstraintKinds #-}
2
1
{-# LANGUAGE CPP #-}
2
+ {-# LANGUAGE ConstraintKinds #-}
3
3
{-# LANGUAGE DataKinds #-}
4
4
{-# LANGUAGE EmptyCase #-}
5
5
{-# LANGUAGE FlexibleContexts #-}
10
10
{-# LANGUAGE StandaloneDeriving #-}
11
11
12
12
{-# OPTIONS_GHC -Wno-unticked-promoted-constructors #-}
13
- {-# LANGUAGE AllowAmbiguousTypes #-}
14
13
15
14
-- | Fee calculation
16
15
--
@@ -42,6 +41,7 @@ module Cardano.Api.Fees (
42
41
mapTxScriptWitnesses ,
43
42
) where
44
43
44
+ import Control.Monad (forM_ )
45
45
import qualified Data.Array as Array
46
46
import Data.Bifunctor (bimap , first )
47
47
import qualified Data.ByteString as BS
@@ -102,7 +102,6 @@ import Cardano.Api.Address
102
102
import Cardano.Api.Certificate
103
103
import Cardano.Api.Eras
104
104
import Cardano.Api.Error
105
- import Cardano.Api.Modes
106
105
import Cardano.Api.NetworkId
107
106
import Cardano.Api.ProtocolParameters
108
107
import Cardano.Api.Query
@@ -238,31 +237,30 @@ estimateTransactionFee _ _ _ (ByronTx _) =
238
237
--
239
238
evaluateTransactionFee :: forall era .
240
239
IsShelleyBasedEra era
241
- => ProtocolParameters
240
+ => BundledProtocolParameters era
242
241
-> TxBody era
243
242
-> Word -- ^ The number of Shelley key witnesses
244
243
-> Word -- ^ The number of Byron key witnesses
245
244
-> Lovelace
246
245
evaluateTransactionFee _ _ _ byronwitcount | byronwitcount > 0 =
247
246
error " evaluateTransactionFee: TODO support Byron key witnesses"
248
247
249
- evaluateTransactionFee pparams txbody keywitcount _byronwitcount =
248
+ evaluateTransactionFee bpparams txbody keywitcount _byronwitcount =
250
249
case makeSignedTransaction [] txbody of
251
250
ByronTx {} -> case shelleyBasedEra :: ShelleyBasedEra era of {}
252
251
-- TODO: we could actually support Byron here, it'd be different but simpler
253
252
254
- ShelleyTx era tx -> withLedgerConstraints era (evalShelleyBasedEra era tx)
253
+ ShelleyTx era tx -> withLedgerConstraints era (evalShelleyBasedEra tx)
255
254
where
256
255
evalShelleyBasedEra :: forall ledgerera .
257
256
ShelleyLedgerEra era ~ ledgerera
258
257
=> Ledger. CLI ledgerera
259
- => ShelleyBasedEra era
260
- -> Ledger. Tx ledgerera
258
+ => Ledger. Tx ledgerera
261
259
-> Lovelace
262
- evalShelleyBasedEra era tx =
260
+ evalShelleyBasedEra tx =
263
261
fromShelleyLovelace $
264
262
Ledger. evaluateTransactionFee
265
- (toLedgerPParams era pparams )
263
+ (unbundleLedgerShelleyBasedProtocolParams shelleyBasedEra bpparams )
266
264
tx
267
265
keywitcount
268
266
@@ -505,40 +503,41 @@ instance Error TransactionValidityError where
505
503
-- are actually used.
506
504
--
507
505
evaluateTransactionExecutionUnits
508
- :: forall era mode .
509
- EraInMode era mode
510
- -> SystemStart
506
+ :: forall era .
507
+ SystemStart
511
508
-> LedgerEpochInfo
512
- -> ProtocolParameters
509
+ -> BundledProtocolParameters era
513
510
-> UTxO era
514
511
-> TxBody era
515
512
-> Either TransactionValidityError
516
513
(Map ScriptWitnessIndex (Either ScriptExecutionError ExecutionUnits ))
517
- evaluateTransactionExecutionUnits _eraInMode systemstart (LedgerEpochInfo ledgerEpochInfo)
518
- pparams utxo txbody =
514
+ evaluateTransactionExecutionUnits systemstart epochInfo bpp utxo txbody =
519
515
case makeSignedTransaction [] txbody of
520
- ByronTx {} -> evalPreAlonzo
521
- ShelleyTx era tx' ->
522
- case era of
516
+ ByronTx {} -> evalPreAlonzo
517
+ ShelleyTx sbe tx' ->
518
+ case sbe of
523
519
ShelleyBasedEraShelley -> evalPreAlonzo
524
520
ShelleyBasedEraAllegra -> evalPreAlonzo
525
521
ShelleyBasedEraMary -> evalPreAlonzo
526
- ShelleyBasedEraAlonzo -> evalAlonzo era tx'
522
+ ShelleyBasedEraAlonzo -> evalAlonzo sbe tx'
527
523
ShelleyBasedEraBabbage ->
528
- case collateralSupportedInEra $ shelleyBasedToCardanoEra era of
529
- Just supp -> obtainHasFieldConstraint supp $ evalBabbage era tx'
524
+ case collateralSupportedInEra $ shelleyBasedToCardanoEra sbe of
525
+ Just supp -> obtainHasFieldConstraint supp $ evalBabbage sbe tx'
530
526
Nothing -> return mempty
531
527
ShelleyBasedEraConway ->
532
- case collateralSupportedInEra $ shelleyBasedToCardanoEra era of
533
- Just supp -> obtainHasFieldConstraint supp $ evalConway era tx'
528
+ case collateralSupportedInEra $ shelleyBasedToCardanoEra sbe of
529
+ Just supp -> obtainHasFieldConstraint supp $ evalConway sbe tx'
534
530
Nothing -> return mempty
535
531
where
536
- -- Pre-Alonzo eras do not support languages with execution unit accounting.
532
+ LedgerEpochInfo ledgerEpochInfo = epochInfo
533
+
534
+ -- | Pre-Alonzo eras do not support languages with execution unit accounting.
537
535
evalPreAlonzo :: Either TransactionValidityError
538
536
(Map ScriptWitnessIndex
539
- (Either ScriptExecutionError ExecutionUnits ))
537
+ (Either ScriptExecutionError ExecutionUnits ))
540
538
evalPreAlonzo = Right Map. empty
541
539
540
+
542
541
evalAlonzo :: forall ledgerera .
543
542
ShelleyLedgerEra era ~ ledgerera
544
543
=> ledgerera ~ Alonzo. AlonzoEra Ledger. StandardCrypto
@@ -551,9 +550,9 @@ evaluateTransactionExecutionUnits _eraInMode systemstart (LedgerEpochInfo ledger
551
550
(Map ScriptWitnessIndex
552
551
(Either ScriptExecutionError ExecutionUnits ))
553
552
evalAlonzo era tx = do
554
- cModelArray <- toAlonzoCostModelsArray (protocolParamCostModels pparams )
553
+ cModelArray <- toAlonzoCostModelsArray (protocolParamCostModels (unbundleProtocolParams bpp) )
555
554
case Alonzo. evaluateTransactionExecutionUnits
556
- (toLedgerPParams era pparams )
555
+ (unbundleLedgerShelleyBasedProtocolParams era bpp )
557
556
tx
558
557
(toLedgerUTxO era utxo)
559
558
ledgerEpochInfo
@@ -573,9 +572,9 @@ evaluateTransactionExecutionUnits _eraInMode systemstart (LedgerEpochInfo ledger
573
572
(Map ScriptWitnessIndex
574
573
(Either ScriptExecutionError ExecutionUnits ))
575
574
evalBabbage era tx = do
576
- costModelsArray <- toAlonzoCostModelsArray (protocolParamCostModels pparams )
575
+ costModelsArray <- toAlonzoCostModelsArray (protocolParamCostModels (unbundleProtocolParams bpp) )
577
576
case Alonzo. evaluateTransactionExecutionUnits
578
- (toLedgerPParams era pparams )
577
+ (unbundleLedgerShelleyBasedProtocolParams era bpp )
579
578
tx
580
579
(toLedgerUTxO era utxo)
581
580
ledgerEpochInfo
@@ -595,9 +594,9 @@ evaluateTransactionExecutionUnits _eraInMode systemstart (LedgerEpochInfo ledger
595
594
(Map ScriptWitnessIndex
596
595
(Either ScriptExecutionError ExecutionUnits ))
597
596
evalConway era tx = do
598
- costModelsArray <- toAlonzoCostModelsArray (protocolParamCostModels pparams )
597
+ costModelsArray <- toAlonzoCostModelsArray (protocolParamCostModels (unbundleProtocolParams bpp) )
599
598
case Alonzo. evaluateTransactionExecutionUnits
600
- (toLedgerPParams era pparams )
599
+ (toLedgerPParams era (unbundleProtocolParams bpp) )
601
600
tx
602
601
(toLedgerUTxO era utxo)
603
602
ledgerEpochInfo
@@ -662,7 +661,6 @@ evaluateTransactionExecutionUnits _eraInMode systemstart (LedgerEpochInfo ledger
662
661
obtainHasFieldConstraint CollateralInBabbageEra f = f
663
662
obtainHasFieldConstraint CollateralInConwayEra f = f
664
663
665
-
666
664
-- ----------------------------------------------------------------------------
667
665
-- Transaction balance
668
666
--
@@ -675,7 +673,7 @@ evaluateTransactionExecutionUnits _eraInMode systemstart (LedgerEpochInfo ledger
675
673
--
676
674
evaluateTransactionBalance :: forall era .
677
675
IsShelleyBasedEra era
678
- => ProtocolParameters
676
+ => BundledProtocolParameters era
679
677
-> Set PoolId
680
678
-> UTxO era
681
679
-> TxBody era
@@ -684,7 +682,7 @@ evaluateTransactionBalance _ _ _ (ByronTxBody _) =
684
682
case shelleyBasedEra :: ShelleyBasedEra era of {}
685
683
-- TODO: we could actually support Byron here, it'd be different but simpler
686
684
687
- evaluateTransactionBalance pparams poolids utxo
685
+ evaluateTransactionBalance bpp poolids utxo
688
686
(ShelleyTxBody era txbody _ _ _ _) =
689
687
withLedgerConstraints
690
688
era
@@ -716,7 +714,7 @@ evaluateTransactionBalance pparams poolids utxo
716
714
evalMultiAsset evidence =
717
715
TxOutValue evidence . fromMaryValue $
718
716
Ledger. evaluateTransactionBalance
719
- (toLedgerPParams era pparams )
717
+ (unbundleLedgerShelleyBasedProtocolParams era bpp )
720
718
(toLedgerUTxO era utxo)
721
719
isNewPool
722
720
txbody
@@ -731,7 +729,7 @@ evaluateTransactionBalance pparams poolids utxo
731
729
evalAdaOnly evidence =
732
730
TxOutAdaOnly evidence . fromShelleyLovelace
733
731
$ Ledger. evaluateTransactionBalance
734
- (toLedgerPParams era pparams )
732
+ (unbundleLedgerShelleyBasedProtocolParams era bpp )
735
733
(toLedgerUTxO era utxo)
736
734
isNewPool
737
735
txbody
@@ -952,10 +950,9 @@ data BalancedTxBody era
952
950
-- which can be queried from a local node.
953
951
--
954
952
makeTransactionBodyAutoBalance
955
- :: forall era mode .
953
+ :: forall era .
956
954
IsShelleyBasedEra era
957
- => EraInMode era mode
958
- -> SystemStart
955
+ => SystemStart
959
956
-> LedgerEpochInfo
960
957
-> ProtocolParameters
961
958
-> Set PoolId -- ^ The set of registered stake pools
@@ -964,7 +961,7 @@ makeTransactionBodyAutoBalance
964
961
-> AddressInEra era -- ^ Change address
965
962
-> Maybe Word -- ^ Override key witnesses
966
963
-> Either TxBodyErrorAutoBalance (BalancedTxBody era )
967
- makeTransactionBodyAutoBalance eraInMode systemstart history pparams
964
+ makeTransactionBodyAutoBalance systemstart history pparams
968
965
poolids utxo txbodycontent changeaddr mnkeys = do
969
966
970
967
-- Our strategy is to:
@@ -982,9 +979,8 @@ makeTransactionBodyAutoBalance eraInMode systemstart history pparams
982
979
983
980
exUnitsMap <- first TxBodyErrorValidityInterval $
984
981
evaluateTransactionExecutionUnits
985
- eraInMode
986
982
systemstart history
987
- pparams
983
+ bpparams
988
984
utxo
989
985
txbody0
990
986
@@ -1025,7 +1021,7 @@ makeTransactionBodyAutoBalance eraInMode systemstart history pparams
1025
1021
1026
1022
let nkeys = fromMaybe (estimateTransactionKeyWitnessCount txbodycontent1)
1027
1023
mnkeys
1028
- fee = evaluateTransactionFee pparams txbody1 nkeys 0 -- TODO: byron keys
1024
+ fee = evaluateTransactionFee bpparams txbody1 nkeys 0 -- TODO: byron keys
1029
1025
(retColl, reqCol) = calcReturnAndTotalCollateral
1030
1026
fee pparams (txInsCollateral txbodycontent)
1031
1027
(txReturnCollateral txbodycontent)
@@ -1041,9 +1037,9 @@ makeTransactionBodyAutoBalance eraInMode systemstart history pparams
1041
1037
txReturnCollateral = retColl,
1042
1038
txTotalCollateral = reqCol
1043
1039
}
1044
- let balance = evaluateTransactionBalance pparams poolids utxo txbody2
1040
+ let balance = evaluateTransactionBalance bpparams poolids utxo txbody2
1045
1041
1046
- mapM_ ( `checkMinUTxOValue` pparams ) $ txOuts txbodycontent1
1042
+ forM_ (txOuts txbodycontent1 ) $ \ txout -> checkMinUTxOValue txout bpparams
1047
1043
1048
1044
-- check if the balance is positive or negative
1049
1045
-- in one case we can produce change, in the other the inputs are insufficient
@@ -1078,6 +1074,8 @@ makeTransactionBodyAutoBalance eraInMode systemstart history pparams
1078
1074
createAndValidateTransactionBody finalTxBodyContent
1079
1075
return (BalancedTxBody finalTxBodyContent txbody3 (TxOut changeaddr balance TxOutDatumNone ReferenceScriptNone ) fee)
1080
1076
where
1077
+ bpparams = bundleProtocolParams era' pparams
1078
+
1081
1079
-- Essentially we check for the existence of collateral inputs. If they exist we
1082
1080
-- create a fictitious collateral return output. Why? Because we need to put dummy values
1083
1081
-- to get a fee estimate (i.e we overestimate the fee.)
@@ -1183,19 +1181,18 @@ makeTransactionBodyAutoBalance eraInMode systemstart history pparams
1183
1181
| txOutValueToLovelace balance < 0 =
1184
1182
Left . TxBodyErrorAdaBalanceNegative $ txOutValueToLovelace balance
1185
1183
| otherwise =
1186
- case checkMinUTxOValue (TxOut changeaddr balance TxOutDatumNone ReferenceScriptNone ) pparams of
1184
+ case checkMinUTxOValue (TxOut changeaddr balance TxOutDatumNone ReferenceScriptNone ) bpparams of
1187
1185
Left (TxBodyErrorMinUTxONotMet txOutAny minUTxO) ->
1188
1186
Left $ TxBodyErrorAdaBalanceTooSmall txOutAny minUTxO (txOutValueToLovelace balance)
1189
1187
Left err -> Left err
1190
1188
Right _ -> Right ()
1191
1189
1192
1190
checkMinUTxOValue
1193
1191
:: TxOut CtxTx era
1194
- -> ProtocolParameters
1192
+ -> BundledProtocolParameters era
1195
1193
-> Either TxBodyErrorAutoBalance ()
1196
- checkMinUTxOValue txout@ (TxOut _ v _ _) pparams' = do
1197
- minUTxO <- first TxBodyErrorMinUTxOMissingPParams
1198
- $ calculateMinimumUTxO era txout pparams'
1194
+ checkMinUTxOValue txout@ (TxOut _ v _ _) bpp = do
1195
+ minUTxO <- first TxBodyErrorMinUTxOMissingPParams $ calculateMinimumUTxO era txout bpp
1199
1196
if txOutValueToLovelace v >= minUTxO
1200
1197
then Right ()
1201
1198
else Left $ TxBodyErrorMinUTxONotMet
@@ -1356,36 +1353,33 @@ mapTxScriptWitnesses f txbodycontent@TxBodyContent {
1356
1353
calculateMinimumUTxO
1357
1354
:: ShelleyBasedEra era
1358
1355
-> TxOut CtxTx era
1359
- -> ProtocolParameters
1356
+ -> BundledProtocolParameters era
1360
1357
-> Either MinimumUTxOError Lovelace
1361
- calculateMinimumUTxO era txout@ (TxOut _ v _ _) pparams' =
1358
+ calculateMinimumUTxO era txout@ (TxOut _ v _ _) bpp =
1362
1359
case era of
1363
- ShelleyBasedEraShelley -> getMinUTxOPreAlonzo pparams'
1360
+ ShelleyBasedEraShelley -> getMinUTxOPreAlonzo (unbundleProtocolParams bpp)
1364
1361
ShelleyBasedEraAllegra -> calcMinUTxOAllegraMary
1365
1362
ShelleyBasedEraMary -> calcMinUTxOAllegraMary
1366
1363
ShelleyBasedEraAlonzo ->
1367
1364
let lTxOut = toShelleyTxOutAny era txout
1368
- babPParams = toAlonzoPParams pparams'
1369
- minUTxO = Shelley. evaluateMinLovelaceOutput babPParams lTxOut
1365
+ minUTxO = Shelley. evaluateMinLovelaceOutput (unbundleLedgerShelleyBasedProtocolParams era bpp) lTxOut
1370
1366
val = fromShelleyLovelace minUTxO
1371
1367
in Right val
1372
1368
ShelleyBasedEraBabbage ->
1373
1369
let lTxOut = toShelleyTxOutAny era txout
1374
- babPParams = toBabbagePParams pparams'
1375
- minUTxO = Shelley. evaluateMinLovelaceOutput babPParams lTxOut
1370
+ minUTxO = Shelley. evaluateMinLovelaceOutput (unbundleLedgerShelleyBasedProtocolParams era bpp) lTxOut
1376
1371
val = fromShelleyLovelace minUTxO
1377
1372
in Right val
1378
1373
ShelleyBasedEraConway ->
1379
1374
let lTxOut = toShelleyTxOutAny era txout
1380
- babPParams = toConwayPParams pparams'
1381
- minUTxO = Shelley. evaluateMinLovelaceOutput babPParams lTxOut
1375
+ minUTxO = Shelley. evaluateMinLovelaceOutput (unbundleLedgerShelleyBasedProtocolParams era bpp) lTxOut
1382
1376
val = fromShelleyLovelace minUTxO
1383
1377
in Right val
1384
1378
where
1385
1379
calcMinUTxOAllegraMary :: Either MinimumUTxOError Lovelace
1386
1380
calcMinUTxOAllegraMary = do
1387
1381
let val = txOutValueToValue v
1388
- minUTxO <- getMinUTxOPreAlonzo pparams'
1382
+ minUTxO <- getMinUTxOPreAlonzo (unbundleProtocolParams bpp)
1389
1383
Right $ calcMinimumDeposit val minUTxO
1390
1384
1391
1385
getMinUTxOPreAlonzo
0 commit comments