Practical DeFi Guides

The math behind the trade.

Each guide explains a key DeFi concept, the formula that governs it, and how to use OrnyTools to get the right answer quickly.

Contents
Guide 01

Optimal trade size on Uniswap

Every AMM trade has two costs: a fixed gas fee and a variable slippage. Trade too small and gas dominates. Trade too large and slippage does. The optimal size sits exactly at the crossover.

Why this matters

On Ethereum mainnet a typical Uniswap V3 swap costs ~$3–8 in gas. On Arbitrum it's less than $0.05. If you trade $50 on Ethereum and pay $5 in gas, that's a 10% overhead before slippage. The optimal size is the point where gas cost = slippage cost.

The formula

For a constant-product pool (x·y=k), the total cost as a fraction of trade size A is:

cost(A) = gas/A + A/(R+A) + fee

Where R = pool TVL × 0.5 (single-sided depth), gas = network fee in USD, fee = pool fee. Minimising gives the exact optimal A:

A* = √(gas × R)

The √(2·gas·TVL) approximation is commonly cited but overestimates optimal size by up to 2× — OrnyTools uses the exact constant-product derivation, not the shortcut. est souvent citée mais surestime la taille optimale jusqu'à 2× — OrnyTools utilise la dérivation exacte à produit constant, pas le raccourci. se cita frecuentemente pero sobreestima el tamaño óptimo hasta 2× — OrnyTools usa la derivación exacta de producto constante, no el atajo.

Rule of thumb: At the optimum, your gas cost and slippage cost are approximately equal. If you see one dominating the other in the result breakdown, you're away from the optimum.

Practical steps

1. Load the target pool by address (or pick from the curated list). The optimizer fetches TVL and fee live from GeckoTerminal. 2. Check the gas estimate — the optimizer auto-fills based on the selected network. 3. The result gives you the optimal size, the expected slippage at that size, and the total cost breakdown.

Open Trade Size Optimizer →
Guide 02

Aave V3 health factor — what it means and when to act

The health factor is Aave's single number summary of how close your position is to liquidation. Below 1.0, a liquidator can take your collateral.

The formula

Aave computes health factor as:

HF = Σ(collateral_i × price_i × LT_i) / Σ(debt_j × price_j)

Where LT is the liquidation threshold per asset (e.g., WBTC = 75%, USDC = 85%). When HF drops to 1.0, your weighted collateral exactly equals your debt — any further drop and a liquidator takes over.

Estimated liquidation price

If you have a single collateral and single debt position, you can estimate the price level at which liquidation triggers:

P_liq = debt_USD / (collateral_qty × LT)

This assumes all collateral value moves together. Real portfolios with mixed assets are more complex — use the Dashboard to see the on-chain estimate directly from the Aave V3 contract.

HF < 1.2 is critical. The gap between 1.2 and 1.0 can be covered by a 15–20% price drop depending on your LT. Add collateral or reduce debt before a volatile move.
Safe zone: HF > 1.5 with volatile assets (BTC, ETH), HF > 1.3 with stablecoins. Lower than these and a single bad wick can trigger liquidation.

The Position Dashboard reads your HF, collateral, debt, and margin-to-liquidation directly from the Aave V3 contract — no wallet connection needed.

Check my position →
Guide 03

GMX liquidation price — how to calculate it

On GMX, leveraged positions get liquidated when collateral drops to the maintenance margin. The exact liquidation price depends on opening fees, borrow fees, and leverage.

The formula (long position)

P_liq = P_entry × (1 − (collateral − fees − MAINT × size) / size)

Where fees = opening fee (0.07% × 2 × size) + accrued borrow fees, and MAINT = 1% of position size. The higher your leverage and the longer you hold, the closer your liquidation price moves toward entry.

GMX V2 fee structure

Opening fee: 0.07% of position size, charged each way (open + close = 0.14% round-trip). Borrow fee: approximately 0.01%/hour on the borrowed portion. For a 5× leveraged $1,000 trade, borrowed = $4,000 → borrow fee ≈ $0.40/hour.

Break-even distance = (collateral − opening fees) / size. This is the minimum favorable move needed just to recover your fees. Many traders focus on liquidation price but ignore break-even — both matter.

The GMX V2 Calculator lets you input size, leverage, and collateral, fetches the live BTC or ETH price, and outputs liquidation price, break-even, opening fees, and max borrow fees — all in one view.

Open GMX Calculator →
Guide 04

Uniswap V3 range selection — concentrated liquidity explained

In Uniswap V3, you earn fees only while the price stays within your chosen range. Narrower range = higher yield per dollar, but more frequent out-of-range periods.

Capital efficiency formula

The capital efficiency multiplier compared to full-range V2 liquidity is:

CE = 1 / (1 − √(P_lower / P_upper))

A range of ±20% gives CE ≈ 5.5× — you need 5.5× less capital to provide the same depth as a V2 position. Narrower to ±10% gives CE ≈ 11×, but any wick beyond those bounds earns nothing.

Key tradeoff: Narrow ranges maximize fee yield when in range, but require active management (repositioning) as the price moves. Wider ranges set-and-forget but dilute your fee rate.

The V3 Range Optimizer takes a pool, your capital, and a target range and shows you projected daily fees, capital efficiency, and time-out-of-range estimate based on recent volatility.

Open V3 Range Optimizer →
Guide 05

Impermanent loss — when does it become permanent?

Impermanent loss (IL) is the opportunity cost of holding assets in an AMM pool vs. holding them in a wallet. It's "impermanent" only if the price returns to entry.

The formula

For a 50/50 pool with a price ratio of r (current price / entry price):

IL = 2√r / (1+r) − 1

At 2× price move (r=2): IL ≈ −5.7%. At 5× (r=5): IL ≈ −25.1%. The loss is symmetric — a 5× price move up or down gives the same IL because the pool rebalances both ways.

IL becomes permanent when you exit the position while the ratio is off-peg. If fees earned over the period exceed the IL, you're net positive — but the loss is real and realized the moment you withdraw.

Enter your entry price ratio and see IL across a range of scenarios. The IL Calculator also shows what fee yield you'd need to break even at each price level.

Open IL Calculator →