Skip to content

Research notes

Curve NG Last Price vs EMA Oracle: Follow the Clock

Follow one constructed Curve NG price through read-only calls and two updates at the same timestamp, separating stored values from the current EMA view.

An ivory frame holds a large vertical ivory pendulum beside a small lime pendulum angled to the right.
Illustration for research notes; examples in this article are educational.

In the pinned Curve StableSwap-NG implementation, last_price returns the last stored, capped pool state price. ema_price returns the stored exponential moving average. price_oracle calculates an EMA for the current block timestamp from those stored values without writing storage. A second upkeep at the same timestamp can replace the stored spot while leaving the EMA unchanged, affecting what the oracle returns later.

Four getters answer four different questions

A dashboard can display several plausible prices for one pool. Before interpreting a gap, identify which value it retrieved. This article follows the plain-pool implementation at commit 3332cd4, rather than assuming every contract called “Curve NG” has identical behavior. Pinned CurveStableSwapNG source.

GetterWhat it returnsDoes calling it save an oracle update?
get_p(i)State price recomputed from current normalized balances, rates and amplificationNo
last_price(i)Previously stored state price, capped when saved by oracle upkeepNo
ema_price(i)Previously stored EMANo
price_oracle(i)EMA calculated for the read's block timestampNo

The oracle index also needs care. In this implementation, index 0 describes coin 1 relative to coin 0; it does not describe coin 0 relative to itself. Getter prices use a 10¹⁸ fixed-point scale, separate from token decimals. The worked example below uses ordinary 18-decimal tokens, X at coin 0 and Y at coin 1, with no additional exchange-rate conversion. Its displayed prices mean X per Y. Curve's integration guide.

For a rate-bearing asset, establish the normalized price's meaning before turning it into a price per token. Curve's own explanation distinguishes the underlying reference from a yield-bearing token's conversion rate. A display label such as “Y price” can conceal that extra conversion. Curve's oracle interpretation notes.

Nor is last_price the average execution price of the latest swap. It is a cached AMM state-price observation. Its cap and the state snapshot used during an action can make it differ from a fresh get_p call. If amplification is changing, the current A versus ramp-target timeline explains why the parameter used now also needs a timestamp.

A 600-second setting does not mean a 600-second half-life

Let S be the stored spot, E the stored EMA and Δt the seconds since the price oracle's stored timestamp. In a continuous reference calculation, the old EMA weight is:

α = exp(−Δt ÷ τ).

The value returned is S × (1 − α) + E × α, where τ is ma_exp_time. This is exponential decay toward a held observation. It is not an arithmetic average of the last τ seconds with an abrupt cutoff. The prior TWAP versus spot-price guide explains why naming the averaging method matters.

At Δt = τ, the old EMA still has about 36.79% weight. Its weight halves after τ × ln(2). For a hypothetical τ of 600 seconds, the half-life is approximately 415.888308 seconds. Conversely, targeting a 600-second half-life requires a time constant of about 866 seconds. Curve's factory documentation explicitly describes the division by ln(2); read the actual getter rather than treating an example configuration as a pool's setting. Curve factory parameter conventions.

Calling a view more often does not give it additional observations. If the packed state and smoothing setting stay unchanged, another read merely evaluates the same held spot and stored EMA at another block timestamp.

Follow one constructed price through two updates

Consider a hypothetical starting snapshot at 12:00:00 UTC:

  • Stored spot S = 1.200000 X per Y.
  • Stored EMA E = 1.000000 X per Y.
  • Price timestamp = 12:00:00.
  • ma_exp_time = 600 seconds.

All values here are illustrative. The calculations use a continuous exponential and are rounded to six decimals. They demonstrate the update order rather than exact contract output at 10¹⁸ precision. The pinned implementation uses integer arithmetic and its own fixed-point exponential routine.

At 12:05, a read uses 300 seconds of elapsed time. The old EMA weight is exp(−0.5), so the current oracle value is approximately 1.078694. Storage still says spot 1.200000, EMA 1.000000 and timestamp 12:00. The read has not committed a new checkpoint.

At 12:10, before any new upkeep, the oracle value is approximately 1.126424. That is 1.2 − 0.2 ÷ e. Now suppose a successful pool action produces a new state-price observation of 0.900000, and a second successful action at the same timestamp produces 1.300000. Both are below the implementation's stored-spot cap.

Time and eventStored spot after eventStored EMA after eventView at that timestamp
12:00 constructed snapshot1.2000001.0000001.000000
12:05 read only1.2000001.0000001.078694
12:10 read before upkeep1.2000001.0000001.126424
12:10 first upkeep; new spot 0.90.9000001.1264241.126424
12:10 second upkeep; new spot 1.31.3000001.1264241.126424
12:15 read only1.3000001.1264241.194721

The first upkeep advances the EMA using the old packed spot of 1.2 for the elapsed interval. It then pairs that EMA with the new stored spot of 0.9. It does not pretend the newly observed 0.9 prevailed during the preceding ten minutes.

The second upkeep has zero additional elapsed time. Its EMA remains 1.126424, but it replaces the stored spot with 1.3. These transitions follow upkeep_oracles and its call to _calc_moving_average, which reads the previous packed values. Pinned oracle update logic.

At 12:15, the read moves toward 1.3, using 300 seconds since the saved 12:10 timestamp. Had 0.9 remained the final stored spot, the same reference calculation would instead return about 1.037333. Thus an unchanged EMA within the timestamp does not mean the second action has no future influence.

These prices are assumed observations supplied to the oracle state machine. They are not inferred from a particular trade size, balance configuration or historical market path.

“Once per block” describes the elapsed-time gate

Curve's documentation summarizes the EMA as updating at most once per block. The underlying condition compares timestamps: the saved price timestamp must be strictly earlier than block.timestamp for elapsed-time decay. Transactions within one block share that timestamp. The stored spot can still change on later relevant actions. Curve's oracle update description.

That distinction gives an integration three useful tests. First, two actions at the same timestamp must not manufacture two intervals of decay. Second, the next later read must use the final stored spot. Third, a read between actions must not be treated as a storage write in a replay ledger.

Relevant upkeep paths include swaps, liquidity additions, single-coin removals and imbalanced removals. A proportional remove_liquidity follows separate D-oracle handling; an updated D timestamp does not establish an updated price timestamp. To trace the successful swap path itself, use the exchange versus exchange_received guide. A reverted action supplies no lasting oracle-state change.

Do not decode the packed ma_last_time as one ordinary timestamp. The lower 128 bits belong to prices and the upper 128 bits to D. Comparing the wrong half can create a convincing but irrelevant freshness result. Curve's packed timestamp specification.

A moving read is not a new market report

The 12:05 and 12:10 values moved despite no new state-price observation in the example. That is expected extrapolation toward the held spot. It does not prove that another venue confirmed 1.2, that fresh trading occurred, or that the market remained there throughout the interval.

The oracle staleness and deviation guide discusses external report timestamps. Here, an integration must additionally separate the last stored observation from the timestamp used to evaluate a view. A monitor should retain both.

Similarly, the spot cap of 2 × 10¹⁸ limits an input to this implementation's EMA. It does not establish a dollar peg, guarantee a redeemable value or make a thin pool safe for unlimited downstream exposure. A longer time constant adds lag as well as smoothing. Curve's security discussion itself relates oracle suitability to pool liquidity and the size of the consuming system. Curve's responsiveness and liquidity discussion.

Before investigating an apparent jump as market activity, check the pool implementation. Curve documents an early NG oracle bug in the imbalanced-removal path, where raw balances reached upkeep without the required normalization. The identified fix is commit 4bb402e; the pinned implementation used here includes the normalized call. That history is a reason to verify deployed source, rather than applying one explanation to every NG pool. Official normalization warning, named fix commit.

Build a receipt that explains the gap

For a reproducible comparison, keep these fields together:

RecordWhat to verify
Chain, pool and implementationWhich deployed code governs the read?
Coin addresses and oracle indexWhich asset and quote direction do the values describe?
Rates and output scaleAre normalized values being confused with raw token units?
Block identity and timestampWere all getters read at the same state?
Stored spot, EMA and price timestampWhat state is being carried forward?
Actual ma_exp_timeWhich time constant applies at this block?
Getter and last relevant actionIs the result stored, recomputed or extrapolated?

Reproduce a zero-elapsed-time read, a later read without upkeep, two same-timestamp actions and a proportional withdrawal. Then compare the expected state transitions with the saved evidence. For the constructed 12:15 snapshot, the question is precise: does the read start from EMA 1.126424 at 12:10 and decay toward the final stored spot of 1.3? That receipt explains the result far better than a label saying only “latest price.”

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.