VIEW THIS AS

Auto mode follows the Route Engine until you choose a viewpoint.

YOU ARE HERE

ROUTE CHECK

CONNECTED TO

WHAT NEXT

Use the canonical route for this room, or HELP if you are unsure.

Automated Market Maker Arithmetic: Read the Pool Before Calling It a Price

Three students in school uniforms work through open books at a classroom table, with textbooks and stationery nearby and study notes on the whiteboard behind them.

中文版 · Blockchain learning guide · Previous article

Blockchain learning · Article BC077

Three students in school uniforms work through open books at a classroom table, with textbooks and stationery nearby and study notes on the whiteboard behind them.
Study photograph for illustration; it does not establish that the pictured students are studying blockchain.

A two-asset pool can quote one marginal exchange rate before an exchange, deliver a different average rate over the whole exchange and finish with a third marginal rate. Those numbers can all be correct. They answer different questions.

This lesson works through an inert constant-product model with fictional units X and Y. You will calculate the output, check the resulting reserves and compare the initial, average and final rates. Then you will test what happens when the pool changes between an observation and a later candidate operation.

There is no live exchange, wallet, asset recommendation or trading strategy here. The numbers are a mathematical teaching model. They do not represent market value, investment return or access to a financial service.

Choose your reading route

In this guide

  • Read sections 1–3 to establish the pool rule and the units.
  • Work through sections 4–6 to calculate one complete exchange and distinguish three rates.
  • Read sections 7–9 for a changed-state case, an output condition and a reverse-operation check.
  • Use the exercises and FAQ to verify the arithmetic and identify what the model leaves out.

Back to contents · Next section

1. A rule-based pool is one way to match exchanges

An automated market maker, or AMM, uses specified rules to determine exchanges against supplied liquidity. A constant-product pool is one particular model, not a definition of every AMM or every decentralised exchange.

The Uniswap v2 Core paper, dated March 2020, describes a named constant-product design with additional implementation features, including fees. The v2 pricing documentation also distinguishes pricing calculations from the core contract’s invariant checks.

Our example removes fees and implementation details to isolate one relationship. We are not reproducing the exact output of a production contract. Real systems can involve different curves, liquidity ranges, integer rounding, token behavior and additional checks.

The benefit of a small model is that every state change can be audited on paper. Its limitation is equally important: a correct calculation inside this model is not evidence that a real transaction is safe, desirable, permitted or certain to execute.

Back to contents · Next section

2. Define the reserves and the invariant

Let x mean the pool’s reserve of X, and y its reserve of Y. Initially x is 100 and y is 200. Their product is 20,000.

For this exercise, an accepted exchange preserves x times y exactly at 20,000. The units are divisible, and calculations use exact fractions where necessary. There are no fees, reserve donations, liquidity additions or withdrawals, transfer taxes, rebasing, integer rounding or outside transactions unless explicitly introduced in a later case.

These exclusions matter. In a fee-bearing implementation, the relationship between observed reserve products before and after an operation may differ from this exact-equality model. Do not copy the exercise’s result into another system without its actual rules.

Also define direction. We will first put X into the pool and take Y out. An increase in x is therefore accompanied by a decrease in y under the positive-reserve constant-product rule.

The pool cannot simply pay any desired Y amount. The resulting pair of reserves must satisfy the supplied rule. That condition, rather than the appearance of a screen quote, determines the mathematical output in this example.

Back to contents · Next section

3. Name the rate and its unit

The initial marginal rate for exchanging a very small amount of X into Y is y divided by x: 200/100, or 2 Y per X in this no-fee model. Marginal means the local rate for an infinitesimal change at that state.

It does not promise 2 Y for every X in an arbitrarily large exchange. As X enters and Y leaves, the reserve ratio changes. A finite exchange traverses that changing relationship.

The average realised rate for a completed finite exchange is total Y output divided by total X input. The final marginal rate uses the reserves after that exchange. These definitions keep three different measurements from being compressed into one ambiguous word.

Be careful with direction. Y per X and X per Y are reciprocal units at a specified marginal state. A number without its direction is incomplete. In the main calculation we consistently use Y per X when comparing the three rates.

No external market price is supplied. The reserve ratio is the local rate implied by the toy curve, not a declaration of fair value or a prediction of prices elsewhere.

Back to contents · Next section

4. Work the 25-X input from beginning to end

Start at x100 and y200. The supplied input is 25 X. With no fee or other deduction, the final X reserve is 100 plus 25, giving 125.

The invariant requires the final Y reserve to be 20,000 divided by 125. That is 160. Since the pool began with 200 Y and finishes with 160, the output is 40 Y.

The resulting state is therefore x125, y160. Check it independently: 125 times 160 equals 20,000. The product is preserved, both reserves remain positive and the directions match the operation: X increased by 25 while Y decreased by 40.

The hypothetical participant gives up 25 X and receives 40 Y. This is not a statement that the participant gained value; no external valuation, costs or preferences have been supplied. It is a quantity exchange under a rule.

A common incorrect calculation multiplies the initial marginal rate of 2 by the entire input of 25 and predicts 50 Y. If the pool paid 50, it would finish with x125 and y150. Their product would be 18,750, violating our exact 20,000 rule.

The wrong result is therefore not merely a different interpretation of price. It fails the model’s state condition.

Back to contents · Next section

5. Compare the initial, average and final rates

The initial marginal rate was 2 Y per X. The actual finite output is 40 Y for 25 X, so the average rate is 40/25, or 1.6 Y per X.

At the final reserves, the marginal rate is 160/125, or 1.28 Y per X. Thus the three rates are 2 initially, 1.6 on average for the completed exchange and 1.28 at the end.

The average lies between the two marginal rates in this particular direction and model. The pool did not use one fixed rate for the whole input. Its changing reserves are why a finite exchange has a different average from the initial local rate.

Now make two comparisons with explicit reference points. The average rate is 0.4 below the initial 2, a shortfall of 20% relative to that initial rate. The final marginal rate is 0.72 below the initial 2, a decrease of 36% relative to the same initial rate.

Those percentages describe different objects. One compares a whole-exchange average to a starting marginal rate; the other compares two marginal states. Calling both simply “the price impact percentage” without explaining the definition would invite confusion.

They are also not percentages of guaranteed financial loss. We have not supplied market valuations or an alternative executable transaction. They describe the toy curve’s rates under stated units and denominators.

Back to contents · Next section

6. Separate an exchange’s own effect from a later-state difference

The Uniswap Labs explanation of price impact and slippage, updated on 11 February 2025, distinguishes an exchange’s own effect on price from a difference between expected and received execution.

Terminology can vary by context, so this lesson states the numerical comparisons rather than relying on a label alone. In the baseline, we knew the reserves and calculated the finite output of 40 before considering any intervening operation. The difference between 2 and 1.6 was already implied by the candidate’s size on the initial curve.

If the same candidate executes against the same supplied state with the same rules, its output is still 40. The initial marginal rate was never a promise of 50.

A separate question arises if another event changes the state after the observation used for the calculation. The previously calculated output may no longer be available. We need a new starting state to compute that difference; we cannot infer it merely because the candidate is large.

The next case supplies such a state change explicitly. It is a mathematical sequence, not a claim about how a real network orders transactions or how any participant should influence that order.

Back to contents · Next section

7. A changed-state candidate produces a different output

Reset the pool to x100 and y200. A reader observes that state and computes 40 Y for a candidate input of 25 X, exactly as before.

Before the candidate is considered, another supplied operation puts 25 X into the pool and takes out 40 Y. The actual pool is now x125, y160. This earlier operation is already completed and is separate from the candidate.

Now calculate the candidate’s 25-X input from the changed state. Its proposed final X reserve would be 150. The corresponding Y reserve would be 20,000/150, or 400/3 Y. The output would therefore be 160 minus 400/3, which equals 80/3 Y, approximately 26.6667 Y.

The candidate’s potential output is lower than the earlier 40-Y calculation because the starting reserves differ. Its average rate would be (80/3)/25, or 16/15 Y per X, approximately 1.0667.

This does not contradict the baseline formula. The formula was applied to a different state. A quote or calculation needs the state it used if another reader is to reproduce it.

The fractional values are exact mathematical quantities in this model. The rounded decimals are explanatory approximations, not production token-unit calculations. A real integer implementation would require its own rounding rules.

Back to contents · Next section

8. Test a minimum-output condition without changing it

Add a supplied rule for this candidate: it may execute only if its computed Y output is at least 38. This is an invented acceptance condition for the exercise, not a suggested setting for a real trade.

Against the original x100/y200 state, the output would be 40, so the numerical condition would pass. Against the changed x125/y160 state, the potential output is 80/3, which is less than 38. The condition fails.

The model rejects the candidate and applies none of that candidate’s reserve changes. The pool therefore remains x125/y160 from the earlier completed operation. It does not reset to x100/y200, because rejecting the candidate does not erase an unrelated prior operation.

Nor does it end at x150/y400/3, because those were proposed reserves for an operation that was not accepted. The distinction between a calculated proposal and an applied state is essential.

We exclude transaction fees, network charges and all other state in this paper model. Its no-change result concerns the candidate’s pool reserve update only and should not be read as a universal claim that a rejected blockchain transaction has no cost or side effects anywhere.

Do not respond to a failed condition by silently weakening it. In an evidence exercise, the task is to report why the supplied rule rejected the candidate, not to make the operation go through.

Back to contents · Next section

9. A reverse-operation check tests consistency

Return to the successful baseline state x125/y160. Independently of the changed-state rejection case, suppose an operation puts 40 Y back into this no-fee pool.

The Y reserve becomes 200. To preserve the product of 20,000, the X reserve becomes 100. The pool therefore outputs 25 X and returns to its original x100/y200 state.

This is a useful consistency check for the idealised arithmetic. The exact reverse quantities recover the original reserves because the model excludes fees and every intervening change.

It is not a real-world round-trip promise or a source of profit. Actual systems can charge fees, use discrete units and change between operations. The model does not include those conditions, nor does it supply an opportunity to execute either side.

The reverse direction also changes the rate unit. Here input is Y and output is X, so the whole-operation average is 25/40, or 0.625 X per Y. That is the reciprocal of 1.6 Y per X for these exact reversed quantities. State the unit so the apparent smaller number does not become a misleading comparison.

Back to contents · Next section

10. What the invariant does not establish

Preserving a reserve product is a mathematical condition. It does not establish that the assets are trustworthy, that their external prices are fair or that an implementation has no bugs. It also does not determine the legal availability of a financial service.

A pool can satisfy its local rule while a user misunderstands the units or the state used for a quote. Conversely, a correctly calculated candidate can fail an independent acceptance condition. These are different problems and should receive different explanations.

The official swap concepts documentation describes integration-level guards such as minimum output. We use that narrow concept here; the invented threshold and rejection trace are our own supplied model, not a claim about every version or interface.

For mainland China readers, the PBOC-led notice 银发〔2026〕42号 of 6 February 2026 provides relevant virtual-currency business restrictions. This lesson is technical education and does not imply permission to access or operate an exchange.

The earlier Liquidity article explains broader exchange frictions. This workshop adds a reproducible reserve-state calculation, not a recommendation to provide liquidity or trade.

Back to contents · Next section

11. Exercises with answers

Exercise 1: Check a proposed output

Starting from x100/y200, someone proposes input 25 X and output 50 Y. Does it satisfy the rule?

Answer: No. Proposed reserves 125/150 have product 18,750, not 20,000. The accepted no-fee output under the supplied rule is 40 Y, leaving 125/160.

Exercise 2: Name all three rates

What are the initial marginal, whole-exchange average and final marginal rates for the successful baseline?

Answer: 2, 1.6 and 1.28 Y per X respectively. They answer different questions; none should be substituted for another without explanation.

Exercise 3: Compare percentages carefully

Why do 20% and 36% both appear?

Answer: The average rate 1.6 is 20% below initial 2, while final marginal 1.28 is 36% below initial 2. One comparison uses the finite-exchange average and the other uses the ending marginal state.

Exercise 4: Apply the changed-state condition

After the earlier operation leaves 125/160, does candidate input 25 meet minimum output 38?

Answer: Its potential output is 80/3, about 26.6667, so it fails. The candidate is rejected and the pool remains 125/160. The earlier completed operation is not undone.

Exercise 5: Reverse the idealised baseline

Starting from 125/160, what happens if 40 Y enters under the same no-fee rule?

Answer: Y becomes 200, X becomes 100 and output is 25 X. This checks the toy arithmetic only; it is not a production round-trip guarantee.

Back to contents · Next section

12. Frequently asked questions

Are all AMMs constant-product pools?

No. This is one specified model. Other mechanisms and implementations need their own rules and evidence.

Does a large difference from the initial marginal rate prove a bad calculation?

No. A finite exchange changes reserves, so its average can differ from the starting local rate. Check the state equation and the stated comparison.

Does a minimum-output condition guarantee execution?

No. In this model it is one necessary acceptance condition. A candidate can fail it, and real systems can have additional requirements. The condition is not a promise that the desired amount will become available.

Does an invariant make the quoted assets safe?

No. It constrains the modeled exchange arithmetic, not all technical, economic, custody or legal risks.

Back to contents · Next section

13. Sources and further reading

Sources checked on 12 October 2026. All quantities are fictional; exact rational arithmetic and no-fee assumptions are deliberate.

A pool calculation becomes clear when the starting state, input, rule, output, rate definition and acceptance condition are all visible. The word price is useful only after those details are fixed.

Back to contents