fix(stableswap)!: fix peg ordering by accepting and co-sorting asset-peg pairs#1424
Open
fix(stableswap)!: fix peg ordering by accepting and co-sorting asset-peg pairs#1424
Conversation
|
Crate versions that have been updated:
Runtime version has been increased. |
There was a problem hiding this comment.
Pull request overview
Fixes a correctness bug in the Stableswap pallet where assets were internally sorted during pool creation but peg sources were not co-sorted, causing silent peg/asset misalignment and incorrect pricing.
Changes:
- Breaking API change:
create_pool_with_pegsnow takes explicit(asset_id, peg_source)pairs and co-sorts them byasset_idbefore storage. - Moves/clarifies sorting responsibility:
create_poolsorts assets at dispatch;do_create_poolno longer sorts internally and expects pre-sorted assets. - Adds/updates extensive unit + integration regression coverage for peg ordering, and bumps versions accordingly.
Reviewed changes
Copilot reviewed 22 out of 23 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| runtime/hydradx/src/lib.rs | Bumps runtime spec_version to reflect the breaking change. |
| runtime/hydradx/Cargo.toml | Bumps runtime crate version. |
| pallets/stableswap/src/lib.rs | Changes peg-pool creation API to asset/peg pairs, co-sorts pairs, removes internal sorting in do_create_pool, and adds a sorted-assets debug assertion for peg resolution. |
| pallets/stableswap/src/benchmarks.rs | Updates benchmarks to call create_pool_with_pegs using (asset, peg) pairs. |
| pallets/stableswap/README.md | Updates documentation to describe (asset_id, peg_source) pairing and sorting semantics. |
| pallets/stableswap/Cargo.toml | Major version bump for the breaking pallet API. |
| pallets/stableswap/src/tests/mod.rs | Registers new peg_ordering test module. |
| pallets/stableswap/src/tests/mock.rs | Updates test ExtBuilder to construct (asset, peg) pairs when creating peg pools. |
| pallets/stableswap/src/tests/peg.rs | Updates tests to the new peg-pool creation signature. |
| pallets/stableswap/src/tests/peg_one.rs | Updates tests to the new peg-pool creation signature. |
| pallets/stableswap/src/tests/peg_ordering.rs | Adds dedicated regression tests ensuring peg sources are co-sorted with assets and pricing behavior matches sorted-reference pools. |
| pallets/stableswap/src/tests/pegs_with_different_decimals.rs | Updates tests to the new peg-pool creation signature. |
| pallets/stableswap/src/tests/remove_liquidity.rs | Updates tests to the new peg-pool creation signature. |
| pallets/stableswap/src/tests/update_peg_source.rs | Updates tests to the new peg-pool creation signature. |
| pallets/stableswap/src/tests/update_max_peg_update.rs | Updates tests to the new peg-pool creation signature. |
| pallets/hsm/src/tests/mock.rs | Updates HSM test ExtBuilder to call create_pool_with_pegs using (asset, peg) pairs. |
| pallets/hsm/src/benchmarks.rs | Updates HSM benchmarks to call create_pool_with_pegs using (asset, peg) pairs. |
| pallets/hsm/Cargo.toml | Patch bump reflecting dependent breaking change adaptation. |
| integration-tests/src/stableswap.rs | Updates existing peg-pool creation usage and adds an integration regression test for reversed-order asset inputs. |
| integration-tests/src/omnipool_liquidity_mining.rs | Updates peg-pool creation usage to the new signature. |
| integration-tests/src/hsm.rs | Updates peg-pool creation usage to the new signature. |
| integration-tests/Cargo.toml | Patch bump for integration tests crate. |
| Cargo.lock | Updates lockfile versions for the bumped crates. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes a peg-ordering bug in
create_pool_with_pegswhere passing assets and pegsources in different orders caused silent misalignment after internal sorting.
Root cause:
do_create_poolsorted assets internally, but the correspondingpeg_sourcearray was not co-sorted — sopeg_source[i]could end up paired withthe wrong asset after sort.
Changes:
create_pool_with_pegsnow acceptsBoundedVec<(AssetId, PegSource), _>instead of separate
assetsandpeg_sourceparameters. Pairing is now explicit atthe call site, making misalignment impossible.
do_create_poolno longer sorts internally (callers are now responsible for providing sorted assets).
create_pool(non-peg variant) sorts assets at dispatch.debug_assert!inget_target_pegsverifying assets arrive pre-sorted.Related Issue
Fixes: #1164
Motivation and Context
A pool created via
create_pool_with_pegswith assets provided in non-ascending orderwould store a peg source at an index that no longer matched its intended asset after the
internal sort. This caused incorrect peg values to be applied to assets, a correctness
issue with direct impact on pool pricing.
How Has This Been Tested?
(
pallets/stableswap/src/tests/peg_ordering.rs)peg.rs,peg_one.rs,peg_ordering.rs,pegs_with_different_decimals.rs,update_peg_source.rs,update_max_peg_update.rs,remove_liquidity.rsintegration-tests/src/stableswap.rsthatcreates a pool with out-of-order assets and asserts correct peg assignment post-sort
Checklist: