Skip to content

Overshoot Spardose fake balance by 1%#4362

Merged
squadgazzz merged 3 commits intomainfrom
spardose-overshoot-for-aave-atoken-quotes
Apr 24, 2026
Merged

Overshoot Spardose fake balance by 1%#4362
squadgazzz merged 3 commits intomainfrom
spardose-overshoot-for-aave-atoken-quotes

Conversation

@squadgazzz
Copy link
Copy Markdown
Contributor

Problem

Quote verification for small aToken sell amounts intermittently failed in prod with execution reverted: trader does not have enough sell token: 0.1 aEthWETH quote failed, 1 aEthWETH quote minutes earlier passed.

aToken does not store a balance directly. It stores a deposit and a growth factor, where balanceOf() = deposit * growth_factor. The factor ticks up every second as interest accrues.

Quote verification writes a fake deposit into the Spardose (our donor contract) so the sim can pretend the trader has funds. The old code sized the fake deposit from a pre-sim getReserveNormalizedIncome RPC read. The sim then re-read the growth factor inside the EVM at its own pinned block. Those two reads can disagree by one block, leaving the Spardose's balanceOf one wei below amount when it tries to fund the trader. Aave's internal scaled-balance subtraction underflows, revert.

The same kind of boundary issue could in principle hit other tokens (rebasing, tiny fee-on-transfer, future weird ones).

Fix

Bump the Spardose's fake balance by 1% before passing it to the override:

spardose_amount = needed + needed / 100

Spardose ends up slightly richer than the sim will ever transfer. Any rounding, accrual, or per-block drift is absorbed by the buffer. The Spardose is a throwaway donor, overshoot costs nothing: no gas difference, no side effects, only the trader-requested amount actually moves.

Generic by construction. Fixes aToken and covers any future token with similar near-boundary math.

Context

Supersedes #4361, which took an Aave-specific approach (scale against the stored liquidityIndex instead of the accrued one). Replaced with this generic overshoot per Martin's review.

Changes

  • price-estimation/trade_verifier: 1% overshoot on the amount passed to the Spardose balance override.
  • Unit test covering the overshoot helper.

How to test

cargo test -p price-estimation trade_verifier. Existing tests pass plus the new spardose_amount_applies_1pct_overshoot.

Quote verification sizes the Spardose's fake balance from the trader's
needed amount via `state_override`. On aTokens this sat exactly at the
1-wei boundary: if the sim's `getReserveNormalizedIncome` landed on a
slightly different block than our pre-sim read, `balanceOf` rounded
down to `amount - 1` and Aave's internal scaled-balance subtraction
underflowed, surfacing as `trader does not have enough sell token`.

Fix by giving the Spardose 1% more than requested. Spardose is a
throwaway donor, overshoot has no cost, and the buffer absorbs any
rounding or per-block accrual drift between our read and the sim's
execution. Generic across tokens, not Aave-specific.

Supersedes #4361.
@squadgazzz squadgazzz marked this pull request as ready for review April 24, 2026 08:29
@squadgazzz squadgazzz requested a review from a team as a code owner April 24, 2026 08:29
@squadgazzz squadgazzz added the hotfix Labels PRs that should be applied into production right away label Apr 24, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a 1% buffer (minimum 1 wei) to the balance override amount for the 'spardose' donor in the trade verifier. This change, implemented via the new spardose_amount_with_buffer function, aims to prevent simulation failures caused by rounding or per-block accruals. The implementation includes saturating arithmetic to handle potential overflows and is supported by new unit tests. No critical issues were found, and I have no feedback to provide.

@squadgazzz squadgazzz added this pull request to the merge queue Apr 24, 2026
Merged via the queue into main with commit 3cda241 Apr 24, 2026
23 of 24 checks passed
@squadgazzz squadgazzz deleted the spardose-overshoot-for-aave-atoken-quotes branch April 24, 2026 09:39
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 24, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

hotfix Labels PRs that should be applied into production right away

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants