@@ -10,9 +10,10 @@ module Testnet.Process.Cli.Transaction
1010 , retrieveTransactionId
1111 , SignedTx
1212 , TxBody
13- , TxOutAddress (.. )
13+ , TxOutAddress (.. )
1414 , VoteFile
15- ) where
15+ )
16+ where
1617
1718import Cardano.Api hiding (Certificate , TxBody )
1819import Cardano.Api.Experimental (Some (.. ))
@@ -29,83 +30,93 @@ import GHC.IO.Exception (ExitCode (..))
2930import GHC.Stack
3031import System.FilePath ((</>) )
3132
33+ import Hedgehog (MonadTest )
34+ import qualified Hedgehog.Extras as H
35+
3236import Testnet.Components.Query (EpochStateView , findLargestUtxoForPaymentKey )
3337import Testnet.Process.Run (execCli' )
3438import Testnet.Start.Types (anyEraToString )
3539import Testnet.Types
3640
37- import Hedgehog (MonadTest )
38- import qualified Hedgehog.Extras as H
39-
4041-- Transaction signing
4142data VoteFile
4243
4344data TxBody
4445
4546data SignedTx
4647
47- data ReferenceScriptJSON
48+ data ScriptJSON
4849
49- data TxOutAddress = PubKeyAddress PaymentKeyInfo
50- | ReferenceScriptAddress ( File ReferenceScriptJSON In )
51- -- ^ The output will be created at the script address
52- -- and the output will include the reference script.
50+ data TxOutAddress
51+ = PubKeyAddress PaymentKeyInfo
52+ | -- | The output will be created at the script address.
53+ ScriptAddress ( File ScriptJSON In )
5354
5455-- | Calls @cardano-cli@ to build a simple ADA transfer transaction to
55- -- the specified outputs of the specified amount of ADA. In the case of
56- -- a reference script address, the output will be created at the
57- -- corresponding script address, and the output will contain the reference
58- -- script.
56+ -- the specified outputs of the specified amount of ADA. Destination
57+ -- address may be specified as a 'PaymentKeyInfo' or with a script file.
58+ -- For each output, an extra optional script file may be provided, and
59+ -- if provided, the script provided will be published in that output
60+ -- as a reference script.
5961--
6062-- Returns the generated @File TxBody In@ file path to the created unsigned
6163-- transaction file.
6264mkSpendOutputsOnlyTx
63- :: HasCallStack
64- => Typeable era
65- => H. MonadAssertion m
66- => MonadTest m
67- => MonadCatch m
68- => MonadIO m
69- => H. ExecConfig -- ^ Specifies the CLI execution configuration.
70- -> EpochStateView -- ^ Current epoch state view for transaction building. It can be obtained
71- -- using the 'getEpochStateView' function.
72- -> ShelleyBasedEra era -- ^ Witness for the current Cardano era.
73- -> FilePath -- ^ Base directory path where the unsigned transaction file will be stored.
74- -> String -- ^ Prefix for the output unsigned transaction file name. The extension will be @.txbody@.
75- -> PaymentKeyInfo -- ^ Payment key pair used for paying the transaction.
76- -> [(TxOutAddress , Coin )] -- ^ List of pairs of transaction output addresses and amounts.
65+ :: (HasCallStack , Typeable era , H. MonadAssertion m , MonadTest m , MonadCatch m , MonadIO m )
66+ => H. ExecConfig
67+ -- ^ Specifies the CLI execution configuration.
68+ -> EpochStateView
69+ -- ^ Current epoch state view for transaction building. It can be obtained
70+ -- using the 'getEpochStateView' function.
71+ -> ShelleyBasedEra era
72+ -- ^ Witness for the current Cardano era.
73+ -> FilePath
74+ -- ^ Base directory path where the unsigned transaction file will be stored.
75+ -> String
76+ -- ^ Prefix for the output unsigned transaction file name. The extension will be @.txbody@.
77+ -> PaymentKeyInfo
78+ -- ^ Payment key pair used for paying the transaction.
79+ -> [(TxOutAddress , Coin , Maybe (File ScriptJSON In ))]
80+ -- ^ List of tuples with transaction output addresses, amounts, and reference scripts.
7781 -> m (File TxBody In )
7882mkSpendOutputsOnlyTx execConfig epochStateView sbe work prefix srcWallet txOutputs = do
79-
8083 txIn <- findLargestUtxoForPaymentKey epochStateView sbe srcWallet
8184 fixedTxOuts :: [String ] <- computeTxOuts
82- void $ execCli' execConfig $ mconcat
83- [ [ anyEraToString cEra, " transaction" , " build"
84- , " --change-address" , srcAddress
85- , " --tx-in" , T. unpack $ renderTxIn txIn
86- ]
87- , fixedTxOuts
88- , [ " --out-file" , unFile txBody
89- ]
90- ]
85+ void $ execCli' execConfig $
86+ mconcat
87+ [ [ anyEraToString cEra
88+ , " transaction" , " build"
89+ , " --change-address" , srcAddress
90+ , " --tx-in" , T. unpack $ renderTxIn txIn
91+ ]
92+ , fixedTxOuts
93+ , [ " --out-file" , unFile txBody
94+ ]
95+ ]
9196 return txBody
92- where
93- era = toCardanoEra sbe
94- cEra = AnyCardanoEra era
95- txBody = File (work </> prefix <> " .txbody" )
96- srcAddress = T. unpack $ paymentKeyInfoAddr srcWallet
97- computeTxOuts = concat <$> sequence
97+ where
98+ era = toCardanoEra sbe
99+ cEra = AnyCardanoEra era
100+ txBody = File (work </> prefix <> " .txbody" )
101+ srcAddress = T. unpack $ paymentKeyInfoAddr srcWallet
102+ computeTxOuts =
103+ concat <$> sequence
98104 [ case txOut of
99105 PubKeyAddress dstWallet ->
100- return [" --tx-out" , T. unpack (paymentKeyInfoAddr dstWallet) <> " +" ++ show (unCoin amount) ]
101- ReferenceScriptAddress (File referenceScriptJSON) -> do
102- scriptAddress <- execCli' execConfig [ anyEraToString cEra, " address" , " build"
103- , " --payment-script-file" , referenceScriptJSON
104- ]
105- return [ " --tx-out" , scriptAddress <> " +" ++ show (unCoin amount)
106- , " --tx-out-reference-script-file" , referenceScriptJSON
107- ]
108- | (txOut, amount) <- txOutputs
106+ return [" --tx-out" , T. unpack (paymentKeyInfoAddr dstWallet) <> " +" ++ show (unCoin amount)]
107+ ScriptAddress (File referenceScriptJSON) -> do
108+ scriptAddress <-
109+ execCli'
110+ execConfig
111+ [ anyEraToString cEra
112+ , " address" , " build"
113+ , " --payment-script-file" , referenceScriptJSON
114+ ]
115+ return
116+ ( [" --tx-out" , scriptAddress <> " +" ++ show (unCoin amount)]
117+ <> maybe [] (\ (File newRefScript) -> [" --tx-out-reference-script-file" , newRefScript]) mNewRefScript
118+ )
119+ | (txOut, amount, mNewRefScript) <- txOutputs
109120 ]
110121
111122-- | Calls @cardano-cli@ to build a simple ADA transfer transaction to
@@ -131,7 +142,7 @@ mkSimpleSpendOutputsOnlyTx
131142 -> Coin -- ^ Amount of ADA to transfer (in Lovelace).
132143 -> m (File TxBody In )
133144mkSimpleSpendOutputsOnlyTx execConfig epochStateView sbe work prefix srcWallet dstWallet amount =
134- mkSpendOutputsOnlyTx execConfig epochStateView sbe work prefix srcWallet [(PubKeyAddress dstWallet, amount)]
145+ mkSpendOutputsOnlyTx execConfig epochStateView sbe work prefix srcWallet [(PubKeyAddress dstWallet, amount, Nothing )]
135146
136147-- | Calls @cardano-cli@ to signs a transaction body using the specified key pairs.
137148--
0 commit comments