@@ -24,6 +24,7 @@ import PlutusLedgerApi.Test.EvaluationEvent
2424import PlutusLedgerApi.V1 qualified as V1
2525import PlutusLedgerApi.V2 qualified as V2
2626import PlutusLedgerApi.V3 qualified as V3
27+ import PlutusLedgerApi.V4 qualified as V4
2728import PlutusTx.AssocMap qualified as M
2829import UntypedPlutusCore as UPLC
2930
@@ -73,6 +74,16 @@ stringOfPurposeV3 = \case
7374 V3. VotingScript {} -> " V3 Voting"
7475 V3. ProposingScript {} -> " V3 Proposing"
7576
77+ stringOfPurposeV4 :: V4. ScriptInfo -> String
78+ stringOfPurposeV4 = \ case
79+ V4. MintingScript {} -> " V4 Minting"
80+ V4. SpendingScript {} -> " V4 Spending"
81+ V4. WithdrawingScript {} -> " V4 Withdrawing"
82+ V4. CertifyingScript {} -> " V4 Certifying"
83+ V4. VotingScript {} -> " V4 Voting"
84+ V4. ProposingScript {} -> " V4 Proposing"
85+ V4. GuardingScript {} -> " V4 Guarding"
86+
7687shapeOfValue :: V1. Value -> String
7788shapeOfValue (V1. Value m) =
7889 let l = M. toList m
@@ -117,6 +128,16 @@ analyseTxInfoV3 i = do
117128 analyseValue $ V3. mintValueBurned (V3. txInfoMint i)
118129 analyseOutputs (V3. txInfoOutputs i) V3. txOutValue
119130
131+ analyseTxInfoV4 :: V4. TxInfo -> IO ()
132+ analyseTxInfoV4 i = do
133+ putStr " Fee: "
134+ print $ V4. txInfoFee i
135+ putStr " Mint: "
136+ analyseValue $ V4. mintValueMinted (V4. txInfoMint i)
137+ putStr " Burn: "
138+ analyseValue $ V4. mintValueBurned (V4. txInfoMint i)
139+ analyseOutputs (V4. txInfoOutputs i) V4. txOutValue
140+
120141analyseScriptContext :: EventAnalyser
121142analyseScriptContext _ctx _params ev = case ev of
122143 PlutusEvent PlutusV1 ScriptEvaluationData {.. } _expected ->
@@ -134,6 +155,11 @@ analyseScriptContext _ctx _params ev = case ev of
134155 [_, _, c] -> analyseCtxV3 c
135156 [_, c] -> analyseCtxV3 c
136157 l -> error $ printf " Unexpected number of V3 script arguments: %d" (length l)
158+ PlutusEvent PlutusV4 ScriptEvaluationData {.. } _expected ->
159+ case dataInputs of
160+ [_, _, c] -> analyseCtxV4 c
161+ [_, c] -> analyseCtxV4 c
162+ l -> error $ printf " Unexpected number of V4 script arguments: %d" (length l)
137163 where
138164 analyseCtxV1 c =
139165 case V1. fromData @ V1. ScriptContext c of
@@ -176,6 +202,11 @@ analyseScriptContext _ctx _params ev = case ev of
176202 printV1info p
177203 Nothing -> putStrLn " * Failed to decode V1 ScriptContext for V3 event: giving up\n "
178204
205+ analyseCtxV4 c =
206+ case V4. fromData @ V4. ScriptContext c of
207+ Just p -> printV4info p
208+ Nothing -> putStrLn " \n * Failed to decode V4 ScriptContext for V4 event: giving up\n "
209+
179210 printV1info p = do
180211 putStrLn " ----------------"
181212 putStrLn $ stringOfPurposeV1 $ V1. scriptContextPurpose p
@@ -191,6 +222,11 @@ analyseScriptContext _ctx _params ev = case ev of
191222 putStrLn $ stringOfPurposeV3 $ V3. scriptContextScriptInfo p
192223 analyseTxInfoV3 $ V3. scriptContextTxInfo p
193224
225+ printV4info p = do
226+ putStrLn " ----------------"
227+ putStrLn $ stringOfPurposeV4 $ V4. scriptContextScriptInfo p
228+ analyseTxInfoV4 $ V4. scriptContextTxInfo p
229+
194230-- Data object analysis
195231
196232-- Statistics about a Data object
@@ -416,6 +452,25 @@ analyseCosts ctx _ ev =
416452 (_, Left _) -> Failed
417453 (_, Right cost) -> OK cost
418454 printCost result dataBudget
455+ PlutusEvent PlutusV4 ScriptEvaluationData {.. } _ -> do
456+ dataInput <-
457+ case dataInputs of
458+ [input] -> pure input
459+ _ -> throwIO $ userError " PlutusV4 script expects exactly one input"
460+ let result =
461+ case deserialiseScript PlutusV4 dataProtocolVersion dataScript of
462+ Left _ -> DeserialisationError
463+ Right script -> do
464+ case V4. evaluateScriptRestricting
465+ dataProtocolVersion
466+ V4. Quiet
467+ ctx
468+ dataBudget
469+ script
470+ dataInput of
471+ (_, Left _) -> Failed
472+ (_, Right cost) -> OK cost
473+ printCost result dataBudget
419474 where
420475 printCost :: EvaluationResult -> ExBudget -> IO ()
421476 printCost result claimedCost =
@@ -463,12 +518,14 @@ analyseOneFile analyse eventFile = do
463518 case ( mkContext V1. mkEvaluationContext (eventsCostParamsV1 events)
464519 , mkContext V2. mkEvaluationContext (eventsCostParamsV2 events)
465520 , mkContext V3. mkEvaluationContext (eventsCostParamsV2 events)
521+ , mkContext V4. mkEvaluationContext (eventsCostParamsV2 events)
466522 ) of
467- (Right ctxV1, Right ctxV2, Right ctxV3) ->
468- mapM_ (runSingleEvent ctxV1 ctxV2 ctxV3) (eventsOf events)
469- (Left err, _, _) -> error $ display err
470- (_, Left err, _) -> error $ display err
471- (_, _, Left err) -> error $ display err
523+ (Right ctxV1, Right ctxV2, Right ctxV3, Right ctxV4) ->
524+ mapM_ (runSingleEvent ctxV1 ctxV2 ctxV3 ctxV4) (eventsOf events)
525+ (Left err, _, _, _) -> error $ display err
526+ (_, Left err, _, _) -> error $ display err
527+ (_, _, Left err, _) -> error $ display err
528+ (_, _, _, Left err) -> error $ display err
472529 where
473530 mkContext f = \ case
474531 Nothing -> Right Nothing
@@ -478,9 +535,10 @@ analyseOneFile analyse eventFile = do
478535 :: Maybe (EvaluationContext , [Int64 ])
479536 -> Maybe (EvaluationContext , [Int64 ])
480537 -> Maybe (EvaluationContext , [Int64 ])
538+ -> Maybe (EvaluationContext , [Int64 ])
481539 -> ScriptEvaluationEvent
482540 -> IO ()
483- runSingleEvent ctxV1 ctxV2 ctxV3 event =
541+ runSingleEvent ctxV1 ctxV2 ctxV3 ctxV4 event =
484542 case event of
485543 PlutusEvent PlutusV1 _ _ ->
486544 case ctxV1 of
@@ -494,6 +552,10 @@ analyseOneFile analyse eventFile = do
494552 case ctxV3 of
495553 Just (ctx, params) -> analyse ctx params event
496554 Nothing -> putStrLn " *** ctxV3 missing ***"
555+ PlutusEvent PlutusV4 _ _ ->
556+ case ctxV4 of
557+ Just (ctx, params) -> analyse ctx params event
558+ Nothing -> putStrLn " *** ctxV4 missing ***"
497559
498560main :: IO ()
499561main =
0 commit comments