-
Notifications
You must be signed in to change notification settings - Fork 317
Re-add Swap::AlphaSqrtPrice for backwards compatibility; bump spec to… #2799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,28 @@ impl<T: Config> Pallet<T> { | |
| } | ||
| } | ||
|
|
||
| /// Store `sqrt(price)` into the backwards-compatibility `AlphaSqrtPrice` map for a subnet. | ||
| /// | ||
| /// The balancer derives price on the fly, but external consumers still read the legacy | ||
| /// `Swap::AlphaSqrtPrice` map directly, so we mirror the price wherever it changes: | ||
| /// initialization, protocol-liquidity adjustment (emission), and each swap. The price is | ||
| /// passed in rather than re-read from reserves because a swap commits its reserve deltas in | ||
| /// the caller (outside this pallet) — at swap time we use the swap step's computed | ||
| /// post-swap price instead. | ||
| pub(crate) fn store_alpha_sqrt_price(netuid: NetUid, price: U64F64) { | ||
| // Epsilon for the bisection sqrt: 1e-9 is well below the price precision consumers need. | ||
| let epsilon = | ||
| U64F64::saturating_from_num(1).safe_div(U64F64::saturating_from_num(1_000_000_000_u64)); | ||
| let sqrt_price = price.checked_sqrt(epsilon).unwrap_or_default(); | ||
| AlphaSqrtPrice::<T>::insert(netuid, sqrt_price); | ||
|
Comment on lines
+45
to
+50
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [MEDIUM] New price-mirror storage write is not reflected in weights
|
||
| } | ||
|
|
||
| /// Refresh `AlphaSqrtPrice` from the current derived price. Safe to call where reserves are | ||
| /// already up to date (initialization, emission) — not on the swap path. | ||
| pub(crate) fn refresh_alpha_sqrt_price(netuid: NetUid) { | ||
| Self::store_alpha_sqrt_price(netuid, Self::current_price(netuid)); | ||
| } | ||
|
|
||
| // initializes pal-swap (balancer) for a subnet if needed | ||
| pub fn maybe_initialize_palswap( | ||
| netuid: NetUid, | ||
|
|
@@ -76,6 +98,9 @@ impl<T: Config> Pallet<T> { | |
|
|
||
| PalSwapInitialized::<T>::insert(netuid, true); | ||
|
|
||
| // Seed the legacy AlphaSqrtPrice mirror for backwards compatibility. | ||
| Self::refresh_alpha_sqrt_price(netuid); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
|
|
@@ -112,6 +137,9 @@ impl<T: Config> Pallet<T> { | |
| (TaoBalance::ZERO, AlphaBalance::ZERO) | ||
| } else { | ||
| SwapBalancer::<T>::insert(netuid, balancer); | ||
| // Emission changed reserves/weights; refresh the legacy AlphaSqrtPrice mirror. | ||
| // Reserves are already committed here, so deriving from current_price is correct. | ||
| Self::refresh_alpha_sqrt_price(netuid); | ||
| (tao_delta, alpha_delta) | ||
| } | ||
| } | ||
|
|
@@ -218,6 +246,12 @@ impl<T: Config> Pallet<T> { | |
| log::trace!("Fees: {}", swap_result.fee_paid); | ||
| log::trace!("======== End Swap ========"); | ||
|
|
||
| // Mirror the post-swap price into the legacy AlphaSqrtPrice map for backwards | ||
| // compatibility. We use the swap step's computed final price because the reserve deltas | ||
| // are applied by the caller (outside this pallet) and aren't visible yet. This runs in | ||
| // the swap's transactional scope, so it is rolled back on simulated swaps. | ||
| Self::store_alpha_sqrt_price(netuid, swap_result.final_price); | ||
|
|
||
| Ok(SwapResult { | ||
| amount_paid_in: swap_result.delta_in, | ||
| amount_paid_out: swap_result.delta_out, | ||
|
|
@@ -276,6 +310,7 @@ impl<T: Config> Pallet<T> { | |
|
|
||
| FeeRate::<T>::remove(netuid); | ||
| SwapBalancer::<T>::remove(netuid); | ||
| AlphaSqrtPrice::<T>::remove(netuid); | ||
|
|
||
| log::debug!( | ||
| "clear_protocol_liquidity: netuid={netuid:?}, protocol_burned: τ={burned_tao:?}, α={burned_alpha:?}; state cleared" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[MEDIUM] New price-mirror storage write is not reflected in weights
refresh_alpha_sqrt_priceperforms a persistent storage write and is now called from hot paths includingadjust_protocol_liquidityandswap_inner, but this PR does not update the affected runtime weights. Those paths are reachable from per-block emission handling and user-facing swap/stake extrinsics, so blocks can now do at least one extra storage write, plus the helper's price reads, beyond what the declared weights account for. Regenerate or manually update the affected weights and block-emission accounting before merging.