Skip to content

Commit 50702e6

Browse files
authored
Merge pull request #698 from IntersectMBO/haddock-for-experimental-api
Added examples of transaction creation to Haddock
2 parents ba6003d + 7c976f6 commit 50702e6

File tree

9 files changed

+947
-172
lines changed

9 files changed

+947
-172
lines changed

cardano-api/cardano-api.cabal

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ library
7777
Cardano.Api.Internal.Eon.ShelleyBasedEra
7878
Cardano.Api.Internal.Eras
7979
Cardano.Api.Internal.Error
80+
Cardano.Api.Internal.Experimental.Eras
81+
Cardano.Api.Internal.Experimental.Tx
8082
Cardano.Api.Internal.Fees
8183
Cardano.Api.Internal.Genesis
8284
Cardano.Api.Internal.GenesisParameters
@@ -92,6 +94,7 @@ library
9294
Cardano.Api.Internal.Script
9395
Cardano.Api.Internal.SerialiseLedgerCddl
9496
Cardano.Api.Internal.SerialiseTextEnvelope
97+
Cardano.Api.Internal.Tx.Body
9598
Cardano.Api.Internal.Tx.Sign
9699
Cardano.Api.Ledger
97100
Cardano.Api.Ledger.Lens
@@ -197,8 +200,6 @@ library
197200
Cardano.Api.Internal.Eon.ShelleyToMaryEra
198201
Cardano.Api.Internal.Eras.Case
199202
Cardano.Api.Internal.Eras.Core
200-
Cardano.Api.Internal.Experimental.Eras
201-
Cardano.Api.Internal.Experimental.Tx
202203
Cardano.Api.Internal.Feature
203204
Cardano.Api.Internal.Governance.Actions.ProposalProcedure
204205
Cardano.Api.Internal.Governance.Actions.VotingProcedure
@@ -247,7 +248,6 @@ library
247248
Cardano.Api.Internal.SerialiseUsing
248249
Cardano.Api.Internal.SpecialByron
249250
Cardano.Api.Internal.StakePoolMetadata
250-
Cardano.Api.Internal.Tx.Body
251251
Cardano.Api.Internal.Tx.UTxO
252252
Cardano.Api.Internal.TxIn
253253
Cardano.Api.Internal.TxMetadata

cardano-api/src/Cardano/Api/Experimental.hs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1-
-- | This module provides an experimental library interface that is intended
2-
-- to replace the existing api. It is subject to dramatic changes so use with caution.
1+
-- |
2+
-- This module provides an experimental library interface intended to replace the existing API.
3+
-- It is subject to significant changes. Please, use it with caution.
34
module Cardano.Api.Experimental
4-
( -- * Tx related
5+
( -- * Creating transactions
6+
7+
-- |
8+
-- For details and an example of creating a transaction using the experimental API,
9+
-- see the "Cardano.Api.Internal.Experimental.Tx" documentation.
10+
11+
-- * Contents
12+
13+
-- ** Transaction-related
514
UnsignedTx (..)
615
, UnsignedTxError (..)
716
, makeUnsignedTx
@@ -12,7 +21,8 @@ module Cardano.Api.Experimental
1221
, obtainCommonConstraints
1322
, hashTxBody
1423
, evaluateTransactionExecutionUnitsShelley
15-
-- Era related
24+
25+
-- ** Era-related
1626
, BabbageEra
1727
, ConwayEra
1828
, Era (..)

cardano-api/src/Cardano/Api/Internal/Experimental/Eras.hs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,16 @@ import GHC.Exts (IsString)
6161
import Prettyprinter
6262

6363
-- | Users typically interact with the latest features on the mainnet or experiment with features
64-
-- from the upcoming era. Hence, the protocol versions are limited to the current mainnet era
65-
-- and the next era (upcoming era).
64+
-- from the upcoming era. Therefore, protocol versions are limited to the current mainnet era
65+
-- and the next (upcoming) era.
6666
type family LedgerEra era = (r :: Type) | r -> era where
6767
LedgerEra BabbageEra = Ledger.Babbage
6868
LedgerEra ConwayEra = Ledger.Conway
6969

70-
-- | An existential wrapper for types of kind @k -> Types@. Use it to hold any era e.g. @Some Era@. One can
71-
-- then bring the era witness back into scope for example using this pattern:
70+
-- | An existential wrapper for types of kind @k -> Type@. It can hold any
71+
-- era, for example, @Some Era@. The era witness can be brought back into scope,
72+
-- for example, using this pattern:
73+
--
7274
-- @
7375
-- anyEra = Some ConwayEra
7476
-- -- then later in the code
@@ -82,15 +84,15 @@ data Some (f :: k -> Type) where
8284
=> f a
8385
-> Some f
8486

85-
-- | Represents the eras in Cardano's blockchain.
86-
-- This type represents eras currently on mainnet and new eras which are
87-
-- in development.
87+
-- | Represents the latest Cardano blockchain eras, including
88+
-- the one currently on mainnet and the upcoming one.
8889
--
89-
-- After a hardfork, the era from which we hardfork from gets deprecated and
90-
-- after deprecation period, gets removed. During deprecation period,
91-
-- consumers of cardano-api should update their codebase to the mainnet era.
90+
-- After a hard fork takes place, the era on mainnet before the hard fork
91+
-- is deprecated and, after a deprecation period, removed from @cardano-api@.
92+
-- During the deprecation period, @cardano-api@ users should update their
93+
-- codebase to the new mainnet era.
9294
data Era era where
93-
-- | The era currently active on Cardano's mainnet.
95+
-- | The currently active era on the Cardano mainnet.
9496
BabbageEra :: Era BabbageEra
9597
-- | The upcoming era in development.
9698
ConwayEra :: Era ConwayEra
@@ -159,15 +161,16 @@ eraFromStringLike = \case
159161
"Conway" -> pure $ Some ConwayEra
160162
wrong -> Left wrong
161163

162-
-- | How to deprecate an era
164+
-- | How to deprecate an era:
165+
--
166+
-- 1. Add the DEPRECATED pragma to the era type tag and constructor at the same time:
163167
--
164-
-- 1. Add DEPRECATED pragma to the era type tag and the era constructor at the same time:
165168
-- @
166169
-- {-# DEPRECATED BabbageEra "BabbageEra no longer supported, use ConwayEra" #-}
167170
-- data BabbageEra
168171
-- @
169172
--
170-
-- 2. Update haddock for the constructor of the deprecated era, mentioning deprecation.
173+
-- 2. Update the Haddock documentation for the constructor of the deprecated era, mentioning the deprecation.
171174
--
172175
-- @
173176
-- data Era era where
@@ -177,7 +180,9 @@ eraFromStringLike = \case
177180
-- ConwayEra :: Era ConwayEra
178181
-- @
179182
--
180-
-- 3. Add new 'IsEra' instance and update the deprecated era instance to produce a compile-time error:
183+
-- 3. Add a new 'IsEra' instance and update the deprecated era instance to
184+
-- produce a compile-time error:
185+
--
181186
-- @
182187
-- instance TypeError ('Text "IsEra BabbageEra: Deprecated. Update to ConwayEra") => IsEra BabbageEra where
183188
-- useEra = error "unreachable"
@@ -248,7 +253,7 @@ instance IsEra BabbageEra where
248253
instance IsEra ConwayEra where
249254
useEra = ConwayEra
250255

251-
-- | A temporary compatibility instance, for easier conversion between experimental and old API.
256+
-- | A temporary compatibility instance for easier conversion between the experimental and old APIs.
252257
instance Eon Era where
253258
inEonForEra v f = \case
254259
Api.ConwayEra -> f ConwayEra

cardano-api/src/Cardano/Api/Internal/Experimental/Tx.hs

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,110 @@
1010
{-# LANGUAGE UndecidableInstances #-}
1111

1212
module Cardano.Api.Internal.Experimental.Tx
13-
( UnsignedTx (..)
13+
( -- * Creating transactions using the new API
14+
15+
-- |
16+
-- Both the old and new APIs can be used to create transactions, and
17+
-- it is possible to transform a transaction from one format to the other
18+
-- as they share the same representation. However, the focus will shift
19+
-- towards using the new API, while the old API will be deprecated to ensure
20+
-- simplicity, closer alignment with the ledger, and easier maintenance.
21+
--
22+
-- In both the new and old APIs, constructing a transaction requires creating
23+
-- a 'TxBodyContent', along with at least one witness (for example, a
24+
-- 'ShelleyWitnessSigningKey') to sign the transaction.
25+
-- This process remains unchanged.
26+
--
27+
-- To learn how to create a transaction using the old API, see the
28+
-- "Cardano.Api.Internal.Tx.Body" documentation.
29+
--
30+
-- In the examples below, the following qualified modules are used:
31+
--
32+
-- @
33+
-- import qualified Cardano.Api as Api -- the general `cardano-api` exports (including the old API)
34+
-- import qualified Cardano.Api.Script as Script -- types related to scripts (Plutus and native)
35+
-- import qualified Cardano.Api.Ledger as Ledger -- cardano-ledger re-exports
36+
-- import qualified Cardano.Api.Experimental as Exp -- the experimental API
37+
-- @
38+
--
39+
-- For instructions on how to do this, refer to the @Test.Cardano.Api.Experimental@ documentation.
40+
41+
-- ** Creating a 'TxBodyContent'
42+
43+
-- |
44+
-- Regardless of whether the experimental or the traditional API is used, creating a 'TxBodyContent'
45+
-- is necessary.
46+
--
47+
-- You can see how to do this in the documentation of the "Cardano.Api.Internal.Tx.Body" module.
48+
49+
-- ** Balancing a transaction
50+
51+
-- |
52+
-- If a UTXO has exactly 12 ada, the transaction could be constructed as described in
53+
-- "Cardano.Api.Internal.Tx.Body", and it would be valid. However:
54+
--
55+
-- * Ada may be wasted
56+
-- * The UTXO that we intend to spend may not contain exactly 12 ada
57+
-- * The transaction may not be this simple.
58+
--
59+
-- For these reasons, it is recommended to balance the transaction before proceeding with
60+
-- signing and submitting.
61+
--
62+
-- For instructions on how to balance a transaction, refer to the "Cardano.Api.Internal.Fees" documentation.
63+
64+
-- ** Creating a 'ShelleyWitnessSigningKey'
65+
66+
-- |
67+
-- Signing a transaction requires a witness, such as a 'ShelleyWitnessSigningKey'.
68+
--
69+
-- For instructions on creating a 'ShelleyWitnessSigningKey' refer to the "Cardano.Api.Internal.Tx.Sign" documentation.
70+
71+
-- ** Creating a transaction using the new API
72+
73+
-- |
74+
-- This section outlines how to create a transaction using the new API. First,
75+
-- create an 'UnsignedTx' using the 'makeUnsignedTx' function and the 'Era' and
76+
-- 'TxBodyContent' that we defined earlier:
77+
--
78+
-- @
79+
-- let (Right unsignedTx) = Exp.makeUnsignedTx era txBodyContent
80+
-- @
81+
--
82+
-- Next, use the key witness to sign the unsigned transaction with the 'makeKeyWitness' function:
83+
--
84+
-- @
85+
-- let transactionWitness = Exp.makeKeyWitness era unsignedTx (Api.WitnessPaymentKey signingKey)
86+
-- @
87+
--
88+
-- Finally, sign the transaction using the 'signTx' function:
89+
--
90+
-- @
91+
-- let newApiSignedTx :: Ledger.Tx (Exp.LedgerEra Exp.ConwayEra) = Exp.signTx era [] [transactionWitness] unsignedTx
92+
-- @
93+
--
94+
-- The empty list represents the bootstrap witnesses, which are not needed in this case.
95+
--
96+
-- The transaction is now signed.
97+
98+
-- ** Converting a transaction from the new API to the old API
99+
100+
-- |
101+
-- A transaction created with the new API can be easily converted to the old API by
102+
-- wrapping it with the 'ShelleyTx' constructor:
103+
--
104+
-- @
105+
-- let oldStyleTx :: Api.Tx Api.ConwayEra = ShelleyTx sbe newApiSignedTx
106+
-- @
107+
108+
-- ** Inspecting transactions
109+
110+
-- |
111+
-- When using a 'Tx' created with the experimental API, the 'TxBody' and
112+
-- 'TxWits' can be extracted using the 'txBody' and 'txWits' lenses from
113+
-- "Cardano.Api.Ledger" respectively.
114+
115+
-- * Contents
116+
UnsignedTx (..)
14117
, UnsignedTxError (..)
15118
, makeUnsignedTx
16119
, makeKeyWitness
@@ -47,7 +150,7 @@ import GHC.Stack
47150
import Lens.Micro
48151

49152
-- | A transaction that can contain everything
50-
-- except key witnesses
153+
-- except key witnesses.
51154
newtype UnsignedTx era
52155
= UnsignedTx (Ledger.Tx (LedgerEra era))
53156

0 commit comments

Comments
 (0)