55{-# LANGUAGE OverloadedStrings #-}
66{-# LANGUAGE ScopedTypeVariables #-}
77{-# LANGUAGE TypeApplications #-}
8- {-# LANGUAGE TypeOperators #-}
98
109module Cardano.Testnet.Test.Rpc.Transaction
1110 ( hprop_rpc_transaction
1211 )
1312where
1413
1514import Cardano.Api
15+ import qualified Cardano.Api.Experimental as Exp
16+ import qualified Cardano.Api.Experimental.Tx as Exp
1617import qualified Cardano.Api.Ledger as L
1718
1819import Cardano.Rpc.Client (Proto )
@@ -49,29 +50,28 @@ import RIO (ByteString)
4950hprop_rpc_transaction :: Property
5051hprop_rpc_transaction = integrationRetryWorkspace 2 " rpc-tx" $ \ tempAbsBasePath' -> H. runWithDefaultWatchdog_ $ do
5152 conf <- mkConf tempAbsBasePath'
52- let (ceo, eraProxy) =
53- (conwayBasedEra, asType) :: era ~ ConwayEra => (ConwayEraOnwards era , AsType era )
54- sbe = convert ceo
53+ let era = Exp. ConwayEra
54+ sbe = convert era
5555 creationOptions = def{creationEra = AnyShelleyBasedEra sbe}
5656 runtimeOptions = def{runtimeEnableRpc = RpcEnabled }
57- addrInEra = AsAddressInEra eraProxy
57+ addressInEra = asAddressInEra sbe
5858
5959 TestnetRuntime
6060 { configurationFile
6161 , testnetNodes = node0 : _
62- , wallets = wallet0@ (PaymentKeyInfo _ addrTxt0 ) : (PaymentKeyInfo _ addrTxt1 ) : _
62+ , wallets = wallet0@ (PaymentKeyInfo _ addressText0 ) : (PaymentKeyInfo _ addressText1 ) : _
6363 } <-
6464 createAndRunTestnet creationOptions runtimeOptions conf
6565
6666 epochStateView <- getEpochStateView configurationFile $ nodeSocketPath node0
6767 rpcSocket <- H. note . unFile $ nodeRpcSocketPath node0
6868
6969 -- prepare tx inputs and output address
70- H. noteShow_ addrTxt0
71- addr0 <- H. nothingFail $ deserialiseAddress addrInEra addrTxt0
70+ H. noteShow_ addressText0
71+ address0 <- H. nothingFail $ deserialiseAddress addressInEra addressText0
7272
73- H. noteShow_ addrTxt1
74- addr1 <- H. nothingFail $ deserialiseAddress addrInEra addrTxt1
73+ H. noteShow_ addressText1
74+ address1 <- H. nothingFail $ deserialiseAddress addressInEra addressText1
7575
7676 -- read key witnesses
7777 wit0 :: ShelleyWitnessSigningKey <-
@@ -90,66 +90,69 @@ hprop_rpc_transaction = integrationRetryWorkspace 2 "rpc-tx" $ \tempAbsBasePath'
9090 Rpc. nonStreaming conn (Rpc. rpc @ (Rpc. Protobuf UtxoRpc. QueryService " readParams" )) req
9191
9292 utxos' <- do
93- let req = def -- & # U5c.keys .~ [T.encodeUtf8 addrTxt0 ]
93+ let req = def -- & # U5c.keys .~ [T.encodeUtf8 addressText0 ]
9494 Rpc. nonStreaming conn (Rpc. rpc @ (Rpc. Protobuf UtxoRpc. QueryService " readUtxos" )) req
9595 pure (pparams', utxos')
9696
97- pparams <- H. leftFail $ utxoRpcPParamsToProtocolParams (convert ceo) $ pparamsResponse ^. U5c. values . U5c. cardano
97+ pparams <- H. leftFail $ utxoRpcPParamsToProtocolParams era $ pparamsResponse ^. U5c. values . U5c. cardano
9898
9999 txOut0 : _ <- H. noteShowM . flip filterM (utxosResponse ^. U5c. items) $ \ utxo -> do
100- utxoAddress <- deserialiseAddressBs addrInEra $ utxo ^. U5c. cardano . U5c. address
101- pure $ addr0 == utxoAddress
100+ utxoAddress <- deserialiseAddressBs addressInEra $ utxo ^. U5c. cardano . U5c. address
101+ pure $ address0 == utxoAddress
102102 txIn0 <- txoRefToTxIn $ txOut0 ^. U5c. txoRef
103103
104104 outputCoin <- H. leftFail $ txOut0 ^. U5c. cardano . U5c. coin . to utxoRpcBigIntToInteger
105105 let amount = 200_000_000
106106 fee = 500
107107 change = outputCoin - amount - fee
108- txOut = TxOut addr1 (lovelaceToTxOutValue sbe $ L. Coin amount) TxOutDatumNone ReferenceScriptNone
109- changeTxOut = TxOut addr0 (lovelaceToTxOutValue sbe $ L. Coin change) TxOutDatumNone ReferenceScriptNone
108+ mkOut ledgerAddress coin = Exp. obtainCommonConstraints era $
109+ Exp. TxOut $ L. mkBasicTxOut ledgerAddress $ L. inject $ L. Coin coin
110110 content =
111- defaultTxBodyContent sbe
112- & setTxIns [(txIn0, pure $ KeyWitness KeyWitnessForSpending )]
113- & setTxFee (TxFeeExplicit sbe ( L. Coin fee) )
114- & setTxOuts [txOut, changeTxOut ]
115- & setTxProtocolParams ( pure . pure $ LedgerProtocolParameters pparams)
111+ Exp. defaultTxBodyContent
112+ & Exp. setTxIns [(txIn0, Exp. AnyKeyWitnessPlaceholder )]
113+ & Exp. setTxFee (L. Coin fee)
114+ & Exp. setTxOuts [mkOut (toShelleyAddr address1) amount, mkOut (toShelleyAddr address0) change ]
115+ & Exp. setTxProtocolParams pparams
116116
117- txBody <- H. leftFail $ createTransactionBody sbe content
118-
119- let signedTx = signShelleyTransaction sbe txBody [wit0]
120- txId' <- H. noteShow . getTxId $ getTxBody signedTx
117+ unsignedTx <- H. leftFail $ Exp. makeUnsignedTx era content
118+ let keyWit = Exp. makeKeyWitness era unsignedTx wit0
119+ Exp. SignedTx signedLedgerTx = Exp. signTx era [] [keyWit] unsignedTx
120+ txId' <- H. noteShow . Exp. obtainCommonConstraints era . TxId $ Exp. hashTxBody (signedLedgerTx ^. L. bodyTxL)
121121
122122 H. noteShowPretty_ utxosResponse
123123
124124 liftBaseOp (Rpc. withConnection def rpcServer) $ \ conn -> do
125125 submitResponse <- H. noteShowM . H. evalIO $
126126 Rpc. nonStreaming conn (Rpc. rpc @ (Rpc. Protobuf UtxoRpc. SubmitService " submitTx" )) $
127- def & U5c. tx .~ (def & U5c. raw .~ serialiseToCBOR signedTx )
127+ def & U5c. tx .~ (def & U5c. raw .~ serialiseToRawBytes ( Exp. SignedTx signedLedgerTx) )
128128
129129 submittedTxId <- H. leftFail . deserialiseFromRawBytes AsTxId $ submitResponse ^. U5c. ref
130130
131131 H. note_ " Ensure that submitTx returns the same transaction ID as the locally computed signed transaction ID"
132132 txId' === submittedTxId
133133
134134 -- TODO use searchUtxos when available
135- H. note_ $ " Ensure that there are 2 UTXOs in the address " <> show addrTxt1
135+ H. note_ $ " Ensure that there are 2 UTXOs in the address " <> show addressText1
136136 utxosForAddress <- retryUntilM epochStateView (WaitForBlocks 10 )
137137 (do utxos <- H. evalIO $
138138 Rpc. nonStreaming conn (Rpc. rpc @ (Rpc. Protobuf UtxoRpc. QueryService " readUtxos" )) def
139139 flip filterM (utxos ^. U5c. items) $ \ utxo -> do
140- utxoAddress <- deserialiseAddressBs addrInEra $ utxo ^. U5c. cardano . U5c. address
141- pure $ addr1 == utxoAddress
140+ utxoAddress <- deserialiseAddressBs addressInEra $ utxo ^. U5c. cardano . U5c. address
141+ pure $ address1 == utxoAddress
142142 )
143143 (\ xs -> length xs == 2 )
144144
145145 let outputsAmounts = map (^. U5c. cardano . U5c. coin) utxosForAddress
146- H. note_ $ " Ensure that the output sent is one of the utxos for the address " <> show addrTxt1
146+ H. note_ $ " Ensure that the output sent is one of the utxos for the address " <> show addressText1
147147 H. assertWith outputsAmounts $ elem (inject amount)
148148
149+ asAddressInEra :: ShelleyBasedEra era -> AsType (AddressInEra era )
150+ asAddressInEra s = shelleyBasedEraConstraints s $ AsAddressInEra asType
151+
149152txoRefToTxIn :: (HasCallStack , MonadTest m ) => Proto UtxoRpc. TxoRef -> m TxIn
150153txoRefToTxIn r = withFrozenCallStack $ do
151154 txId' <- H. leftFail $ deserialiseFromRawBytes AsTxId $ r ^. U5c. hash
152155 pure $ TxIn txId' (TxIx . fromIntegral $ r ^. U5c. index)
153156
154157deserialiseAddressBs :: (MonadTest m , SerialiseAddress c ) => AsType c -> ByteString -> m c
155- deserialiseAddressBs addrInEra = H. nothingFail . deserialiseAddress addrInEra <=< H. leftFail . T. decodeUtf8'
158+ deserialiseAddressBs addressInEra = H. nothingFail . deserialiseAddress addressInEra <=< H. leftFail . T. decodeUtf8'
0 commit comments