<template>
  <div>
    <h1>The Optimizer</h1>
    <p>
      The Kraiken protocol doesn't rely on static liquidity parameters. Instead, it reads on-chain staker behavior as a real-time
      sentiment signal and adapts its liquidity positioning accordingly. No admin or owner can intervene — the optimizer operates
      autonomously within immutable contracts.
    </p>
    <p>
      In the chapter <strong>Liquidity Management</strong>, we described the static behavior of the contract, outlining how the liquidity
      manager maintains predefined parameters for market interactions. While effective in stable conditions, this static behavior lacks
      adaptability to dynamic market changes. The optimizer adds this adaptability — reading staker sentiment and adjusting parameters
      in real-time to optimize liquidity positioning.
    </p>

    <p>
      The optimizer incorporates real-time data directly sourced from stakers.
      Parameters such as the percentage staked and average tax rate provide valuable sentiment indicators that would otherwise only be
      available through off-chain analysis, enriching the protocol's decision-making with actionable insights from on-chain
      activity.
    </p>

    <h2>Inputs to the Optimizer</h2>
    <p>
      The optimizer consumes key input parameters that capture the state of the market, user behavior, and
      the system itself. These inputs are normalized and structured to drive parameter selection.
    </p>
    <ol>
      <li>
        <strong>Price Position Relative to Range:</strong>
        <ul>
          <li>
            <em>Description:</em> The normalized position of the current token price within a predefined range (e.g., VWAP ± volatility
            bounds).
          </li>
          <li><em>Range:</em> 0 (lower bound) to 1e18 (upper bound).</li>
        </ul>
      </li>
      <li>
        <strong>Volatility Ratio:</strong>
        <ul>
          <li><em>Description:</em> The ratio of current price volatility to a baseline, such as weekly historical volatility.</li>
          <li><em>Range:</em> 0 (low volatility) to 1e18 (high volatility relative to baseline).</li>
        </ul>
      </li>
      <li>
        <strong>Volume-to-Liquidity Ratio:</strong>
        <ul>
          <li><em>Description:</em> The ratio of trading volume to liquidity in the relevant range, indicating potential profitability.</li>
          <li><em>Range:</em> 0 (low volume relative to liquidity) to 1e18 (high volume relative to liquidity).</li>
        </ul>
      </li>
      <li>
        <strong>Time Since Last Call:</strong>
        <ul>
          <li>
            <em>Description:</em> The elapsed time since the last optimizer update, expressed as a fraction of a predefined target interval.
          </li>
          <li><em>Range:</em> 0 (just called) to 1e18 (maximum target interval elapsed).</li>
        </ul>
      </li>
      <li>
        <strong>Percentage Staked:</strong>
        <ul>
          <li><em>Description:</em> The proportion of available staking slots currently utilized by users.</li>
          <li><em>Range:</em> 0 (no stake) to 1e18 (maximum stake capacity utilized).</li>
        </ul>
      </li>
      <li>
        <strong>Average Tax Rate:</strong>
        <ul>
          <li><em>Description:</em> The average tax rate applied to transactions, representing the system's cost structure.</li>
          <li><em>Range:</em> 0 (0% tax) to 1e18 (4700% tax, maximum rate).</li>
        </ul>
      </li>
    </ol>

    <h2>Outputs from the Optimizer</h2>
    <p>
      The optimizer selects liquidity management parameters based on its input data, dynamically adjusting them to improve market
      responsiveness and profitability. These outputs are sent to the liquidity manager contract for execution.
    </p>
    <ol>
      <li>
        <strong>Capital Inefficiency:</strong>
        <ul>
          <li>
            <strong>Description:</strong> Measures the deviation from optimal capital allocation, indicating potential areas for improvement
            in resource utilization.
          </li>
          <li><strong>Type:</strong> <code>uint256</code></li>
        </ul>
      </li>
      <li>
        <strong>Anchor Share:</strong>
        <ul>
          <li>
            <strong>Description:</strong> Represents the proportion of resources allocated to the anchor position, reflecting the agent's
            confidence in the current market stability.
          </li>
          <li><strong>Type:</strong> <code>uint256</code></li>
        </ul>
      </li>
      <li>
        <strong>Anchor Width:</strong>
        <ul>
          <li>
            <strong>Description:</strong> Defines the range or bandwidth of the anchor position, determining the scope of market conditions
            under consideration.
          </li>
          <li><strong>Type:</strong> <code>uint24</code></li>
        </ul>
      </li>
      <li>
        <strong>Discovery Depth:</strong>
        <ul>
          <li>
            <strong>Description:</strong> Indicates the extent to which the agent explores new strategies or market opportunities beyond the
            established anchor parameters.
          </li>
          <li><strong>Type:</strong> <code>uint256</code></li>
        </ul>
      </li>
    </ol>

    <h2>Optimizer Contract</h2>
    <p>
      The Optimizer Contract (OptimizerV3) serves as the decision layer, interfacing directly with the liquidity manager
      contract. It is queried during every rebalance to determine how liquidity should be positioned. The Optimizer uses a direct 2D mapping
      from on-chain staking sentiment to binary bear/bull configurations:
    </p>
    <ol>
      <li>
        <strong>Sentiment Signal Collection:</strong>
        <ul>
          <li>
            The contract reads two key signals directly from the Harberger staking system: the percentage of staking slots currently
            utilized, and the average tax rate across all active stakers.
          </li>
          <li>These signals together form a real-time sentiment indicator that captures community conviction.</li>
        </ul>
      </li>
      <li>
        <strong>Bear/Bull Classification:</strong>
        <ul>
          <li>When staking utilization is at or below 91%, the optimizer always selects the defensive <strong>bear</strong> configuration. This represents the vast majority (~94%) of the staking state space.</li>
          <li>Bull mode requires both very high staking (&gt;91%) <em>and</em> low average tax rates &mdash; a signal of genuine community euphoria rather than speculative over-bidding.</li>
          <li>Any decline in staking instantly snaps the system back to bear mode, providing rapid downside protection.</li>
        </ul>
      </li>
      <li>
        <strong>Binary Parameter Output:</strong>
        <ul>
          <li><strong>Bear mode:</strong> Conservative anchor (30% of ETH, wide 100-tick range), minimal discovery. Maximizes floor protection.</li>
          <li><strong>Bull mode:</strong> Aggressive anchor (100% of ETH, narrow 20-tick range), full discovery depth. Maximizes fee capture during euphoria.</li>
          <li>The binary step avoids intermediate parameter ranges that were found to be exploitable during extensive fuzzing and adversarial testing.</li>
        </ul>
      </li>
      <li>
        <strong>Forwarding to Liquidity Manager:</strong>
        <ul>
          <li>
            The outputs are forwarded to the liquidity manager contract, which applies the recommended changes to reposition liquidity
            during the next rebalance.
          </li>
        </ul>
      </li>
    </ol>

    <p>
      The Optimizer is upgradeable via UUPS proxy pattern, allowing the sentiment-to-parameter mapping to evolve as the protocol gathers
      more real-world data. However, the core Liquidity Manager remains immutable &mdash; the Optimizer can only influence <em>how</em>
      liquidity is positioned, never the fundamental safety guarantees of the protocol.
    </p>

    <h2>Dynamic Adaptation Through Sentiment</h2>
    <p>
      The Optimizer's ability to read staking sentiment and adapt parameters allows the liquidity manager to respond to community conviction
      in real-time. The system naturally traces a cycle through three phases:
    </p>
    <ul>
      <li>
        <strong>Bear Mode (Default):</strong> With staking below 91%, the Optimizer maintains a wide, conservative anchor and minimal
        discovery. The Floor is maximally protected, and the protocol prioritizes solvency over aggressive fee capture.
      </li>
      <li>
        <strong>Bull Transition:</strong> As staking fills past 91% with low tax rates &mdash; a signal of genuine community euphoria
        &mdash; the Optimizer switches to an aggressive configuration. The narrow anchor concentrates liquidity near the current price,
        maximizing trading fee revenue during periods of high activity.
      </li>
      <li>
        <strong>Snap-Back Protection:</strong> Any decline in staking instantly reverts to bear mode. The cubic sensitivity ensures that
        even a small 4-6% drop in staking triggers the transition, providing rapid downside protection.
      </li>
    </ul>

    <p>
      By replacing static configurations with sentiment-driven adaptation, the liquidity manager evolves into a dynamic system that responds
      to community behavior. Staking signals provide critical real-time sentiment data, creating a cybernetic feedback loop: stakers
      influence liquidity positioning through their commitment, and the resulting market dynamics in turn affect staking incentives.
    </p>
  </div>
</template>

<script setup lang="ts">
import { onMounted } from 'vue';

interface AiAgentEmits {
  (event: 'onmounted'): void;
}

const emit = defineEmits<AiAgentEmits>();

onMounted(() => {
  emit('onmounted');
});
</script>
