Skip to content

Commit 1b5e68a

Browse files
committed
Update according to revie
Change-type: patch Signed-off-by: Giovanni Garufi <giovanni@mlabs.city>
1 parent 26ea424 commit 1b5e68a

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

src/BotPlutusInterface/Contract.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,14 @@ writeBalancedTx contractEnv (Right tx) = do
265265
-- We read back the tx from file as tx currently has the wrong id (but the one we create with cardano-cli is correct)
266266
alonxoTx <- firstEitherT (Text.pack . show) $ newEitherT $ readFileTextEnvelope @w (AsTx AsAlonzoEra) path
267267
let cardanoTx = Tx.SomeTx alonxoTx AlonzoEraInCardanoMode
268-
-- We need to replace the outfile we created at the previous step, as it currently still has the old (incorrect) id
269-
mvFiles (Files.txFilePath pabConf "raw" (Tx.txId tx)) (Files.txFilePath pabConf "raw" (Ledger.getCardanoTxId $ Left cardanoTx))
270-
when signable $ mvFiles (Files.txFilePath pabConf "signed" (Tx.txId tx)) (Files.txFilePath pabConf "signed" (Ledger.getCardanoTxId $ Left cardanoTx))
271268

272269
when (not pabConf.pcDryRun && signable) $ do
273270
newEitherT $ CardanoCLI.submitTx @w pabConf tx
274271

272+
-- We need to replace the outfile we created at the previous step, as it currently still has the old (incorrect) id
273+
mvFiles (Files.txFilePath pabConf "raw" (Tx.txId tx)) (Files.txFilePath pabConf "raw" (Ledger.getCardanoTxId $ Left cardanoTx))
274+
when signable $ mvFiles (Files.txFilePath pabConf "signed" (Tx.txId tx)) (Files.txFilePath pabConf "signed" (Ledger.getCardanoTxId $ Left cardanoTx))
275+
275276
pure cardanoTx
276277
where
277278
mvFiles :: Text -> Text -> EitherT Text (Eff effs) ()

test/Spec/BotPlutusInterface/Contract.hs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ sendAda = do
108108
Constraints.mustPayToPubKey paymentPkh2 (Ada.lovelaceValueOf 1000)
109109
submitTx constraints
110110

111-
assertContractWithTxId contract initState $ \state _ ->
111+
assertContract contract initState $ \state ->
112112
assertCommandHistory
113113
state
114114
[
@@ -189,7 +189,7 @@ sendAdaNoChange = do
189189
Constraints.mustPayToPubKey paymentPkh2 (Ada.lovelaceValueOf 1000)
190190
submitTx constraints
191191

192-
assertContractWithTxId contract initState $ \state _ ->
192+
assertContract contract initState $ \state ->
193193
assertCommandHistory
194194
state
195195
[
@@ -222,7 +222,7 @@ sendAdaStaking = do
222222
Constraints.mustPayToPubKeyAddress paymentPkh2 stakePkh3 (Ada.lovelaceValueOf 1000)
223223
submitTx constraints
224224

225-
assertContractWithTxId contract initState $ \state _ ->
225+
assertContract contract initState $ \state ->
226226
assertCommandHistory
227227
state
228228
[
@@ -303,7 +303,7 @@ multisigSupport = do
303303
submitTx constraints
304304

305305
-- Building and siging the tx should include both signing keys
306-
assertContractWithTxId contract initState $ \state _ ->
306+
assertContract contract initState $ \state ->
307307
assertCommandHistory
308308
state
309309
[
@@ -366,7 +366,7 @@ withoutSigning = do
366366
submitTx constraints
367367

368368
-- Building and siging the tx should include both signing keys
369-
assertContractWithTxId contract initState $ \state _ -> do
369+
assertContract contract initState $ \state -> do
370370
assertCommandHistory
371371
state
372372
[
@@ -411,7 +411,7 @@ sendTokens = do
411411
(Ada.lovelaceValueOf 1000 <> Value.singleton "abcd1234" "testToken" 5)
412412
submitTx constraints
413413

414-
assertContractWithTxId contract initState $ \state _ ->
414+
assertContract contract initState $ \state ->
415415
assertCommandHistory
416416
state
417417
[
@@ -455,7 +455,7 @@ sendTokensWithoutName = do
455455
(Ada.lovelaceValueOf 1000 <> Value.singleton "abcd1234" "" 5)
456456
submitTx constraints
457457

458-
assertContractWithTxId contract initState $ \state _ ->
458+
assertContract contract initState $ \state ->
459459
assertCommandHistory
460460
state
461461
[
@@ -506,7 +506,7 @@ mintTokens = do
506506
(Ada.lovelaceValueOf 1000 <> Value.singleton curSymbol "testToken" 5)
507507
submitTxConstraintsWith @Void lookups constraints
508508

509-
assertContractWithTxId contract initState $ \state _ -> do
509+
assertContract contract initState $ \state -> do
510510
assertCommandHistory
511511
state
512512
[
@@ -600,7 +600,7 @@ spendToValidator = do
600600
Constraints.mustPayToOtherScript valHash datum (Ada.lovelaceValueOf 500)
601601
submitTxConstraintsWith @Void lookups constraints
602602

603-
assertContractWithTxId contract initState $ \state _ -> do
603+
assertContract contract initState $ \state -> do
604604
assertCommandHistory
605605
state
606606
[
@@ -691,7 +691,7 @@ redeemFromValidator = do
691691
<> Constraints.mustPayToPubKey paymentPkh2 (Ada.lovelaceValueOf 500)
692692
submitTxConstraintsWith @Void lookups constraints
693693

694-
assertContractWithTxId contract initState $ \state _ -> do
694+
assertContract contract initState $ \state -> do
695695
assertCommandHistory
696696
state
697697
[
@@ -782,7 +782,7 @@ withValidRange = do
782782
<> Constraints.mustValidateIn (interval (POSIXTime 1643636293000) (POSIXTime 1646314693000))
783783
submitTx constraints
784784

785-
assertContractWithTxId contract initState $ \state _ ->
785+
assertContract contract initState $ \state ->
786786
assertCommandHistory
787787
state
788788
[
@@ -820,20 +820,17 @@ useWriter = do
820820
let txOutRef = TxOutRef "e406b0cf676fc2b1a9edb0617f259ad025c20ea6f0333820aa7cef1bfe7302e5" 0
821821
txOut = TxOut pkhAddr1 (Ada.lovelaceValueOf 1200) Nothing
822822
initState = def & utxos .~ [(txOutRef, txOut)]
823-
test = "test"
824823

825824
contract :: Contract (Last Text) (Endpoint "SendAda" ()) Text CardanoTx
826825
contract = do
827826
tell $ Last $ Just "Init contract"
828827
let constraints =
829828
Constraints.mustPayToPubKey paymentPkh2 (Ada.lovelaceValueOf 1000)
830-
txId <- submitTx constraints
831-
tell $ Last $ Just test
832-
pure txId
829+
submitTx constraints
833830

834-
assertContractWithTxId contract initState $ \state _ -> do
831+
assertContract contract initState $ \state -> do
835832
(state ^. observableState)
836-
@?= Last (Just test)
833+
@?= Last (Just "Init contract")
837834

838835
waitNextBlock :: Assertion
839836
waitNextBlock = do
@@ -888,6 +885,20 @@ assertContractWithTxId contract initState assertion = do
888885
let outTxId = encodeByteString $ fromBuiltin $ TxId.getTxId $ Tx.getCardanoTxId tx
889886
in assertion state outTxId
890887

888+
assertContract ::
889+
forall (w :: Type) (s :: Row Type).
890+
(ToJSON w, Monoid w) =>
891+
Contract w s Text CardanoTx ->
892+
MockContractState w ->
893+
(MockContractState w -> Assertion) ->
894+
Assertion
895+
assertContract contract initState assertion = do
896+
let (result, state) = runContractPure contract initState
897+
898+
case result of
899+
Left errMsg -> assertFailure (show errMsg)
900+
Right _ -> assertion state
901+
891902
assertCommandHistory :: forall (w :: Type). MockContractState w -> [(Int, Text)] -> Assertion
892903
assertCommandHistory state =
893904
mapM_

0 commit comments

Comments
 (0)