Skip to content

Commit

Permalink
Use consistent namespace for Cardano.Api.ShelleyLedgerEra.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanknowles committed Nov 15, 2023
1 parent 12e6af9 commit 652d61a
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 79 deletions.
84 changes: 45 additions & 39 deletions lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx/Balance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ import Internal.Cardano.Write.Tx
, RecentEra (..)
, RecentEraConstraints
, RecentEraLedgerConstraints
, ShelleyLedgerEra
, Tx
, TxBody
, TxIn
Expand Down Expand Up @@ -363,19 +362,19 @@ instance Buildable (BuildableInAnyEra a) where
--
data ErrBalanceTxInsufficientCollateralError era =
ErrBalanceTxInsufficientCollateralError
{ largestCombinationAvailable :: UTxO (ShelleyLedgerEra era)
{ largestCombinationAvailable :: UTxO (CardanoApi.ShelleyLedgerEra era)
-- ^ The largest available combination of pure ada UTxOs.
, minimumCollateralAmount :: Coin
-- ^ The minimum quantity of ada necessary for collateral.
}
deriving Generic

deriving instance
RecentEraLedgerConstraints (ShelleyLedgerEra era) =>
RecentEraLedgerConstraints (CardanoApi.ShelleyLedgerEra era) =>
Eq (ErrBalanceTxInsufficientCollateralError era)

deriving instance
RecentEraLedgerConstraints (ShelleyLedgerEra era) =>
RecentEraLedgerConstraints (CardanoApi.ShelleyLedgerEra era) =>
Show (ErrBalanceTxInsufficientCollateralError era)

-- | Indicates that there was not enough ada available to create change outputs.
Expand Down Expand Up @@ -421,7 +420,8 @@ data ErrBalanceTxAssetsInsufficientError = ErrBalanceTxAssetsInsufficientError

data ErrBalanceTxInternalError era
= RecentEraConstraints era =>
ErrUnderestimatedFee Coin (Tx (ShelleyLedgerEra era)) KeyWitnessCount
ErrUnderestimatedFee
Coin (Tx (CardanoApi.ShelleyLedgerEra era)) KeyWitnessCount
| ErrFailedBalancing Value

deriving instance Eq (ErrBalanceTxInternalError era)
Expand All @@ -435,15 +435,19 @@ data ErrBalanceTx era
| ErrBalanceTxExistingCollateral
| ErrBalanceTxExistingTotalCollateral
| ErrBalanceTxExistingReturnCollateral
| RecentEraLedgerConstraints (ShelleyLedgerEra era)
| RecentEraLedgerConstraints (CardanoApi.ShelleyLedgerEra era)
=> ErrBalanceTxInsufficientCollateral
(ErrBalanceTxInsufficientCollateralError era)
| ErrBalanceTxConflictingNetworks
| ErrBalanceTxAssignRedeemers ErrAssignRedeemers
| ErrBalanceTxInternalError (ErrBalanceTxInternalError era)
| RecentEraLedgerConstraints (ShelleyLedgerEra era)
=> ErrBalanceTxInputResolutionConflicts
(NonEmpty (TxOut (ShelleyLedgerEra era), TxOut (ShelleyLedgerEra era)))
| RecentEraLedgerConstraints (CardanoApi.ShelleyLedgerEra era) =>
ErrBalanceTxInputResolutionConflicts
(NonEmpty
( TxOut (CardanoApi.ShelleyLedgerEra era)
, TxOut (CardanoApi.ShelleyLedgerEra era)
)
)
| ErrBalanceTxUnresolvedInputs (NonEmpty TxIn)
| ErrBalanceTxOutputError ErrBalanceTxOutputError
| ErrBalanceTxUnableToCreateChange ErrBalanceTxUnableToCreateChangeError
Expand Down Expand Up @@ -472,22 +476,22 @@ deriving instance Show (ErrBalanceTx era)
-- even though they are in an "unordered" set.
data PartialTx era = PartialTx
{ tx :: CardanoApi.Tx era
, inputs :: UTxO (ShelleyLedgerEra era)
, inputs :: UTxO (CardanoApi.ShelleyLedgerEra era)
-- ^ NOTE: Can we rename this to something better? Perhaps 'extraUTxO'?
, redeemers :: [Redeemer]
}
deriving Generic

deriving instance
RecentEraLedgerConstraints (ShelleyLedgerEra era) =>
RecentEraLedgerConstraints (CardanoApi.ShelleyLedgerEra era) =>
Eq (PartialTx era)

deriving instance
RecentEraLedgerConstraints (ShelleyLedgerEra era) =>
RecentEraLedgerConstraints (CardanoApi.ShelleyLedgerEra era) =>
Show (PartialTx era)

instance
RecentEraLedgerConstraints (ShelleyLedgerEra era) =>
RecentEraLedgerConstraints (CardanoApi.ShelleyLedgerEra era) =>
Buildable (PartialTx era)
where
build (PartialTx tx (UTxO ins) redeemers)
Expand All @@ -505,12 +509,12 @@ instance
data UTxOIndex era = UTxOIndex
{ walletUTxO :: !W.UTxO
, walletUTxOIndex :: !(UTxOIndex.UTxOIndex WalletUTxO)
, ledgerUTxO :: !(UTxO (ShelleyLedgerEra era))
, ledgerUTxO :: !(UTxO (CardanoApi.ShelleyLedgerEra era))
}

constructUTxOIndex
:: forall era. IsRecentEra era
=> UTxO (ShelleyLedgerEra era)
=> UTxO (CardanoApi.ShelleyLedgerEra era)
-> UTxOIndex era
constructUTxOIndex ledgerUTxO =
UTxOIndex {walletUTxO, walletUTxOIndex, ledgerUTxO}
Expand All @@ -522,14 +526,14 @@ constructUTxOIndex ledgerUTxO =
fromWalletUTxO
:: RecentEra era
-> W.UTxO
-> UTxO (ShelleyLedgerEra era)
-> UTxO (CardanoApi.ShelleyLedgerEra era)
fromWalletUTxO era (W.UTxO m) = withConstraints era $ UTxO
$ Map.mapKeys Convert.toLedger
$ Map.map (toLedgerTxOut era) m

toWalletUTxO
:: RecentEra era
-> UTxO (ShelleyLedgerEra era)
-> UTxO (CardanoApi.ShelleyLedgerEra era)
-> W.UTxO
toWalletUTxO era (UTxO m) = withConstraints era $ W.UTxO
$ Map.mapKeys Convert.toWallet
Expand Down Expand Up @@ -874,8 +878,8 @@ balanceTransactionWithSelectionStrategyAndNoZeroAdaAdjustment

guardTxSize
:: KeyWitnessCount
-> Tx (ShelleyLedgerEra era)
-> ExceptT (ErrBalanceTx era) m (Tx (ShelleyLedgerEra era))
-> Tx (CardanoApi.ShelleyLedgerEra era)
-> ExceptT (ErrBalanceTx era) m (Tx (CardanoApi.ShelleyLedgerEra era))
guardTxSize witCount tx =
withConstraints era $ do
let maxSize = TxSize (pp ^. ppMaxTxSizeL)
Expand All @@ -884,8 +888,8 @@ balanceTransactionWithSelectionStrategyAndNoZeroAdaAdjustment
pure tx

guardTxBalanced
:: Tx (ShelleyLedgerEra era)
-> ExceptT (ErrBalanceTx era) m (Tx (ShelleyLedgerEra era))
:: Tx (CardanoApi.ShelleyLedgerEra era)
-> ExceptT (ErrBalanceTx era) m (Tx (CardanoApi.ShelleyLedgerEra era))
guardTxBalanced tx = do
let bal = txBalance tx
if bal == mempty
Expand All @@ -894,14 +898,14 @@ balanceTransactionWithSelectionStrategyAndNoZeroAdaAdjustment
$ ErrBalanceTxInternalError
$ ErrFailedBalancing bal

txBalance :: Tx (ShelleyLedgerEra era) -> Value
txBalance :: Tx (CardanoApi.ShelleyLedgerEra era) -> Value
txBalance
= withConstraints era
. evaluateTransactionBalance era pp combinedUTxO
. txBody era

balanceAfterSettingMinFee
:: Tx (ShelleyLedgerEra era)
:: Tx (CardanoApi.ShelleyLedgerEra era)
-> ExceptT (ErrBalanceTx era) m
(CardanoApi.Value, CardanoApi.Lovelace, KeyWitnessCount)
balanceAfterSettingMinFee tx = ExceptT . pure $ do
Expand Down Expand Up @@ -930,7 +934,7 @@ balanceTransactionWithSelectionStrategyAndNoZeroAdaAdjustment
-- NOTE: Representing the wallet utxo as a @CardanoApi.UTxO@ will not make
-- this check easier, even if it may be useful in other regards.
guardWalletUTxOConsistencyWith
:: UTxO (ShelleyLedgerEra era)
:: UTxO (CardanoApi.ShelleyLedgerEra era)
-> ExceptT (ErrBalanceTx era) m ()
guardWalletUTxOConsistencyWith u' = do
let W.UTxO u = toWalletUTxO (recentEra @era) u'
Expand All @@ -947,7 +951,7 @@ balanceTransactionWithSelectionStrategyAndNoZeroAdaAdjustment
$ withConstraints era
$ ErrBalanceTxInputResolutionConflicts (c :| cs)

combinedUTxO :: UTxO (ShelleyLedgerEra era)
combinedUTxO :: UTxO (CardanoApi.ShelleyLedgerEra era)
combinedUTxO = withConstraints era $ mconcat
-- The @CardanoApi.UTxO@ can contain strictly more information than
-- @W.UTxO@. Therefore we make the user-specified @inputUTxO@ to take
Expand All @@ -966,22 +970,22 @@ balanceTransactionWithSelectionStrategyAndNoZeroAdaAdjustment
$ outputs era
$ txBody era tx
where
fromLedgerTxOut :: TxOut (ShelleyLedgerEra era) -> W.TxOut
fromLedgerTxOut :: TxOut (CardanoApi.ShelleyLedgerEra era) -> W.TxOut
fromLedgerTxOut o = case era of
RecentEraBabbage -> Convert.fromBabbageTxOut o
RecentEraConway -> Convert.fromConwayTxOut o

assembleTransaction
:: TxUpdate
-> ExceptT (ErrBalanceTx era) m (Tx (ShelleyLedgerEra era))
-> ExceptT (ErrBalanceTx era) m (Tx (CardanoApi.ShelleyLedgerEra era))
assembleTransaction update = ExceptT . pure $ do
tx' <- left ErrBalanceTxUpdateError $ updateTx era partialLedgerTx update
left ErrBalanceTxAssignRedeemers $
assignScriptRedeemers
era pp timeTranslation combinedUTxO redeemers tx'

guardExistingCollateral
:: Tx (ShelleyLedgerEra era)
:: Tx (CardanoApi.ShelleyLedgerEra era)
-> ExceptT (ErrBalanceTx era) m ()
guardExistingCollateral tx = withConstraints era $ do
-- Coin selection does not support pre-defining collateral. In Sep 2021
Expand All @@ -993,7 +997,7 @@ balanceTransactionWithSelectionStrategyAndNoZeroAdaAdjustment
throwE ErrBalanceTxExistingCollateral

guardExistingTotalCollateral
:: Tx (ShelleyLedgerEra era)
:: Tx (CardanoApi.ShelleyLedgerEra era)
-> ExceptT (ErrBalanceTx era) m ()
guardExistingTotalCollateral tx = withConstraints era $ do
let totColl = tx ^. (bodyTxL . totalCollateralTxBodyL)
Expand All @@ -1002,7 +1006,7 @@ balanceTransactionWithSelectionStrategyAndNoZeroAdaAdjustment
SJust _ -> throwE ErrBalanceTxExistingTotalCollateral

guardExistingReturnCollateral
:: Tx (ShelleyLedgerEra era)
:: Tx (CardanoApi.ShelleyLedgerEra era)
-> ExceptT (ErrBalanceTx era) m ()
guardExistingReturnCollateral tx = withConstraints era $ do
let collRet = tx ^. (bodyTxL . collateralReturnTxBodyL)
Expand Down Expand Up @@ -1117,7 +1121,7 @@ selectAssets era (ProtocolParameters pp) utxoAssumptions outs redeemers
=> RecentEra era
-> W.Address
-> W.TokenBundle
-> TxOut (ShelleyLedgerEra era)
-> TxOut (CardanoApi.ShelleyLedgerEra era)
mkLedgerTxOut txOutEra address bundle =
case txOutEra of
RecentEraBabbage -> Convert.toBabbageTxOut txOut
Expand Down Expand Up @@ -1273,9 +1277,9 @@ newtype ErrUpdateSealedTx
-- be used to *add* tx body content.
updateTx
:: forall era. RecentEra era
-> Tx (ShelleyLedgerEra era)
-> Tx (CardanoApi.ShelleyLedgerEra era)
-> TxUpdate
-> Either ErrUpdateSealedTx (Tx (ShelleyLedgerEra era))
-> Either ErrUpdateSealedTx (Tx (CardanoApi.ShelleyLedgerEra era))
updateTx era tx extraContent = withConstraints era $ do
let tx' = tx
& over bodyTxL (modifyShelleyTxBody extraContent era)
Expand All @@ -1294,7 +1298,9 @@ updateTx era tx extraContent = withConstraints era $ do
TxUpdate _ _ _ extraInputScripts _ = extraContent

extraInputScripts'
:: Map (ScriptHash StandardCrypto) (Script (ShelleyLedgerEra era))
:: Map
(ScriptHash StandardCrypto)
(Script (CardanoApi.ShelleyLedgerEra era))
extraInputScripts' = withConstraints era $
Map.fromList $ map (pairWithHash . convert) extraInputScripts
where
Expand All @@ -1304,16 +1310,16 @@ updateTx era tx extraContent = withConstraints era $ do
toLedgerScript
:: CA.Script CA.KeyHash
-> RecentEra era
-> Core.Script (ShelleyLedgerEra era)
-> Core.Script (CardanoApi.ShelleyLedgerEra era)
toLedgerScript s = \case
RecentEraBabbage -> TimelockScript $ Convert.toLedgerTimelockScript s
RecentEraConway -> TimelockScript $ Convert.toLedgerTimelockScript s

modifyShelleyTxBody
:: forall era. TxUpdate
-> RecentEra era
-> TxBody (ShelleyLedgerEra era)
-> TxBody (ShelleyLedgerEra era)
-> TxBody (CardanoApi.ShelleyLedgerEra era)
-> TxBody (CardanoApi.ShelleyLedgerEra era)
modifyShelleyTxBody txUpdate era = withConstraints era $
over feeTxBodyL modifyFee
. over outputsTxBodyL
Expand Down Expand Up @@ -1553,15 +1559,15 @@ toLedgerTxOut
:: HasCallStack
=> RecentEra era
-> W.TxOut
-> TxOut (ShelleyLedgerEra era)
-> TxOut (CardanoApi.ShelleyLedgerEra era)
toLedgerTxOut txOutEra txOut =
case txOutEra of
RecentEraBabbage -> Convert.toBabbageTxOut txOut
RecentEraConway -> Convert.toConwayTxOut txOut

toWalletTxOut
:: RecentEra era
-> TxOut (ShelleyLedgerEra era)
-> TxOut (CardanoApi.ShelleyLedgerEra era)
-> W.TxOut
toWalletTxOut RecentEraBabbage = Convert.fromBabbageTxOut
toWalletTxOut RecentEraConway = Convert.fromConwayTxOut
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ import Data.IntCast
import Internal.Cardano.Write.Tx
( PParams
, RecentEra
, ShelleyLedgerEra
, Value
, Version
, withConstraints
)

import qualified Cardano.Api.Shelley as CardanoApi
import qualified Cardano.Wallet.Primitive.Types.TokenBundle as TokenBundle
import qualified Cardano.Wallet.Shelley.Compatibility.Ledger as Convert
import qualified Data.ByteString.Lazy as BL
Expand All @@ -51,7 +51,7 @@ import qualified Data.ByteString.Lazy as BL
--
mkTokenBundleSizeAssessor
:: RecentEra era
-> PParams (ShelleyLedgerEra era)
-> PParams (CardanoApi.ShelleyLedgerEra era)
-> TokenBundleSizeAssessor
mkTokenBundleSizeAssessor era pp = TokenBundleSizeAssessor $ \tb ->
if computeTokenBundleSerializedLengthBytes tb ver > maxValSize
Expand Down
Loading

0 comments on commit 652d61a

Please sign in to comment.