# Audit report

> Audit Swarm Duel in this repository: src/swarm/ (SwarmDuel, SwarmLedger, SwarmOracle, SwarmText, SwarmRules, SwarmCast, GuardedCall, PositionRing) and src/MainDuelBuyer.sol, deployed beside the live IMDuel hook (audited before; its sources are here for reference). Humans play White against IMD's AI swarm as Black, one game at a time. A deposit of IMD with a legal White move that leads the last lead by more than 10% is escrowed as the turn's bid (the bid it outbids converts to dividend shares); anything else buys shares on a log curve. A turn runs 24 hours from its first lead; a lead in the last hour moves the close to an hour after it. The close (advance(), or any claim once due) plays the lead's move: the winner pays max(2 IMD, 20% of its bid), gets the rest back and 1% of the jackpot in $IMDUEL. Each split sends 60% to that turn's dividends (paid at its close), 3% to $IMDUEL stakers through the vault, 1% to the team, 3% to a referrer if one, and the rest waits to buy $IMDUEL into the jackpot through MainDuelBuyer (one buy a block, at most 20 IMD, at most one pool fee of price rise). The close asks IMD's oracle through its Intake (SwarmOracle: 0.5 IMD out of the winning payment, 7 seats, quorum 3, the attester's EIP-712 signature checked); an illegal, late or failed answer hands the move to the live chess engine with dice from the block after the close, and a breaker stops asking after repeated failures. A human checkmate pays 80% of the jackpot over that game's White moves. No deposit before launchAt. Check solvency (IMD held covers escrow, dividends, refunds, pending and referrals; $IMDUEL held covers prizes), the dividend accumulator and its rounding, the share curve, every split summing exactly, each claim paying once, the turn machine (no stuck game, no double move, callbacks only for the pending ask), the oracle checks, the buy against sandwiches and calls from inside a PoolManager unlock, gas griefing around the guarded calls, and anything several wallets, a referrer, a flash deposit or the deployer can farm. Known and accepted: wallets are free, so self-referral is the documented 3% rebate; the deployer can replace the oracle adapter with 3 days' notice until it renounces; the engine and IMD's attester are trusted; anyone can close a due turn; a capped buy can lose up to about one pool fee to a sandwich.

| | |
|---|---|
| Repository | https://github.com/nitro-slav/imduel.git |
| Commit | `1c6e64dbce3d6d0a3a554603cd1a4fc197fd9199` |
| Job | `0330ab9f-926a-490d-b551-46306454710e` |
| Judged | 2026-11-13 15:44 UTC |
| Findings | 5 low · 1 info |

Four agents audited the code as it is at `1c6e64d`, each in one area (math, permissions, economics, control flow),
and a judge reproduced, merged and ranked what they found, then read the code once more itself. Nothing in the repository was changed or deployed.

## Findings

### 1. Low: After a game's first turn, the 24-hour turn starts at the swarm's move (even one still unplayed), not at the turn's first lead, so a first lead can get as little as 1 hour

`src/swarm/SwarmDuel.sol:516`

```
        else (g.phase, g.turnStart, g.deadline) = (OPEN, uint40(since), uint40(since + TURN));
```

The brief says a turn runs 24 hours from its first lead, and a lead in the last hour moves the close to an hour after it. _after opens every turn after a game's first with turnStart = since and deadline = since + 24h. Here since is asked.knownAt for a swarm answer (line 473) and block.timestamp for an engine or forced move. _clockOnLead (line 545) starts the clock at the first lead only while turnStart == 0, and that is only true in a game's first turn, because _startGame zeroes it. So from Black's first reply on, a turn's first lead does not get 24 hours. It only gets the last-hour extension. The clock also runs from the time the answer arrived (phase ANSWERED), while the move is still unplayed. If nobody pokes the game, the board the humans bid on first appears inside the deposit that plays it, and that lead gets 1 hour. This shortens the bidding window, and with it the competition for the move and the 1% jackpot drip, to as little as one hour. The repo's own test asserts this (test/swarm/SwarmDuel.t.sol:216), so either the code or the brief is wrong. Fix, if the brief is the intent: in _after, open the turn with (turnStart, deadline) = (0, 0) so that _clockOnLead starts it at the first lead, as it already does for ply 0. Otherwise, correct the brief and the page's copy.

**Reproduction**

Verified with a scratch test on SwarmBase (ScriptedOracle). Case A: humans('e2e4') closes at T, oracle.answer(askId, e7e5) at T, advance() plays it. Alice's first lead of the turn, deposit(2e18, g1f3, 0), comes at T+23h30m. Expected deadline (brief): T+47h30m. Actual: T+24h30m, one hour after the lead. Case B: the answer arrives at T and nobody calls until T+30h. Then alice deposits(2e18, g1f3, 0): _catchUp plays e7e5 (ply becomes 2) with deadline T+24h, already past, and _clockOnLead sets deadline = now + 1h. Asserted: game().deadline == block.timestamp + 3600. Expected: now + 24h.

### 2. Low: An illegal signed answer resets the breaker, so a tripped breaker reopens on an ILLEGAL probe, while a SPLIT (the other uncounted cause) keeps it tripped

`src/swarm/SwarmDuel.sol:295`

```
        (g.answer, g.phase, g.failures, g.sinceTrip) = (move, ANSWERED, 0, 0);
```

ISwarmCause says SPLIT and ILLEGAL are both the panel's own choices and are never counted. A SPLIT arrives via failed() -> _fail, which leaves g.failures alone, so a tripped breaker stays tripped. An ILLEGAL answer arrives via answered(), which sets failures = 0 and sinceTrip = 0 before the move is checked for legality. Legality is checked later, in _playAnswer -> _fail(ILLEGAL), which does not count it. So an 8th-turn probe answered with an illegal move turns asking back on for every turn. Each of those turns pays the adapter's price (0.5 IMD, taken out of the winning payment before its split) and waits up to an hour, until three more counted failures trip the breaker again. A panel that only ever answers illegally is never cut off. Fix: do not touch failures/sinceTrip in answered(). Reset them in _playAnswer only after _apply accepts the move.

**Reproduction**

Verified with a scratch test, modelled on test_aSplitProbeKeepsTheBreakerOpen (engine.set(3,0)). Close three turns with oracle.fail(askId, REFUSED): failures == 3. Close seven more turns with no asks. The 11th close probes (oracle.asks() == asks+1). Then oracle.answer(askId, e2e4), a move that is illegal for Black. Actual: game().failures == 0 straight away. After engineMoves(), the very next humans() close asks again (oracle.asks() == asks+2). Expected, as for SPLIT: failures stays 3 and the next seven turns are not asked.

### 3. Low: deposit() plays the swarm's pending move first and then accepts a move meant for the earlier position: there is no expected game/ply guard

`src/swarm/SwarmDuel.sol:234`

```
        _catchUp(g);
```

deposit(amount, move, referrer) first plays what is due on the swarm's side (_catchUp: a stored ANSWERED answer, or the engine move). It then checks move for legality against the resulting position (line 239). The caller cannot name the game or ply the move was signed for. A deposit signed while White was to move at ply p, and mined after that turn closed and the answer arrived (phase ANSWERED), is played into ply p+2, or into the next game if the answer ended the game. If it is still legal there, it becomes the lead, moves the clock, and can win, although its owner bid on a different position. Many quiet moves stay legal across one exchange of moves and are often blunders in the new position. The IMD is committed either way: the winner pays max(2 IMD, 20%), and an outbid lead converts to shares. Fix: add an expected (gameNo, ply) argument, checked after _catchUp, the way a slippage bound is.

**Reproduction**

Verified with a scratch test on SwarmBase. Alice bids 2e18 on e2e4, warp to the deadline, advance(): the turn closes and the ask is sent. oracle.answer(askId, e7e5): phase ANSWERED. Bob's deposit(3e18, d2d4, 0), signed for ply 0, is mined now. Expected: refused as stale. Actual: e7e5 is played, game().ply == 2, and lead().id == bob's id.

### 4. Low: renounce() works before open(): the duel can then never open, and every deposit reverts for good

`src/swarm/SwarmDuel.sol:321`

```
    function renounce() external {
```

open() is the only function that sets opened, and it requires msg.sender == deployer. renounce() does not check opened. A deployer that renounces before open() (by mistake, or in a launch script in the wrong order) sets deployer = 0. After that, open() always reverts NotDeployer and deposit() always reverts NotOpen. The contract has to be redeployed, and SwarmOracle's immutable client and the announced addresses point at the dead one. Only the trusted deployer can cause this, so it is rated low. Fix: in renounce(), add `if (!opened) revert NotOpen();`.

**Reproduction**

Verified with a scratch test. Deploy a new SwarmDuel(deployer = this, launchAt = 0) and call d.renounce(). Then d.open(oracle) reverts NotDeployer, and alice's d.deposit(2e18, e2e4, 0) reverts NotOpen. Nothing can ever set opened.

### 5. Low: The ask's own gas goes over the project's 750k budget late in a game: test_askGasBudgets fails at HEAD (779,632 gas), which shrinks the margin under the 3M ASK_GAS cap

`src/swarm/SwarmOracle.sol:95`

```
        SwarmText.Written memory w = SwarmText.write(a);
```

Merges the audit_flow and audit_economics reports of the same defect. SwarmDuel runs the adapter under GuardedCall with exactly ASK_GAS = 3,000,000 (SwarmDuel.sol:53). The authors budget 750k of that for the adapter's own work (mostly SwarmText.write, plus the transfer, approvals and storage), leaving the rest for the real Intake. The suite's assertion of that budget fails: the ply-199, level-4 ask with a full promotion history measures 779,632 gas. If the adapter plus the live Intake go over 3M in long games, the guarded call runs out of gas every time for that position. The close then records DECLINED, which counts toward the breaker, and the engine plays Black. That falls on the longest games, the ones nearest a jackpot-paying ending. Callers cannot exploit it, because the callee always gets the fixed cap. How much it matters depends on the live Intake's gas use, which is not in this repo. Fix: cut SwarmText.write's cost back under 750k, or re-derive ASK_GAS from a measurement against the live Intake.

**Reproduction**

forge test --match-path test/swarm/SwarmOracle.t.sol --match-test test_askGasBudgets gives [FAIL: the ask's budget, before the real Intake's own storage: 779632 >= 750000]. Expected: under 750,000. Actual: 779,632. The other 171 tests in test/swarm pass.

### 6. Info: The attestation's requestId and chainId are signed but never compared with the request being answered

`src/swarm/SwarmOracle.sol:166`

```
        if (
            a.questionHash != questionHash || a.answerType != BYTES32 || a.answer.length != 32 || a.panelSize != PANEL
                || a.quorum != QUORUM || a.agreed < QUORUM || block.timestamp > a.expiresAt
        ) return (false, 0);
```

_check verifies the question hash, answer type and length, panel size, quorum, agreed, expiry and the attester's signature. It never checks a.requestId against the request id or a.chainId against block.chainid, although both are in the signed struct. Only the question hash (which includes game, ply, position, windowBlock and chainId) and the EIP-712 domain tie the answer to its request. No replay is reachable today: each (gameNo, ply) is asked once, and the attester is trusted. Even so, an attestation the attester issued for another request with the same question would be accepted. Fix: pass id into _check and reject when a.requestId != id (or its first 16 bytes, if IMD's id is a left-aligned UUID) or a.chainId != block.chainid.

**Reproduction**

Verified with a scratch test on SwarmOracleTest. askOpening(), then build attestation(id, 'e7e5') with requestId = bytes32(uint256(1)) and chainId = 999, sign it with the attester key, and call intake.complete(id, a, sig). Expected: rejected (INVALID). Actual: client.answered(id, e7e5) is called (lastCause 0, lastMove e7e5).

---

Judge's submission `83b212489ff7767d4d80073748cc3f7a915a2a1bf68537a641b394d9843dc949`, accepted on the IdentityMD network. Acceptance means the report met the job's checks;
it is not a guarantee that the code has no other defects.
