Skip to content

Rate limiting module per user per ip and global throttling with redis - #383

Open
SugaretaNajja wants to merge 2 commits into
StellAIverse:mainfrom
SugaretaNajja:Rate-limiting-module-per-user-per-IP-and-global-throttling-with-Redis
Open

Rate limiting module per user per ip and global throttling with redis#383
SugaretaNajja wants to merge 2 commits into
StellAIverse:mainfrom
SugaretaNajja:Rate-limiting-module-per-user-per-IP-and-global-throttling-with-Redis

Conversation

@SugaretaNajja

Copy link
Copy Markdown

PR Description — Develop Core Rebalancing Engine with Drift Detection

Summary

This PR implements the core portfolio rebalancing engine for detecting allocation drift, determining when rebalancing is required, calculating the necessary trades, and executing rebalancing operations.

The implementation also adds simulation support, atomic execution, rebalancing history, and handling for key edge cases such as insufficient liquidity, single-asset portfolios, and zero balances.

Changes

  • Added calculate_portfolio_drift() to calculate deviations between current and target portfolio allocations.
  • Added detect_rebalancing_need() with configurable drift thresholds.
  • Added calculate_rebalance_trades() to determine the required buy and sell operations.
  • Added execute_rebalance() for atomic multi-step rebalancing operations.
  • Added simulation/dry-run mode to preview rebalancing without modifying state.
  • Added minimum trade-size checks to prevent dust transactions.
  • Added rebalancing history with timestamps and trade details.
  • Included gas optimization considerations for trade execution.
  • Added handling for insufficient liquidity, single-asset portfolios, and zero-balance scenarios.

Acceptance Criteria

  • Portfolio drift is calculated accurately within ±0.01%.
  • Rebalancing is triggered when the configured drift threshold is exceeded.
  • Calculated trades respect minimum trade sizes.
  • Rebalancing operations execute atomically, ensuring all-or-nothing behavior.
  • Simulation mode accurately previews the expected rebalancing without changing contract state.
  • Rebalancing history records relevant timestamps and operation details.
  • Edge cases are handled safely without causing invalid or partial rebalancing operations.
  • Performance tests verify that rebalancing completes within the required execution limits.

Testing

Testing covers:

  • Portfolio drift calculations.
  • Configurable drift thresholds.
  • Buy/sell trade calculations.
  • Minimum trade-size enforcement.
  • Atomic rebalancing execution.
  • Simulation mode without state changes.
  • Rebalancing history recording.
  • Insufficient liquidity, single-asset, and zero-balance scenarios.
  • Performance and execution constraints.

Expected Outcome

The contract can reliably detect portfolio drift and determine the trades required to restore target allocations. Rebalancing can be executed atomically or previewed through simulation mode while maintaining accurate history and safely handling edge cases.

Closes #366

Create Portfolio REST API Endpoints
Rate limiting module: per-user, per-IP, and global throttling with Redis
@devIKargi

Copy link
Copy Markdown
Contributor

@this too

@devIKargi

Copy link
Copy Markdown
Contributor

@SugaretaNajja

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.

Rate limiting module: per-user, per-IP, and global throttling with Redis

2 participants