Skip to content

Commit

Permalink
Update cardano-api: 9.1 -> 9.3
Browse files Browse the repository at this point in the history
  • Loading branch information
locallycompact authored and ch1bo committed Oct 4, 2024
1 parent d3db623 commit 3e288c3
Show file tree
Hide file tree
Showing 37 changed files with 134 additions and 132 deletions.
2 changes: 1 addition & 1 deletion hydra-cardano-api/hydra-cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ library
, base >=4.16
, base16-bytestring
, bytestring
, cardano-api ^>=9.1
, cardano-api ^>=9.3
, cardano-binary
, cardano-crypto-class
, cardano-ledger-allegra
Expand Down
15 changes: 8 additions & 7 deletions hydra-cardano-api/src/Hydra/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import Cardano.Api as X hiding (
import Cardano.Api.Byron as X (
Address (..),
)
import Cardano.Api.Experimental as X (UnsignedTx (..))
import Cardano.Api.Ledger as X (
PParams,
)
Expand Down Expand Up @@ -205,7 +206,7 @@ pattern ShelleyAddressInAnyEra <-
type BalancedTxBody = Cardano.Api.BalancedTxBody Era
{-# COMPLETE BalancedTxBody #-}

pattern BalancedTxBody :: TxBodyContent BuildTx -> TxBody -> TxOut CtxTx -> Coin -> BalancedTxBody
pattern BalancedTxBody :: TxBodyContent BuildTx -> UnsignedTx Era -> TxOut CtxTx -> Coin -> BalancedTxBody
pattern BalancedTxBody{balancedTxBodyContent, balancedTxBody, balancedTxChangeOutput, balancedTxFee} <-
Cardano.Api.BalancedTxBody balancedTxBodyContent balancedTxBody balancedTxChangeOutput balancedTxFee
where
Expand Down Expand Up @@ -373,7 +374,7 @@ pattern TxBody{txBodyContent} <-
{-# COMPLETE TxBody #-}

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

defaultTxBodyContent :: TxBodyContent BuildTx
defaultTxBodyContent = Cardano.Api.defaultTxBodyContent shelleyBasedEra
Expand All @@ -386,7 +387,7 @@ type TxBodyContent buidl = Cardano.Api.TxBodyContent buidl Era
pattern TxBodyContent ::
TxIns buidl ->
TxInsCollateral ->
TxInsReference buidl ->
TxInsReference ->
[TxOut CtxTx] ->
TxTotalCollateral Era ->
TxReturnCollateral CtxTx Era ->
Expand All @@ -404,7 +405,7 @@ pattern TxBodyContent ::
TxScriptValidity ->
Maybe (Featured ConwayEraOnwards Era (TxProposalProcedures buidl Era)) ->
Maybe (Featured ConwayEraOnwards Era (TxVotingProcedures buidl Era)) ->
Maybe (Featured ConwayEraOnwards Era Coin) ->
Maybe (Featured ConwayEraOnwards Era (Maybe Coin)) ->
Maybe (Featured ConwayEraOnwards Era Coin) ->
TxBodyContent buidl
pattern TxBodyContent
Expand Down Expand Up @@ -515,17 +516,17 @@ type TxIns buidl = [(TxIn, BuildTxWith buidl (Cardano.Api.Witness WitCtxTxIn Era

-- ** TxInsReference

type TxInsReference buidl = Cardano.Api.TxInsReference buidl Era
type TxInsReference = Cardano.Api.TxInsReference Era
{-# COMPLETE TxInsReferenceNone, TxInsReference #-}

pattern TxInsReferenceNone :: TxInsReference buidl
pattern TxInsReferenceNone :: TxInsReference
pattern TxInsReferenceNone <-
Cardano.Api.TxInsReferenceNone
where
TxInsReferenceNone =
Cardano.Api.TxInsReferenceNone

pattern TxInsReference :: [TxIn] -> TxInsReference buidl
pattern TxInsReference :: [TxIn] -> TxInsReference
pattern TxInsReference{txInsReference'} <-
Cardano.Api.TxInsReference _ txInsReference'
where
Expand Down
3 changes: 2 additions & 1 deletion hydra-cardano-api/src/Hydra/Cardano/Api/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Data.Function (on)
import Data.List (intercalate, sort, sortBy)
import Data.Map.Strict qualified as Map
import Data.Text qualified as T
import GHC.IsList (IsList (..))
import Hydra.Cardano.Api.ScriptData (fromLedgerData)

-- | Obtain a human-readable pretty text representation of a transaction.
Expand Down Expand Up @@ -96,7 +97,7 @@ renderTxWithUTxO utxo (Tx body _wits) =

totalNumberOfAssets =
let totalValue = foldMap Api.txOutValue outs
in length $ valueToList totalValue
in length $ toList totalValue

validityLines =
[ "== VALIDITY"
Expand Down
7 changes: 4 additions & 3 deletions hydra-cardano-api/src/Hydra/Cardano/Api/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Cardano.Ledger.Alonzo.Plutus.TxInfo qualified as Ledger
import Cardano.Ledger.Core (getMinCoinTxOut)
import Cardano.Ledger.Mary.Value qualified as Ledger
import Data.Word (Word64)
import GHC.IsList (IsList (..))
import Hydra.Cardano.Api.CtxUTxO (ToUTxOContext (..))
import Hydra.Cardano.Api.PolicyId (fromPlutusCurrencySymbol)
import PlutusLedgerApi.V1.Value (flattenValue)
Expand Down Expand Up @@ -47,7 +48,7 @@ minUTxOValue pparams (TxOut addr val dat ref) =

-- | Count number of assets in a 'Value'.
valueSize :: Value -> Int
valueSize = length . valueToList
valueSize = length . toList

-- | Access minted assets of a transaction, as an ordered association list.
txMintAssets :: Tx era -> [(AssetId, Quantity)]
Expand All @@ -56,7 +57,7 @@ txMintAssets =
where
asList = \case
TxMintNone -> []
TxMintValue _ val _ -> valueToList val
TxMintValue _ val _ -> toList val

-- * Type Conversions

Expand All @@ -80,7 +81,7 @@ toLedgerValue =
-- | Convert a plutus 'Value' into a cardano-api 'Value'.
fromPlutusValue :: Plutus.Value -> Maybe Value
fromPlutusValue plutusValue = do
fmap valueFromList . mapM convertAsset $ flattenValue plutusValue
fmap fromList . mapM convertAsset $ flattenValue plutusValue
where
convertAsset (cs, tk, i)
| cs == adaSymbol && tk == adaToken =
Expand Down
16 changes: 9 additions & 7 deletions hydra-cluster/src/Hydra/Cluster/Faucet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ seedFromFaucet node@RunningNode{networkId, nodeSocket} receivingVerificationKey
let changeAddress = ShelleyAddressInEra (buildAddress faucetVk networkId)
buildTransaction networkId nodeSocket changeAddress faucetUTxO [] [theOutput] >>= \case
Left e -> throwIO $ FaucetFailedToBuildTx{reason = e}
Right body -> do
let signedTx = sign faucetSk body
submitTransaction networkId nodeSocket (sign faucetSk body)
Right tx -> do
let signedTx = sign faucetSk $ getTxBody tx
submitTransaction networkId nodeSocket signedTx
pure signedTx

receivingAddress = buildAddress receivingVerificationKey networkId
Expand Down Expand Up @@ -138,7 +138,7 @@ returnFundsToFaucet' tracer RunningNode{networkId, nodeSocket} senderSk = do
-- entire value is created and paid to the faucet address.
buildTransaction networkId nodeSocket faucetAddress utxo [] [] >>= \case
Left e -> throwIO $ FaucetFailedToBuildTx{reason = e}
Right body -> pure body
Right tx -> pure $ getTxBody tx

-- Use the Faucet utxo to create the output at specified address
createOutputAtAddress ::
Expand Down Expand Up @@ -167,8 +167,10 @@ createOutputAtAddress node@RunningNode{networkId, nodeSocket} atAddress datum va
collateralTxIns
[output]
>>= \case
Left e -> throwErrorAsException e
Right body -> do
Left e ->
throwErrorAsException e
Right x -> do
let body = getTxBody x
let tx = makeSignedTransaction [makeShelleyKeyWitness body (WitnessPaymentKey faucetSk)] body
submitTransaction networkId nodeSocket tx
newUtxo <- awaitTransaction networkId nodeSocket tx
Expand All @@ -193,7 +195,7 @@ calculateTxFee RunningNode{networkId, nodeSocket} secretKey utxo addr lovelace =
let theOutput = TxOut addr (lovelaceToValue lovelace) TxOutDatumNone ReferenceScriptNone
in buildTransaction networkId nodeSocket addr utxo [] [theOutput] >>= \case
Left e -> throwIO $ FaucetFailedToBuildTx{reason = e}
Right body -> pure $ txFee' (sign secretKey body)
Right tx -> pure $ txFee' (sign secretKey $ getTxBody tx)

-- | Try to submit tx and retry when some caught exception/s take place.
retryOnExceptions :: (MonadCatch m, MonadDelay m) => Tracer m FaucetLog -> m a -> m a
Expand Down
8 changes: 4 additions & 4 deletions hydra-cluster/src/Hydra/Cluster/Scenarios.hs
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,8 @@ singlePartyCommitsFromExternalTxBlueprint tracer workDir node hydraScriptsTxId =
ReferenceScriptNone
buildTransaction networkId nodeSocket someAddress utxoToCommit (fst <$> UTxO.pairs someUTxO) [someOutput] >>= \case
Left e -> failure $ show e
Right body -> do
let unsignedTx = makeSignedTransaction [] body
Right tx -> do
let unsignedTx = makeSignedTransaction [] $ getTxBody tx
let clientPayload =
Aeson.object
[ "blueprintTx" .= unsignedTx
Expand Down Expand Up @@ -598,8 +598,8 @@ canSubmitTransactionThroughAPI tracer workDir node hydraScriptsTxId =
-- prepare fully balanced tx body
buildTransaction networkId nodeSocket bobsAddress bobUTxO (fst <$> UTxO.pairs bobUTxO) [carolsOutput] >>= \case
Left e -> failure $ show e
Right body -> do
let unsignedTx = makeSignedTransaction [] body
Right tx -> do
let unsignedTx = makeSignedTransaction [] $ getTxBody tx
let unsignedRequest = toJSON unsignedTx
sendRequest hydraNodeId unsignedRequest
`shouldThrow` expectErrorStatus 400 (Just "MissingVKeyWitnessesUTXOW")
Expand Down
4 changes: 2 additions & 2 deletions hydra-cluster/src/Hydra/Generator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ generateDemoUTxODataset network nodeSocket allClientKeys nTxs = do
ReferenceScriptNone
buildTransaction network nodeSocket changeAddress faucetUTxO [] recipientOutputs >>= \case
Left e -> throwIO $ FaucetFailedToBuildTx{reason = e}
Right body -> do
let signedTx = sign faucetSk body
Right tx -> do
let signedTx = sign faucetSk $ getTxBody tx
pure signedTx
let dataset clientKeys =
generateClientDataset network fundingTransaction clientKeys nTxs generateOneSelfTransfer
Expand Down
4 changes: 2 additions & 2 deletions hydra-node/src/Hydra/Chain/CardanoClient.hs
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ buildTransaction ::
[TxIn] ->
-- | Outputs to create.
[TxOut CtxTx] ->
IO (Either (TxBodyErrorAutoBalance Era) TxBody)
IO (Either (TxBodyErrorAutoBalance Era) Tx)
buildTransaction networkId socket changeAddress utxoToSpend collateral outs = do
pparams <- queryProtocolParameters networkId socket QueryTip
systemStart <- querySystemStart networkId socket QueryTip
eraHistory <- queryEraHistory networkId socket QueryTip
stakePools <- queryStakePools networkId socket QueryTip
pure $
second balancedTxBody $
second ((\(UnsignedTx unsignedTx) -> fromLedgerTx unsignedTx) . balancedTxBody) $
makeTransactionBodyAutoBalance
shelleyBasedEra
systemStart
Expand Down
13 changes: 8 additions & 5 deletions hydra-node/src/Hydra/Chain/Direct/State.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Data.Fixed (Milli)
import Data.Map qualified as Map
import Data.Maybe (fromJust)
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
import GHC.IsList qualified as IsList
import Hydra.Cardano.Api (
AssetId (..),
AssetName (AssetName),
Expand Down Expand Up @@ -49,8 +50,7 @@ import Hydra.Cardano.Api (
txIns',
txOutScriptData,
txOutValue,
valueFromList,
valueToList,
txSpendingUTxO,
pattern ByronAddressInEra,
pattern ShelleyAddressInEra,
pattern TxOut,
Expand Down Expand Up @@ -117,7 +117,7 @@ import Hydra.Tx.Init (initTx)
import Hydra.Tx.OnChainId (OnChainId)
import Hydra.Tx.Recover (recoverTx)
import Hydra.Tx.Snapshot (genConfirmedSnapshot)
import Hydra.Tx.Utils (splitUTxO, txSpendingUTxO, verificationKeyToOnChainId)
import Hydra.Tx.Utils (splitUTxO, verificationKeyToOnChainId)
import Test.Hydra.Tx.Fixture (testNetworkId)
import Test.Hydra.Tx.Gen (
genOneUTxOFor,
Expand Down Expand Up @@ -745,7 +745,7 @@ utxoOfThisHead :: PolicyId -> UTxO -> UTxO
utxoOfThisHead policy = UTxO.filter hasHeadToken
where
hasHeadToken =
isJust . find isHeadToken . valueToList . txOutValue
isJust . find isHeadToken . IsList.toList . txOutValue

isHeadToken (assetId, quantity) =
case assetId of
Expand Down Expand Up @@ -1114,7 +1114,10 @@ genCommits' genUTxO ctx txInit = do
in map (fmap (modifyTxOutValue (scaleQuantitiesDownBy numberOfUTxOs))) commitUTxOs

scaleQuantitiesDownBy x =
valueFromList . map (\(an, Quantity q) -> (an, Quantity $ q `div` fromIntegral x)) . valueToList
-- XXX: Foldable Value instance would be nice here
IsList.fromList
. map (\(an, Quantity q) -> (an, Quantity $ q `div` fromIntegral x))
. IsList.toList

genCommitFor :: VerificationKey PaymentKey -> Gen UTxO
genCommitFor vkey =
Expand Down
5 changes: 3 additions & 2 deletions hydra-node/src/Hydra/Chain/Direct/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ module Hydra.Chain.Direct.Tx (
) where

import Hydra.Cardano.Api
import Hydra.Prelude
import Hydra.Prelude hiding (toList)

import Cardano.Api.UTxO qualified as UTxO
import Data.Aeson qualified as Aeson
import Data.ByteString qualified as BS
import Data.ByteString.Base16 qualified as Base16
import Data.Map qualified as Map
import GHC.IsList (IsList (..))
import Hydra.Contract.Commit qualified as Commit
import Hydra.Contract.Deposit qualified as Deposit
import Hydra.Contract.Head qualified as Head
Expand Down Expand Up @@ -604,7 +605,7 @@ txInToHeadSeed txin = UnsafeHeadSeed $ toStrict $ Aeson.encode txin

findHeadAssetId :: TxOut ctx -> Maybe (PolicyId, AssetName)
findHeadAssetId txOut =
flip findFirst (valueToList $ txOutValue txOut) $ \case
flip findFirst (toList $ txOutValue txOut) $ \case
(AssetId pid aname, q)
| aname == hydraHeadV1AssetName && q == 1 ->
Just (pid, aname)
Expand Down
6 changes: 5 additions & 1 deletion hydra-node/src/Hydra/Chain/ScriptRegistry.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Hydra.Chain.ScriptRegistry where

import Hydra.Prelude

import Cardano.Api.Experimental.Tx (UnsignedTx (..))
import Cardano.Api.UTxO qualified as UTxO
import Hydra.Cardano.Api (
Key (..),
Expand All @@ -17,6 +18,8 @@ import Hydra.Cardano.Api (
TxIx (..),
WitCtx (..),
examplePlutusScriptAlwaysFails,
fromLedgerTx,
getTxBody,
getTxId,
makeShelleyKeyWitness,
makeSignedTransaction,
Expand Down Expand Up @@ -95,7 +98,8 @@ publishHydraScripts networkId socketPath sk = do
>>= \case
Left e ->
throwErrorAsException e
Right body -> do
Right x -> do
let body = getTxBody x
let tx = makeSignedTransaction [makeShelleyKeyWitness body (WitnessPaymentKey sk)] body
submitTransaction networkId socketPath tx
void $ awaitTransaction networkId socketPath tx
Expand Down
4 changes: 2 additions & 2 deletions hydra-node/test/Hydra/Chain/Direct/TxSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ generateCommitUTxOs parties = do
mconcat
[ lovelaceToValue (Coin 2000000)
, foldMap txOutValue utxo
, valueFromList
, fromList
[ (AssetId testPolicyId (assetNameFromVerificationKey vk), 1)
]
]
Expand Down Expand Up @@ -407,7 +407,7 @@ genAbortableOutputs parties =
toUTxOContext $
TxOut
(mkScriptAddress @PlutusScriptV2 testNetworkId initialScript)
(valueFromList [(AssetId testPolicyId (assetNameFromVerificationKey vk), 1)])
(fromList [(AssetId testPolicyId (assetNameFromVerificationKey vk), 1)])
(mkTxOutDatumInline initialDatum)
ReferenceScriptNone

Expand Down
7 changes: 4 additions & 3 deletions hydra-node/test/Hydra/Ledger/CardanoSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Hydra.Ledger.CardanoSpec where

import Hydra.Cardano.Api
import Hydra.Prelude
import Hydra.Prelude hiding (toList)
import Test.Hydra.Prelude

import Cardano.Ledger.Api (ensureMinCoinTxOut)
Expand All @@ -12,6 +12,7 @@ import Data.Aeson (eitherDecode, encode)
import Data.Aeson qualified as Aeson
import Data.Aeson.Lens (key)
import Data.Text (unpack)
import GHC.IsList (IsList (..))
import Hydra.Cardano.Api.Pretty (renderTx)
import Hydra.Chain.ChainState (ChainSlot (ChainSlot))
import Hydra.JSONSchema (prop_validateJSONSchema)
Expand Down Expand Up @@ -140,7 +141,7 @@ propRealisticValue value =
numberOfAssets < 100
& counterexample ("too many individual assets: " <> show numberOfAssets)
where
numberOfAssets = length (valueToList value)
numberOfAssets = length (toList value)

-- | Check that an output has enough lovelace to cover asset deposits.
propHasEnoughLovelace :: TxOut CtxUTxO -> Property
Expand Down Expand Up @@ -180,7 +181,7 @@ propGeneratesGoodTxOut txOut =

hasMultiAssets = any (\(an, _) -> an /= AdaAssetId) assets

assets = valueToList $ txOutValue txOut
assets = toList $ txOutValue txOut

isVKOutput = case txOutAddress txOut of
ByronAddressInEra ByronAddress{} -> False
Expand Down
Loading

0 comments on commit 3e288c3

Please sign in to comment.