Skip to content

Research notes

Curve NG exchange vs exchange_received: Which Input Counts?

Trace a 120-token pool surplus through both Curve NG swap paths, then check minimum input, rebasing restrictions and atomic transfer failures.

Ivory pebbles enter a divided ceramic bowl from a chute, with a lime pebble resting on its inner ledge.
Illustration for research notes; examples in this article are educational.

In Curve StableSwap-NG, exchange pulls tokens and measures what arrives during that transfer. exchange_received instead measures the pool's actual input-token balance minus its stored balance. Its requested dx is a minimum check on that surplus, not a cap: a qualifying surplus can become the full swap input.

Read the input baseline before the function name

Both paths exchange one pool coin for another. The difference is the observation used to establish input. Curve's StableSwap-NG integration guide describes the ordinary approval-and-pull flow and the alternative in which tokens reach the pool before the swap call.

This article follows the plain-pool implementation at commit 3332cd44656ec64b3c048885e1dd71955254b262. It is a reference-code analysis, not a claim that every deployed Curve pool runs those bytes. Verify the pool, chain and implementation before applying the result.

For the input coin, keep three quantities separate:

  • Requested input: the _dx supplied in the call.
  • Actual balance: the token contract's reported holding at the pool address.
  • Stored balance: the pool's internal accounting checkpoint for that coin.

In the ordinary path, the pool observes its token balance, calls transferFrom, then measures the increase. In the optimistic path, it measures the excess over the stored checkpoint and checks that this excess is at least _dx. Both branches then add their measured amount to stored accounting. The official plain-pool transfer reference shows those two branches.

The word “received” therefore refers to an accounting difference. It does not establish which address supplied each unit in that difference.

An illustrative ledger: 120 present, 100 requested

Consider a hypothetical input token X with six decimals. The pool's stored X balance is 1,000 tokens, while its actual X holding is 1,120. Assume a prior transfer explains all 120 extra tokens. There are no rebases, transfer charges or concurrent movements, and the pool contains no rebasing assets.

The constructed comparison isolates input accounting. It does not calculate output: that would also require the other balances, rates, amplification, fees and rounding. Assume the remaining swap checks and output minimum pass, with output paid in a different token Y.

Reset to the same starting state for each row:

Requested _dxSurplus checkCounted swap inputStored X after success
100 X120 ≥ 100: passes120 X1,120 X
120 X120 ≥ 120: passes120 X1,120 X
121 X120 ≥ 121: failsNo committed swapRemains 1,000 X

In raw units, the calculation is 1,120,000,000 − 1,000,000,000 = 120,000,000. A requested 100 X means a 100,000,000-unit threshold. The extra 20 X is not left behind merely because the caller requested 100. The entire 120 X difference is returned from the input-transfer routine and used by the swap calculation in the pinned implementation.

After a successful first row, actual and stored X both equal 1,120. A later call requesting even 1 X, with no fresh arrival, sees zero surplus and fails. The same 120 cannot be counted twice under this unchanged state.

The failed row leaves stored X at 1,000. Because the example starts with the extra tokens already present, the failed swap call does not undo their earlier transfer. That distinction matters when deciding what a failed transaction actually recovered.

Run an ordinary pull against the same state

Now reset actual X to 1,120 and stored X to 1,000, then call ordinary exchange for 100 X. Assume a conventional transfer delivers exactly 100.

The before-transfer observation is 1,120; the after-transfer observation is 1,220. The measured arrival is 1,220 − 1,120 = 100 X. Stored X increases to 1,100, leaving the earlier 120 X difference outside this call's counted input. With a clean starting state of 1,000 actual and 1,000 stored, the same transfer would instead end with both at 1,100.

The two paths can thus receive the same requested amount while selecting different balance differences. Recording only _dx would conceal the distinction; recording only the final actual balance would also omit it.

A pool surplus is not a sender reservation

Suppose the hypothetical 120 X reaches the pool in one transaction and its intended owner plans to call exchange_received in another. Between those transactions, the balance difference exists at a shared contract address. The input test does not look up a deposit credited to that owner.

The observed-surplus test therefore creates an attribution risk. Another qualifying call can consume the difference and choose its output receiver. The original transfer's sender address does not reserve that surplus in the accounting shown above.

For a pre-transfer route, require the transfer and swap to occur within one transaction whose failure handling preserves the intended atomic outcome. A router may already hold the intermediate token from another swap; the critical question is whether the intended transfer and consumption form one indivisible route execution.

Atomicity also needs a failure policy. If the swap's failure propagates through the outer transaction, its earlier state changes roll back. If an outer contract catches the failed subcall and continues, an earlier successful transfer can remain. EIP-140 defines reverted execution and the failure returned to the caller; the caller's handling determines whether the surrounding route also fails.

Review the route's source or execution evidence for that behavior. “Both actions are in one transaction” is incomplete if the wrapper can keep a transfer after swallowing the swap failure.

Separate the input threshold from the output minimum

_dx and _min_dy protect different boundaries. For exchange_received, the former checks whether enough unaccounted input exists. The latter checks the swap's computed output against the minimum supplied by the caller. Passing the input threshold cannot guarantee acceptable output.

For another explicitly hypothetical diagnostic, assume a 100 X transfer deducts a 2% charge, leaving 98 X at a pool whose prior actual and stored balances matched. The fee is 2,000,000 raw units; the arrival is 98,000,000. A 100 X optimistic threshold fails. A 98 X threshold satisfies the arithmetic equality, but this does not establish that the token or pool configuration supports the route.

Ordinary input measurement would observe a 98 X arrival rather than silently turn a 100 X debit into 100 X of input. Compatibility still requires the actual token and deployed pool behavior. The broader fee-on-transfer and rebasing token accounting guide separates sender debit, pool arrival and recipient receipt; retain those distinctions here.

There is a stronger restriction for rebasing pools. The pinned constructor and guard set the rebasing flag when any configured asset has type 2 and reject exchange_received under that pool-wide condition. It is not enough to select a non-rebasing input from a pool that contains another rebasing asset. Curve's asset-type overview explains why rebasing-aware balances need different treatment. Incorrect classification is not a compatibility workaround.

An output minimum also needs a defined meaning in the integration. The shared swap routine checks computed dy before sending the output token; a custom token's final receiver credit needs its own reconciliation. As the receipt-success versus token-transfer guide explains, top-level success alone cannot prove the intended wallet amount arrived.

Build a route worksheet that explains the difference

Curve's integration reference lists discovery methods for coins, asset types and implementation addresses. Use those as starting evidence, then bind the observations to the exact pool and state being evaluated.

Do not substitute an LP-facing balance view for the stored checkpoint. The documented get_balances path excludes admin balances, so it is a different quantity. If the deployed ABI does not expose the checkpoint, establish it from verified storage layout or execution reconstruction. The plain-pool balance reference shows that distinction.

RecordEvidence to retainQuestion answered
Pool identityChain, pool address, implementation and source revisionWhich accounting branch exists here?
Asset mappingToken addresses, coin indices, decimals and asset typesWhich raw units and restrictions apply?
Input checkpointActual and stored input balances at the relevant execution stateWhat surplus can the call observe?
Transfer pathToken debit, pool arrival and transaction orderingWhich movements explain that surplus?
Acceptance rulesRequested _dx, _min_dy and intended receiverWhich conditions must succeed?
Completion evidenceRealized input, output transfer and attributable receiver creditWhat did the route actually deliver?

Exercise the worksheet against four controlled conditions: exact arrival, surplus above the threshold, arrival below it, and no new arrival after an earlier successful call. Include the route's handling of an output-minimum failure. Those cases reveal whether the integration incorrectly assumes _dx is an exact cap or assumes a revert recovers a prior independent transfer.

Keep each observation's block and execution scope. Reading two balances at different times can manufacture a difference unrelated to the intended route. Even same-block end states can combine multiple transactions. A reconstruction needs the relevant pre-state and ordered calls; if those are unavailable, record the attribution gap rather than assign unexplained tokens to a trader.

Version-check event decoding too. At the pinned revision, TokenExchange records measured input; the current plain-pool documentation's embedded example uses requested _dx in its log statement. Treat this mismatch as a reason to inspect the actual implementation, not to select whichever amount fits a report.

Finally, keep downstream metrics separate from this input ledger. Last price versus price oracle concerns different price observations, while virtual price versus token redemption concerns LP accounting and withdrawal outcomes. Neither substitutes for the balance difference consumed by this call. These protocol examples are educational and do not describe a KLINEO integration.

Before accepting a route, be able to write one concrete sentence: this call counted this many raw input units because these two balance observations differed by that amount, and its output reached the recorded receiver under the stated acceptance rule.

Keep the context with the example.

Each worked example is illustrative, with its sources and assumptions in the article. Read our editorial standards for authorship, verification and corrections.