Skip to content

taking Price Impact into account for partial trade size#435

Merged
rouzwelt merged 16 commits into
masterfrom
2026-02-12-sushi-price-impact
Jun 15, 2026
Merged

taking Price Impact into account for partial trade size#435
rouzwelt merged 16 commits into
masterfrom
2026-02-12-sushi-price-impact

Conversation

@rouzwelt

@rouzwelt rouzwelt commented Feb 12, 2026

Copy link
Copy Markdown
Collaborator

Motivation

Caution

Do NOT merge before #430

This PR enables accounting for sushi's swap Price Impact for finding the partial trade size.

Solution

Checks

By submitting this for review, I'm confirming I've done the following:

  • made this PR as small as possible
  • unit-tested any new functionality
  • linked any relevant issues or PRs
  • included screenshots (if this involves a front-end change)

Summary by CodeRabbit

Release Notes

  • New Features

    • Added a new CLI command to analyze and check raindex route profitability with customizable parameters for orders and pricing configuration.
  • Improvements

    • Market price queries now return route information alongside pricing data.
    • Enhanced quote caching mechanism and price impact tolerance handling for improved accuracy and efficiency.
  • Bug Fixes

    • Fixed edge case preventing division by zero in price calculations.

@rouzwelt rouzwelt requested a review from hardyjosh February 12, 2026 21:32
@rouzwelt rouzwelt self-assigned this Feb 12, 2026
@coderabbitai

coderabbitai Bot commented Feb 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: ab54a8be-4653-48f9-ab94-98f102adb390

📥 Commits

Reviewing files that changed from the base of the PR and between ea0a88a and 724fb61.

📒 Files selected for processing (19)
  • lib/sushiswap
  • src/cli/commands/index.ts
  • src/cli/commands/raindex-route.test.ts
  • src/cli/commands/raindex-route.ts
  • src/core/modes/index.test.ts
  • src/core/modes/raindex/index.test.ts
  • src/core/modes/raindex/index.ts
  • src/core/modes/raindex/simulation.test.ts
  • src/core/modes/raindex/simulation.ts
  • src/core/modes/raindex/utils.test.ts
  • src/core/modes/raindex/utils.ts
  • src/order/pair.test.ts
  • src/order/pair.ts
  • src/router/router.ts
  • src/router/sushi/index.test.ts
  • src/router/sushi/index.ts
  • src/router/types.ts
  • src/state/contracts.test.ts
  • src/state/index.ts

Walkthrough

This PR introduces price-impact-aware market price lookup by adding DEFAULT_PRICE_IMPACT_TOLERANCE, a calculateEffectivePrice helper, and propagating an optional MultiRoute through SushiRouter, RainSolverRouter, and SharedState. Several raindex router bugs are fixed: cache-conditional skipFetch, division-by-zero in calcCounterpartyInputToEthPrice, and incorrect counterparty IO decimal scaling. A new raindex-route CLI command for offline profitability checks is also added.

Changes

Price Impact Routing

Layer / File(s) Summary
DEFAULT_PRICE_IMPACT_TOLERANCE constant and router return type
src/router/types.ts, src/router/sushi/index.ts, src/router/router.ts
Adds DEFAULT_PRICE_IMPACT_TOLERANCE = 2.5 and updates SushiRouter.getMarketPrice and RainSolverRouter.getMarketPrice return types to include route?: MultiRoute alongside price.
calculateEffectivePrice helper and findLargestTradeSize update
src/router/sushi/index.ts, src/router/sushi/index.test.ts
Adds exported calculateEffectivePrice that adjusts base price by (1 - priceImpact), updates findLargestTradeSize absolute branch to compare priceImpact against tolerance, and returns route from getMarketPrice. New test suite covers all calculateEffectivePrice edge cases.
SharedState.getMarketPrice price impact gate
src/state/index.ts
Early return on OK market price is now conditional: skipped when priceImpact exceeds DEFAULT_PRICE_IMPACT_TOLERANCE, falling through to the findLargestTradeSize retry path instead.

Raindex Router Fixes

Layer / File(s) Summary
calcCounterpartyInputToEthPrice zero-price guard
src/core/modes/raindex/utils.ts, src/core/modes/raindex/utils.test.ts
Adds early return of 0n when quote.price is 0n to prevent division by zero, with a new covering test case.
Cache-conditional skipFetch in sushi.tryQuote
src/core/modes/raindex/index.ts, src/core/modes/raindex/index.test.ts
Builds a lowercased key from fromToken/toToken addresses and passes skipFetch: cache.has(key) instead of unconditional true. All affected test assertions updated to expect skipFetch: false and a cache: new Map() in mocked state.
Simulation span attributes and counterparty IO decimal fix
src/core/modes/raindex/simulation.ts, src/core/modes/raindex/simulation.test.ts
Expands route span attribute with order/counterparty token labels, adds initEstimatedProfitETH attribute, and fixes second takeOrders entry to scale minimumIO/maximumIO using counterpartyOrderDetails.buyTokenDecimals.
Modes test mock and description corrections
src/core/modes/index.test.ts, src/state/contracts.test.ts
Removes stale balancer mock comment, corrects mocked trade type from "interOrderbook" to "raindex", fixes getEnabledTradeTypeFunctions test description, and corrects raindexArb variable name in contracts test.

raindex-route CLI Command

Layer / File(s) Summary
RaindexRouteCmd and checkRaindexRoute implementation
src/cli/commands/raindex-route.ts, src/cli/commands/index.ts, src/cli/commands/raindex-route.test.ts
Adds RaindexRouteLiveOptions type, RaindexRouteCmd commander command with env var bindings, and checkRaindexRoute which converts option strings to 18-decimal fixed-point, builds orderIn/orderOut, calls estimateProfit, and prints formatted profit. Registered on root RainSolverCmd. Full Vitest coverage included.

Minor Fixes and Cleanup

Layer / File(s) Summary
Submodule bump, JSDoc and test description cleanup
lib/sushiswap, src/order/pair.ts, src/order/pair.test.ts
Updates lib/sushiswap submodule pointer, removes outdated sortBy JSDoc parameter from getSortedPairList, and corrects a pair test description.

Sequence Diagram(s)

sequenceDiagram
  participant SharedState
  participant RainSolverRouter
  participant SushiRouter

  SharedState->>RainSolverRouter: getMarketPrice(params)
  RainSolverRouter->>SushiRouter: getMarketPrice(params)
  SushiRouter-->>RainSolverRouter: Result<{price, route?: MultiRoute}>
  RainSolverRouter-->>SharedState: Result<{price, route?: MultiRoute}>
  alt route.priceImpact missing OR <= DEFAULT_PRICE_IMPACT_TOLERANCE
    SharedState-->>SharedState: return early with price
  else priceImpact exceeds tolerance
    SharedState->>RainSolverRouter: findLargestTradeSize + retry
    RainSolverRouter-->>SharedState: partial-trade market price
  end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • rainlanguage/rain.solver#433: Directly overlaps with changes to src/router/sushi/index.ts, src/router/types.ts, and src/state/index.ts for price impact accounting in partial trade sizing.

Suggested reviewers

  • hardyjosh
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch 2026-02-12-sushi-price-impact

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install failed due to a network error.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

This reverts commit cc2e65f.
@rouzwelt rouzwelt changed the base branch from 2026-01-30-raindex-router-mode to master June 15, 2026 16:08
@rouzwelt rouzwelt merged commit 9a50606 into master Jun 15, 2026
1 of 13 checks passed
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.

1 participant