Trade
Fees
What the clearing fee and a pair's liquidity fee are charged on, in which token, and how each rounds.
A trade meets two fees. The clearing fee is taken from an order's output when it fills. A pair's liquidity fee is taken from what that pair's liquidity pays out in the batch. Placing, cancelling and settling an order pay the program nothing else.
| Clearing fee | Liquidity fee | |
|---|---|---|
| Rate | One rate for the whole exchange, clearing_fee_bps | One rate per pair, the pair's fee_bps |
| Charged on | The order's gross output in a batch | What the pair's liquidity pays out in a batch |
| Token | The buy token | The token the pair pays out |
| Rounding | Down, per order per batch | Down, per segment of the pair's move |
| Seen as | feeAmount on the order | Part of the cleared price |
Both rates are in basis points, so the divisor is 10,000. Where each fee goes is on Fees and UNA.
The clearing fee
For each batch an order fills in:
gross = floor(filled * price(sell) / price(buy)) fee = floor(gross * clearing_fee_bps / 10000) net = gross - fee
filled is the atoms of the sell token the order trades in the batch, and the prices are the batch's cleared prices of the two tokens. clearing_fee_bps is the rate the batch stored when it cleared, so an order settled after the rate changes pays the rate of its batch. How fills are decided has how filled is set.
The fee is in atoms of the buy token. The owner receives net, and the fee stays in the buy token's vault. Both divisions round down, so a fill whose gross * clearing_fee_bps is under 10,000 pays a fee of 0.
The fee is charged on what fills and nothing else:
- An order that fills over several batches pays it in each, on that batch's
gross, rounded on its own. - The part of an order that does not fill pays nothing, whether it moves into the next batch or returns to the wallet.
- A cancelled order pays nothing. Neither does an order in a rolled batch, or an unsettled order in a slashed one.
An order's limit is compared with the cleared price before the fee. An order that clears exactly at its limit receives floor(amount * limit / 2^64) less the fee.
A batch's result carries the total per token: the sum of fee over every order buying that token. The proof checks that figure, and it is feeAmount on each price of a BatchDetail.
The liquidity fee
A pair's liquidity trades in the same clearing as the orders. A pair moves toward the cleared price of its two tokens and stops short of it by its fee plus one basis point, so it trades only when the cleared price is further than that from its own. When it moves, the pair pays out one of its two tokens and takes in the other, one segment of its curve at a time. On each segment:
out = what the curve pays out across the segment, rounded down fee = floor(out * fee_bps / 10000) delivers = out - fee
The pair delivers out - fee to the batch, and takes in at least what its curve asks for the whole of out. The fee stays in the pair, credited to the positions whose range covers that segment.
No order has a line for this fee. An order's gross is computed from the price vector alone, and the price vector is the one at which every token balances with each pair delivering out - fee. An order that buys what a pair pays out meets the fee in the cleared price it gets.
A batch in which a pair's price does not move pays out nothing from that pair and charges no liquidity fee on it. Orders that fill against each other alone pay the clearing fee only.
The clearing conditions has the pair's full arithmetic, and What a position earns follows the fee to a position.
A worked order
These are sample inputs. The clearing fee of 5 basis points and the liquidity fee of 30 basis points are chosen for the example. Read the live rates as the next section shows.
An order sells 0.75 SOL for a token with 6 decimals and accepts at least 140 of it per SOL. In atoms: amount = 750,000,000 lamports, and the limit is 0.14 atoms per lamport. The batch clears the token at 7 lamports per atom, and the order fills in full.
| Step | Arithmetic | Result |
|---|---|---|
| Rate | 1 / 7 atoms per lamport, against a limit of 0.14 | 0.142857, in the money |
| Gross | floor(750,000,000 × 1 / 7) | 107,142,857 atoms |
| Clearing fee | floor(107,142,857 × 5 / 10000) | 53,571 atoms |
| Net | 107,142,857 − 53,571 | 107,089,286 atoms, 107.089286 tokens |
The order pays 53,571 atoms of the token it buys, 0.053571 tokens, and nothing in SOL.
In the same batch the pair of SOL and this token moves along its curve and pays out the token. With a fee of 30 basis points it stops 31 basis points short of the cleared price. On one segment of that move:
| Step | Arithmetic | Result |
|---|---|---|
| Out | The curve's change in the token across the segment | 40,000,000 atoms |
| Liquidity fee | floor(40,000,000 × 30 / 10000) | 120,000 atoms |
| Delivered | 40,000,000 − 120,000 | 39,880,000 atoms |
| Taken in | floor(39,880,000 × 7 / 1), or the curve's amount if that is greater | 279,160,000 lamports |
The 120,000 atoms stay with the positions in range on that segment. The 39,880,000 atoms delivered are part of what the batch's buyers of the token receive, this order among them, at 7 lamports per atom.
Reading the rates
const protocol = await una.protocol()
console.log(`clearing fee: ${protocol.clearingFeeBps} basis points of each order's output`)
const { items: pairs } = await una.pairs()
for (const p of pairs) console.log(`${p.slug}: liquidity fee ${p.feeBps} basis points`)
console.log(`this order: about ${quote.estimatedFee} atoms of ${quote.buyToken.symbol} in clearing fee`)una and quote are from Quickstart. clearingFeeBps is on ProtocolParams, feeBps on PairSummary, and estimatedFee on QuoteResponse, computed at the last proven prices. After a fill, feeAmount on the Order is what it paid.
Both rates are also on Parameters.