;; Contrarian Optimizer — Push3 seed
;;
;; Philosophy: "when everyone is bullish, prepare for the crash."
;;
;; Inputs on DYADIC stack (slot 0 on top, slot 7 at bottom):
;;   [0] percentageStaked      (0 to 1e18, where 1e18 = 100%)
;;   [1] averageTaxRate        (0 to 1e18, normalized)
;;   [2-7] reserved/future     (0 if unavailable)
;;
;; Outputs (top to bottom on termination):
;;   ci             = 0 always (contrarian on positions, not floor)
;;   anchorShare    (0..1e18)
;;   anchorWidth    (tick units)
;;   discoveryDepth (0..1e18)
;;
;; Logic:
;;   stakedPct = percentageStaked * 100 / 1e18  (0-100)
;;   taxHigh   = averageTaxRate >= 1e17         (>= 10% = expect mean reversion)
;;
;;   stakedPct >= 50 (bullish crowd) → defensive:
;;     taxHigh → VERY DEFENSIVE: CI=0, AS=0.15e18, AW=15,  DD=0.10e18
;;     else    → DEFENSIVE:      CI=0, AS=0.25e18, AW=25,  DD=0.20e18
;;
;;   stakedPct < 50 (bearish crowd) → aggressive:
;;     taxHigh → VERY AGGRESSIVE: CI=0, AS=0.85e18, AW=100, DD=0.85e18
;;     else    → AGGRESSIVE:      CI=0, AS=0.70e18, AW=80,  DD=0.70e18

(
  ;; Step 1: Bind slot 0 (top) to PERCENTAGESTAKED, slot 1 to TAXRATE.
  PERCENTAGESTAKED DYADIC.DEFINE
  ;; Stack: [slot7(bot), slot6, slot5, slot4, slot3, slot2, slot1(top)]
  TAXRATE DYADIC.DEFINE
  ;; Stack: [slot7(bot), slot6, slot5, slot4, slot3, slot2(top)]

  ;; Step 2: Discard unused inputs (slots 2-7) — 6 pops.
  DYADIC.POP
  DYADIC.POP
  DYADIC.POP
  DYADIC.POP
  DYADIC.POP
  DYADIC.POP
  ;; Stack: []

  ;; Step 3: Compute stakedPct = percentageStaked * 100 / 1e18  (integer 0..100)
  PERCENTAGESTAKED
  100 DYADIC.*
  1000000000000000000 DYADIC./
  STAKED DYADIC.DEFINE
  ;; Stack: []

  ;; Step 4: stakedPct >= 50? (majority bullish = contrarian sell signal)
  STAKED 50 DYADIC.>=
  ;; bool_stack: [stakedPct >= 50]

  EXEC.IF

  ;; TRUE branch: bullish crowd → go defensive
  (
    ;; Tax high (>= 10%) means volatile / mean-reversion expected → extra defensive
    TAXRATE 100000000000000000 DYADIC.>=
    EXEC.IF
    ;; VERY DEFENSIVE: floor-heavy, narrow anchor, minimal discovery
    (
      100000000000000000
      15
      150000000000000000
      0
    )
    ;; DEFENSIVE: modest floor bias, narrow-ish anchor
    (
      200000000000000000
      25
      250000000000000000
      0
    )
  )

  ;; FALSE branch: bearish crowd → go aggressive
  (
    ;; Tax high (>= 10%) means volatile → amplify the contrarian signal further
    TAXRATE 100000000000000000 DYADIC.>=
    EXEC.IF
    ;; VERY AGGRESSIVE: wide discovery, high anchor share
    (
      850000000000000000
      100
      850000000000000000
      0
    )
    ;; AGGRESSIVE: wide discovery, high anchor share
    (
      700000000000000000
      80
      700000000000000000
      0
    )
  )
)
