Skip to content

Conversation

@gztensor
Copy link
Contributor

Description

Implement PalSwap (similar to balancer swap) to replace Uniswap V3.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Other (please describe):

Breaking Change

This PR changes the following externally accessible entities:

  • add_liqudiity extrinsic: Remove tick_low and tick_high parameters.
  • Positions map is replaced with PositionsV2 map
  • Following state maps removed:
    • AlphaSqrtPrice
    • CurrentLiquidity
    • CurrentTick
    • FeeGlobalTao
    • FeeGlobalAlpha
    • ScrapReservoirTao (applied before removing)
    • ScrapReservoirAlpha (applied before removing)
    • Ticks
    • SwapV3Initialized
    • TickIndexBitmapWords

Checklist

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have run ./scripts/fix_rust.sh to ensure my code is formatted and linted correctly
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're using Sam's bigmath crate now to handle high precision exponents.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have changed U96F32 to U64F64 for representation of price. F64 has more fractional bits and prices are virtually never greater than 1000 anyway, which is way below u64::MAX.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing storage for provided reserves. From now on, we can calculate provided reserves using balancer very precisely, no need to store them separately. Anyway, this is only relevant when/if we decide to go for user liquidity with balancer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are any left-overs in provided reserves, add them to reserves before removing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adjust_protocol_liquidity needs to report how much it actually added to reserves because it is adding fees and this addition needs to be considered when we modify SubnetTAO and SubnetAlphaIn in run_coinbase.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not operating square root of prices anymore, so renamed this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file contains the heart of balancer math. All swaps come through this logic. Please read the comments to understand what we're doing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is where uniswap v3 logic used to be. Reworked to use balancer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now we can remove all swap v3 maps.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed swap v3 maps and replaced with balancer maps.

Copy link
Contributor Author

@gztensor gztensor Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unline uniswap v3, balancer swap is always a single step, but yet, swap_step provides cleaner separation of swap logic, and I decided to preserve the structure. See how much simpler it is now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adjust_protocol_liquidity returns the actual adjustment that needs to be made to reserves, which includes previously collected tao and alpha fees.

When tao fees are charged, the tao is coming from user balance to FeesTao map, not accounted for in SubnetTAO, so it needs to be added here.

When alpha fees are charged, the alpha is coming from user's stake, and is also not accounted for in SubnetAlphaIn.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still an approximation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Continue removing storage for provided reserves here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Positions are unused atm, but I preserved the struct for later use, when we have user liquidity.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is part of uniswap v3 logic. Not needed anymore.

@gztensor gztensor added the skip-cargo-audit This PR fails cargo audit but needs to be merged anyway label Jan 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-cargo-audit This PR fails cargo audit but needs to be merged anyway

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants