Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cardano-api: 8.29.1.0 -> 8.37.0.0 #1232

Merged
merged 5 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ repository cardano-haskell-packages

-- See CONTRIBUTING.md for information about when and how to update these.
index-state:
, hackage.haskell.org 2023-12-06T15:07:04Z
, cardano-haskell-packages 2023-12-04T19:04:02Z
, hackage.haskell.org 2024-01-29T15:07:04Z
, cardano-haskell-packages 2024-01-29T19:04:02Z

packages:
hydra-prelude
Expand Down
42 changes: 30 additions & 12 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions hydra-cardano-api/hydra-cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ library
, base >=4.16
, base16-bytestring
, bytestring
, cardano-api >=8.29.1 && <8.30
, cardano-api >=8.37.0 && <8.38
, cardano-binary >=1.7.0 && <1.8
, cardano-crypto-class >=2.1.1 && <2.2
, cardano-ledger-allegra >=1.2.1 && <1.3
Expand All @@ -96,9 +96,9 @@ library
, cardano-ledger-babbage >=1.5 && <1.6
, cardano-ledger-binary >=1.2 && <1.3
, cardano-ledger-byron >=1.0.0 && <1.1
, cardano-ledger-core >=1.8 && <1.9
, cardano-ledger-mary >=1.3.1 && <1.4
, cardano-ledger-shelley >=1.7 && <1.8
, cardano-ledger-core >=1.9 && <1.10
, cardano-ledger-mary >=1.4 && <1.5
, cardano-ledger-shelley >=1.8 && <1.9
, containers
, lens
, plutus-ledger-api >=1.15.0.1 && <1.16
Expand Down
54 changes: 29 additions & 25 deletions hydra-cardano-api/src/Cardano/Api/UTxO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Cardano.Api.UTxO where
import Cardano.Api hiding (UTxO, toLedgerUTxO)
import Cardano.Api qualified
import Cardano.Api.Shelley (ReferenceScript (..))
import Cardano.Ledger.Babbage ()
import Data.Bifunctor (second)
import Data.Coerce (coerce)
import Data.List qualified as List
Expand Down Expand Up @@ -89,33 +90,36 @@ min = UTxO . uncurry Map.singleton . Map.findMin . toMap
-- | Transforms a UTxO containing tx outs from any era into Babbage era.
fromApi :: Cardano.Api.UTxO era -> UTxO
fromApi (Cardano.Api.UTxO eraUTxO) =
let eraPairs = Map.toList eraUTxO
babbagePairs = second coerceOutputToEra <$> eraPairs
in fromPairs babbagePairs
fromPairs $ second convertOutputToEra <$> Map.toList eraUTxO
where
coerceOutputToEra :: TxOut CtxUTxO era -> TxOut CtxUTxO Era
coerceOutputToEra (TxOut eraAddress eraValue eraDatum eraRefScript) =
-- NOTE: At latest the TxOutValue is an existential where we need to case on
-- the 'sbe' witness to get constraints on the contained 'value', but the
-- 'cardano-api' does that already when allowing conversion of their
-- (complicated) constrained types to the cardano-ledger types - so we just
-- convert forth and back.
convertOutputToEra :: TxOut CtxUTxO era -> TxOut CtxUTxO Era
convertOutputToEra (TxOut eraAddress eraValue eraDatum eraRefScript) =
TxOut
(coerceAddressToEra eraAddress)
(coerceValueToEra eraValue)
(coerceDatumToEra eraDatum)
(coerceRefScriptToEra eraRefScript)

coerceAddressToEra :: AddressInEra era -> AddressInEra Era
coerceAddressToEra (AddressInEra _ eraAddress) = anyAddressInShelleyBasedEra ShelleyBasedEraBabbage (toAddressAny eraAddress)

coerceValueToEra :: TxOutValue era -> TxOutValue Era
coerceValueToEra (TxOutAdaOnly _ eraLovelace) = lovelaceToTxOutValue BabbageEra eraLovelace
coerceValueToEra (TxOutValue _ value) = TxOutValue MaryEraOnwardsBabbage value

coerceDatumToEra :: TxOutDatum CtxUTxO era -> TxOutDatum CtxUTxO Era
coerceDatumToEra TxOutDatumNone = TxOutDatumNone
coerceDatumToEra (TxOutDatumHash _ hashScriptData) = TxOutDatumHash AlonzoEraOnwardsBabbage hashScriptData
coerceDatumToEra (TxOutDatumInline _ hashableScriptData) = TxOutDatumInline BabbageEraOnwardsBabbage hashableScriptData

coerceRefScriptToEra :: ReferenceScript era -> ReferenceScript Era
coerceRefScriptToEra ReferenceScriptNone = ReferenceScriptNone
coerceRefScriptToEra (ReferenceScript _ scriptInAnyLang) = ReferenceScript BabbageEraOnwardsBabbage scriptInAnyLang
(convertAddressToEra eraAddress)
(convertValueToEra eraValue)
(convertDatumToEra eraDatum)
(convertRefScriptToEra eraRefScript)

convertAddressToEra :: AddressInEra era -> AddressInEra Era
convertAddressToEra (AddressInEra _ eraAddress) = anyAddressInShelleyBasedEra ShelleyBasedEraBabbage (toAddressAny eraAddress)

convertValueToEra :: TxOutValue era -> TxOutValue Era
convertValueToEra (TxOutValueByron lovelace) = lovelaceToTxOutValue shelleyBasedEra lovelace
convertValueToEra (TxOutValueShelleyBased sbe value) = TxOutValueShelleyBased shelleyBasedEra (toLedgerValue MaryEraOnwardsBabbage $ fromLedgerValue sbe value)

convertDatumToEra :: TxOutDatum CtxUTxO era -> TxOutDatum CtxUTxO Era
convertDatumToEra TxOutDatumNone = TxOutDatumNone
convertDatumToEra (TxOutDatumHash _ hashScriptData) = TxOutDatumHash AlonzoEraOnwardsBabbage hashScriptData
convertDatumToEra (TxOutDatumInline _ hashableScriptData) = TxOutDatumInline BabbageEraOnwardsBabbage hashableScriptData

convertRefScriptToEra :: ReferenceScript era -> ReferenceScript Era
convertRefScriptToEra ReferenceScriptNone = ReferenceScriptNone
convertRefScriptToEra (ReferenceScript _ scriptInAnyLang) = ReferenceScript BabbageEraOnwardsBabbage scriptInAnyLang

toApi :: UTxO -> Cardano.Api.UTxO Era
toApi = coerce
12 changes: 6 additions & 6 deletions hydra-cardano-api/src/Hydra/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import Cardano.Api as X hiding (
Witness (..),
createAndValidateTransactionBody,
defaultTxBodyContent,
fromLedgerValue,
makeShelleyKeyWitness,
policyId,
queryEraHistory,
Expand All @@ -76,6 +77,7 @@ import Cardano.Api as X hiding (
scriptLanguageSupportedInEra,
signShelleyTransaction,
toLedgerUTxO,
toLedgerValue,
)
import Cardano.Api.Byron as X (
Address (..),
Expand All @@ -94,10 +96,8 @@ import Cardano.Api.Shelley as X (
VerificationKey (..),
fromAlonzoCostModels,
fromAlonzoPrices,
fromConsensusPointInMode,
fromPlutusData,
toAlonzoPrices,
toConsensusPointInMode,
toPlutusData,
toShelleyNetwork,
)
Expand Down Expand Up @@ -372,10 +372,10 @@ pattern TxBody{txBodyContent} <-
{-# COMPLETE TxBody #-}

createAndValidateTransactionBody :: TxBodyContent BuildTx -> Either TxBodyError TxBody
createAndValidateTransactionBody = Cardano.Api.createAndValidateTransactionBody cardanoEra
createAndValidateTransactionBody = Cardano.Api.createAndValidateTransactionBody shelleyBasedEra

defaultTxBodyContent :: TxBodyContent BuildTx
defaultTxBodyContent = Cardano.Api.defaultTxBodyContent cardanoEra
defaultTxBodyContent = Cardano.Api.defaultTxBodyContent shelleyBasedEra

-- ** TxBodyContent

Expand Down Expand Up @@ -595,14 +595,14 @@ pattern TxOut :: AddressInEra -> Value -> TxOutDatum ctx -> ReferenceScript -> T
pattern TxOut{txOutAddress, txOutValue, txOutDatum, txOutReferenceScript} <-
Cardano.Api.TxOut
txOutAddress
(TxOutValue MaryEraOnwardsBabbage txOutValue)
(TxOutValueShelleyBased ShelleyBasedEraBabbage (Extras.fromLedgerValue -> txOutValue))
txOutDatum
txOutReferenceScript
where
TxOut addr value datum ref =
Cardano.Api.TxOut
addr
(TxOutValue MaryEraOnwardsBabbage value)
(TxOutValueShelleyBased ShelleyBasedEraBabbage (Extras.toLedgerValue value))
datum
ref

Expand Down
2 changes: 1 addition & 1 deletion hydra-cardano-api/src/Hydra/Cardano/Api/Hash.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Hydra.Cardano.Api.Hash where

import Hydra.Cardano.Api.Prelude

import Cardano.Ledger.Alonzo.TxInfo qualified as Ledger
import Cardano.Ledger.Alonzo.Plutus.TxInfo qualified as Ledger
import Cardano.Ledger.Keys qualified as Ledger
import Cardano.Ledger.SafeHash (unsafeMakeSafeHash)
import Cardano.Ledger.Shelley.Scripts qualified as Ledger
Expand Down
2 changes: 1 addition & 1 deletion hydra-cardano-api/src/Hydra/Cardano/Api/PlutusScript.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module Hydra.Cardano.Api.PlutusScript where

import Hydra.Cardano.Api.Prelude

import Cardano.Ledger.Alonzo.Language qualified as Ledger
import Cardano.Ledger.Alonzo.Scripts qualified as Ledger
import Cardano.Ledger.Plutus.Language qualified as Ledger
import Data.ByteString.Short qualified as SBS
import PlutusLedgerApi.Common qualified as Plutus
import Test.QuickCheck (listOf)
Expand Down
2 changes: 1 addition & 1 deletion hydra-cardano-api/src/Hydra/Cardano/Api/PolicyId.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Hydra.Cardano.Api.PolicyId where

import Hydra.Cardano.Api.Prelude

import Cardano.Ledger.Alonzo.TxInfo qualified as Ledger
import Cardano.Ledger.Alonzo.Plutus.TxInfo qualified as Ledger
import Cardano.Ledger.Hashes qualified as Ledger
import Cardano.Ledger.Mary.Value qualified as Ledger
import Hydra.Cardano.Api.ScriptHash ()
Expand Down
4 changes: 1 addition & 3 deletions hydra-cardano-api/src/Hydra/Cardano/Api/ScriptData.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ module Hydra.Cardano.Api.ScriptData where

import Hydra.Cardano.Api.Prelude

import Cardano.Api.Byron (TxBody (..))
import Cardano.Ledger.Alonzo.Scripts.Data qualified as Ledger
import Cardano.Ledger.Alonzo.TxWits qualified as Ledger
import Cardano.Ledger.Era qualified as Ledger
import Cardano.Ledger.Plutus.Data qualified as Ledger
import Codec.Serialise (deserialiseOrFail, serialise)
import Control.Arrow (left)
import Data.Aeson (Value (String), withText)
Expand Down Expand Up @@ -55,7 +54,6 @@ lookupScriptData ::
Tx era ->
TxOut CtxUTxO era ->
Maybe HashableScriptData
lookupScriptData (Tx ByronTxBody{} _) _ = Nothing
lookupScriptData (Tx (ShelleyTxBody _ _ _ scriptsData _ _) _) (TxOut _ _ datum _) =
case datum of
TxOutDatumNone ->
Expand Down
6 changes: 1 addition & 5 deletions hydra-cardano-api/src/Hydra/Cardano/Api/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,10 @@ utxoProducedByTx tx =
TxBody body = getTxBody tx

-- | Get explicit fees allocated to a transaction.
--
-- NOTE: this function is partial and throws if given a Byron transaction for
-- which fees are necessarily implicit.
txFee' :: HasCallStack => Tx era -> Lovelace
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we keep the HasCallStack here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this function seemingly can be made total now

txFee' :: Tx era -> Lovelace
txFee' (getTxBody -> TxBody body) =
case txFee body of
TxFeeExplicit _ y -> y
TxFeeImplicit _ -> error "impossible: TxFeeImplicit on non-Byron transaction."

-- * Type Conversions

Expand Down
2 changes: 1 addition & 1 deletion hydra-cardano-api/src/Hydra/Cardano/Api/TxBody.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module Hydra.Cardano.Api.TxBody where

import Hydra.Cardano.Api.Prelude

import Cardano.Ledger.Alonzo.Scripts.Data qualified as Ledger
import Cardano.Ledger.Alonzo.TxWits qualified as Ledger
import Cardano.Ledger.Babbage.Tx qualified as Ledger
import Cardano.Ledger.BaseTypes (strictMaybeToMaybe)
import Cardano.Ledger.Core qualified as Ledger
import Cardano.Ledger.Plutus.Data qualified as Ledger
import Data.List (find)
import Data.Map qualified as Map
import Hydra.Cardano.Api.PlutusScript (fromLedgerScript)
Expand Down
2 changes: 1 addition & 1 deletion hydra-cardano-api/src/Hydra/Cardano/Api/TxIn.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ module Hydra.Cardano.Api.TxIn where

import Hydra.Cardano.Api.Prelude

import Cardano.Ledger.Alonzo.TxInfo qualified as Ledger
import Cardano.Ledger.BaseTypes qualified as Ledger
import Cardano.Ledger.Binary qualified as Ledger
import Cardano.Ledger.Plutus.TxInfo qualified as Ledger
import Cardano.Ledger.TxIn qualified as Ledger
import Data.ByteString qualified as BS
import Data.Set qualified as Set
Expand Down
6 changes: 4 additions & 2 deletions hydra-cardano-api/src/Hydra/Cardano/Api/TxOut.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ mkTxOutAutoBalance ::
ReferenceScript Era ->
TxOut CtxTx Era
mkTxOutAutoBalance pparams addr val dat ref =
let out = TxOut addr (TxOutValue maryEraOnwards val) dat ref
let out = TxOut addr (TxOutValueShelleyBased (shelleyBasedEra @Era) (toLedgerValue (maryEraOnwards @Era) val)) dat ref
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should: use mkTxOutValue

This seemingly has become a lot more complicated now (too complicated? smell?) and hence we should keep the complexity in one function only.

minValue = minUTxOValue pparams out
in modifyTxOutValue (const minValue) out

Expand All @@ -61,6 +61,7 @@ modifyTxOutAddress fn (TxOut addr value dat ref) =
-- | Alter the value of a 'TxOut' with the given transformation.
modifyTxOutValue ::
IsMaryEraOnwards era =>
IsShelleyBasedEra era =>
(Value -> Value) ->
TxOut ctx era ->
TxOut ctx era
Expand Down Expand Up @@ -160,12 +161,13 @@ toLedgerTxOut =
-- NOTE: Requires the 'Network' discriminator (Testnet or Mainnet) because
-- Plutus addresses are stripped off it.
fromPlutusTxOut ::
forall era.
(IsMaryEraOnwards era, IsAlonzoEraOnwards era, IsBabbageEraOnwards era, IsShelleyBasedEra era) =>
Network ->
Plutus.TxOut ->
Maybe (TxOut CtxUTxO era)
fromPlutusTxOut network out = do
value <- TxOutValue maryEraOnwards <$> fromPlutusValue plutusValue
value <- shelleyBasedEraConstraints (shelleyBasedEra @era) $ TxOutValueShelleyBased (shelleyBasedEra @era) <$> (toLedgerValue (maryEraOnwards @era) <$> fromPlutusValue plutusValue)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should: use mkTxOutValue

This seemingly has become a lot more complicated now (too complicated? smell?) and hence we should keep the complexity in one function only.

pure $ TxOut addressInEra value datum ReferenceScriptNone
where
addressInEra = fromPlutusAddress network plutusAddress
Expand Down
5 changes: 3 additions & 2 deletions hydra-cardano-api/src/Hydra/Cardano/Api/TxOutValue.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import Hydra.Cardano.Api.MaryEraOnwards (IsMaryEraOnwards (..))
-- | Inject some 'Value' into a 'TxOutValue'
mkTxOutValue ::
forall era.
IsShelleyBasedEra era =>
IsMaryEraOnwards era =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to keep both constraints here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Neither implies the other

Value ->
TxOutValue era
mkTxOutValue =
TxOutValue (maryEraOnwards @era)
mkTxOutValue v =
shelleyBasedEraConstraints (shelleyBasedEra @era) $ TxOutValueShelleyBased (shelleyBasedEra @era) (toLedgerValue (maryEraOnwards @era) v)
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ toLedgerValidityInterval (lowerBound, upperBound) =
TxValidityLowerBound _ s -> SJust s
, Ledger.invalidHereafter =
case upperBound of
TxValidityNoUpperBound _ -> SNothing
TxValidityUpperBound _ s -> maybeToStrictMaybe s
}
fromLedgerValidityInterval ::
Expand Down
Loading
Loading