⚙️ MICHELSON RUNE DIFFICULTY: Adept Behavioral

The Chain of Responsibility Pattern in Michelson

Routing a request through a hierarchy of on-chain handlers until one resolves it.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Chain of Responsibility
ARCANUM SCHOOL: Enchantment // Stackmancy
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// MICHELSON PATTERN //
parameter (int %process_request);
storage (list address);
code {
  UNPAIR;
  # Check if this handler can process it
  DUP;
  PUSH int 100;
  COMPARE; LT;
  IF {
    # Processed
    DROP; DROP;
    NIL operation;
  } {
    # Pass to next in chain
    DROP;
    DUP;
    IF_CONS {
      SWAP; DROP;
      # In reality we would construct an operation to call this address
      DROP;
    } {
      PUSH string "ChainEnd: Unhandled Request"; FAILWITH;
    };
    NIL operation;
  };
  # Return empty storage conceptually
  PUSH (list address) {};
  SWAP; PAIR
}
Runic Tags: #behavioral #chain-of-responsibility #stack-based smart contracts #michelson #tezos