cardano-api
Safe HaskellNone
LanguageHaskell2010

Cardano.Api.Experimental.Tx

Synopsis

Creating transactions using the new API

Both the old and new APIs can be used to create transactions, and it is possible to transform a transaction from one format to the other as they share the same representation. However, the focus will shift towards using the new API, while the old API will be deprecated to ensure simplicity, closer alignment with the ledger, and easier maintenance.

In both the new and old APIs, constructing a transaction requires creating a TxBodyContent, along with at least one witness (for example, a ShelleyWitnessSigningKey) to sign the transaction. This process remains unchanged.

To learn how to create a transaction using the old API, see the Cardano.Api.Tx.Internal.Body documentation.

In the examples below, the following qualified modules are used:

import qualified Cardano.Api as Api                -- the general `cardano-api` exports (including the old API)
import qualified Cardano.Api.Script as Script      -- types related to scripts (Plutus and native)
import qualified Cardano.Api.Ledger as Ledger      -- cardano-ledger re-exports
import qualified Cardano.Api.Experimental as Exp   -- the experimental API

For instructions on how to do this, refer to the Test.Cardano.Api.Experimental documentation.

Creating a TxBodyContent

Regardless of whether the experimental or the traditional API is used, creating a TxBodyContent is necessary.

You can see how to do this in the documentation of the Cardano.Api.Tx.Internal.Body module.

Balancing a transaction

If a UTXO has exactly 12 ada, the transaction could be constructed as described in Cardano.Api.Tx.Internal.Body, and it would be valid. However:

  • Ada may be wasted
  • The UTXO that we intend to spend may not contain exactly 12 ada
  • The transaction may not be this simple.

For these reasons, it is recommended to balance the transaction before proceeding with signing and submitting.

For instructions on how to balance a transaction, refer to the Cardano.Api.Tx.Internal.Fee documentation.

Creating a ShelleyWitnessSigningKey

Signing a transaction requires a witness, such as a ShelleyWitnessSigningKey.

For instructions on creating a ShelleyWitnessSigningKey refer to the Cardano.Api.Tx.Internal.Sign documentation.

Creating a transaction using the new API

This section outlines how to create a transaction using the new API. First, create an UnsignedTx using the makeUnsignedTx function and the Era and TxBodyContent that we defined earlier:

let (Right unsignedTx) = Exp.makeUnsignedTx era txBodyContent

Next, use the key witness to sign the unsigned transaction with the makeKeyWitness function:

let transactionWitness = Exp.makeKeyWitness era unsignedTx (Api.WitnessPaymentKey signingKey)

Finally, sign the transaction using the signTx function:

let newApiSignedTx :: Ledger.Tx (Exp.LedgerEra Exp.ConwayEra) = Exp.signTx era [] [transactionWitness] unsignedTx

The empty list represents the bootstrap witnesses, which are not needed in this case.

The transaction is now signed.

Converting a transaction from the new API to the old API

A transaction created with the new API can be easily converted to the old API by wrapping it with the ShelleyTx constructor:

let oldStyleTx :: Api.Tx Api.ConwayEra = ShelleyTx sbe newApiSignedTx

Inspecting transactions

When using a Tx created with the experimental API, the TxBody and TxWits can be extracted using the txBody and txWits lenses from Cardano.Api.Ledger respectively.

Contents

data UnsignedTx era Source #

A transaction that can contain everything except key witnesses.

Constructors

EraTx era => UnsignedTx (Tx era) 

Instances

Instances details
Typeable era => HasTypeProxy (UnsignedTx era) Source # 
Instance details

Defined in Cardano.Api.Experimental.Tx.Internal.Type

Associated Types

data AsType (UnsignedTx era) 
Instance details

Defined in Cardano.Api.Experimental.Tx.Internal.Type

data AsType (UnsignedTx era) = AsUnsignedTx (AsType (ToApiEra era))
EraTx era => SerialiseAsRawBytes (UnsignedTx era) Source # 
Instance details

Defined in Cardano.Api.Experimental.Tx.Internal.Type

Show (UnsignedTx era) Source # 
Instance details

Defined in Cardano.Api.Experimental.Tx.Internal.Type

Eq (UnsignedTx era) Source # 
Instance details

Defined in Cardano.Api.Experimental.Tx.Internal.Type

Methods

(==) :: UnsignedTx era -> UnsignedTx era -> Bool Source #

(/=) :: UnsignedTx era -> UnsignedTx era -> Bool Source #

data AsType (UnsignedTx era) Source # 
Instance details

Defined in Cardano.Api.Experimental.Tx.Internal.Type

data AsType (UnsignedTx era) = AsUnsignedTx (AsType (ToApiEra era))

data SignedTx era Source #

A transaction that has been witnesssed

Constructors

EraTx (LedgerEra era) => SignedTx (Tx (LedgerEra era)) 

Instances

Instances details
HasTypeProxy era => HasTypeProxy (SignedTx era) Source # 
Instance details

Defined in Cardano.Api.Experimental.Tx

Associated Types

data AsType (SignedTx era) 
Instance details

Defined in Cardano.Api.Experimental.Tx

data AsType (SignedTx era) = AsSignedTx (AsType era)
(HasTypeProxy era, EraTx (LedgerEra era)) => SerialiseAsRawBytes (SignedTx era) Source # 
Instance details

Defined in Cardano.Api.Experimental.Tx

Show (SignedTx era) Source # 
Instance details

Defined in Cardano.Api.Experimental.Tx

Eq (SignedTx era) Source # 
Instance details

Defined in Cardano.Api.Experimental.Tx

Methods

(==) :: SignedTx era -> SignedTx era -> Bool Source #

(/=) :: SignedTx era -> SignedTx era -> Bool Source #

data AsType (SignedTx era) Source # 
Instance details

Defined in Cardano.Api.Experimental.Tx

data AsType (SignedTx era) = AsSignedTx (AsType era)

TxBodyContent

mkTxCertificates :: Era era -> [(Certificate (LedgerEra era), AnyWitness (LedgerEra era))] -> TxCertificates (LedgerEra era) Source #

Create TxCertificates. Note that 'Certificate era' will be deduplicated. Only Certificates with a stake credential will be in the result.

Note that, when building a transaction in Conway era, a witness is not required for staking credential registration, but this is only the case during the transitional period of Conway era and only for staking credential registration certificates without a deposit. Future eras will require a witness for registration certificates, because the one without a deposit will be removed.

mkTxVotingProcedures :: [(VotingProcedures era, AnyWitness era)] -> Either (VotesMergingConflict era) (TxVotingProcedures era) Source #

Create voting procedures from map of voting procedures and optional witnesses. Validates the function argument, to make sure the list of votes is legal. See mergeVotingProcedures for validation rules.

mkTxProposalProcedures :: IsEra era => [(ProposalProcedure (LedgerEra era), AnyWitness (LedgerEra era))] -> TxProposalProcedures (LedgerEra era) Source #

A smart constructor for TxProposalProcedures. It makes sure that the value produced is consistent - the witnessed proposals are also present in the first constructor parameter.

modTxOuts :: ([TxOut era] -> [TxOut era]) -> TxBodyContent era -> TxBodyContent era Source #

Legacy Conversions

TxBodyContent sub type

data TxOut era where Source #

Constructors

TxOut :: forall era. EraTxOut era => TxOut era -> TxOut era 

Instances

Instances details
Show (TxOut era) Source # 
Instance details

Defined in Cardano.Api.Experimental.Tx.Internal.BodyContent.New

Methods

showsPrec :: Int -> TxOut era -> ShowS Source #

show :: TxOut era -> String Source #

showList :: [TxOut era] -> ShowS Source #

Eq (TxOut era) Source # 
Instance details

Defined in Cardano.Api.Experimental.Tx.Internal.BodyContent.New

Methods

(==) :: TxOut era -> TxOut era -> Bool Source #

(/=) :: TxOut era -> TxOut era -> Bool Source #

data TxInsReference era Source #

Constructors

TxInsReference [TxIn] (Set (Datum CtxTx era)) 

Witness

Any witness (key, simple script, plutus script).

data AnyWitness era where Source #

Here we consider three types of witnesses in Cardano: * key witnesses * simple script witnesses * Plutus script witnesses

Note that AnyKeyWitnessPlaceholder does not contain the actual key witness. This is because key witnesses are provided in the signing stage of the transaction. However we need this constuctor to index the witnessable things correctly when plutus scripts are being used within the transaction. AnyWitness is solely used to contruct the transaction body.

Constructors

AnyKeyWitnessPlaceholder :: forall era. AnyWitness era 
AnySimpleScriptWitness :: forall era. SimpleScriptOrReferenceInput era -> AnyWitness era 
AnyPlutusScriptWitness :: forall (lang :: Language) (purpose :: PlutusScriptPurpose) era. AnyPlutusScriptWitness lang purpose era -> AnyWitness era 

Instances

Instances details
Show (AnyWitness era) Source # 
Instance details

Defined in Cardano.Api.Experimental.Tx.Internal.AnyWitness

Eq (AnyWitness era) Source # 
Instance details

Defined in Cardano.Api.Experimental.Tx.Internal.AnyWitness

Methods

(==) :: AnyWitness era -> AnyWitness era -> Bool Source #

(/=) :: AnyWitness era -> AnyWitness era -> Bool Source #

getAnyWitnessScriptData :: Era era => AnyWitness era -> TxDats era Source #

NB this does not include datums from inline datums existing at tx outputs!

All the parts that constitute a plutus script witness but also including simple scripts

data TxScriptWitnessRequirements era Source #

This type collects all the requirements for script witnesses in a transaction.

Instances

Instances details
Monoid (TxScriptWitnessRequirements AlonzoEra) Source # 
Instance details

Defined in Cardano.Api.Experimental.Tx.Internal.TxScriptWitnessRequirements

Monoid (TxScriptWitnessRequirements BabbageEra) Source # 
Instance details

Defined in Cardano.Api.Experimental.Tx.Internal.TxScriptWitnessRequirements

Monoid (TxScriptWitnessRequirements ConwayEra) Source # 
Instance details

Defined in Cardano.Api.Experimental.Tx.Internal.TxScriptWitnessRequirements

Monoid (TxScriptWitnessRequirements DijkstraEra) Source # 
Instance details

Defined in Cardano.Api.Experimental.Tx.Internal.TxScriptWitnessRequirements

Semigroup (TxScriptWitnessRequirements AlonzoEra) Source # 
Instance details

Defined in Cardano.Api.Experimental.Tx.Internal.TxScriptWitnessRequirements

Semigroup (TxScriptWitnessRequirements BabbageEra) Source # 
Instance details

Defined in Cardano.Api.Experimental.Tx.Internal.TxScriptWitnessRequirements

Semigroup (TxScriptWitnessRequirements ConwayEra) Source # 
Instance details

Defined in Cardano.Api.Experimental.Tx.Internal.TxScriptWitnessRequirements

Semigroup (TxScriptWitnessRequirements DijkstraEra) Source # 
Instance details

Defined in Cardano.Api.Experimental.Tx.Internal.TxScriptWitnessRequirements

Plutus related

data Datum ctx era where Source #

Constructors

TxOutDatumHash :: forall ctx era. DataHash -> Datum ctx era 
TxOutSupplementalDatum :: forall era. DataHash -> Data era -> Datum CtxTx era 
TxOutDatumInline :: forall era ctx. DataHash -> Data era -> Datum ctx era 

Instances

Instances details
Show (Datum ctx era) Source # 
Instance details

Defined in Cardano.Api.Experimental.Tx.Internal.BodyContent.New

Methods

showsPrec :: Int -> Datum ctx era -> ShowS Source #

show :: Datum ctx era -> String Source #

showList :: [Datum ctx era] -> ShowS Source #

Eq (Datum ctx era) Source # 
Instance details

Defined in Cardano.Api.Experimental.Tx.Internal.BodyContent.New

Methods

(==) :: Datum ctx era -> Datum ctx era -> Bool Source #

(/=) :: Datum ctx era -> Datum ctx era -> Bool Source #

getDatums Source #

Arguments

:: IsEra era 
=> TxInsReference (LedgerEra era)

reference inputs

-> [TxOut (LedgerEra era)] 
-> TxDats (LedgerEra era) 

Extract datum: 1. supplemental datums from transaction outputs 2. datums from reference inputs

Note that this function does not check whose datum hashes are present in the reference inputs. This means if there are redundant datums in TxInsReference, a submission of such transaction will fail.

Collecting plutus script witness related transaction requirements.

collectPlutusScriptHashes :: IsEra era => UnsignedTx (LedgerEra era) -> UTxO (LedgerEra era) -> Map ScriptWitnessIndex ScriptHash Source #

Collect all plutus script hashes that are needed to validate the given transaction and return them in a map with their corresponding ScriptWitnessIndex as key.

Balancing transactions

evaluateTransactionExecutionUnits :: IsEra era => SystemStart -> LedgerEpochInfo -> PParams (LedgerEra era) -> UTxO (LedgerEra era) -> Tx (LedgerEra era) -> Map ScriptWitnessIndex (Either ScriptExecutionError (EvalTxExecutionUnitsLog, ExecutionUnits)) Source #

Compute the ExecutionUnits required for each script in the transaction.

This process involves executing all scripts and counting the actual execution units consumed.

makeTransactionBodyAutoBalance Source #

Arguments

:: (HasCallStack, IsEra era) 
=> SystemStart 
-> LedgerEpochInfo 
-> PParams (LedgerEra era) 
-> Set PoolId

The set of registered stake pools, being unregistered in this transaction.

-> Map StakeCredential Coin

The map of all deposits for stake credentials that are being unregistered in this transaction

-> Map (Credential 'DRepRole) Coin

The map of all deposits for DRep credentials that are being unregistered in this transaction

-> UTxO (LedgerEra era)

The transaction inputs (including reference and collateral ones), not the entire UTxO.

-> TxBodyContent (LedgerEra era) 
-> AddressInEra era

Change address

-> Maybe Word

Override key witnesses

-> Either (TxBodyErrorAutoBalance (LedgerEra era)) (UnsignedTx (LedgerEra era), TxBodyContent (LedgerEra era)) 

This is similar to makeTransactionBody but with greater automation to calculate suitable values for several things.

In particular:

  • It calculates the correct script ExecutionUnits (ignoring the provided values, which can thus be zero).
  • It calculates the transaction fees based on the script ExecutionUnits, the current ProtocolParameters, and an estimate of the number of key witnesses (i.e. signatures). There is an override for the number of key witnesses.
  • It accepts a change address, calculates the balance of the transaction and puts the excess change into the change output.
  • It also checks that the balance is positive and the change is above the minimum threshold.

To do this, it requires more information than makeTransactionBody, all of which can be queried from a local node.

data TxBodyErrorAutoBalance era Source #

Constructors

TxBodyErrorBalanceNegative Coin MultiAsset

There is not enough ada and non-ada to cover both the outputs and the fees. The transaction should be changed to provide more input assets, or otherwise adjusted to need less (e.g. outputs, script etc).

TxBodyErrorAdaBalanceTooSmall

There is enough ada to cover both the outputs and the fees, but the resulting change is too small: it is under the minimum value for new UTXO entries. The transaction should be changed to provide more input ada.

Fields

TxBodyErrorMinUTxONotMet

The minimum spendable UTxO threshold has not been met.

Fields

TxBodyErrorNonAdaAssetsUnbalanced Value 
TxBodyErrorScriptWitnessIndexMissingFromExecUnitsMap ScriptWitnessIndex (Map ScriptWitnessIndex ExecutionUnits) 
TxBodyScriptExecutionError [(ScriptWitnessIndex, ScriptExecutionError)]

One or more scripts failed to execute correctly.

TxBodyScriptBadScriptValidity

One or more scripts were expected to fail validation, but none did.

BalanceIsNegative 

Fields

NotEnoughAdaInUTxO 

Fields

Internal functions