Skip to content

Commit 39eed0b

Browse files
committed
Copy Empty TxOut Check
1 parent 07d2597 commit 39eed0b

File tree

2 files changed

+62
-55
lines changed

2 files changed

+62
-55
lines changed

src/BotPlutusInterface/Balance.hs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,16 @@ balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx =
136136
(unBalancedTxValidityTimeRange unbalancedTx)
137137
(unBalancedTxTx unbalancedTx)
138138

139-
-- Checks that there is a non-zero output in the transaction.
140139
-- Adds required collaterals in the `Tx`, if `bcHasScripts`
141140
-- is true. Also adds signatures for fee calculation
142141
preBalancedTx <-
143-
if all (Value.isZero . txOutValue) (Tx.txOutputs tx)
144-
then throwE $ WAPI.OtherError "When constructing the transaction, no output values were specified."
145-
else
146-
if bcHasScripts balanceCfg
147-
then
148-
maybe
149-
(throwE $ WAPI.OtherError "Tx uses script but no collateral was provided.")
150-
(hoistEither . addSignatories ownPkh privKeys requiredSigs . flip addTxCollaterals tx)
151-
mcollateral
152-
else hoistEither $ addSignatories ownPkh privKeys requiredSigs tx
142+
if bcHasScripts balanceCfg
143+
then
144+
maybe
145+
(throwE $ WAPI.OtherError "Tx uses script but no collateral was provided.")
146+
(hoistEither . addSignatories ownPkh privKeys requiredSigs . flip addTxCollaterals tx)
147+
mcollateral
148+
else hoistEither $ addSignatories ownPkh privKeys requiredSigs tx
153149

154150
-- Balance the tx
155151
(balancedTx, minUtxos) <- balanceTxLoop utxoIndex privKeys [] preBalancedTx

src/BotPlutusInterface/CardanoCLI.hs

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,23 @@ calculateMinUtxo ::
143143
TxOut ->
144144
Eff effs (Either Text Integer)
145145
calculateMinUtxo pabConf datums txOut = do
146-
join
147-
<$> callCommand @w
148-
ShellArgs
149-
{ cmdName = "cardano-cli"
150-
, cmdArgs =
151-
mconcat
152-
[ ["transaction", "calculate-min-required-utxo", "--alonzo-era"]
153-
, txOutOpts pabConf datums [txOut]
154-
, ["--protocol-params-file", pabConf.pcProtocolParamsFile]
155-
]
156-
, cmdOutParser = mapLeft Text.pack . parseOnly UtxoParser.feeParser . Text.pack
157-
}
146+
let outs = txOutOpts pabConf datums [txOut]
147+
148+
case outs of
149+
[] -> pure $ Left "When constructing the transaction, no output values were specified."
150+
_ ->
151+
join
152+
<$> callCommand @w
153+
ShellArgs
154+
{ cmdName = "cardano-cli"
155+
, cmdArgs =
156+
mconcat
157+
[ ["transaction", "calculate-min-required-utxo", "--alonzo-era"]
158+
, outs
159+
, ["--protocol-params-file", pabConf.pcProtocolParamsFile]
160+
]
161+
, cmdOutParser = mapLeft Text.pack . parseOnly UtxoParser.feeParser . Text.pack
162+
}
158163

159164
-- | Calculating fee for an unbalanced transaction
160165
calculateMinFee ::
@@ -191,40 +196,46 @@ buildTx ::
191196
Tx ->
192197
Eff effs (Either Text ExBudget)
193198
buildTx pabConf privKeys txBudget tx = do
194-
let (ins, valBudget) = txInOpts (spendBudgets txBudget) pabConf (txInputs tx)
195-
(mints, mintBudget) = mintOpts (mintBudgets txBudget) pabConf (txMintScripts tx) (txRedeemers tx) (txMint tx)
196-
callCommand @w $ ShellArgs "cardano-cli" (opts ins mints) (const $ valBudget <> mintBudget)
197-
where
198-
requiredSigners =
199-
concatMap
200-
( \pubKey ->
201-
let pkh = Ledger.pubKeyHash pubKey
202-
in case Map.lookup pkh privKeys of
203-
Just (FromSKey _) ->
204-
["--required-signer", signingKeyFilePath pabConf pkh]
205-
Just (FromVKey _) ->
206-
["--required-signer-hash", encodeByteString $ fromBuiltin $ getPubKeyHash pkh]
207-
Nothing ->
208-
[]
209-
)
210-
(Map.keys (Ledger.txSignatures tx))
199+
let outs = txOutOpts pabConf (txData tx) (txOutputs tx)
211200

212-
opts ins mints =
213-
mconcat
214-
[ ["transaction", "build-raw", "--alonzo-era"]
215-
, ins
216-
, txInCollateralOpts (txCollateral tx)
217-
, txOutOpts pabConf (txData tx) (txOutputs tx)
218-
, mints
219-
, validRangeOpts (txValidRange tx)
220-
, metadataOpts pabConf (txMetadata tx)
221-
, requiredSigners
222-
, ["--fee", showText . getLovelace . fromValue $ txFee tx]
223-
, mconcat
224-
[ ["--protocol-params-file", pabConf.pcProtocolParamsFile]
225-
, ["--out-file", txFilePath pabConf "raw" (txId tx)]
201+
case outs of
202+
[] -> pure $ Left "When constructing the transaction, no output values were specified."
203+
_ ->
204+
callCommand @w $ ShellArgs "cardano-cli" opts (const $ valBudget <> mintBudget)
205+
where
206+
(ins, valBudget) = txInOpts (spendBudgets txBudget) pabConf (txInputs tx)
207+
(mints, mintBudget) = mintOpts (mintBudgets txBudget) pabConf (txMintScripts tx) (txRedeemers tx) (txMint tx)
208+
209+
requiredSigners =
210+
concatMap
211+
( \pubKey ->
212+
let pkh = Ledger.pubKeyHash pubKey
213+
in case Map.lookup pkh privKeys of
214+
Just (FromSKey _) ->
215+
["--required-signer", signingKeyFilePath pabConf pkh]
216+
Just (FromVKey _) ->
217+
["--required-signer-hash", encodeByteString $ fromBuiltin $ getPubKeyHash pkh]
218+
Nothing ->
219+
[]
220+
)
221+
(Map.keys (Ledger.txSignatures tx))
222+
223+
opts =
224+
mconcat
225+
[ ["transaction", "build-raw", "--alonzo-era"]
226+
, ins
227+
, txInCollateralOpts (txCollateral tx)
228+
, outs
229+
, mints
230+
, validRangeOpts (txValidRange tx)
231+
, metadataOpts pabConf (txMetadata tx)
232+
, requiredSigners
233+
, ["--fee", showText . getLovelace . fromValue $ txFee tx]
234+
, mconcat
235+
[ ["--protocol-params-file", pabConf.pcProtocolParamsFile]
236+
, ["--out-file", txFilePath pabConf "raw" (txId tx)]
237+
]
226238
]
227-
]
228239

229240
-- Signs and writes a tx (uses the tx body written to disk as input)
230241
signTx ::

0 commit comments

Comments
 (0)