⚙️ MICHELSON RUNE DIFFICULTY: Adept Behavioral

The State Pattern in Michelson

Altering smart contract behavior fundamentally when its internal phase shifts.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: State
ARCANUM SCHOOL: Enchantment // Stackmancy
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// MICHELSON PATTERN //
parameter (int %transition);
storage (or (unit %state_dormant) (unit %state_active));
code {
  UNPAIR;
  SWAP;
  IF_LEFT {
    # In Dormant State
    DROP;
    PUSH int 1;
    COMPARE; EQ;
    IF {
      # Transition to Active
      RIGHT unit;
      NIL operation; PAIR
    } {
      PUSH string "Invalid Transition from Dormant"; FAILWITH;
    }
  } {
    # In Active State
    DROP;
    PUSH int 0;
    COMPARE; EQ;
    IF {
      # Transition to Dormant
      LEFT unit;
      NIL operation; PAIR
    } {
      PUSH string "Invalid Transition from Active"; FAILWITH;
    }
  }
}
Runic Tags: #behavioral #state #stack-based smart contracts #michelson #tezos