Reproduced Exploit

Peapods: the swap amount reads V2 reserves backwards

_getSwapAmt takes getReserves() and treats slot 0 as _t0. Uniswap V2 returns reserves in ascending token address order. Here _t0 is the higher address, whose reserve is 10 tokens, and slot 0 is the lower token's reserve of 1,000,000. The 0.3 percent zap formula therefore sells 500.63 of the scarce…

Jan 2025•Ethereum•logic•3 min read

Loss

500.63 scarce tokens dumped into a 10 token reserve. The sorted swap dumps 90.63. Extra 409.99 tokens sit in…

Chain

Ethereum

Category

logic

Date

Jan 2025

Source

AuditVault

EVM Playground

Source-level debugger — step opcodes and Solidity in sync

evm-hack-analyzer

The attack is replayed in an in-browser EVM preloaded with the exact dumped fork state. The execution tree shows every call; step by Solidity line or by opcode across all depths — source, Stack, Memory, Storage, Balances (native / ERC-20 / NFT), Transient storage and Return value stay in sync. Click a tree node, opcode, or source line to jump. Query opens a search over the trace: previous and next writes, balance changes and calls, breakpoints, and a diff of everything that changed between two steps. No backend, no live RPC.

Loading fork state…

Source & credit. Reproduction of a public audit finding curated by AuditVault — the original finding: 52752-h-7-incorrect-reserve-mapping-leads-to-fault-swap-sherlock-p. Standalone Foundry PoC and full write-up: 52752-h-7-incorrect-reserve-mapping-leads-to-fault-swap-sherlock-p_exp in the evm-hack-registry mirror.


Vulnerability classes: vuln/logic/price-calculation · vuln/defi/slippage

Key info#

FieldValue
Loss500.63 scarce tokens dumped into a 10 token reserve. The sorted swap dumps 90.63. Extra 409.99 tokens sit in the pair
Vulnerable contractPod._getSwapAmt
Attacker EOA0x1111111111111111111111111111111111111111
Attack contractExploit
Attack txExploit.run()
Chain / block / dateEthereum model · block 0 · 2025-01
Compilersolc 0.8.27, cancun, via IR
Bug classReserve slot 0 treated as the caller's token0

TL;DR#

_getSwapAmt takes getReserves() and treats slot 0 as _t0. Uniswap V2 returns reserves in ascending token address order. Here _t0 is the higher address, whose reserve is 10 tokens, and slot 0 is the lower token's reserve of 1,000,000. The 0.3 percent zap formula therefore sells 500.63 of the scarce token instead of 90.63. The pair's balance of that token becomes 10 plus 500.63.

Background#

Sherlock's January 2025 Peapods contest, issue 512, reported by pkqs90 and others. AutoCompoundingPodLp._getSwapAmt is the blamed function. The recommended fix sorts the two tokens and swaps the reserve tuple when _t0 is not token0.

The vulnerable code#

SOLIDITY
(uint112 _r0, uint112 _r1,) = pair.getReserves();
uint112 _r = _swapT == _t0 ? _r0 : _r1;
return (_sqrt(uint256(_r) * (_fullAmt * 3988000 + uint256(_r) * 3988009)) - (uint256(_r) * 1997)) / 1994; // @> VULN

The fix is the report's sort:

SOLIDITY
(address token0,) = sortTokens(_t0, _t1);
(_r0, _r1) = _t0 == token0 ? (_r0, _r1) : (_r1, _r0);

Root cause#

getReserves is ordered by address. The zap formula is applied to whichever reserve happens to sit in the slot the caller hoped belonged to _swapT.

Preconditions#

  • The two token addresses are not equal. _t0 is the higher one.
  • The pair holds 1,000,000 of the lower token and 10 of the higher token.
  • The pod holds 1000 of the higher token and calls _getSwapAmt to compound it.

Attack walkthrough#

  1. compound asks for a swap of the high token against the low token, passing the high token as _t0.
  2. getReserves returns the low reserve in slot 0.
  3. Because _swapT == _t0, the formula uses slot 0. The amount is 500626001745396739492 wei.
  4. The reserve-sorted amount is 90634800368085047639 wei. The difference is 409991201377311691853 wei.
  5. The pod transfers the buggy amount. The pair's high balance is 10 ether plus that amount.

Diagrams#

flowchart TD A["Pod compounds the higher-address token"] --> B["getReserves returns the lower token in slot 0"] B --> C["VULN the formula treats slot 0 as the scarce reserve"] C --> D["Swap size is 500.63 instead of 90.63"] D --> E["409.99 extra scarce tokens are sold"] E --> F["The pair holds 10 plus the whole bad swap"]

Impact#

The pod sells 500.63 units of a token whose pool reserve is 10. The fair zap sells 90.63. The extra 409.99 units are in the pair, not in the pod, and the pool price is set by that oversized sell. Users whose compound was supposed to add liquidity at the pool ratio are left with the wrong inventory.

How to reproduce#

BASH
cd 52752-h-7-incorrect-reserve-mapping-leads-to-fault-swap-sherlock-p_exp
forge test -vv

test_exploit_wrong_reserve_dumps_scarce_token logs the buggy amount, the fair amount, and the extra, and asserts the pair absorbed the bad swap. Output is output.txt.

Sources#


Sources & further analysis#

Reproductions & code

Alerts & third-party analyses

  • Web3Sec X hacked database: search.
  • Rekt leaderboard: search.
  • Solodit incident search: search.

These dashboards index community alerts tweets, post-mortems, and independent write-ups. Reach them through the protocol name above to cross-check this reproduction against other analyses.