DocsPools
On this page

Pools

Pools is a small exchange for your layer, deployed in one signature. Anyone can open a pool for any ERC-20 token on the layer, against HYPE or against another token, add and remove liquidity, and swap. Liquidity providers hold ERC-20 shares of each pool, and every swap leaves 0.30% of its input in the pool for them. The contract has no owner, no fee switch and no admin function: only share holders take liquidity out.

It is the Uniswap v2 design, rewritten in Solidity 0.8.28: constant-product pools, a factory that creates them, and a router with slippage limits and deadlines, in one contract.

#What one deployment contains

Contract Created by What it does
Pools Your wallet, in the deploy transaction The factory and router: opens pools, adds and removes liquidity, swaps, quotes. It holds nothing between transactions.
WrappedHype Pools, in its constructor HYPE as an ERC-20, one for one, backed by the HYPE it holds. Each Pools app has its own.
Pool Pools, one per pair, with CREATE2 Holds the two tokens of one pair and is the ERC-20 of its shares (name "Pools LP share", symbol POOLS-LP, 18 decimals).

The deploy transaction takes about 4.6 million gas, because the pool code and the Wrapped HYPE code travel inside it. The Builder asks the layer for an estimate and sends at least 6,000,000 as the gas limit; unused gas is not charged.

#Parameters

Field in the Builder Constructor argument Notes
App name (none) The name of the listing. The contract takes no arguments.

#HYPE

HYPE is wrapped inside the app. In every token argument of Pools, the zero address 0x0000000000000000000000000000000000000000 (the constant HYPE()) stands for native HYPE:

  • going in, Pools wraps the HYPE you send into its WrappedHype and moves it to the pool;
  • coming out, it unwraps and sends you HYPE.

A HYPE pool is therefore a pool of a token and WrappedHype, and whype() returns that token's address. You never need to hold Wrapped HYPE yourself, though you can: deposit() with HYPE as the value and withdraw(amount) are open to anyone, and WrappedHype always holds exactly as much HYPE as its total supply.

#Opening a pool

addLiquidity opens the pool when it does not exist yet, so opening a pool and its opening deposit are one transaction. createPool(tokenA, tokenB) opens an empty one without a deposit. Both refuse two identical tokens (IdenticalTokens), an address with no contract code (NotAToken) and a pair that exists already (PoolExists, for createPool).

The opening deposit sets the price: depositing 30,000 of a token and 0.0003 HYPE prices one token at 0.00000001 HYPE. It mints sqrt(amountA × amountB) shares, of which 1,000 (MINIMUM_LIQUIDITY) go to the burn address 0x...dEaD and can never be redeemed. That keeps every pool from being emptied completely, and makes a deposit too small to cover them revert with InsufficientSharesMinted.

Each pool is a CREATE2 address derived from the Pools address and the two sorted tokens. getPool(tokenA, tokenB) and poolFor(tokenA, tokenB) (which accepts the zero address for HYPE) return it in either order.

#Adding liquidity

solidity
addLiquidity(tokenA, tokenB, amountADesired, amountBDesired, amountAMin, amountBMin, to, deadline)
  • When one side is HYPE, send exactly its desired amount as the value (BadValue otherwise). Whatever the pool does not take comes back to you in the same transaction.
  • On a pool with liquidity, the amounts follow the pool's ratio: the contract keeps your full amount of one side and computes the other from the reserves, rounded down, so the pool never receives less than its ratio. It reverts with Slippage if either amount would fall below its minimum.
  • Approve Pools for the token side beforehand. The app page asks your wallet for the exact amount when the approval is missing.
  • You receive shares in proportion to what you add, rounded down: min(amountA × supply / reserveA, amountB × supply / reserveB).

#Removing liquidity

solidity
removeLiquidity(tokenA, tokenB, shares, amountAMin, amountBMin, to, deadline)

Approve Pools for the shares beforehand (the shares are the pool's own ERC-20). The pool burns them and pays out your part of both balances, shares × balance / supply, rounded down. HYPE comes back as HYPE. The call reverts with Slippage below either minimum. There is no lock and no exit fee: shares are redeemable at any time, by whoever holds them.

#Swapping

solidity
swapExactIn(path, amountIn, minOut, to, deadline)
  • path lists two to four tokens (at most three hops); the zero address at either end is HYPE. When the path starts with HYPE, send exactly amountIn as the value.
  • Each hop pays out amountIn × 9970 × reserveOut / (reserveIn × 10000 + amountIn × 9970), rounded down. The 0.30% (FEE_BPS = 30, a constant) stays in the pool.
  • The call reverts with Slippage if what reaches to is below minOut, and with Expired after deadline.
  • Each pool checks, after paying out, that its balances net of 0.30% of what came in still hold the product of its reserves. A swap that asks for even one unit more than this formula reverts with K. Rounding is always paid by the trader, never by the pool.

The app page routes a swap through one pool when the pair exists, and through HYPE (two pools) when both tokens have a HYPE pool.

#Quotes

Function Returns
quoteOut(path, amountIn) What swapExactIn would pay out at the current reserves.
quoteIn(path, amountOut) The input that pays out at least amountOut, rounded up.
quoteAdd(tokenA, tokenB, amountA) The matching amount of tokenB and the shares it would mint; both zero for a pool with no liquidity yet.
quoteRemove(tokenA, tokenB, shares) What burning shares pays out now.
getAmountOut(amountIn, reserveIn, reserveOut), getAmountIn(...) One hop, for your own routing.
listPools(start, count, user) A page of pools with tokens, reserves, share supply and user's shares.

#The page

The app page lists every pool of the app with its reserves, its price and your position (your share of the pool and what it is worth in both tokens), read from the layer every few seconds. Four tabs act on the selected pool:

  • Swap: pick the two tokens and an amount, see the route, the quote and the minimum after slippage, and swap.
  • Add liquidity: type either side and the other fills in at the pool's ratio; the page shows the shares you receive.
  • Remove: 25, 50, 75 or 100% of your shares, with what comes back.
  • New pool: paste a token address, pair it with HYPE or another token, and set the opening price with your two amounts.

The slippage limit is 1% unless you pick 0.5% or 3%, and every transaction carries a deadline 20 minutes ahead. Before your wallet opens, the page simulates the call, so a deposit that is too small or a price that moved shows as a sentence instead of a failed transaction. Recent swaps through the app, from the last 9,000 blocks, are listed under the pools.

#On the Commons

A Pools app runs on the Commons, with a demo pool you can trade against:

What Address on the Commons
Pools 0x403d3ce745ec648e46d50525b99e6a8bc1f085eb
Its Wrapped HYPE 0x373354379ED98b3656A2605463e3408a659Cc62C
Pools Demo Coin (PDC), a Token and pool app made for the demo 0xef8ba0e56444e719939c8b402b14bcfa7cb4ba6a
The PDC / HYPE pool 0x10f4cfd7c1aF26e4d127c862d14C93c7479C0dc1

#Functions

Function Who may call What it does
createPool(address,address) Anyone Opens an empty pool.
addLiquidity(...) payable Anyone Opens the pool if needed and adds to it.
removeLiquidity(...) Share holders Burns shares for both sides.
swapExactIn(...) payable Anyone Swaps along a path of up to three pools.
quoteOut, quoteIn, quoteAdd, quoteRemove, getAmountOut, getAmountIn Read Quotes at the current reserves.
poolCount(), allPools(i), getPool(a, b), poolFor(a, b), listPools(...), whype() Read The registry.
Pool.getReserves(), token0(), token1(), totalSupply() Read One pool's state. getReserves reverts while the pool is mid-call.
Pool.mint(to), burn(to), swap(out0, out1, to) Anyone The pool's own settlement, for routers of your own. They settle against tokens sent in the same transaction.
Pool.sync(), Pool.skim(to) Anyone Take tokens sent in by mistake into the reserves, or send them out. Reserves are never touched by skim.

Pools events: PoolCreated(token0, token1, pool, index), Swapped(trader, tokenIn, tokenOut, amountIn, amountOut, to). Pool events: Added, Removed, Swap, Sync, and the ERC-20 Transfer and Approval. Pools errors: Expired, IdenticalTokens, PoolExists, NoPool, NotAToken, BadValue, Slippage, BadPath, BadRecipient, ZeroAmount, NoLiquidity, TransferFailed, OnlyWrapped. Pool errors: Locked, InsufficientSharesMinted, InsufficientSharesBurned, InsufficientOutput, InsufficientInput, InsufficientLiquidity, InvalidTo, K, Overflow.

#Tests

contracts/test/Pools.t.sol and contracts/test/PoolsInvariant.t.sol cover the contract with 27 unit tests, 5 fuzz tests and 5 invariants. The invariants drive random adds, removes, one and two hop swaps in both directions, HYPE in and out, donations, syncs, skims, and direct attempts to take one unit more than the formula, 128,000 calls per invariant, and check after every step that:

  • k (the product of the reserves) never falls except on a withdrawal;
  • no add, remove, donation or skim lowers either reserve per share, every share is accounted for, and a pool with shares never runs dry;
  • no swap pays more than the exact formula, and no direct attempt at one unit more goes through;
  • Pools holds no token and no HYPE between transactions, and Wrapped HYPE stays fully backed.

#Good to know

  • Tokens that take a fee on transfer or change balances on their own are not supported.
  • Reserves are stored as 112-bit numbers, as in Uniswap v2: a pool holds at most about 5.2 × 10^33 base units of each token (Overflow beyond).
  • Use Pools or the app page rather than sending tokens or shares straight to a pool: a pool settles against what reached it, so tokens left there without the follow-up call in the same transaction can be taken by the next caller.
  • Prices come from the pools alone. A pool with little liquidity moves a lot on each swap; the quote and the slippage limit show it before you sign.