Skip to content

Add a deterministic tie-break (by anchor id) to QuoteService.quote's largest-first sort when two anchors hold equal amounts #157

Description

@Jagadeeshftw

📌 Description

QuoteService.quote sorts liquidity sources with .sort((a, b) => b.amount - a.amount), which is stable per the ECMAScript spec but only guarantees insertion-order stability, not a deterministic tie-break tied to any meaningful field. Two anchors with exactly equal balances for an asset will be ordered based on LiquidityRepository.byAsset's internal Map iteration order (insertion order), which is an implementation detail a caller shouldn't have to rely on for reproducible quotes.

🧩 Requirements and context

  • Add an explicit secondary sort key (e.g. anchor id, ascending) applied when amounts are equal, so the resulting route order is fully deterministic regardless of insertion order.
  • Do not change the primary largest-first ordering.

🛠️ Suggested execution

  • Modify src/services/quoteService.ts's sort comparator: .sort((a, b) => b.amount - a.amount || a.anchor.localeCompare(b.anchor)).
  • Add a test in src/services/quoteService.test.ts with two anchors holding equal balances inserted in varying orders, asserting the resulting route order is identical regardless of insertion order.

✅ Acceptance criteria

  • Quotes involving tied anchor balances produce a deterministic, insertion-order-independent route.
  • Primary largest-first ordering is unaffected for non-tied cases.
  • New test passes; existing quoteService.test.ts suite unaffected.

🔒 Security notes

N/A — determinism/correctness fix; makes quote results reproducible and auditable rather than dependent on incidental map ordering.

📋 Guidelines

  • Minimum 95% test coverage
  • Clear documentation
  • Timeframe: 96 hours

Metadata

Metadata

Assignees

No one assigned

    Labels

    GrantFox OSSGrantFox open-source programMaybe RewardedGrantFox: potentially rewarded contributionOfficial Campaign | FWC26GrantFox official campaign issuebugSomething isn't workingtestingTests and coverage

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions