11{-# LANGUAGE AllowAmbiguousTypes #-}
22{-# LANGUAGE RankNTypes #-}
33
4- module BotPlutusInterface.Contract (runContract , handleContract ) where
4+ module BotPlutusInterface.Contract (runContract , runContract' , handleContract ) where
55
66import BotPlutusInterface.Balance qualified as PreBalance
77import BotPlutusInterface.BodyBuilder qualified as BodyBuilder
@@ -17,11 +17,11 @@ import BotPlutusInterface.Effects (
1717 queryChainIndex ,
1818 readFileTextEnvelope ,
1919 threadDelay ,
20- uploadDir ,
20+ uploadDir , estimateBudget , saveBudget
2121 )
2222import BotPlutusInterface.Files (DummyPrivKey (FromSKey , FromVKey ))
2323import BotPlutusInterface.Files qualified as Files
24- import BotPlutusInterface.Types (ContractEnvironment (.. ), LogLevel (Debug , Warn ), Tip (block , slot ))
24+ import BotPlutusInterface.Types (ContractEnvironment (.. ), LogLevel (Debug , Warn ), Tip (block , slot ), Budgets , TxFile ( Signed ), TxBudget )
2525import Cardano.Api (AsType (.. ), EraInMode (.. ), Tx (Tx ))
2626import Control.Lens (preview , (^.) )
2727import Control.Monad (join , void , when )
@@ -64,15 +64,29 @@ import Plutus.Contract.Types (Contract (..), ContractEffs)
6464import PlutusTx.Builtins (fromBuiltin )
6565import Wallet.Emulator.Error (WalletAPIError (.. ))
6666import Prelude
67+ import Control.Concurrent.STM (newTVarIO , TVar , readTVarIO )
68+ import Data.Map (Map )
6769
6870runContract ::
6971 forall (w :: Type ) (s :: Row Type ) (e :: Type ) (a :: Type ).
7072 (ToJSON w , Monoid w ) =>
7173 ContractEnvironment w ->
7274 Contract w s e a ->
7375 IO (Either e a )
74- runContract contractEnv (Contract effs) = do
75- runM $ handlePABEffect @ w contractEnv $ raiseEnd $ handleContract contractEnv effs
76+ runContract contractEnv contract =
77+ fmap fst <$> runContract' contractEnv contract
78+
79+ runContract' ::
80+ forall (w :: Type ) (s :: Row Type ) (e :: Type ) (a :: Type ).
81+ (ToJSON w , Monoid w ) =>
82+ ContractEnvironment w ->
83+ Contract w s e a ->
84+ IO (Either e (a , Map Text TxBudget ))
85+ runContract' contractEnv (Contract effs) = do
86+ emptyBudgets :: Budgets <- newTVarIO mempty
87+ res <- runM $ handlePABEffect @ w contractEnv emptyBudgets $ raiseEnd $ handleContract contractEnv effs
88+ budgets <- readTVarIO emptyBudgets
89+ return $ (,budgets) <$> res
7690
7791handleContract ::
7892 forall (w :: Type ) (e :: Type ) (a :: Type ).
@@ -272,6 +286,12 @@ writeBalancedTx contractEnv (Right tx) = do
272286 -- We need to replace the outfile we created at the previous step, as it currently still has the old (incorrect) id
273287 mvFiles (Files. txFilePath pabConf " raw" (Tx. txId tx)) (Files. txFilePath pabConf " raw" (Ledger. getCardanoTxId $ Left cardanoTx))
274288 when signable $ mvFiles (Files. txFilePath pabConf " signed" (Tx. txId tx)) (Files. txFilePath pabConf " signed" (Ledger. getCardanoTxId $ Left cardanoTx))
289+
290+ let txId = Ledger. getCardanoTxId $ Left cardanoTx
291+ path = Text. unpack $ Files. txFilePath pabConf " signed" txId
292+ b <- firstEitherT (Text. pack . show ) $ newEitherT $ estimateBudget @ w (Signed path)
293+
294+ _ <- newEitherT (Right <$> saveBudget @ w (Text. pack $ show txId) b)
275295
276296 pure cardanoTx
277297 where
0 commit comments