@@ -20,7 +20,7 @@ module BotPlutusInterface.Types (
2020 BudgetEstimationError (.. ),
2121 SpendBudgets ,
2222 MintBudgets ,
23- TxStats (.. ),
23+ ContractStats (.. ),
2424 emptyStats ,
2525 addBudget ,
2626) where
@@ -82,13 +82,76 @@ data PABConfig = PABConfig
8282 , pcTipPollingInterval :: ! Natural
8383 , pcPort :: ! Port
8484 , pcEnableTxEndpoint :: ! Bool
85+ , pcCollectStats :: ! Bool
8586 }
8687 deriving stock (Show , Eq )
8788
89+ -- Budget estimation types
90+
91+ {- | Error returned in case any error happened during budget estimation
92+ (wraps whatever received in `Text`)
93+ -}
94+ data BudgetEstimationError
95+ = -- | general error for `Cardano.Api` errors
96+ BudgetEstimationError ! Text
97+ | -- | script evaluation failed during budget estimation
98+ ScriptFailure ScriptExecutionError
99+ | {- budget for input or policy was not found after estimation
100+ (arguably should not happen at all) -}
101+ BudgetNotFound ScriptWitnessIndex
102+ deriving stock (Show )
103+
104+ -- | Type of transaction file used for budget estimation
105+ data TxFile
106+ = -- | for using with ".raw" files
107+ Raw ! FilePath
108+ | -- | for using with ".signed" files
109+ Signed ! FilePath
110+
111+ -- TODO; maybe, Monoid instance could be handy later
112+ emptyStats :: ContractStats
113+ emptyStats = ContractStats mempty
114+
115+ addBudget :: TxId -> TxBudget -> ContractStats -> ContractStats
116+ addBudget txId budget stats =
117+ stats {estimatedBudgets = Map. insert txId budget (estimatedBudgets stats)}
118+
119+ -- | Result of budget estimation
120+ data TxBudget = TxBudget
121+ { -- | budgets for spending inputs
122+ spendBudgets :: ! SpendBudgets
123+ , -- | budgets for minting policies
124+ mintBudgets :: ! MintBudgets
125+ }
126+ deriving stock (Show )
127+
128+ instance Semigroup TxBudget where
129+ TxBudget s m <> TxBudget s' m' = TxBudget (s <> s') (m <> m')
130+
131+ instance Monoid TxBudget where
132+ mempty = TxBudget mempty mempty
133+
134+ type SpendBudgets = Map TxOutRef ExBudget
135+
136+ type MintBudgets = Map MintingPolicyHash ExBudget
137+
138+ {- | Collection of stats that could be collected py `bpi`
139+ about contract it runs
140+ -}
141+ newtype ContractStats = ContractStats
142+ { estimatedBudgets :: Map TxId TxBudget
143+ }
144+ deriving stock (Show )
145+ deriving newtype (Semigroup , Monoid )
146+
147+ instance Show (TVar ContractStats ) where
148+ show _ = " <ContractStats>"
149+
88150data ContractEnvironment w = ContractEnvironment
89151 { cePABConfig :: PABConfig
90152 , ceContractInstanceId :: ContractInstanceId
91153 , ceContractState :: TVar (ContractState w )
154+ , ceContractStats :: TVar ContractStats
92155 }
93156 deriving stock (Show )
94157
@@ -145,6 +208,7 @@ instance Default PABConfig where
145208 , pcOwnStakePubKeyHash = Nothing
146209 , pcPort = 9080
147210 , pcEnableTxEndpoint = False
211+ , pcCollectStats = False
148212 }
149213
150214data RawTx = RawTx
@@ -157,60 +221,3 @@ data RawTx = RawTx
157221-- type is a reserved keyword in haskell and can not be used as a field name
158222-- when converting this to JSON we drop the _ prefix from each field
159223deriveJSON defaultOptions {fieldLabelModifier = drop 1 } ''RawTx
160-
161- -- Budget estimation types
162-
163- {- | Error returned in case any error happened during budget estimation
164- (wraps whatever received in `Text`)
165- -}
166- data BudgetEstimationError
167- = -- | general error for `Cardano.Api` errors
168- BudgetEstimationError ! Text
169- | -- | script evaluation failed during budget estimation
170- ScriptFailure ScriptExecutionError
171- | {- budget for input or policy was not found after estimation
172- (arguably should not happen at all) -}
173- BudgetNotFound ScriptWitnessIndex
174- deriving stock (Show )
175-
176- -- | Type of transaction file used for budget estimation
177- data TxFile
178- = -- | for using with ".raw" files
179- Raw ! FilePath
180- | -- | for using with ".signed" files
181- Signed ! FilePath
182-
183- {- | WIP: Collection of various stats that could be collected py `bpi`
184- about transactions it performs
185- -}
186- data TxStats = TxStats
187- { estimatedBudgets :: ! (Map TxId TxBudget )
188- }
189- deriving stock Show
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-
199- -- | Result of budget estimation
200- data TxBudget = TxBudget
201- { -- | budgets for spending inputs
202- spendBudgets :: ! SpendBudgets
203- , -- | budgets for minting policies
204- mintBudgets :: ! MintBudgets
205- }
206- deriving stock (Show )
207-
208- instance Semigroup TxBudget where
209- TxBudget s m <> TxBudget s' m' = TxBudget (s <> s') (m <> m')
210-
211- instance Monoid TxBudget where
212- mempty = TxBudget mempty mempty
213-
214- type SpendBudgets = Map TxOutRef ExBudget
215-
216- type MintBudgets = Map MintingPolicyHash ExBudget
0 commit comments