Skip to content

Conversation

@nicolad
Copy link

@nicolad nicolad commented Nov 17, 2025

When creating a stableswap pool with pegs, assets are sorted by ID for consistent ordering, but the corresponding peg sources and current pegs were not reordered to match. This caused pegs to be associated with the wrong assets after sorting.

The fix creates an index mapping before sorting and uses it to reorder both peg_info.source and peg_info.current arrays to maintain correct asset-to-peg associations.

Related Issue

Fixes: #1164

Motivation and Context

When creating a pool with assets [3, 1, 2] and pegs [Peg3, Peg1, Peg2]:

  • Assets get sorted to [1, 2, 3]
  • Pegs stayed as [Peg3, Peg1, Peg2] (wrong!)
  • Result: Asset 1 incorrectly gets Peg3 instead of Peg1

This caused incorrect price calculations and potential trading failures in stableswap pools using pegged assets.

How Has This Been Tested?

  • Added 7 comprehensive tests in pallets/stableswap/src/tests/peg.rs covering:
    • Unsorted assets with pegs
    • Reverse order assets
    • 2-asset and 5-asset pools
    • Complex ordering scenarios
    • Index-based peg retrieval validation
    • Asset-peg association verification
  • All 170 existing stableswap tests pass
  • Formatting and clippy checks pass

Checklist

  • I have updated the documentation if necessary.
  • I have added tests to cover my changes, regression test if fixing an issue.
  • This is a breaking change.

@nicolad
Copy link
Author

nicolad commented Dec 21, 2025

Please have a look here
@martinfridrich
@Roznovjak

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.

stableswap asset order dependency issue

1 participant