@@ -11,20 +11,26 @@ import BotPlutusInterface.Effects (PABEffect, createDirectoryIfMissingCLI, print
1111import BotPlutusInterface.Files (DummyPrivKey , unDummyPrivateKey )
1212import BotPlutusInterface.Files qualified as Files
1313import BotPlutusInterface.Types (LogLevel (Debug ), PABConfig )
14+ import Cardano.Api (ExecutionUnitPrices (ExecutionUnitPrices ))
15+ import Cardano.Api.ProtocolParameters (ProtocolParameters )
1416import Control.Monad (foldM , void , zipWithM )
1517import Control.Monad.Freer (Eff , Member )
1618import Control.Monad.Trans.Class (lift )
1719import Control.Monad.Trans.Either (EitherT , hoistEither , newEitherT , runEitherT )
20+ import Data.Aeson qualified as Aeson
21+ import Data.Coerce (coerce )
1822import Data.Either.Combinators (rightToMaybe )
23+ import Data.HashMap.Strict qualified as HashMap
1924import Data.Kind (Type )
2025import Data.List (partition , (\\) )
2126import Data.Map (Map )
2227import Data.Map qualified as Map
23- import Data.Maybe (fromMaybe , mapMaybe )
28+ import Data.Maybe (fromJust , fromMaybe , mapMaybe )
2429import Data.Set (Set )
2530import Data.Set qualified as Set
2631import Data.Text (Text )
2732import Data.Text qualified as Text
33+ import GHC.Real (Ratio ((:%) ))
2834import Ledger qualified
2935import Ledger.Ada qualified as Ada
3036import Ledger.Address (Address (.. ))
@@ -128,8 +134,10 @@ balanceTxIO pabConf ownPkh unbalancedTx =
128134 txWithoutFees <-
129135 hoistEither $ balanceTxStep minUtxos utxoIndex ownPkh $ tx `withFee` 0
130136
131- void $ newEitherT $ CardanoCLI. buildTx @ w pabConf privKeys txWithoutFees
132- fees <- newEitherT $ CardanoCLI. calculateMinFee @ w pabConf txWithoutFees
137+ exBudget <- newEitherT $ CardanoCLI. buildTx @ w pabConf privKeys txWithoutFees
138+ nonBudgettedFees <- newEitherT $ CardanoCLI. calculateMinFee @ w pabConf txWithoutFees
139+ -- TODO: not fromJust here
140+ let fees = nonBudgettedFees + getBudgetPrice (fromJust $ getProtocolParamsPrices pabConf. pcProtocolParams) exBudget
133141
134142 lift $ printLog @ w Debug $ " Fees: " ++ show fees
135143
@@ -140,6 +148,27 @@ balanceTxIO pabConf ownPkh unbalancedTx =
140148 then pure (balancedTx, minUtxos)
141149 else loop utxoIndex privKeys minUtxos balancedTx
142150
151+ -- I can't seem to import the accessor for this?
152+ -- As such, heres a disgusting hack to read the data
153+ -- Another possibility is converting the Protocol params to Alonzo params, and reading the `Prices` field
154+ -- (This is probably safer, I should do that)
155+ getProtocolParamsPrices :: ProtocolParameters -> Maybe ExecutionUnitPrices
156+ getProtocolParamsPrices params = do
157+ (Aeson. Object o) <- pure $ Aeson. toJSON params
158+ unitsValue <- HashMap. lookup " executionUnitPrices" o
159+ (Aeson. Success units) <- pure $ Aeson. fromJSON unitsValue
160+ pure units
161+
162+ getBudgetPrice :: ExecutionUnitPrices -> Ledger. ExBudget -> Integer
163+ getBudgetPrice (ExecutionUnitPrices cpuPrice memPrice) (Ledger. ExBudget cpuUsed memUsed) =
164+ round cpuCost + round memCost
165+ where
166+ cpuCost = cpuPrice `multRational` (toInteger @ Ledger. SatInt $ coerce cpuUsed)
167+ memCost = memPrice `multRational` (toInteger @ Ledger. SatInt $ coerce memUsed)
168+
169+ multRational :: Rational -> Integer -> Rational
170+ multRational (num :% denom) s = (s * num) :% denom
171+
143172withFee :: Tx -> Integer -> Tx
144173withFee tx fee = tx {txFee = Ada. lovelaceValueOf fee}
145174
0 commit comments