Skip to content

Commit 0e3a56c

Browse files
Merge pull request #74 from mlabs-haskell/bug/fix-calculateExBudget
Add Script context to budget estimation
2 parents 3dce48a + c8a9151 commit 0e3a56c

File tree

12 files changed

+261
-90
lines changed

12 files changed

+261
-90
lines changed

README.md

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

bot-plutus-interface.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ library
130130
, text ^>=1.2.4.0
131131
, transformers
132132
, transformers-either
133+
, unordered-containers
133134
, uuid
134135
, wai
135136
, warp

examples/plutus-game/app/Main.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ main = do
6767
, pcDryRun = True
6868
, pcLogLevel = Debug
6969
, pcProtocolParamsFile = "./protocol.json"
70+
, pcForceBudget = Just (1000, 1000)
7071
, pcEnableTxEndpoint = True
7172
}
7273
BotPlutusInterface.runPAB @GameContracts pabConf

examples/plutus-nft/app/Main.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ main = do
6767
, pcDryRun = True
6868
, pcLogLevel = Debug
6969
, pcProtocolParamsFile = "./protocol.json"
70+
, pcForceBudget = Just (1000, 1000)
7071
, pcEnableTxEndpoint = True
7172
}
7273
BotPlutusInterface.runPAB @MintNFTContracts pabConf

examples/plutus-transfer/app/Main.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ main = do
6666
, pcDryRun = True
6767
, pcLogLevel = Debug
6868
, pcProtocolParamsFile = "./protocol.json"
69+
, pcForceBudget = Nothing
6970
, pcEnableTxEndpoint = True
7071
}
7172
BotPlutusInterface.runPAB @TransferContracts pabConf

src/BotPlutusInterface/Balance.hs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ import BotPlutusInterface.Effects (PABEffect, createDirectoryIfMissingCLI, print
1111
import BotPlutusInterface.Files (DummyPrivKey, unDummyPrivateKey)
1212
import BotPlutusInterface.Files qualified as Files
1313
import BotPlutusInterface.Types (LogLevel (Debug), PABConfig)
14+
import Cardano.Api (ExecutionUnitPrices (ExecutionUnitPrices))
15+
import Cardano.Api.Shelley (ProtocolParameters (protocolParamPrices))
1416
import Control.Monad (foldM, void, zipWithM)
1517
import Control.Monad.Freer (Eff, Member)
1618
import Control.Monad.Trans.Class (lift)
1719
import Control.Monad.Trans.Either (EitherT, hoistEither, newEitherT, runEitherT)
20+
import Data.Coerce (coerce)
1821
import Data.Either.Combinators (rightToMaybe)
1922
import Data.Kind (Type)
2023
import Data.List (partition, (\\))
@@ -25,6 +28,7 @@ import Data.Set (Set)
2528
import Data.Set qualified as Set
2629
import Data.Text (Text)
2730
import Data.Text qualified as Text
31+
import GHC.Real (Ratio ((:%)))
2832
import Ledger qualified
2933
import Ledger.Ada qualified as Ada
3034
import Ledger.Address (Address (..))
@@ -128,8 +132,10 @@ balanceTxIO pabConf ownPkh unbalancedTx =
128132
txWithoutFees <-
129133
hoistEither $ balanceTxStep minUtxos utxoIndex ownPkh $ tx `withFee` 0
130134

131-
newEitherT $ CardanoCLI.buildTx @w pabConf privKeys txWithoutFees
132-
fees <- newEitherT $ CardanoCLI.calculateMinFee @w pabConf txWithoutFees
135+
exBudget <- newEitherT $ CardanoCLI.buildTx @w pabConf privKeys txWithoutFees
136+
nonBudgettedFees <- newEitherT $ CardanoCLI.calculateMinFee @w pabConf txWithoutFees
137+
138+
let fees = nonBudgettedFees + getBudgetPrice (getExecutionUnitPrices pabConf) exBudget
133139

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

@@ -140,6 +146,19 @@ balanceTxIO pabConf ownPkh unbalancedTx =
140146
then pure (balancedTx, minUtxos)
141147
else loop utxoIndex privKeys minUtxos balancedTx
142148

149+
getExecutionUnitPrices :: PABConfig -> ExecutionUnitPrices
150+
getExecutionUnitPrices pabConf = fromMaybe (ExecutionUnitPrices 0 0) $ protocolParamPrices pabConf.pcProtocolParams
151+
152+
getBudgetPrice :: ExecutionUnitPrices -> Ledger.ExBudget -> Integer
153+
getBudgetPrice (ExecutionUnitPrices cpuPrice memPrice) (Ledger.ExBudget cpuUsed memUsed) =
154+
round cpuCost + round memCost
155+
where
156+
cpuCost = cpuPrice `multRational` (toInteger @Ledger.SatInt $ coerce cpuUsed)
157+
memCost = memPrice `multRational` (toInteger @Ledger.SatInt $ coerce memUsed)
158+
159+
multRational :: Rational -> Integer -> Rational
160+
multRational (num :% denom) s = (s * num) :% denom
161+
143162
withFee :: Tx -> Integer -> Tx
144163
withFee tx fee = tx {txFee = Ada.lovelaceValueOf fee}
145164

0 commit comments

Comments
 (0)