Skip to content

Commit 26ea424

Browse files
committed
Update the raw and signed files generated by cardano-cli with the new tx id
Change-type: minor Signed-off-by: Giovanni Garufi <giovanni@mlabs.city>
1 parent bd6421f commit 26ea424

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

src/BotPlutusInterface/CardanoCLI.hs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import Ledger.Tx (
7676
TxInType (..),
7777
TxOut (..),
7878
TxOutRef (..),
79+
txId,
7980
)
8081
import Ledger.TxId (TxId (..))
8182
import Ledger.Value (Value)
@@ -176,7 +177,7 @@ calculateMinFee pabConf tx =
176177
, cmdArgs =
177178
mconcat
178179
[ ["transaction", "calculate-min-fee"]
179-
, ["--tx-body-file", txFilePath pabConf "raw" tx]
180+
, ["--tx-body-file", txFilePath pabConf "raw" (txId tx)]
180181
, ["--tx-in-count", showText $ length $ txInputs tx]
181182
, ["--tx-out-count", showText $ length $ txOutputs tx]
182183
, ["--witness-count", showText $ length $ txSignatures tx]
@@ -295,7 +296,7 @@ buildTx pabConf privKeys tx = do
295296
, ["--fee", showText . getLovelace . fromValue $ txFee tx]
296297
, mconcat
297298
[ ["--protocol-params-file", pabConf.pcProtocolParamsFile]
298-
, ["--out-file", txFilePath pabConf "raw" tx]
299+
, ["--out-file", txFilePath pabConf "raw" (txId tx)]
299300
]
300301
]
301302

@@ -318,9 +319,9 @@ signTx pabConf tx pubKeys =
318319
opts =
319320
mconcat
320321
[ ["transaction", "sign"]
321-
, ["--tx-body-file", txFilePath pabConf "raw" tx]
322+
, ["--tx-body-file", txFilePath pabConf "raw" (txId tx)]
322323
, signingKeyFiles
323-
, ["--out-file", txFilePath pabConf "signed" tx]
324+
, ["--out-file", txFilePath pabConf "signed" (txId tx)]
324325
]
325326

326327
budgetFromConfig :: PABConfig -> ExBudget -> ExBudget
@@ -343,7 +344,7 @@ submitTx pabConf tx =
343344
"cardano-cli"
344345
( mconcat
345346
[ ["transaction", "submit"]
346-
, ["--tx-file", txFilePath pabConf "signed" tx]
347+
, ["--tx-file", txFilePath pabConf "signed" (txId tx)]
347348
, networkOpt pabConf
348349
]
349350
)
@@ -480,8 +481,8 @@ networkOpt pabConf = case pabConf.pcNetwork of
480481
Mainnet -> ["--mainnet"]
481482

482483
txOutRefToCliArg :: TxOutRef -> Text
483-
txOutRefToCliArg (TxOutRef (TxId txId) txIx) =
484-
encodeByteString (fromBuiltin txId) <> "#" <> showText txIx
484+
txOutRefToCliArg (TxOutRef (TxId tId) txIx) =
485+
encodeByteString (fromBuiltin tId) <> "#" <> showText txIx
485486

486487
flatValueToCliArg :: (CurrencySymbol, TokenName, Integer) -> Text
487488
flatValueToCliArg (curSymbol, name, amount)

src/BotPlutusInterface/Contract.hs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import BotPlutusInterface.Balance qualified as PreBalance
77
import BotPlutusInterface.CardanoCLI qualified as CardanoCLI
88
import BotPlutusInterface.Effects (
99
PABEffect,
10+
ShellArgs (..),
11+
callCommand,
1012
createDirectoryIfMissing,
1113
handlePABEffect,
1214
logToContract,
@@ -28,7 +30,7 @@ import Control.Monad.Freer.Extras.Log (handleLogIgnore)
2830
import Control.Monad.Freer.Extras.Modify (raiseEnd)
2931
import Control.Monad.Freer.Writer (Writer (Tell))
3032
import Control.Monad.Trans.Class (lift)
31-
import Control.Monad.Trans.Either (eitherT, firstEitherT, newEitherT)
33+
import Control.Monad.Trans.Either (EitherT, eitherT, firstEitherT, newEitherT)
3234
import Data.Aeson (ToJSON, Value)
3335
import Data.Aeson.Extras (encodeByteString)
3436
import Data.Either (fromRight)
@@ -253,20 +255,34 @@ writeBalancedTx contractEnv (Right tx) = do
253255
else
254256
lift . printLog @w Warn . Text.unpack . Text.unlines $
255257
[ "Not all required signatures have signing key files. Please sign and submit the tx manually:"
256-
, "Tx file: " <> Files.txFilePath pabConf "raw" tx
258+
, "Tx file: " <> Files.txFilePath pabConf "raw" (Tx.txId tx)
257259
, "Signatories (pkh): " <> Text.unwords (map pkhToText requiredSigners)
258260
]
259261

262+
-- TODO: This whole part is hacky and we should remove it.
260263
let ext = if signable then "signed" else "raw"
261-
path = Text.unpack $ Files.txFilePath pabConf ext tx
264+
path = Text.unpack $ Files.txFilePath pabConf ext (Tx.txId tx)
262265
-- We read back the tx from file as tx currently has the wrong id (but the one we create with cardano-cli is correct)
263266
alonxoTx <- firstEitherT (Text.pack . show) $ newEitherT $ readFileTextEnvelope @w (AsTx AsAlonzoEra) path
264267
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))
265271

266272
when (not pabConf.pcDryRun && signable) $ do
267273
newEitherT $ CardanoCLI.submitTx @w pabConf tx
268274

269275
pure cardanoTx
276+
where
277+
mvFiles :: Text -> Text -> EitherT Text (Eff effs) ()
278+
mvFiles src dst =
279+
newEitherT $
280+
callCommand @w
281+
ShellArgs
282+
{ cmdName = "mv"
283+
, cmdArgs = [src, dst]
284+
, cmdOutParser = const ()
285+
}
270286

271287
pkhToText :: Ledger.PubKey -> Text
272288
pkhToText = encodeByteString . fromBuiltin . Ledger.getPubKeyHash . Ledger.pubKeyHash

src/BotPlutusInterface/Files.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ signingKeyFilePath pabConf (PubKeyHash pubKeyHash) =
115115
let h = encodeByteString $ fromBuiltin pubKeyHash
116116
in pabConf.pcSigningKeyFileDir <> "/signing-key-" <> h <> ".skey"
117117

118-
txFilePath :: PABConfig -> Text -> Tx.Tx -> Text
119-
txFilePath pabConf ext tx = pabConf.pcTxFileDir <> "/" <> txFileName (Tx.txId tx) ext
118+
txFilePath :: PABConfig -> Text -> TxId.TxId -> Text
119+
txFilePath pabConf ext txId = pabConf.pcTxFileDir <> "/" <> txFileName txId ext
120120

121121
txFileName :: TxId.TxId -> Text -> Text
122122
txFileName txId ext = "tx-" <> txIdToText txId <> "." <> ext

test/Spec/MockContract.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ mockCallCommand ShellArgs {cmdName, cmdArgs, cmdOutParser} = do
346346
pure $ Right $ cmdOutParser ""
347347
("cardano-cli", "transaction" : "submit" : _) ->
348348
pure $ Right $ cmdOutParser ""
349+
("mv", _) -> pure $ Right $ cmdOutParser ""
349350
(unsupportedCmd, unsupportedArgs) ->
350351
throwError @Text
351352
("Unsupported command: " <> Text.intercalate " " (unsupportedCmd : unsupportedArgs))

0 commit comments

Comments
 (0)