@@ -20,6 +20,8 @@ module BotPlutusInterface.Types (
2020 BudgetEstimationError (.. ),
2121 SpendBudgets ,
2222 MintBudgets ,
23+ ContractStats (.. ),
24+ addBudget ,
2325) where
2426
2527import Cardano.Api (NetworkId (Testnet ), NetworkMagic (.. ), ScriptExecutionError , ScriptWitnessIndex )
@@ -31,13 +33,15 @@ import Data.Aeson.TH (Options (..), defaultOptions, deriveJSON)
3133import Data.Default (Default (def ))
3234import Data.Kind (Type )
3335import Data.Map (Map )
36+ import Data.Map qualified as Map
3437import Data.Text (Text )
3538import GHC.Generics (Generic )
3639import Ledger (
3740 ExBudget ,
3841 MintingPolicyHash ,
3942 PubKeyHash ,
4043 StakePubKeyHash ,
44+ TxId ,
4145 TxOutRef ,
4246 )
4347import Ledger.TimeSlot (SlotConfig )
@@ -77,13 +81,72 @@ data PABConfig = PABConfig
7781 , pcTipPollingInterval :: ! Natural
7882 , pcPort :: ! Port
7983 , pcEnableTxEndpoint :: ! Bool
84+ , pcCollectStats :: ! Bool
8085 }
8186 deriving stock (Show , Eq )
8287
88+ -- Budget estimation types
89+
90+ {- | Error returned in case any error happened during budget estimation
91+ (wraps whatever received in `Text`)
92+ -}
93+ data BudgetEstimationError
94+ = -- | general error for `Cardano.Api` errors
95+ BudgetEstimationError ! Text
96+ | -- | script evaluation failed during budget estimation
97+ ScriptFailure ScriptExecutionError
98+ | {- budget for input or policy was not found after estimation
99+ (arguably should not happen at all) -}
100+ BudgetNotFound ScriptWitnessIndex
101+ deriving stock (Show )
102+
103+ -- | Type of transaction file used for budget estimation
104+ data TxFile
105+ = -- | for using with ".raw" files
106+ Raw ! FilePath
107+ | -- | for using with ".signed" files
108+ Signed ! FilePath
109+
110+ -- | Result of budget estimation
111+ data TxBudget = TxBudget
112+ { -- | budgets for spending inputs
113+ spendBudgets :: ! SpendBudgets
114+ , -- | budgets for minting policies
115+ mintBudgets :: ! MintBudgets
116+ }
117+ deriving stock (Show )
118+
119+ addBudget :: TxId -> TxBudget -> ContractStats -> ContractStats
120+ addBudget txId budget stats =
121+ stats {estimatedBudgets = Map. insert txId budget (estimatedBudgets stats)}
122+
123+ instance Semigroup TxBudget where
124+ TxBudget s m <> TxBudget s' m' = TxBudget (s <> s') (m <> m')
125+
126+ instance Monoid TxBudget where
127+ mempty = TxBudget mempty mempty
128+
129+ type SpendBudgets = Map TxOutRef ExBudget
130+
131+ type MintBudgets = Map MintingPolicyHash ExBudget
132+
133+ {- | Collection of stats that could be collected py `bpi`
134+ about contract it runs
135+ -}
136+ newtype ContractStats = ContractStats
137+ { estimatedBudgets :: Map TxId TxBudget
138+ }
139+ deriving stock (Show )
140+ deriving newtype (Semigroup , Monoid )
141+
142+ instance Show (TVar ContractStats ) where
143+ show _ = " <ContractStats>"
144+
83145data ContractEnvironment w = ContractEnvironment
84146 { cePABConfig :: PABConfig
85147 , ceContractInstanceId :: ContractInstanceId
86148 , ceContractState :: TVar (ContractState w )
149+ , ceContractStats :: TVar ContractStats
87150 }
88151 deriving stock (Show )
89152
@@ -140,6 +203,7 @@ instance Default PABConfig where
140203 , pcOwnStakePubKeyHash = Nothing
141204 , pcPort = 9080
142205 , pcEnableTxEndpoint = False
206+ , pcCollectStats = False
143207 }
144208
145209data RawTx = RawTx
@@ -152,44 +216,3 @@ data RawTx = RawTx
152216-- type is a reserved keyword in haskell and can not be used as a field name
153217-- when converting this to JSON we drop the _ prefix from each field
154218deriveJSON defaultOptions {fieldLabelModifier = drop 1 } ''RawTx
155-
156- -- Budget estimation types
157-
158- {- | Error returned in case any error happened during budget estimation
159- (wraps whatever received in `Text`)
160- -}
161- data BudgetEstimationError
162- = -- | general error for `Cardano.Api` errors
163- BudgetEstimationError ! Text
164- | -- | script evaluation failed during budget estimation
165- ScriptFailure ScriptExecutionError
166- | {- budget for input or policy was not found after estimation
167- (arguably should not happen at all) -}
168- BudgetNotFound ScriptWitnessIndex
169- deriving stock (Show )
170-
171- -- | Type of transaction file used for budget estimation
172- data TxFile
173- = -- | for using with ".raw" files
174- Raw ! FilePath
175- | -- | for using with ".signed" files
176- Signed ! FilePath
177-
178- -- | Result of budget estimation
179- data TxBudget = TxBudget
180- { -- | budgets for spending inputs
181- spendBudgets :: ! SpendBudgets
182- , -- | budgets for minting policies
183- mintBudgets :: ! MintBudgets
184- }
185- deriving stock (Show )
186-
187- instance Semigroup TxBudget where
188- TxBudget s m <> TxBudget s' m' = TxBudget (s <> s') (m <> m')
189-
190- instance Monoid TxBudget where
191- mempty = TxBudget mempty mempty
192-
193- type SpendBudgets = Map TxOutRef ExBudget
194-
195- type MintBudgets = Map MintingPolicyHash ExBudget
0 commit comments