Skip to content

Commit

Permalink
fix(Python): Check for supportsUnbalancedLiquidity in pool state for …
Browse files Browse the repository at this point in the history
…unbalanced operations.
  • Loading branch information
johngrantuk committed Dec 19, 2024
1 parent a7e8f64 commit b0162ed
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions python/src/add_liquidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
_to_raw_undo_rate_round_up,
_compute_and_charge_aggregate_swap_fees,
_get_single_input_index,
_require_unbalanced_liquidity_enabled
)
from src.base_pool_math import (
compute_add_liquidity_unbalanced,
Expand Down Expand Up @@ -48,6 +49,7 @@ def add_liquidity(add_liquidity_input, pool_state, pool_class, hook_class, hook_
updated_balances_live_scaled18[i] = a

if add_liquidity_input["kind"] == Kind.UNBALANCED.value:
_require_unbalanced_liquidity_enabled(pool_state)
amounts_in_scaled18 = max_amounts_in_scaled18
computed = compute_add_liquidity_unbalanced(
updated_balances_live_scaled18,
Expand All @@ -63,6 +65,7 @@ def add_liquidity(add_liquidity_input, pool_state, pool_class, hook_class, hook_
swap_fee_amounts_scaled18 = computed["swap_fee_amounts"]

elif add_liquidity_input["kind"] == Kind.SINGLE_TOKEN_EXACT_OUT.value:
_require_unbalanced_liquidity_enabled(pool_state)
token_index = _get_single_input_index(max_amounts_in_scaled18)
amounts_in_scaled18 = max_amounts_in_scaled18
bpt_amount_out = add_liquidity_input["min_bpt_amount_out_raw"]
Expand Down
3 changes: 3 additions & 0 deletions python/src/remove_liquidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
_get_single_input_index,
_to_raw_undo_rate_round_down,
_compute_and_charge_aggregate_swap_fees,
_require_unbalanced_liquidity_enabled,
)
from src.base_pool_math import (
compute_proportional_amounts_out,
Expand Down Expand Up @@ -64,6 +65,7 @@ def remove_liquidity(
remove_liquidity_input["max_bpt_amount_in_raw"],
)
elif remove_liquidity_input["kind"] == RemoveKind.SINGLE_TOKEN_EXACT_IN.value:
_require_unbalanced_liquidity_enabled(pool_state)
bpt_amount_in = remove_liquidity_input["max_bpt_amount_in_raw"]
amounts_out_scaled18 = min_amounts_out_scaled18
token_out_index = _get_single_input_index(
Expand All @@ -83,6 +85,7 @@ def remove_liquidity(
amounts_out_scaled18[token_out_index] = computed["amount_out_with_fee"]
swap_fee_amounts_scaled18 = computed["swap_fee_amounts"]
elif remove_liquidity_input["kind"] == RemoveKind.SINGLE_TOKEN_EXACT_OUT.value:
_require_unbalanced_liquidity_enabled(pool_state)
amounts_out_scaled18 = min_amounts_out_scaled18
token_out_index = _get_single_input_index(
remove_liquidity_input["min_amounts_out_raw"]
Expand Down
5 changes: 5 additions & 0 deletions python/src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,8 @@ def _get_single_input_index(max_amounts_in):
raise ValueError("All zero inputs for single token add")

return input_index


def _require_unbalanced_liquidity_enabled(pool_state):
if not pool_state.get("supportsUnbalancedLiquidity", True):
raise ValueError("DoesNotSupportUnbalancedLiquidity")

0 comments on commit b0162ed

Please sign in to comment.