Plutus.V2.Ledger.Api

  • Table of Contents

    1. 📖 Introduction

    2. 📜 Scripts 2.1 📜 SerializedScript 2.2 📜 Script 2.3 📜 fromCompiledCode

    3. ✅ Validating Scripts 3.1 ✅ isScriptWellFormed

    4. 🏃 Running Scripts 4.1 🏃 evaluateScriptCounting 4.2 🏃 evaluateScriptRestricting 4.3 🏷️ ProtocolVersion 4.4 📝 VerboseMode & LogOutput

    5. 💰 Costing-related Types 5.1 💰 ExBudget, ExCPU, ExMemory, SatInt 5.2 ⚙️ EvaluationContext & Cost Model

    6. 🗂️ Context Types 6.1 🗂️ ScriptContext & ScriptPurpose 6.2 🔗 Supporting Types

    7. 📦 Data

    8. ⚠️ Errors

    9. 📖 Glossary of Terms

    1. 📖 Introduction

    Welcome to the Plutus V2 Ledger API Tutorial. This guide will walk you through the key types and functions exposed by the Plutus.V2.Ledger.Api module, helping you understand how to build, validate, and evaluate on‑chain scripts in Plutus V2.

    2. 📜 Scripts

    2.1 📜 SerializedScript

    A SerializedScript is the raw bytes of a compiled Plutus script, ready for on‑chain submission.

    2.2 📜 Script

    A Script is a high‑level wrapper around SerializedScript with type safety.

    2.3 📜 fromCompiledCode

    fromCompiledCode :: CompiledCode a -> Script

    Converts compiled Plutus Core code into a Script for deployment.

    3. ✅ Validating Scripts

    3.1 ✅ isScriptWellFormed

    isScriptWellFormed :: ProtocolVersion -> SerializedScript -> Bool

    Checks that a script deserializes correctly and references only builtins available in the given protocol version.

    4. 🏃 Running Scripts

    4.1 🏃 evaluateScriptCounting

    evaluateScriptCounting
      :: ProtocolVersion
      -> VerboseMode
      -> EvaluationContext
      -> SerializedScript
      -> [PLC.Data]
      -> (LogOutput, Either EvaluationError ExBudget)

    Runs a script to measure its resource usage, returning logs and either an error or the execution budget needed.

    4.2 🏃 evaluateScriptRestricting

    evaluateScriptRestricting
      :: ProtocolVersion
      -> VerboseMode
      -> EvaluationContext
      -> ExBudget
      -> SerializedScript
      -> [PLC.Data]
      -> (LogOutput, Either EvaluationError ExBudget)

    Executes a script with a fixed budget, returning logs and success or actual budget used.

    4.3 🏷️ ProtocolVersion

    Defines the blockchain protocol version, controlling available builtins and cost parameters.

    4.4 📝 VerboseMode & LogOutput

    • VerboseMode: Controls whether evaluation logging is produced.

    • LogOutput: Captures log messages during script execution.

    5.1 💰 ExBudget, ExCPU, ExMemory, SatInt

    • ExBudget: Combined CPU & memory budget.

    • ExCPU, ExMemory: Individual resource meters.

    • SatInt: Saturating integer type for safe arithmetic.

    5.2 ⚙️ EvaluationContext & Cost Model

    • EvaluationContext: Holds cost model parameters for execution.

    • mkEvaluationContext: Builds a context from CostModelParams.

    • CostModelApplyError: Errors when applying cost model.

    • costModelParamNames: Lists parameter names.

    6. 🗂️ Context Types

    6.1 🗂️ ScriptContext & ScriptPurpose

    • ScriptContext: Encapsulates transaction info & purpose for validation.

    • ScriptPurpose: Indicates if script is a validator, minting policy, etc.

    6.2 🔗 Supporting Types

    Includes:

    • ByteStrings: BuiltinByteString, toBuiltin, fromBuiltin

    • Bytes: LedgerBytes, fromBytes

    • Certificates: DCert

    • Credentials: Credential, StakingCredential

    • Value: Value, CurrencySymbol, TokenName, singleton, unionWith, adaSymbol, adaToken

    • Time: POSIXTime, POSIXTimeRange

    • Transactions: Address, PubKeyHash, TxId, TxInfo, TxOut, TxOutRef, TxInInfo, OutputDatum

    • Intervals: Interval, Extended, UpperBound, LowerBound, always, from, to, strictLowerBound, strictUpperBound

    • Maps: Map, fromList

    • Newtypes: Validator, MintingPolicy, StakeValidator, Redeemer, Datum, ScriptHash and associated scripts and hashes.

    7. 📦 Data

    Data serialization types:

    • Data, BuiltinData

    • Typeclasses: ToData, FromData, UnsafeFromData

    • Conversion: toData, fromData, dataToBuiltinData, builtinDataToData

    8. ⚠️ Errors

    • EvaluationError: Errors during script execution, e.g., exceeding budget or type mismatches.

    9. 📖 Glossary of Terms

    Term
    Definition

    SerializedScript

    Raw compiled script bytes.

    Script

    Typed wrapper for SerializedScript.

    ProtocolVersion

    Version tag for blockchain protocol.

    VerboseMode

    Flag for logging during evaluation.

    ExBudget

    Combined CPU & memory budget.

    EvaluationContext

    Cost model parameters container.

    ScriptContext

    Transaction context for validation.

    ScriptPurpose

    Role of a script (validator, policy).

    PLC.Data

    Data value passed into scripts.

    EvaluationError

    Execution failure details.


Last updated