Skip to content

Commit

Permalink
Replace modifyLedgerBody with ledger-api lens (#4237)
Browse files Browse the repository at this point in the history
Suggestion to #4235

- Fix compilation for GHC 9.2
- Replace `modifyLedgerBody` with ledger-api lens
  • Loading branch information
jonathanknowles authored Nov 16, 2023
2 parents bc780bf + 2ca433e commit e9e67fa
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 81 deletions.
45 changes: 0 additions & 45 deletions lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ module Internal.Cardano.Write.Tx
, CardanoApi.ShelleyLedgerEra
, cardanoEraFromRecentEra
, shelleyBasedEraFromRecentEra
, asCardanoApiTx
, fromCardanoApiTx
, toCardanoApiUTxO
, fromCardanoApiUTxO
Expand Down Expand Up @@ -89,7 +88,6 @@ module Internal.Cardano.Write.Tx
, Core.TxBody
, txBody
, outputs
, modifyLedgerBody
, emptyTx
, serializeTx

Expand Down Expand Up @@ -786,56 +784,13 @@ outputs
outputs RecentEraConway = map sizedValue . toList . Conway.ctbOutputs
outputs RecentEraBabbage = map sizedValue . toList . Babbage.btbOutputs

-- NOTE: To reduce the need for the caller to deal with
-- @CardanoApiEra (CardanoApi.ShelleyLedgerEra era) ~ era@, we quantify this
-- function over @cardanoEra@ instead of @era@.
--
-- TODO [ADP-2353] Move to @cardano-api@ related module
modifyLedgerBody
:: forall cardanoEra. IsRecentEra cardanoEra
=> (Core.TxBody (CardanoApi.ShelleyLedgerEra cardanoEra) ->
Core.TxBody (CardanoApi.ShelleyLedgerEra cardanoEra))
-> Core.Tx (CardanoApi.ShelleyLedgerEra cardanoEra)
-> Core.Tx (CardanoApi.ShelleyLedgerEra cardanoEra)
modifyLedgerBody f = asCardanoApiTx @cardanoEra modify
where
modify (CardanoApi.Tx body keyWits) = CardanoApi.Tx body' keyWits
where
body' = case body of
CardanoApi.ByronTxBody {} ->
error "Impossible: ByronTxBody in CardanoApi.ShelleyLedgerEra"
CardanoApi.ShelleyTxBody
shelleyEra
ledgerBody
scripts
scriptData
auxData
validity ->
CardanoApi.ShelleyTxBody
shelleyEra
(f ledgerBody)
scripts
scriptData
auxData
validity

emptyTx :: RecentEra era -> Core.Tx (CardanoApi.ShelleyLedgerEra era)
emptyTx era = withConstraints era $ Core.mkBasicTx Core.mkBasicTxBody

--------------------------------------------------------------------------------
-- Compatibility
--------------------------------------------------------------------------------

asCardanoApiTx
:: forall era. IsRecentEra era
=> (CardanoApi.Tx era -> CardanoApi.Tx era)
-> Core.Tx (CardanoApi.ShelleyLedgerEra era)
-> Core.Tx (CardanoApi.ShelleyLedgerEra era)
asCardanoApiTx f
= fromCardanoApiTx
. f
. toCardanoApiTx

fromCardanoApiTx
:: forall era. IsRecentEra era
=> CardanoApi.Tx era
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ import Internal.Cardano.Write.Tx
, getFeePerByte
, isBelowMinimumCoinForTxOut
, maxScriptExecutionCost
, modifyLedgerBody
, modifyTxOutCoin
, outputs
, toCardanoApiTx
Expand Down Expand Up @@ -657,8 +656,7 @@ assignMinimalAdaQuantitiesToOutputsWithoutAda
-> Tx (CardanoApi.ShelleyLedgerEra era)
assignMinimalAdaQuantitiesToOutputsWithoutAda era pp =
withConstraints era
$ modifyLedgerBody @era
$ over outputsTxBodyL
$ over (bodyTxL . outputsTxBodyL)
$ fmap modifyTxOut
where
modifyTxOut out = flip (modifyTxOutCoin era) out $ \c ->
Expand Down
1 change: 1 addition & 0 deletions lib/read/lib/Cardano/Wallet/Read/Block.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
Expand Down
35 changes: 2 additions & 33 deletions lib/wallet/test/unit/Internal/Cardano/Write/Tx/BalanceSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2038,9 +2038,7 @@ addExtraTxIns
-> PartialTx CardanoApi.BabbageEra
-> PartialTx CardanoApi.BabbageEra
addExtraTxIns extraIns =
#tx %~
Write.asCardanoApiTx
(modifyBabbageTxBody (inputsTxBodyL %~ (<> toLedgerInputs extraIns)))
#tx . bodyTxL . inputsTxBodyL %~ (<> toLedgerInputs extraIns)
where
toLedgerInputs =
Set.map Convert.toLedger . Set.fromList
Expand Down Expand Up @@ -2154,34 +2152,6 @@ mockPParamsForBalancing =
(Write.shelleyBasedEra @era)
mockCardanoApiPParamsForBalancing

-- Ideally merge with 'updateTx'
modifyBabbageTxBody
:: ( Babbage.BabbageTxBody StandardBabbage ->
Babbage.BabbageTxBody StandardBabbage
)
-> CardanoApi.Tx CardanoApi.BabbageEra
-> CardanoApi.Tx CardanoApi.BabbageEra
modifyBabbageTxBody
f
(CardanoApi.Tx
(CardanoApi.ShelleyTxBody
era
body
scripts
scriptData
auxData
scriptValidity)
keyWits)
= CardanoApi.Tx
(CardanoApi.ShelleyTxBody
era
(f body)
scripts
scriptData
auxData
scriptValidity)
keyWits

paymentPartialTx :: [W.TxOut] -> PartialTx CardanoApi.BabbageEra
paymentPartialTx txouts =
PartialTx (fromCardanoApiTx $ CardanoApi.Tx body []) mempty []
Expand Down Expand Up @@ -2307,8 +2277,7 @@ withValidityInterval
:: ValidityInterval
-> PartialTx CardanoApi.BabbageEra
-> PartialTx CardanoApi.BabbageEra
withValidityInterval vi = #tx %~
Write.asCardanoApiTx (modifyBabbageTxBody (vldtTxBodyL .~ vi))
withValidityInterval vi = #tx . bodyTxL %~ vldtTxBodyL .~ vi

walletToCardanoValue :: W.TokenBundle -> CardanoApi.Value
walletToCardanoValue = CardanoApi.fromMaryValue . Convert.toLedger
Expand Down

0 comments on commit e9e67fa

Please sign in to comment.