Skip to content
Business logic Audit finding Beginner

twap() under-charges for multi-period queries due to hardcoded periods=1

The twap() and x_twap() functions in a smart contract pass a hardcoded value of 1 to the fee-charging function instead of the user-supplied records count, causing under-charging for multi-period queries. An attacker could request many historical periods while paying only the single-period fee, reducing operational costs.

Sparrow Follow with 0x_kmr_, 0x1998 and 48 others

Originally disclosed on Code4rena

Submitted by Sparrow, also found by 0x_kmr_, 0x1998, 0xgeeee, 0xnija, 0xpetern, 0xvd, Albort, boodieboodieboo, ChainSentry, CoMMaNDO, cryptoWhale, Daniel526, Dest1ny_rs, edoscoba, escrow, eta, Eurovickk, HalalAudits, hecker_trieu_tien, holtzzx, ht111111, HUNTERRRRRRR, iAfrika, inh3l, jectaw, jerry0422, Jesse, johnyfwesh, Josh4324, jsmaxi, JuggerNaut63, khaye26, Kirkeelee, KKKKK, kmkm, luckygru, markoliver, marsspaceX, mbuba666, Meks079, merlin, mrdafidi, Mylifechangefast_eth, NexusAudits, niffylord, OhmOudits, oxwhite, Petrus, piki, rare_one, shaflow2, sl1, slvDev, th3_hybrid, TheCarrot, touristS, trilobyteS, Wojack, y4y, yixuan, zcai, and zubyoz

lib.rs #L293-L298

Finding description

twap() and x_twap() pass a constant 1 to charge_invocation_fee() instead of the user-supplied records count. This causes massive under-charging for queries requesting multiple historical periods.

// In lib.rs
pub fn twap(e: &Env, caller: Address, asset: Asset, records: u32) -> Option<i128> {
    caller.require_auth();
    charge_invocation_fee(e, &caller, InvocationComplexity::Twap, 1);  // <-- Bug: always 1
    // ...
}

Impact

  • Users requesting TWAP over N periods pay only the single-period fee.
  • Predictable revenue leak; attackers can query with high records to minimize costs.

Each TWAP/X-TWAP call that requests >1 period is under-billed by a factor of N / 1. Heavy integrators (bots, aggregators) can reduce their operational costs nearly to zero by always using large records values.

Proof of concept

// Caller wants TWAP over 15 rounds but pays for 1
let fee_before = BeamOracleContractClient::estimate_cost(
    &InvocationComplexity::Twap,
    &1,   // what contract *believes* is requested
);
let twap = BeamOracleContractClient::twap(&caller, &asset, &15);
// Internally charge_invocation_fee was called with periods = 1

Recommended mitigation steps

Pass the actual records:

charge_invocation_fee(e, &caller, InvocationComplexity::Twap, records);

The original report as published on Code4rena, by Sparrow. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the developer hardcoded the period count as 1 in the fee calculation call instead of using the records parameter.

Impact

Users requesting TWAP over N periods pay only the single-period fee, leading to a predictable revenue leak and allowing heavy integrators to reduce costs nearly to zero.

What the attacker needed

Access to the twap() function; ability to specify records > 1

The fix

The fee-charging function should be called with the actual records value instead of the hardcoded 1.

How it was found

  1. Call twap() with a records value greater than 1.
  2. Observe that the fee charged corresponds to a single period, not the requested number.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

This is a smart contract bug where a function that calculates an average price over several time periods charges a fee based on how many periods you ask for. But the code always tells the fee system you asked for just one period, so you pay less than you should. Someone could exploit this to get lots of data cheaply.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by Sparrow

View all

1 more disclosed report, best first.

More from Reflector V3

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.