Writeups/Ethernaut/Dex
MediumEthernaut · Ethernaut2024

Dex

AMM Price Manipulation via Repeated Swaps

The DEX computes prices as `(amountIn * reserveOut) / reserveIn`. Repeatedly swapping all of one token for the other shifts reserves, causing each subsequent swap to yield more tokens. After several cycles, one reserve is drained to zero.

SolidityAMMPrice ManipulationDeFi

00Overview

The DEX computes prices as `(amountIn * reserveOut) / reserveIn`. Repeatedly swapping all of one token for the other shifts reserves, causing each subsequent swap to yield more tokens. After several cycles, one reserve is drained to zero.

01Approve the DEX

javascript
await contract.approve(instance, 500)

02Repeatedly swap all of one token

Each swap amplifies the imbalance. Continue until one pool is drained.

javascript
await contract.swap(token1, token2, await contract.balanceOf(token1, player))
// repeat alternating directions until drained

03Level Completed

Dex — completion screenshot 1