Skip to content

Commit 412cd7b

Browse files
committed
wip: bpi returns tx stats with contract result
1 parent 5398649 commit 412cd7b

File tree

5 files changed

+67
-30
lines changed

5 files changed

+67
-30
lines changed

src/BotPlutusInterface/CardanoCLI.hs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,16 @@ import BotPlutusInterface.Files (
2525
txFilePath,
2626
validatorScriptFilePath,
2727
)
28-
import BotPlutusInterface.Types (MintBudgets, PABConfig, SpendBudgets, Tip, TxBudget, emptyBudget, mintBudgets, spendBudgets)
28+
import BotPlutusInterface.Types (
29+
MintBudgets,
30+
PABConfig,
31+
SpendBudgets,
32+
Tip,
33+
TxBudget,
34+
emptyBudget,
35+
mintBudgets,
36+
spendBudgets,
37+
)
2938
import BotPlutusInterface.UtxoParser qualified as UtxoParser
3039
import Cardano.Api.Shelley (NetworkId (Mainnet, Testnet), NetworkMagic (..), serialiseAddress)
3140
import Control.Monad (join)

src/BotPlutusInterface/Contract.hs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,28 @@ import BotPlutusInterface.Effects (
1111
ShellArgs (..),
1212
callCommand,
1313
createDirectoryIfMissing,
14+
estimateBudget,
1415
handlePABEffect,
1516
logToContract,
1617
printLog,
1718
queryChainIndex,
1819
readFileTextEnvelope,
20+
saveBudget,
1921
threadDelay,
20-
uploadDir, estimateBudget, saveBudget
22+
uploadDir,
2123
)
2224
import BotPlutusInterface.Files (DummyPrivKey (FromSKey, FromVKey))
2325
import BotPlutusInterface.Files qualified as Files
24-
import BotPlutusInterface.Types (ContractEnvironment (..), LogLevel (Debug, Warn), Tip (block, slot), Budgets, TxFile (Signed), TxBudget)
26+
import BotPlutusInterface.Types (
27+
ContractEnvironment (..),
28+
LogLevel (Debug, Warn),
29+
Tip (block, slot),
30+
TxFile (Signed),
31+
TxStats,
32+
emptyStats,
33+
)
2534
import Cardano.Api (AsType (..), EraInMode (..), Tx (Tx))
35+
import Control.Concurrent.STM (newTVarIO, readTVarIO)
2636
import Control.Lens (preview, (^.))
2737
import Control.Monad (join, void, when)
2838
import Control.Monad.Freer (Eff, Member, interpret, reinterpret, runM, subsume, type (~>))
@@ -64,29 +74,27 @@ import Plutus.Contract.Types (Contract (..), ContractEffs)
6474
import PlutusTx.Builtins (fromBuiltin)
6575
import Wallet.Emulator.Error (WalletAPIError (..))
6676
import Prelude
67-
import Control.Concurrent.STM (newTVarIO, readTVarIO)
68-
import Data.Map (Map)
6977

7078
runContract ::
7179
forall (w :: Type) (s :: Row Type) (e :: Type) (a :: Type).
7280
(ToJSON w, Monoid w) =>
7381
ContractEnvironment w ->
7482
Contract w s e a ->
7583
IO (Either e a)
76-
runContract contractEnv contract =
84+
runContract contractEnv contract =
7785
fst <$> runContract' contractEnv contract
7886

7987
runContract' ::
8088
forall (w :: Type) (s :: Row Type) (e :: Type) (a :: Type).
8189
(ToJSON w, Monoid w) =>
8290
ContractEnvironment w ->
8391
Contract w s e a ->
84-
IO (Either e a, Map Text TxBudget)
92+
IO (Either e a, TxStats)
8593
runContract' contractEnv (Contract effs) = do
86-
emptyBudgets :: Budgets <- newTVarIO mempty
94+
emptyBudgets <- newTVarIO emptyStats
8795
res <- runM $ handlePABEffect @w contractEnv emptyBudgets $ raiseEnd $ handleContract contractEnv effs
8896
budgets <- readTVarIO emptyBudgets
89-
return (res,budgets)
97+
return (res, budgets)
9098

9199
handleContract ::
92100
forall (w :: Type) (e :: Type) (a :: Type).
@@ -286,12 +294,12 @@ writeBalancedTx contractEnv (Right tx) = do
286294
-- We need to replace the outfile we created at the previous step, as it currently still has the old (incorrect) id
287295
mvFiles (Files.txFilePath pabConf "raw" (Tx.txId tx)) (Files.txFilePath pabConf "raw" (Ledger.getCardanoTxId $ Left cardanoTx))
288296
when signable $ mvFiles (Files.txFilePath pabConf "signed" (Tx.txId tx)) (Files.txFilePath pabConf "signed" (Ledger.getCardanoTxId $ Left cardanoTx))
289-
297+
290298
let txId = Ledger.getCardanoTxId $ Left cardanoTx
291299
path = Text.unpack $ Files.txFilePath pabConf "signed" txId
292300
b <- firstEitherT (Text.pack . show) $ newEitherT $ estimateBudget @w (Signed path)
293301

294-
_ <- newEitherT (Right <$> saveBudget @w (Text.pack $ show txId) b)
302+
_ <- newEitherT (Right <$> saveBudget @w txId b)
295303

296304
pure cardanoTx
297305
where

src/BotPlutusInterface/Effects.hs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ module BotPlutusInterface.Effects (
2020
writeFileTextEnvelope,
2121
callCommand,
2222
estimateBudget,
23-
saveBudget) where
23+
saveBudget,
24+
) where
2425

2526
import BotPlutusInterface.ChainIndex (handleChainIndexReq)
2627
import BotPlutusInterface.Estimate qualified as Estimate
@@ -31,12 +32,14 @@ import BotPlutusInterface.Types (
3132
ContractState (ContractState),
3233
LogLevel (..),
3334
TxBudget,
34-
TxFile, Budgets
35+
TxFile,
36+
TxStats,
37+
addBudget,
3538
)
3639
import Cardano.Api (AsType, FileError, HasTextEnvelope, TextEnvelopeDescr, TextEnvelopeError)
3740
import Cardano.Api qualified
3841
import Control.Concurrent qualified as Concurrent
39-
import Control.Concurrent.STM (atomically, modifyTVar)
42+
import Control.Concurrent.STM (TVar, atomically, modifyTVar, modifyTVar')
4043
import Control.Monad (void, when)
4144
import Control.Monad.Freer (Eff, LastMember, Member, interpretM, send, type (~>))
4245
import Data.Aeson (ToJSON)
@@ -47,13 +50,13 @@ import Data.Maybe (catMaybes)
4750
import Data.String (IsString, fromString)
4851
import Data.Text (Text)
4952
import Data.Text qualified as Text
53+
import Ledger qualified
5054
import Plutus.Contract.Effects (ChainIndexQuery, ChainIndexResponse)
5155
import Plutus.PAB.Core.ContractInstance.STM (Activity)
5256
import System.Directory qualified as Directory
5357
import System.Exit (ExitCode (ExitFailure, ExitSuccess))
5458
import System.Process (readProcess, readProcessWithExitCode)
5559
import Prelude hiding (readFile)
56-
import Data.Map qualified as Map
5760

5861
data ShellArgs a = ShellArgs
5962
{ cmdName :: Text
@@ -89,16 +92,16 @@ data PABEffect (w :: Type) (r :: Type) where
8992
UploadDir :: Text -> PABEffect w ()
9093
QueryChainIndex :: ChainIndexQuery -> PABEffect w ChainIndexResponse
9194
EstimateBudget :: TxFile -> PABEffect w (Either BudgetEstimationError TxBudget)
92-
SaveBudget :: Text -> TxBudget -> PABEffect w ()
95+
SaveBudget :: Ledger.TxId -> TxBudget -> PABEffect w ()
9396

9497
handlePABEffect ::
9598
forall (w :: Type) (effs :: [Type -> Type]).
9699
LastMember IO effs =>
97100
(Monoid w) =>
98101
ContractEnvironment w ->
99-
Budgets ->
102+
TVar TxStats ->
100103
Eff (PABEffect w ': effs) ~> Eff effs
101-
handlePABEffect contractEnv budgets =
104+
handlePABEffect contractEnv txStatsVar =
102105
interpretM
103106
( \case
104107
CallCommand shellArgs ->
@@ -135,11 +138,12 @@ handlePABEffect contractEnv budgets =
135138
handleChainIndexReq contractEnv.cePABConfig query
136139
EstimateBudget txPath ->
137140
Estimate.estimateBudget contractEnv.cePABConfig txPath
138-
SaveBudget txId exBudget -> saveBudgetImpl budgets txId exBudget
141+
SaveBudget txId exBudget -> saveBudgetImpl txStatsVar txId exBudget
139142
)
140143

141-
saveBudgetImpl budgets txId budget =
142-
atomically $ modifyTVar budgets (Map.insert txId budget)
144+
saveBudgetImpl :: TVar TxStats -> Ledger.TxId -> TxBudget -> IO ()
145+
saveBudgetImpl txStatsVar txId budget =
146+
atomically $ modifyTVar' txStatsVar (addBudget txId budget)
143147

144148
printLog' :: LogLevel -> LogLevel -> String -> IO ()
145149
printLog' logLevelSetting msgLogLvl msg =
@@ -275,12 +279,10 @@ queryChainIndex ::
275279
Eff effs ChainIndexResponse
276280
queryChainIndex = send @(PABEffect w) . QueryChainIndex
277281

278-
279-
280282
saveBudget ::
281283
forall (w :: Type) (effs :: [Type -> Type]).
282284
Member (PABEffect w) effs =>
283-
Text ->
284-
TxBudget ->
285+
Ledger.TxId ->
286+
TxBudget ->
285287
Eff effs ()
286-
saveBudget txId budget = send @(PABEffect w) $ SaveBudget txId budget
288+
saveBudget txId budget = send @(PABEffect w) $ SaveBudget txId budget

src/BotPlutusInterface/Types.hs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ module BotPlutusInterface.Types (
2020
BudgetEstimationError (..),
2121
SpendBudgets,
2222
MintBudgets,
23+
TxStats,
24+
emptyStats,
25+
addBudget,
2326
emptyBudget,
24-
Budgets) where
27+
) where
2528

2629
import Cardano.Api (NetworkId (Testnet), NetworkMagic (..), ScriptExecutionError, ScriptWitnessIndex)
2730
import Cardano.Api.ProtocolParameters (ProtocolParameters)
@@ -32,13 +35,15 @@ import Data.Aeson.TH (Options (..), defaultOptions, deriveJSON)
3235
import Data.Default (Default (def))
3336
import Data.Kind (Type)
3437
import Data.Map (Map)
38+
import Data.Map qualified as Map
3539
import Data.Text (Text)
3640
import GHC.Generics (Generic)
3741
import Ledger (
3842
ExBudget,
3943
MintingPolicyHash,
4044
PubKeyHash,
4145
StakePubKeyHash,
46+
TxId,
4247
TxOutRef,
4348
)
4449
import Ledger.TimeSlot (SlotConfig)
@@ -176,6 +181,21 @@ data TxFile
176181
| -- | for using with ".signed" files
177182
Signed !FilePath
178183

184+
{- | WIP: Collection of various stats that could be collected py `bpi`
185+
about transactions it performs
186+
-}
187+
data TxStats = TxStats
188+
{ estimatedBudgets :: !(Map TxId TxBudget)
189+
}
190+
191+
-- TODO; maybe, Monoid instance could be handy later
192+
emptyStats :: TxStats
193+
emptyStats = TxStats mempty
194+
195+
addBudget :: TxId -> TxBudget -> TxStats -> TxStats
196+
addBudget txId budget stats =
197+
stats {estimatedBudgets = Map.insert txId budget (estimatedBudgets stats)}
198+
179199
-- | Result of budget estimation
180200
data TxBudget = TxBudget
181201
{ -- | budgets for spending inputs
@@ -192,6 +212,3 @@ emptyBudget = TxBudget mempty mempty
192212
type SpendBudgets = Map TxOutRef ExBudget
193213

194214
type MintBudgets = Map MintingPolicyHash ExBudget
195-
196-
197-
type Budgets = TVar (Map Text TxBudget)

test/Spec/MockContract.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ runPABEffectPure initState req =
298298
go (UploadDir dir) = mockUploadDir dir
299299
go (QueryChainIndex query) = mockQueryChainIndex query
300300
go (EstimateBudget file) = mockExBudget file
301+
go (SaveBudget _ _) = pure () -- TODO
301302
incSlot :: forall (v :: Type). MockContract w v -> MockContract w v
302303
incSlot mc =
303304
mc <* modify @(MockContractState w) (tip %~ incTip)

0 commit comments

Comments
 (0)