Skip to content

Commit 0e1fc6e

Browse files
Use exbudget for fees
1 parent 0a0f31e commit 0e1fc6e

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

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

src/BotPlutusInterface/Balance.hs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,26 @@ 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.ProtocolParameters (ProtocolParameters)
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.Aeson qualified as Aeson
21+
import Data.Coerce (coerce)
1822
import Data.Either.Combinators (rightToMaybe)
23+
import Data.HashMap.Strict qualified as HashMap
1924
import Data.Kind (Type)
2025
import Data.List (partition, (\\))
2126
import Data.Map (Map)
2227
import Data.Map qualified as Map
23-
import Data.Maybe (fromMaybe, mapMaybe)
28+
import Data.Maybe (fromJust, fromMaybe, mapMaybe)
2429
import Data.Set (Set)
2530
import Data.Set qualified as Set
2631
import Data.Text (Text)
2732
import Data.Text qualified as Text
33+
import GHC.Real (Ratio ((:%)))
2834
import Ledger qualified
2935
import Ledger.Ada qualified as Ada
3036
import 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+
143172
withFee :: Tx -> Integer -> Tx
144173
withFee tx fee = tx {txFee = Ada.lovelaceValueOf fee}
145174

test/Spec/BotPlutusInterface/Contract.hs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ sendTokensWithoutName = do
476476
mintTokens :: Assertion
477477
mintTokens = do
478478
let txOutRef = TxOutRef "e406b0cf676fc2b1a9edb0617f259ad025c20ea6f0333820aa7cef1bfe7302e5" 0
479-
txOut = TxOut pkhAddr1 (Ada.lovelaceValueOf 1200) Nothing
479+
txOut = TxOut pkhAddr1 (Ada.lovelaceValueOf 1_000_000) Nothing
480480
initState = def & utxos .~ [(txOutRef, txOut)]
481481
inTxId = encodeByteString $ fromBuiltin $ TxId.getTxId $ Tx.txOutRefId txOutRef
482482

@@ -526,18 +526,19 @@ mintTokens = do
526526
|]
527527
)
528528
,
529-
( 6
529+
( 12
530530
, [text|
531531
cardano-cli transaction build-raw --alonzo-era
532532
--tx-in ${inTxId}#0
533533
--tx-in-collateral ${inTxId}#0
534+
--tx-out ${addr1}+610151
534535
--tx-out ${addr2}+1000 + 5 ${curSymbol'}.74657374546F6B656E
535536
--mint-script-file ./result-scripts/policy-${curSymbol'}.plutus
536537
--mint-redeemer-file ./result-scripts/redeemer-${redeemerHash}.json
537538
--mint-execution-units (387149,1400)
538539
--mint 5 ${curSymbol'}.74657374546F6B656E
539540
--required-signer ./signing-keys/signing-key-${pkh1'}.skey
540-
--fee 200
541+
--fee 388849
541542
--protocol-params-file ./protocol.json --out-file ./txs/tx-${outTxId}.raw
542543
|]
543544
)
@@ -642,7 +643,7 @@ spendToValidator = do
642643
redeemFromValidator :: Assertion
643644
redeemFromValidator = do
644645
let txOutRef = TxOutRef "e406b0cf676fc2b1a9edb0617f259ad025c20ea6f0333820aa7cef1bfe7302e5" 0
645-
txOut = TxOut pkhAddr1 (Ada.lovelaceValueOf 100) Nothing
646+
txOut = TxOut pkhAddr1 (Ada.lovelaceValueOf 1_000_000) Nothing
646647
txOutRef' = TxOutRef "e406b0cf676fc2b1a9edb0617f259ad025c20ea6f0333820aa7cef1bfe7302e5" 1
647648
txOut' = TxOut valAddr (Ada.lovelaceValueOf 1250) (Just datumHash)
648649
initState = def & utxos .~ [(txOutRef, txOut), (txOutRef', txOut')]
@@ -709,19 +710,20 @@ redeemFromValidator = do
709710
|]
710711
)
711712
,
712-
( 12
713+
( 14
713714
, [text|
714715
cardano-cli transaction build-raw --alonzo-era
716+
--tx-in ${inTxId}#0
715717
--tx-in ${inTxId}#1
716718
--tx-in-script-file ./result-scripts/validator-${valHash'}.plutus
717719
--tx-in-datum-file ./result-scripts/datum-${datumHash'}.json
718720
--tx-in-redeemer-file ./result-scripts/redeemer-${redeemerHash}.json
719721
--tx-in-execution-units (476468,1700)
720722
--tx-in-collateral ${inTxId}#0
721-
--tx-out ${addr1}+450
723+
--tx-out ${addr1}+522182
722724
--tx-out ${addr2}+500
723725
--required-signer ./signing-keys/signing-key-${pkh1'}.skey
724-
--fee 300
726+
--fee 478568
725727
--protocol-params-file ./protocol.json --out-file ./txs/tx-${outTxId}.raw
726728
|]
727729
)

0 commit comments

Comments
 (0)