← Writing

Perpetual futures security Solana & Rust Audit methodology

Perp DEX Security Audit: The Trade Worked. Did the Books?

A practical guide to auditing perpetual futures by following one position through orders, collateral, funding, margin, liquidation, and settlement.

Research at a glance

A perp DEX is one connected accounting system. A trade can match correctly and still leave a risk or settlement problem somewhere downstream. This guide shows how I trace that full path in Rust/Solana and other on-chain designs.

Focus
Perpetual futures · Cross-margin · Funding · Liquidation · Solana/Rust
Method
Map the lifecycle, write down the accounting invariants, trace every caller, and validate impact before reporting.

A perpetual futures trade looks simple from the outside: deposit collateral, open a long or short, then close it later. But the protocol has to keep that position correct while prices move, funding accrues, orders rest, fees are charged, and liquidators or keepers act.

That is why I audit a perp DEX as a lifecycle, not as a list of functions. The same economic position can pass through several components, and each handoff is a chance for the system's view of the trader to drift from what the trader is actually owed.

This is a general field guide drawn from perp and on-chain security reviews. It contains no client-specific code, findings, parameters, or private transaction sequences.

Start with one position

Pick a basic example: Alice deposits collateral and opens a long. Then follow that position through the ordinary and stressful paths:

  1. She places an order. What checks her available margin?
  2. The order fills. Where are size, entry price, and fees recorded?
  3. Time passes. How does funding change what she owes or receives?
  4. The price moves. Which price determines her account health?
  5. She closes, gets liquidated, or is reduced by a backstop or auto-deleveraging process. How are all remaining amounts settled?

Write the state down at each step. If the position changes from a long to a short, is the old side fully closed before the new side opens? If only part closes, does the remaining part keep the correct entry price and funding history? If an operation fails, does the whole transaction roll back?

Follow every dollar

For each user action, track the balance equation. A simplified account might look like this:

A useful accounting sketch
ending equity = starting collateral
  + realized and recognized PnL
  - fees
  + or - funding
  - debt and other obligations

The exact formula differs by protocol. The audit task is to find the real formula in code and check that every term has the intended sign, units, rounding, and timing.

Then test the same operation from both directions: deposit and withdraw, borrow and repay, open and reduce, long and short, maker and taker. Similar paths often have different callers and checks. A control that exists on one path does not prove it exists on the other.

Audit question: Can the same value be counted twice, disappear during a transfer, or remain spendable after it has already been used to support another action?

A price is part of the policy

Perp systems may use an oracle price, an index price, a market mark, or a more conservative price for a particular risk check. These names are not interchangeable. The important question is what each price is allowed to decide.

Trace the price from its source to each action: opening a position, withdrawing collateral, calculating funding, liquidating, or settling a loss. Check freshness, confidence, fallback rules, and how the protocol behaves when a feed is delayed or unavailable.

Then compare the implementation with the written design. If two documents describe different price rules, treat that as a question to resolve—not proof of a vulnerability. A confirmed security issue needs a reachable path and a concrete consequence, such as an unsafe withdrawal or an incorrect liquidation.

Funding changes with time

Funding is a transfer between long and short traders that helps keep a perpetual market near its reference price. Many protocols update a market-wide funding index and apply each trader's share when that position is next touched.

This means time is part of the accounting. Check what happens when updates are late, skipped, repeated, or made just before a position changes size. The key invariant is that a trader should receive or pay the funding for the time and position they actually held—not for a different size or interval created by update ordering.

Also distinguish the sampled rate from the rate ultimately applied. Smoothing, caps, rounding, and elapsed-time weights may all affect the result. Test transitions at boundaries: zero rate, a sign change, a long delay, a partial position reduction, and a configuration change.

Liquidation is a system

Liquidation is not just “close the risky position.” A protocol must decide when an account is unhealthy, how much to close, what reward a liquidator earns, where the loss goes, and what happens if normal settlement cannot collect the full amount.

Follow each stage: health check, amount calculation, matching or transfer, fee and reward accounting, post-action health, and any backstop or socialized-loss path. Check that ordinary users can still reduce risk, that partial actions make progress, and that failed attempts do not leave partially changed accounting.

For auto-deleveraging, ask who can be selected, how eligibility is measured, what price is used, and whether a forced close preserves funding and PnL already accumulated. Model both sides of the transfer: the distressed account and the profitable counterparty.

Solana and Rust boundaries

The economic rules matter on every chain, but Solana and Rust add their own boundaries to inspect:

For a deeper Solana-specific starting point, see my guide to auditing a Solana staking protocol. For perp-specific comparisons across execution environments, see the Aptos and Sui guides.

Prove the issue before reporting

A suspicious line is a lead, not a finding. Trace a real entry point to the code that changes state, then show the preconditions an ordinary actor can satisfy. A useful proof of concept demonstrates the state before and after, keeps the protocol's normal checks enabled, and includes a control case.

Separate what you observed from what you infer. If a private dependency prevents a test from running, say so. If a behavior may be intentional, ask the team to clarify the design before calling it a bug. This keeps the report accurate and makes the real impact easier to assess.

Severity should follow the demonstrated loss or disruption and how realistically the required conditions can occur. A large theoretical number is not enough if the configuration is unsupported or the action cannot be reached.

A compact perp audit checklist

Perp DEX security comes down to keeping promises consistent: the price the protocol trusts, the balance it records, the risk it permits, and the amount it can actually settle. I start with one trade, follow every consequence, and keep following until the books balance.

Common questions

What should a perpetual futures audit cover?

At minimum: order entry and matching, oracle and mark prices, margin, PnL, fees, funding, liquidation, settlement, and recovery paths. The connections between these components matter as much as each component alone.

Why trace a position instead of reviewing one function at a time?

A position's accounting changes across many functions and sometimes across transactions. Lifecycle tracing reveals when one component's assumptions differ from another's.

What makes a perp audit on Solana different?

The same market risks apply, with additional attention to account validation, CPIs, transaction atomicity, fixed-point arithmetic, and compute limits in Rust/Solana programs.

Does an unusual behavior automatically mean a vulnerability?

No. Confirm the intended design, prove that an actor can reach the behavior, and show a concrete security impact before reporting it.