Skip to content

Commit

Permalink
Move deserializeTx to lib & tweak imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Anviking committed Jan 13, 2025
1 parent 02f7dff commit ffa3169
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
39 changes: 29 additions & 10 deletions lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,16 @@ module Internal.Cardano.Write.Tx
, Version

-- * Tx
, Core.Tx
, Core.TxBody
, Tx
, TxBody
, serializeTx
, deserializeTx

-- * TxId
, Ledger.TxId

-- * TxOut
, Core.TxOut
, TxOut
, BabbageTxOut (..)
, TxOutInBabbage
, TxOutInRecentEra (..)
Expand Down Expand Up @@ -132,7 +133,10 @@ import Cardano.Ledger.Alonzo.Scripts
( AlonzoScript (..)
)
import Cardano.Ledger.Api
( coinTxOutL
( Tx
, TxBody
, TxOut
, coinTxOutL
, upgradeTxOut
)
import Cardano.Ledger.Api.UTxO
Expand Down Expand Up @@ -284,8 +288,6 @@ unsafeMkTxIn hash ix = Ledger.mkTxInPartial
-- TxOut
--------------------------------------------------------------------------------

type TxOut era = Core.TxOut era

type TxOutInBabbage = Babbage.BabbageTxOut Babbage

type Address = Ledger.Addr StandardCrypto
Expand Down Expand Up @@ -479,26 +481,43 @@ forceUTxOToEra (UTxO utxo) =

serializeTx
:: forall era. IsRecentEra era
=> Core.Tx era
=> Tx era
-> ByteString
serializeTx tx =
CardanoApi.serialiseToCBOR $ toCardanoApiTx tx

deserializeTx :: forall era. IsRecentEra era => ByteString -> Tx era
deserializeTx = case recentEra @era of
RecentEraBabbage -> deserializeBabbageTx
RecentEraConway -> deserializeConwayTx
where
deserializeBabbageTx :: ByteString -> Tx Babbage
deserializeBabbageTx
= fromCardanoApiTx
. either (error . show) id
. CardanoApi.deserialiseFromCBOR (CardanoApi.AsTx CardanoApi.AsBabbageEra)

deserializeConwayTx :: ByteString -> Tx Conway
deserializeConwayTx
= fromCardanoApiTx
. either (error . show) id
. CardanoApi.deserialiseFromCBOR (CardanoApi.AsTx CardanoApi.AsConwayEra)

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

fromCardanoApiTx
:: IsRecentEra era
=> CardanoApi.Tx (CardanoApiEra era)
-> Core.Tx era
-> Tx era
fromCardanoApiTx = \case
CardanoApi.ShelleyTx _era tx ->
tx

toCardanoApiTx
:: forall era. IsRecentEra era
=> Core.Tx era
=> Tx era
-> CardanoApi.Tx (CardanoApiEra era)
toCardanoApiTx =
CardanoApi.ShelleyTx
Expand Down Expand Up @@ -593,7 +612,7 @@ evaluateTransactionBalance
=> PParams era
-> (StakeCredential -> Maybe Coin)
-> Shelley.UTxO era
-> Core.TxBody era
-> TxBody era
-> Core.Value era
evaluateTransactionBalance pp depositLookup =
Ledger.evalBalanceTxBody
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ import Internal.Cardano.Write.Tx
, TxOutInRecentEra (..)
, UTxO (..)
, Value
, deserializeTx
, fromCardanoApiTx
, serializeTx
, toCardanoApiTx
Expand Down Expand Up @@ -1920,23 +1921,6 @@ balanceTxWithDummyChangeState utxoAssumptions utxo seed partialTx =
where
utxoIndex = constructUTxOIndex $ fromWalletUTxO utxo

deserializeTx :: forall era. IsRecentEra era => ByteString -> Tx era
deserializeTx = case recentEra @era of
RecentEraBabbage -> deserializeBabbageTx
RecentEraConway -> deserializeConwayTx

deserializeBabbageTx :: ByteString -> Tx Babbage
deserializeBabbageTx
= fromCardanoApiTx
. either (error . show) id
. CardanoApi.deserialiseFromCBOR (CardanoApi.AsTx CardanoApi.AsBabbageEra)

deserializeConwayTx :: ByteString -> Tx Conway
deserializeConwayTx
= fromCardanoApiTx
. either (error . show) id
. CardanoApi.deserialiseFromCBOR (CardanoApi.AsTx CardanoApi.AsConwayEra)

fromWalletTxIn :: W.TxIn -> TxIn
fromWalletTxIn = Convert.toLedger

Expand Down

0 comments on commit ffa3169

Please sign in to comment.