Skip to content

refactor: standardize admin management accessors and replace direct R…#226

Merged
DevMuhdishaq merged 2 commits into
StelTade:mainfrom
TechBroAfrica:feat/twap-accumulator
Jul 22, 2026
Merged

refactor: standardize admin management accessors and replace direct R…#226
DevMuhdishaq merged 2 commits into
StelTade:mainfrom
TechBroAfrica:feat/twap-accumulator

Conversation

@TechBroAfrica

Copy link
Copy Markdown
Contributor

closes #211

Pull Request: Add Time-Weighted Average Price (TWAP) Accumulator

Summary

This PR introduces a Time-Weighted Average Price (TWAP) accumulator to the counter contract, providing a manipulation-resistant pricing mechanism for consumers such as limit orders, risk checks, and analytics. Instead of relying solely on the latest spot price, the contract now maintains historical observations and computes an average price over a configurable time window.

As part of this work, the test environment was also restored by fixing existing compilation issues in unrelated test modules, allowing the complete test suite to run successfully.

Changes

✨ New

  • Added counter/src/twap.rs implementing the TWAP accumulator.
  • Introduced bounded observation storage using a fixed-size ring buffer (maximum of 20 observations per asset pair).
  • Added get_twap(env, pair, window_seconds) -> i128 for querying the time-weighted average price.
  • Added comprehensive unit tests in twap_tests.rs.

🔄 Updated

lib.rs

  • Registered the new twap module.
  • Exposed the get_twap contract method.

oracle.rs

  • Integrated twap::update_twap() into the price update flow so oracle price updates are recorded automatically.

liquidity_pool.rs

  • Updated swap logic to record TWAP observations after reserve changes.
  • Computes the latest spot price from pool reserves before updating the accumulator.

🛠 Test Environment Fixes

governance_tests.rs

  • Added the missing Ledger test utility import required for timestamp manipulation.
  • Updated test imports/access where necessary to restore compilation.

risk_management_tests.rs

  • Added the missing Ledger import.
  • Fixed a borrow-after-move issue by cloning the reason symbol before use.

Testing

Added unit tests covering:

  • Empty observation history.
  • Single observation behavior.
  • TWAP calculation across three or more observations using controlled ledger timestamps.
  • Resistance to single-block price spikes outside the requested averaging window.
  • Ring buffer rollover while maintaining bounded storage.

Acceptance Criteria

  • get_twap returns the correct average across multiple observations.
  • ✅ Single-block price spikes outside the selected window do not affect the computed TWAP.
  • ✅ Observation storage remains bounded (maximum 20 observations per asset pair).
  • ✅ Tests cover empty history, single observation, and ring buffer rollover.
  • ✅ Existing test suite compiles successfully after restoring broken test modules.

TechBroAfrica and others added 2 commits July 20, 2026 23:09
@DevMuhdishaq
DevMuhdishaq merged commit cc793b6 into StelTade:main Jul 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TWAP price oracle module (twap.rs)

2 participants