⚙️ PLUTUS RUNE DIFFICULTY: Apprentice Creational

The Builder of TxOut Transmutations

Incrementally composing complex Plutus transaction outputs via the State monad.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Builder
ARCANUM SCHOOL: Transmutation // Script Weaving
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// PLUTUS PATTERN //
module LedgerMonad.Builder where

import PlutusTx.Prelude
import Plutus.V2.Ledger.Api
import Plutus.V2.Ledger.Contexts

-- The Arcane Crucible (Builder State)
data TxBuilderState = TxBuilderState
    { bValue    :: Value
    , bDatum    :: Maybe Datum
    }

{-# INLINABLE initialCrucible #-}
initialCrucible :: TxBuilderState
initialCrucible = TxBuilderState mempty Nothing

-- The Transmuter's Tools (State-like composition without full Monad overhead on-chain)
{-# INLINABLE addMana #-}
addMana :: Value -> TxBuilderState -> TxBuilderState
addMana v state = state { bValue = bValue state <> v }

{-# INLINABLE inscribeRune #-}
inscribeRune :: Datum -> TxBuilderState -> TxBuilderState
inscribeRune d state = state { bDatum = Just d }

{-# INLINABLE forgeOutput #-}
forgeOutput :: TxBuilderState -> ValidatorHash -> TxOut
forgeOutput state vh = TxOut 
    (Address (ScriptCredential vh) Nothing) 
    (bValue state) 
    (maybe NoOutputDatum OutputDatum (bDatum state))
    Nothing

-- Weaving the Spell
{-# INLINABLE executeWeave #-}
executeWeave :: Value -> Datum -> ValidatorHash -> TxOut
executeWeave mana rune vh = 
    forgeOutput (inscribeRune rune $ addMana mana initialCrucible) vh

The Builder pattern in the Ledger Monad operates through stateful transmutation. Instead of assembling a monolithic TxOut artifact in a single chaotic burst of mana, the transmuter weaves it incrementally. By threading an arcane crucible (state record) through pure functions, we maintain elegance and safety before the final forge.

Runic Tags: #builder #plutus #transmutation