DEV Community

aleax h
aleax h

Posted on

Your fee model has a flat component and your tests do not

Most fee models in code look like this: cost = notional * rate. It passes review because it matches the exchange's fee schedule, and the schedule is the document everyone trusts. The problem is that the schedule describes one of four costs, and only one of them is proportional to size.

A fee is not always a percentage. Withdrawal fees are flat per transaction. Funding is proportional to position size times time held, not to notional traded. Conversion costs live in the spread rather than in a line item. If your model multiplies by a rate, it silently reports zero for three of the four.

The fix is not a bigger constant. It is to model the shape:

def trading_fee(notional, tier):        # proportional, step function on tier
    return notional * tier.rate

def withdrawal_fee(asset, chain):       # flat, per asset per chain
    return FEES[asset][chain]

def funding(position_notional, rate, hours):
    return position_notional * rate * (hours / 8)
Enter fullscreen mode Exit fullscreen mode

Four shapes, four functions. Collapsing them into one rate is how a cost model ends up 30% low on small accounts and correct on large ones — which is the worst kind of wrong, because it looks fine in the aggregate.

Flat components make small values infeasible, not just expensive. A one-dollar withdrawal fee on a ten-thousand-dollar transfer is noise. On a hundred-dollar transfer it is one percent. Below some threshold the transfer stops making economic sense at all, and below the exchange's minimum it stops being possible. A percentage model has no way to represent "this operation is not worth doing", so it returns a small positive number and the caller proceeds.

If your code decides whether to move money, the decision needs a predicate, not a cost:

def worth_withdrawing(amount, fee, minimum):
    return amount >= minimum and amount > fee * MIN_MULTIPLE
Enter fullscreen mode Exit fullscreen mode

Step functions need explicit boundaries. Tier ladders are steps: the whole trade is priced at the rate of the tier it lands in. That means cost is discontinuous in notional, and a model that assumes continuity will misprice every trade near a boundary — sometimes by more than the edge being measured. Encode the ladder as an ordered list of thresholds and look it up; do not interpolate.

The same asset on two chains is two different fees. Withdrawal fee tables are keyed by asset and network, not by asset. A model keyed only by asset will pick whichever row it finds first and be wrong half the time. This is also where tests usually stop: the fixture has one chain, so the bug ships.

Rounding is a business rule, not a formatting concern. Exchanges round fees in their own favour, at their own precision, sometimes per asset. If your model rounds at the end and theirs rounds per leg, the totals diverge on exactly the cases users complain about. Round where they round.

Test the shapes, not the numbers. Three properties catch most of this class of bug, and none of them require a live exchange:

  • Monotonicity: doubling notional must not decrease the fee.
  • Flatness: for a fixed asset and chain, the withdrawal fee must not change with amount.
  • Infeasibility: below the minimum, the operation must be rejected, not priced.

If a refactor breaks any of these, the model changed shape, and someone should know.

One more: the cost model is a snapshot. Tiers are assigned from trailing volume, fees change per chain, and funding rates move. A model that hardcodes today's numbers is a model with an expiry date nobody wrote down. Store the source and the date with the rate.

I wrote a longer, non-code version of the same argument — what the four costs are and how they interact — in withdrawal fees: the cost that does not scale.

Earlier pieces from the same series: rounding is a business rule, and keeping a fee calculator correct when exchanges change their tiers.

The habit worth building: when a cost model is reviewed, ask which of the four shapes each term is. If the answer is "all percentage", the model is a simplification, and it should say so in its name.

Top comments (1)

Collapse
 
alexshev profile image
Alex Shev •

Flat components are exactly where property-based tests help: generate values around the threshold and assert that the marginal fee changes only where the model says it should. It catches both missing fixed fees and accidental double application more effectively than a few hand-picked examples.