Skip to content

Commit 0a0f31e

Browse files
Expose total ExBudget from buildTx
1 parent 533f2b9 commit 0a0f31e

File tree

3 files changed

+73
-60
lines changed

3 files changed

+73
-60
lines changed

src/BotPlutusInterface/Balance.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ balanceTxIO pabConf ownPkh unbalancedTx =
128128
txWithoutFees <-
129129
hoistEither $ balanceTxStep minUtxos utxoIndex ownPkh $ tx `withFee` 0
130130

131-
newEitherT $ CardanoCLI.buildTx @w pabConf privKeys txWithoutFees
131+
void $ newEitherT $ CardanoCLI.buildTx @w pabConf privKeys txWithoutFees
132132
fees <- newEitherT $ CardanoCLI.calculateMinFee @w pabConf txWithoutFees
133133

134134
lift $ printLog @w Debug $ "Fees: " ++ show fees

src/BotPlutusInterface/CardanoCLI.hs

Lines changed: 71 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import Control.Monad.Freer (Eff, Member)
3333
import Data.Aeson qualified as JSON
3434
import Data.Aeson.Extras (encodeByteString)
3535
import Data.Attoparsec.Text (parseOnly)
36-
import Data.Bifunctor (second)
36+
import Data.Bifunctor (first, second)
3737
import Data.Bool (bool)
3838
import Data.ByteString.Lazy qualified as LazyByteString
3939
import Data.ByteString.Lazy.Char8 qualified as Char8
@@ -45,7 +45,7 @@ import Data.Kind (Type)
4545
import Data.List (nub, sort)
4646
import Data.Map (Map)
4747
import Data.Map qualified as Map
48-
import Data.Maybe (fromMaybe, maybeToList)
48+
import Data.Maybe (fromMaybe)
4949
import Data.Set (Set)
5050
import Data.Set qualified as Set
5151
import Data.Text (Text)
@@ -243,11 +243,14 @@ buildTx ::
243243
PABConfig ->
244244
Map PubKeyHash DummyPrivKey ->
245245
Tx ->
246-
Eff effs (Either Text ())
246+
Eff effs (Either Text ExBudget)
247247
buildTx pabConf privKeys tx = do
248248
eTxInfo <- buildTxInfo @w pabConf tx
249249
case eTxInfo of
250-
Right txInfo -> callCommand @w $ ShellArgs "cardano-cli" (opts txInfo) (const ())
250+
Right txInfo -> do
251+
let (ins, valBudget) = txInOpts pabConf txInfo (txInputs tx)
252+
(mints, mintBudget) = mintOpts pabConf txInfo (txMintScripts tx) (txRedeemers tx) (txMint tx)
253+
callCommand @w $ ShellArgs "cardano-cli" (opts ins mints) (const $ valBudget <> mintBudget)
251254
Left e -> return $ Left e
252255
where
253256
requiredSigners =
@@ -263,13 +266,13 @@ buildTx pabConf privKeys tx = do
263266
[]
264267
)
265268
(Map.keys (Ledger.txSignatures tx))
266-
opts txInfo =
269+
opts ins mints =
267270
mconcat
268271
[ ["transaction", "build-raw", "--alonzo-era"]
269-
, txInOpts pabConf txInfo (txInputs tx)
272+
, ins
270273
, txInCollateralOpts (txCollateral tx)
271274
, txOutOpts pabConf (txData tx) (txOutputs tx)
272-
, mintOpts pabConf txInfo (txMintScripts tx) (txRedeemers tx) (txMint tx)
275+
, mints
273276
, validRangeOpts (txValidRange tx)
274277
, requiredSigners
275278
, ["--fee", showText . getLovelace . fromValue $ txFee tx]
@@ -322,80 +325,90 @@ submitTx pabConf tx =
322325
)
323326
(const ())
324327

325-
txInOpts :: PABConfig -> TxInfo -> Set TxIn -> [Text]
328+
txInOpts :: PABConfig -> TxInfo -> Set TxIn -> ([Text], ExBudget)
326329
txInOpts pabConf txInfo =
327-
concatMap
330+
foldMap
328331
( \(TxIn txOutRef txInType) ->
329-
mconcat
330-
[ ["--tx-in", txOutRefToCliArg txOutRef]
331-
, case txInType of
332-
Just (ConsumeScriptAddress validator redeemer datum) ->
333-
let scriptContext = ScriptContext txInfo $ Plutus.Spending txOutRef
334-
exBudget =
335-
fromRight (ExBudget (ExCPU 0) (ExMemory 0)) $
336-
calculateExBudget
337-
(Scripts.unValidatorScript validator)
338-
[Plutus.getRedeemer redeemer, Plutus.getDatum datum, Plutus.toBuiltinData scriptContext]
339-
in mconcat
340-
[
341-
[ "--tx-in-script-file"
342-
, validatorScriptFilePath pabConf (Ledger.validatorHash validator)
343-
]
344-
,
345-
[ "--tx-in-datum-file"
346-
, datumJsonFilePath pabConf (Ledger.datumHash datum)
347-
]
348-
,
349-
[ "--tx-in-redeemer-file"
350-
, redeemerJsonFilePath pabConf (Ledger.redeemerHash redeemer)
351-
]
352-
,
353-
[ "--tx-in-execution-units"
354-
, exBudgetToCliArg exBudget
355-
]
356-
]
357-
Just ConsumePublicKeyAddress -> []
358-
Just ConsumeSimpleScriptAddress -> []
359-
Nothing -> []
360-
]
332+
let (opts, exBudget) = scriptInputs txOutRef txInType
333+
in (,exBudget) $
334+
mconcat
335+
[ ["--tx-in", txOutRefToCliArg txOutRef]
336+
, opts
337+
]
361338
)
362339
. Set.toList
340+
where
341+
scriptInputs :: TxOutRef -> Maybe TxInType -> ([Text], ExBudget)
342+
scriptInputs txOutRef txInType =
343+
case txInType of
344+
Just (ConsumeScriptAddress validator redeemer datum) ->
345+
let scriptContext = ScriptContext txInfo $ Plutus.Spending txOutRef
346+
exBudget =
347+
fromRight mempty $
348+
calculateExBudget
349+
(Scripts.unValidatorScript validator)
350+
[Plutus.getRedeemer redeemer, Plutus.getDatum datum, Plutus.toBuiltinData scriptContext]
351+
in (,exBudget) $
352+
mconcat
353+
[
354+
[ "--tx-in-script-file"
355+
, validatorScriptFilePath pabConf (Ledger.validatorHash validator)
356+
]
357+
,
358+
[ "--tx-in-datum-file"
359+
, datumJsonFilePath pabConf (Ledger.datumHash datum)
360+
]
361+
,
362+
[ "--tx-in-redeemer-file"
363+
, redeemerJsonFilePath pabConf (Ledger.redeemerHash redeemer)
364+
]
365+
,
366+
[ "--tx-in-execution-units"
367+
, exBudgetToCliArg exBudget
368+
]
369+
]
370+
Just ConsumePublicKeyAddress -> mempty
371+
Just ConsumeSimpleScriptAddress -> mempty
372+
Nothing -> mempty
363373

364374
txInCollateralOpts :: Set TxIn -> [Text]
365375
txInCollateralOpts =
366376
concatMap (\(TxIn txOutRef _) -> ["--tx-in-collateral", txOutRefToCliArg txOutRef]) . Set.toList
367377

368378
-- Minting options
369-
mintOpts :: PABConfig -> TxInfo -> Set Scripts.MintingPolicy -> Redeemers -> Value -> [Text]
379+
mintOpts :: PABConfig -> TxInfo -> Set Scripts.MintingPolicy -> Redeemers -> Value -> ([Text], ExBudget)
370380
mintOpts pabConf txInfo mintingPolicies redeemers mintValue =
371-
mconcat
372-
[ mconcat $
373-
concatMap
381+
let scriptOpts =
382+
foldMap
374383
( \(idx, policy) ->
375384
let redeemerPtr = RedeemerPtr Mint idx
376385
redeemer = Map.lookup redeemerPtr redeemers
377386
curSymbol = Value.mpsSymbol $ Scripts.mintingPolicyHash policy
378387
scriptContext = ScriptContext txInfo $ Plutus.Minting curSymbol
379388
exBudget r =
380-
fromRight (ExBudget (ExCPU 0) (ExMemory 0)) $
389+
fromRight mempty $
381390
calculateExBudget
382391
(Scripts.unMintingPolicyScript policy)
383392
[Plutus.getRedeemer r, Plutus.toBuiltinData scriptContext]
384393
toOpts r =
385-
[ ["--mint-script-file", policyScriptFilePath pabConf curSymbol]
386-
, ["--mint-redeemer-file", redeemerJsonFilePath pabConf (Ledger.redeemerHash r)]
387-
, ["--mint-execution-units", exBudgetToCliArg (exBudget r)]
388-
]
389-
in mconcat $ maybeToList $ fmap toOpts redeemer
394+
let budget = exBudget r
395+
in (,budget) $
396+
mconcat
397+
[ ["--mint-script-file", policyScriptFilePath pabConf curSymbol]
398+
, ["--mint-redeemer-file", redeemerJsonFilePath pabConf (Ledger.redeemerHash r)]
399+
, ["--mint-execution-units", exBudgetToCliArg budget]
400+
]
401+
in orMempty $ fmap toOpts redeemer
390402
)
391403
$ zip [0 ..] $ Set.toList mintingPolicies
392-
, if not (Value.isZero mintValue)
393-
then
394-
[ "--mint"
395-
, valueToCliArg mintValue
396-
]
397-
else []
398-
]
404+
mintOpt =
405+
if not (Value.isZero mintValue)
406+
then ["--mint", valueToCliArg mintValue]
407+
else []
408+
in first (<> mintOpt) scriptOpts
409+
410+
orMempty :: forall (m :: Type). Monoid m => Maybe m -> m
411+
orMempty = fromMaybe mempty
399412

400413
-- | This function does not check if the range is valid, for that see `PreBalance.validateRange`
401414
validRangeOpts :: SlotRange -> [Text]

src/BotPlutusInterface/Contract.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ writeBalancedTx contractEnv (Right tx) = do
204204
skeys = Map.filter (\case FromSKey _ -> True; FromVKey _ -> False) privKeys
205205
signable = all ((`Map.member` skeys) . Ledger.pubKeyHash) requiredSigners
206206

207-
newEitherT $ CardanoCLI.buildTx @w pabConf privKeys tx
207+
void $ newEitherT $ CardanoCLI.buildTx @w pabConf privKeys tx
208208

209209
if signable
210210
then newEitherT $ CardanoCLI.signTx @w pabConf tx requiredSigners

0 commit comments

Comments
 (0)