Skip to content

Commit 99d52dd

Browse files
authored
Merge pull request #86 from mlabs-haskell/misha/ex-units-for-plutip
Calculating exedcution budget with `Cardano.Api` tools
2 parents 91b18e1 + 90c61af commit 99d52dd

File tree

15 files changed

+522
-202
lines changed

15 files changed

+522
-202
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ main = do
9797
, -- Dry run mode will build the tx, but skip the submit step
9898
pcDryRun = False
9999
, pcLogLevel = Debug
100-
, -- | Forced budget for scripts, as optional (CPU Steps, Memory Units)
101-
pcForceBudget = Nothing
102100
, -- Protocol params file location relative to the cardano-cli working directory (needed for the cli)
103101
, pcProtocolParamsFile = "./protocol.json"
104102
, pcEnableTxEndpoint = True

bot-plutus-interface.cabal

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,15 @@ library
7777
exposed-modules:
7878
BotPlutusInterface
7979
BotPlutusInterface.Balance
80+
BotPlutusInterface.BodyBuilder
8081
BotPlutusInterface.CardanoCLI
8182
BotPlutusInterface.ChainIndex
8283
BotPlutusInterface.Contract
8384
BotPlutusInterface.Effects
85+
BotPlutusInterface.ExBudget
8486
BotPlutusInterface.Files
8587
BotPlutusInterface.Helpers
88+
BotPlutusInterface.QueryNode
8689
BotPlutusInterface.Server
8790
BotPlutusInterface.Types
8891
BotPlutusInterface.UtxoParser
@@ -94,6 +97,7 @@ library
9497
, cardano-api
9598
, cardano-crypto
9699
, cardano-ledger-alonzo
100+
, cardano-slotting
97101
, containers
98102
, data-default
99103
, data-default-class

examples/plutus-game/app/Main.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ main = do
6868
, pcDryRun = True
6969
, pcLogLevel = Debug
7070
, pcProtocolParamsFile = "./protocol.json"
71-
, pcForceBudget = Just (9_000_000_000, 15_000_000)
7271
, pcEnableTxEndpoint = True
7372
}
7473
BotPlutusInterface.runPAB @GameContracts pabConf

examples/plutus-nft/app/Main.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ main = do
6868
, pcDryRun = True
6969
, pcLogLevel = Debug
7070
, pcProtocolParamsFile = "./protocol.json"
71-
, pcForceBudget = Just (1000, 1000)
7271
, pcEnableTxEndpoint = True
7372
}
7473
BotPlutusInterface.runPAB @MintNFTContracts pabConf

examples/plutus-transfer/app/Main.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ main = do
6767
, pcDryRun = True
6868
, pcLogLevel = Debug
6969
, pcProtocolParamsFile = "./protocol.json"
70-
, pcForceBudget = Nothing
7170
, pcEnableTxEndpoint = True
7271
}
7372
BotPlutusInterface.runPAB @TransferContracts pabConf

src/BotPlutusInterface/Balance.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ import Plutus.V1.Ledger.Api (
5858
CurrencySymbol (..),
5959
TokenName (..),
6060
)
61+
62+
import BotPlutusInterface.BodyBuilder qualified as BodyBuilder
6163
import Prelude
6264

6365
{- | Collect necessary tx inputs and collaterals, add minimum lovelace values and balance non ada
@@ -134,7 +136,8 @@ balanceTxIO pabConf ownPkh unbalancedTx =
134136
txWithoutFees <-
135137
hoistEither $ balanceTxStep minUtxos utxoIndex changeAddr $ tx `withFee` 0
136138

137-
exBudget <- newEitherT $ CardanoCLI.buildTx @w pabConf privKeys txWithoutFees
139+
exBudget <- newEitherT $ BodyBuilder.buildAndEstimateBudget @w pabConf privKeys txWithoutFees
140+
138141
nonBudgettedFees <- newEitherT $ CardanoCLI.calculateMinFee @w pabConf txWithoutFees
139142

140143
let fees = nonBudgettedFees + getBudgetPrice (getExecutionUnitPrices pabConf) exBudget

src/BotPlutusInterface/BodyBuilder.hs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{-# LANGUAGE AllowAmbiguousTypes #-}
2+
3+
{- | Module provides the way of building ".raw" transactions with execution budget
4+
estimated with `Cardano.Api` tools.
5+
-}
6+
module BotPlutusInterface.BodyBuilder (buildAndEstimateBudget) where
7+
8+
import BotPlutusInterface.CardanoCLI qualified as CardanoCLI
9+
import BotPlutusInterface.Effects (PABEffect, estimateBudget)
10+
11+
import BotPlutusInterface.Files (
12+
DummyPrivKey,
13+
txFilePath,
14+
)
15+
import BotPlutusInterface.Types (PABConfig, TxFile (Raw))
16+
import Control.Monad.Freer (Eff, Member)
17+
import Control.Monad.Trans.Either (firstEitherT, newEitherT, runEitherT)
18+
import Data.Kind (Type)
19+
import Data.Map (Map)
20+
import Data.Text (Text)
21+
import Data.Text qualified as Text
22+
import Ledger (ExBudget, Tx, txId)
23+
import Ledger.Crypto (PubKeyHash)
24+
import Prelude
25+
26+
{- | Build and save raw transaction (transaction body) with estimated execution budgets using `CardanoCLI`.
27+
It builds first transaction body with 0 budget for all spending inputs and minting policies,
28+
then uses body of this transaction to estimate execution budget
29+
and build final body with budget set.
30+
-}
31+
buildAndEstimateBudget ::
32+
forall (w :: Type) (effs :: [Type -> Type]).
33+
Member (PABEffect w) effs =>
34+
PABConfig ->
35+
Map PubKeyHash DummyPrivKey ->
36+
Tx ->
37+
Eff effs (Either Text ExBudget)
38+
buildAndEstimateBudget pabConf privKeys tx = runEitherT $ do
39+
buildDraftTxBody
40+
>> estimateBudgetByDraftBody (Text.unpack $ txFilePath pabConf "raw" (txId tx))
41+
>>= buildBodyUsingEstimatedBudget
42+
where
43+
buildDraftTxBody = newEitherT $ CardanoCLI.buildTx @w pabConf privKeys mempty tx
44+
45+
estimateBudgetByDraftBody path =
46+
firstEitherT toText . newEitherT $ estimateBudget @w (Raw path)
47+
48+
buildBodyUsingEstimatedBudget exBudget =
49+
newEitherT $
50+
CardanoCLI.buildTx @w
51+
pabConf
52+
privKeys
53+
exBudget
54+
tx
55+
56+
toText = Text.pack . show

0 commit comments

Comments
 (0)