Skip to content

Commit

Permalink
[CL:Test] Add sqrtPrice check to swaps_test (#4201)
Browse files Browse the repository at this point in the history
* added sqrt check

* Added all wolfram

* nit

* rebased

* work

* go work sum fix

* rebased

* rebase fix

* fixed some fees

* fixed test

* added checks

* fixed

* fix gosum

* fixed conflicts

* Update x/concentrated-liquidity/swaps_test.go

* Update x/concentrated-liquidity/swaps_test.go
  • Loading branch information
stackman27 authored Feb 23, 2023
1 parent 46530dc commit 52965de
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 64 deletions.
52 changes: 35 additions & 17 deletions scripts/cl/one_for_zero.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@
from common import *
import common.sdk_dec as sdk_dec


def get_next_sqrt_price(liquidity: Decimal, sqrt_price_current: Decimal, token_in: Decimal) -> Decimal:
""" Return the next sqrt price when swapping token one for zero.
"""
return sqrt_price_current + sdk_dec.quo(token_in, liquidity)


def get_token_out(liquidity: Decimal, sqrt_price_current: Decimal, sqrt_price_next: Decimal) -> Decimal:
""" Returns the token out when swapping token one for zero given the token in.
"""
return sdk_dec.mul(liquidity, sdk_dec.quo((sqrt_price_next - sqrt_price_current), sdk_dec.mul(sqrt_price_next, sqrt_price_current)))


def get_token_in_swap_in_given_out(liquidity: Decimal, sqrt_price_current: Decimal, sqrt_price_next: Decimal) -> Decimal:
""" Returns the token in when swapping token one for zero given the token out.
In this case, the calculation is the same as computing token out when given token in.
"""
return get_token_out(liquidity, sqrt_price_current, sqrt_price_next)


def calc_amount_one_delta(liquidity: Decimal, sqrt_price_current: Decimal, sqrt_price_next: Decimal, should_round_up: bool) -> Decimal:
""" Returns the expected token in when swapping token one for zero.
"""
Expand All @@ -29,20 +32,23 @@ def calc_amount_one_delta(liquidity: Decimal, sqrt_price_current: Decimal, sqrt_
return Decimal(math.ceil(result))
return result


def calc_test_case_out_given_in(liquidity: Decimal, sqrt_price_current: Decimal, token_in_remaining: Decimal, swap_fee: Decimal) -> Tuple[Decimal, Decimal, Decimal]:
""" Computes and prints all one for zero test case parameters when swapping for out given in.
Next sqrt price is computed from the given parameters.
Returns the next square root price, token out and fee amount per share.
"""

token_in_remaining_after_fee = sdk_dec.mul(token_in_remaining, (sdk_dec.one - swap_fee))
token_in_remaining_after_fee = sdk_dec.mul(
token_in_remaining, (sdk_dec.one - swap_fee))

sqrt_price_next = get_next_sqrt_price(
liquidity, sqrt_price_current, token_in_remaining_after_fee)

sqrt_price_next = get_next_sqrt_price(liquidity, sqrt_price_current, token_in_remaining_after_fee)

print(F"token_in_remaining_after_fee: {token_in_remaining_after_fee}")

token_in_after_fee_rounded_up = calc_amount_one_delta(liquidity, sqrt_price_current, sqrt_price_next, True)
token_in_after_fee_rounded_up = calc_amount_one_delta(
liquidity, sqrt_price_current, sqrt_price_next, True)

print(F"token_in_after_fee_rounded_up: {token_in_after_fee_rounded_up}")
token_out = get_token_out(liquidity, sqrt_price_current, sqrt_price_next)
Expand All @@ -60,17 +66,21 @@ def calc_test_case_out_given_in(liquidity: Decimal, sqrt_price_current: Decimal,

return sqrt_price_next, token_out, fee_amount_per_share


def calc_test_case_in_given_out(liquidity: Decimal, sqrt_price_current: Decimal, token_in: Decimal, swap_fee: Decimal) -> Tuple[Decimal, Decimal, Decimal]:
""" Computes and prints all one for zero test case parameters when swapping for in given out.
Next sqrt price is computed from the given parameters.
Returns the next square root price, token in and fee amount per share.
"""
sqrt_price_next = get_next_sqrt_price(liquidity, sqrt_price_current, token_in)
sqrt_price_next = get_next_sqrt_price(
liquidity, sqrt_price_current, token_in)
price_next = math.pow(sqrt_price_next, 2)
token_in = get_token_in_swap_in_given_out(liquidity, sqrt_price_current, sqrt_price_next)

token_in = get_token_in_swap_in_given_out(
liquidity, sqrt_price_current, sqrt_price_next)

total_fee = sdk_dec.quo(sdk_dec.mul(token_in, swap_fee), (sdk_dec.one - swap_fee))
total_fee = sdk_dec.quo(sdk_dec.mul(
token_in, swap_fee), (sdk_dec.one - swap_fee))

fee_amount_per_share = sdk_dec.quo(total_fee, liquidity)

Expand All @@ -84,16 +94,18 @@ def calc_test_case_in_given_out(liquidity: Decimal, sqrt_price_current: Decimal,

return sqrt_price_next, token_in_after_fee, fee_amount_per_share


def calc_test_case_with_next_sqrt_price_out_given_in(liquidity: Decimal, sqrt_price_current: Decimal, sqrt_price_next: Decimal, swap_fee: Decimal) -> Tuple[Decimal, Decimal, Decimal]:
""" Computes and prints one for zero test case parameters when next square root price is known.
Assumes swapping for token out given in.
Returns the expected token in, token out and fee amount per share.
"""
expected_token_in_before_fee = calc_amount_one_delta(liquidity, sqrt_price_current, sqrt_price_next, True)
expected_token_in_before_fee = calc_amount_one_delta(
liquidity, sqrt_price_current, sqrt_price_next, True)
expected_fee = sdk_dec.zero
if swap_fee > sdk_dec.zero:
expected_fee = sdk_dec.quo(sdk_dec.mul(expected_token_in_before_fee, swap_fee), sdk_dec.one - swap_fee)
expected_fee = sdk_dec.quo(sdk_dec.mul(
expected_token_in_before_fee, swap_fee), sdk_dec.one - swap_fee)

expected_token_in = expected_token_in_before_fee + expected_fee

Expand All @@ -111,16 +123,22 @@ def calc_test_case_with_next_sqrt_price_out_given_in(liquidity: Decimal, sqrt_pr

return expected_token_in, token_out, fee_amount_per_share


def calc_test_case_with_next_sqrt_price_in_given_out(liquidity: Decimal, sqrt_price_current: Decimal, sqrt_price_next: Decimal, swap_fee: Decimal) -> Tuple[Decimal, Decimal, Decimal]:
""" Computes and prints one for zero test case parameters when next square root price is known.
Assumes swapping for token in given out.
Returns expected token out, token in after fee, and fee amount per share.
"""
expected_token_out = calc_amount_one_delta(liquidity, sqrt_price_current, sqrt_price_next, True)
token_in = get_token_in_swap_in_given_out(liquidity, sqrt_price_current, sqrt_price_next)

expected_token_out = calc_amount_one_delta(
liquidity, sqrt_price_current, sqrt_price_next, True)
token_in = get_token_in_swap_in_given_out(
liquidity, sqrt_price_current, sqrt_price_next)

total_fee = sdk_dec.quo(sdk_dec.mul(
token_in, swap_fee), (sdk_dec.one - swap_fee))

total_fee = sdk_dec.quo(sdk_dec.mul(token_in, swap_fee), (sdk_dec.one - swap_fee))

fee_amount_per_share = sdk_dec.quo(total_fee, liquidity)
token_in_after_fee = token_in + total_fee
Expand Down
56 changes: 37 additions & 19 deletions scripts/cl/zero_for_one.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,56 @@
from common import *
import common.sdk_dec as sdk_dec


def get_next_sqrt_price(liquidity: Decimal, sqrt_price_current: Decimal, token_in: Decimal) -> Decimal:
""" Return the next sqrt price when swapping token zero for one.
"""
return sdk_dec.quo(liquidity, (sdk_dec.quo(liquidity, sqrt_price_current) + token_in))


def get_token_out(liquidity: Decimal, sqrt_price_current: Decimal, sqrt_price_next: Decimal) -> Decimal:
""" Returns the token out when swapping token zero for one given the token in.
"""
return sdk_dec.mul(liquidity, (sqrt_price_current - sqrt_price_next))


def get_token_in_swap_in_given_out(liquidity: Decimal, sqrt_price_current: Decimal, sqrt_price_next: Decimal) -> Decimal:
""" Returns the token in when swapping token zero for one given the token out.
In this case, the calculation is the same as computing token out when given token in.
"""
return get_token_out(liquidity, sqrt_price_current, sqrt_price_next)


def calc_amount_zero_delta(liquidity: Decimal, sqrt_price_current: Decimal, sqrt_price_next: Decimal, should_round_up: bool) -> Decimal:
""" Returns the expected token in when swapping token zero for one.
"""
mul1 = sdk_dec.mul(liquidity, (sqrt_price_current - sqrt_price_next))
print(mul1)
result = sdk_dec.quo(sdk_dec.mul(liquidity, (sqrt_price_current - sqrt_price_next)), sdk_dec.mul(sqrt_price_current, sqrt_price_next))
result = sdk_dec.quo(sdk_dec.mul(liquidity, (sqrt_price_current -
sqrt_price_next)), sdk_dec.mul(sqrt_price_current, sqrt_price_next))
if should_round_up:
return sdk_dec.new(str(math.ceil(result)))
return result


def calc_test_case_out_given_in(liquidity: Decimal, sqrt_price_current: Decimal, token_in_remaining: Decimal, swap_fee: Decimal) -> Tuple[Decimal, Decimal, Decimal]:
""" Computes and prints all zero for one test case parameters. Next sqrt price is computed from the given parameters.
Returns the next square root price, token out and fee amount per share.
"""
token_in_remaining_after_fee = sdk_dec.mul(token_in_remaining, (sdk_dec.one - swap_fee))
token_in_remaining_after_fee = sdk_dec.mul(
token_in_remaining, (sdk_dec.one - swap_fee))
print(F"token_in_remaining_after_fee: {token_in_remaining_after_fee}")

sqrt_price_next = get_next_sqrt_price(liquidity, sqrt_price_current, token_in_remaining_after_fee)

token_in_after_fee_rounded_up = calc_amount_zero_delta(liquidity, sqrt_price_current, sqrt_price_next, True)
sqrt_price_next = get_next_sqrt_price(
liquidity, sqrt_price_current, token_in_remaining_after_fee)

token_in_after_fee_rounded_up = calc_amount_zero_delta(
liquidity, sqrt_price_current, sqrt_price_next, True)

print(F"token_in_after_fee_rounded_up: {token_in_after_fee_rounded_up}")

token_out = get_token_out(liquidity, sqrt_price_current, sqrt_price_next)

fee_charge_total = sdk_dec.zero
if swap_fee > sdk_dec.zero:
fee_charge_total = token_in_remaining - token_in_after_fee_rounded_up
Expand All @@ -61,15 +68,19 @@ def calc_test_case_out_given_in(liquidity: Decimal, sqrt_price_current: Decimal,

return sqrt_price_next, token_out, fee_amount_per_share


def calc_test_case_in_given_out(liquidity: Decimal, sqrt_price_current: Decimal, token_out_remaining: Decimal, swap_fee: Decimal) -> Tuple[Decimal, Decimal, Decimal]:
""" Computes and prints all zero for one test case parameters. Next sqrt price is computed from the given parameters.
Returns the next square root price, token out and fee amount per share.
"""
sqrt_price_next = get_next_sqrt_price(liquidity, sqrt_price_current, token_out_remaining)
token_in = get_token_in_swap_in_given_out(liquidity, sqrt_price_current, sqrt_price_next)

sqrt_price_next = get_next_sqrt_price(
liquidity, sqrt_price_current, token_out_remaining)
token_in = get_token_in_swap_in_given_out(
liquidity, sqrt_price_current, sqrt_price_next)

total_fee = sdk_dec.quo(sdk_dec.mul(token_in, swap_fee), (sdk_dec.one - swap_fee))
total_fee = sdk_dec.quo(sdk_dec.mul(
token_in, swap_fee), (sdk_dec.one - swap_fee))

fee_amount_per_share = sdk_dec.quo(total_fee, liquidity)
token_in_after_fee = token_in + total_fee
Expand All @@ -82,13 +93,16 @@ def calc_test_case_in_given_out(liquidity: Decimal, sqrt_price_current: Decimal,

return sqrt_price_next, token_in_after_fee, fee_amount_per_share


def calc_test_case_with_next_sqrt_price_out_given_in(liquidity: Decimal, sqrt_price_current: Decimal, sqrt_price_next: Decimal, swap_fee: Decimal) -> Tuple[Decimal, Decimal, Decimal]:
""" Computes and prints all zero for one test case parameters when next square root price is known.
Returns the expected token in, token out and fee amount per share.
"""
expected_token_in_before_fee = calc_amount_zero_delta(liquidity, sqrt_price_current, sqrt_price_next, True)
expected_fee = sdk_dec.quo(sdk_dec.mul(expected_token_in_before_fee, swap_fee), (sdk_dec.one - swap_fee))
expected_token_in_before_fee = calc_amount_zero_delta(
liquidity, sqrt_price_current, sqrt_price_next, True)
expected_fee = sdk_dec.quo(sdk_dec.mul(
expected_token_in_before_fee, swap_fee), (sdk_dec.one - swap_fee))
expected_token_in = expected_token_in_before_fee + expected_fee

token_out = get_token_out(liquidity, sqrt_price_current, sqrt_price_next)
Expand All @@ -105,17 +119,21 @@ def calc_test_case_with_next_sqrt_price_out_given_in(liquidity: Decimal, sqrt_pr

return expected_token_in, token_out, fee_amount_per_share


def calc_test_case_with_next_sqrt_price_in_given_out(liquidity: Decimal, sqrt_price_current: Decimal, sqrt_price_next: Decimal, swap_fee: Decimal) -> Tuple[Decimal, Decimal, Decimal]:
""" Computes and prints all zero for one test case parameters when next square root price is known.
Assems swapping token for token in given out.
Returns the expected token out, token in and fee amount per share.
"""
expected_token_out = calc_amount_zero_delta(liquidity, sqrt_price_current, sqrt_price_next, True)
expected_token_out = calc_amount_zero_delta(
liquidity, sqrt_price_current, sqrt_price_next, True)

token_in = get_token_in_swap_in_given_out(liquidity, sqrt_price_current, sqrt_price_next)
token_in = get_token_in_swap_in_given_out(
liquidity, sqrt_price_current, sqrt_price_next)

total_fee = sdk_dec.quo(sdk_dec.mul(token_in, swap_fee), (sdk_dec.one - swap_fee))
total_fee = sdk_dec.quo(sdk_dec.mul(
token_in, swap_fee), (sdk_dec.one - swap_fee))

fee_amount_per_share = sdk_dec.quo(total_fee, liquidity)
token_in_after_fee = token_in + total_fee
Expand Down
Loading

0 comments on commit 52965de

Please sign in to comment.