Skip to content
Verified contractExactMIT

DuelEnrollSystem

Address0x0573f2d2cd4cdb1a1003795a9dcd728b7e7cf1ef
Balance

0 ETH

 

Transactions

0

 

Compiler

solc 0.8.24

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.24+commit.e11b9ed9
Language
solidity
Optimization
Yes, 3,000 runs
EVM version
cancun
Via IR
yes
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
25,481,290
Creation transaction
0x72cf737594…1291d99bc0

89 files

The source code is large: one file is shown at a time. Choose a file in the list.

src/namespaces/root/DuelEnrollSystem.sol

// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24;

import { System } from "@latticexyz/world/src/System.sol";

import { Bracket } from "common/common.sol";
import { LibAuthRoles, Role } from "libraries/utils/LibAuthRoles.sol";
import { LibCommit } from "libraries/utils/LibCommit.sol";
import { LibDuel } from "libraries/LibDuel.sol";
import { LibWeth } from "libraries/LibWeth.sol";
import { Taruchi } from "libraries/Taruchi.sol";

/// @notice Duel enrollment with EIP-712 signature verification.
/// @dev RELAYER-gated: only addresses with `Role.RELAYER` (the trusted matchmaker)
///      can submit. Player intents are still authorized via 2 EIP-712 `DuelAuth` sigs
///      — the gate restricts WHO can submit the bundle, not who can sign it.
///      Mirrors TourneyEnrollSystem's RELAYER gate so duels and festivals share the
///      same trust topology: keryx is the single matchmaker path on-chain, no third
///      party can replay harvested sigs at an inconvenient block. `forceDuelResolve`
///      stays REVEALER-gated as the after-256-block escape valve for stuck duels.
///      Runs in root namespace so WETH gas collection uses World as approval target.
///      CEI: duel row (battleEnroll both taruchis) is written BEFORE either fee transfer —
///      a reentrant caller hits verifyState(IDLE) and reverts without needing a guard.
///      Each signer reimburses half the enroll gas in WETH at their own signed `maxGasPrice` cap;
///      WETH lands at `_msgSender()` (the relayer who submitted the tx).
contract DuelEnrollSystem is System {
  error SelfDuel();

  /// @dev Bundles enrollment numerics into a single memory pointer so helpers don't blow stack.
  struct EnrollArgs {
    uint64 players;
    uint64 specs;
    uint256 deadlines;
    uint256 maxGasPrices;
    Bracket bracket;
  }

  struct DuelSigners {
    address signerA;
    address signerB;
  }

  /// @param players Packed: playerAIndex (low 32), playerBIndex (high 32)
  /// @param bracket Non-festival bracket for the duel
  /// @param specs Packed: specA (low 32), specB (high 32)
  /// @param deadlines Packed: deadlineA (low 128), deadlineB (high 128)
  /// @param maxGasPrices Packed: maxGasPriceA (low 128), maxGasPriceB (high 128) — wei units
  /// @param sigA EIP-712 signature from player A
  /// @param sigB EIP-712 signature from player B
  /// @param secretHash keccak(secret) co-seed commitment, folded into the duelId (see LibCommit).
  /// @dev secretHash is NOT part of the player-signed DuelAuth struct — the RELAYER supplies it and
  ///      players have no on-chain recourse over its value. The co-seed anti-grind guarantee is only
  ///      as strong as the relayer's CSPRNG discipline: a weak/reused/predictable secret silently
  ///      restores the grindable state (the zero value is the one case rejected on-chain). If the
  ///      signing scheme is ever revised, secretHash is a candidate for inclusion in the signed struct.
  function duelEnroll(
    uint64 players,
    Bracket bracket,
    uint64 specs,
    uint256 deadlines,
    uint256 maxGasPrices,
    bytes calldata sigA,
    bytes calldata sigB,
    bytes32 secretHash
  ) external returns (uint256 duelId) {
    LibAuthRoles.verifyRole(_msgSender(), Role.RELAYER);
    if ((players & type(uint32).max) == ((players >> 32) & type(uint32).max)) revert SelfDuel();
    // Reject a zero commitment up front — it binds the id to a secret no one can ever reveal,
    // leaving the duel unresolvable on both the normal and force paths.
    if (secretHash == bytes32(0)) revert LibCommit.ZeroCoSeedCommit();

    EnrollArgs memory args = EnrollArgs(players, specs, deadlines, maxGasPrices, bracket);
    DuelSigners memory signers = _verifyBothIntents(args, sigA, sigB);

    // effects
    duelId = _createDuel(args, secretHash);

    // interactions last
    _settle(signers, args, _msgSender());
  }

  /// @dev Recover both signers in a separate stack frame — inlining hits stack-too-deep.
  function _verifyBothIntents(
    EnrollArgs memory args,
    bytes calldata sigA,
    bytes calldata sigB
  ) internal view returns (DuelSigners memory signers) {
    bytes32 sep = LibDuel.buildDomainSeparator(); // C2: build the EIP-712 separator once for both signers
    signers.signerA = LibDuel.verifyIntent(
      uint32(args.players),
      uint32(args.specs),
      uint128(args.deadlines),
      uint128(args.maxGasPrices),
      sigA,
      args.bracket,
      sep
    );
    signers.signerB = LibDuel.verifyIntent(
      uint32(args.players >> 32),
      uint32(args.specs >> 32),
      args.deadlines >> 128,
      args.maxGasPrices >> 128,
      sigB,
      args.bracket,
      sep
    );
  }

  /// @dev Create the duel row and return its ID. Re-reads taruchiID — warm SLOADs
  ///      after verifyIntent already touched taruchi rows. enrollBlock from LibDuel.create
  ///      is dropped here — the caller doesn't need it; matchmaker captures from receipt.
  function _createDuel(EnrollArgs memory args, bytes32 secretHash) internal returns (uint256 duelId) {
    uint32 playerAIndex = uint32(args.players);
    uint32 playerBIndex = uint32(args.players >> 32);
    uint256 taruchiA = Taruchi.getByIndex(playerAIndex);
    uint256 taruchiB = Taruchi.getByIndex(playerBIndex);
    (duelId, ) = LibDuel.create(
      playerAIndex,
      playerBIndex,
      args.bracket,
      uint32(args.specs),
      uint32(args.specs >> 32),
      taruchiA,
      taruchiB,
      args.maxGasPrices,
      secretHash
    );
  }

  /// @dev Tail-end interactions: WETH gas reimbursement only. Duels are free — no ONYX entry fee.
  function _settle(DuelSigners memory signers, EnrollArgs memory args, address relayer) internal {
    LibWeth.pullWethForGas(
      signers.signerA,
      LibWeth.WETH_GAS_EST_DUEL_ENROLL_PER_USER,
      uint128(args.maxGasPrices),
      relayer
    );
    LibWeth.pullWethForGas(
      signers.signerB,
      LibWeth.WETH_GAS_EST_DUEL_ENROLL_PER_USER,
      args.maxGasPrices >> 128,
      relayer
    );
  }
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "optimizer": {
    "runs": 3000,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "forge-std/=node_modules/forge-std/src/",
    "@latticexyz/=node_modules/@latticexyz/",
    "@openzeppelin/=node_modules/@openzeppelin/",
    "solady/=node_modules/solady/src/",
    "libraries/=src/namespaces/app/libraries/",
    "tables/=src/namespaces/app/codegen/tables/",
    "common/=src/codegen/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.