Skip to content

feat: price impact excludes fees #325

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

Merged
merged 4 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/price_impact.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def usdt_to_vxv_v2():

# Compare the results with the output of:
# https://app.uniswap.org/#/swap?use=v2&inputCurrency=0xdac17f958d2ee523a2206206994597c13d831ec7&outputCurrency=0x7d29a64504629172a429e64183d6673b9dacbfce
qty = 10 * 10 ** 8
qty = 10 * 10**8

# price = uniswap.get_price_input(usdt, vxv, qty, route=route) / 10 ** 18
# print(price)
Expand All @@ -38,7 +38,7 @@ def usdt_to_vxv_v2():
# The slippage for v3 (in example below) returns correct results.
print(f"Impact for buying VXV on v2 with {qty / 10**8} USDT: {_perc(impact)}")

qty = 13900 * 10 ** 8
qty = 13900 * 10**8
impact = uniswap.estimate_price_impact(usdt, vxv, qty, route=route)
print(f"Impact for buying VXV on v2 with {qty / 10**8} USDT: {_perc(impact)}")

Expand All @@ -49,11 +49,11 @@ def eth_to_vxv_v3():

# Compare the results with the output of:
# https://app.uniswap.org/#/swap?use=v3&inputCurrency=ETH&outputCurrency=0x7d29a64504629172a429e64183d6673b9dacbfce
qty = 1 * 10 ** 18
qty = 1 * 10**18
impact = uniswap.estimate_price_impact(eth, vxv, qty, fee=10000)
print(f"Impact for buying VXV on v3 with {qty / 10**18} ETH: {_perc(impact)}")

qty = 100 * 10 ** 18
qty = 100 * 10**18
impact = uniswap.estimate_price_impact(eth, vxv, qty, fee=10000)
print(f"Impact for buying VXV on v3 with {qty / 10**18} ETH: {_perc(impact)}")

Expand Down
8 changes: 8 additions & 0 deletions tests/test_uniswap.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def test_assets(client: Uniswap):
"""
tokens = get_tokens(client.netname)


for token_name, amount in [
("DAI", 10_000 * ONE_DAI),
("USDC", 10_000 * ONE_USDC),
Expand Down Expand Up @@ -133,6 +134,13 @@ def does_not_raise():
yield



ONE_ETH = 10**18
ONE_USDC = 10**6

ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"


# TODO: Change pytest.param(..., mark=pytest.mark.xfail) to the expectation/raises method
@pytest.mark.usefixtures("client", "web3")
class TestUniswap(object):
Expand Down
9 changes: 7 additions & 2 deletions uniswap/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@
MAX_TICK = -MIN_TICK

# Source: https://github.com/Uniswap/v3-core/blob/v1.0.0/contracts/UniswapV3Factory.sol#L26-L31
_tick_spacing = {100:1, 500: 10, 3_000: 60, 10_000: 200}
_tick_spacing = {100: 1, 500: 10, 3_000: 60, 10_000: 200}

# Derived from (MIN_TICK//tick_spacing) >> 8 and (MAX_TICK//tick_spacing) >> 8
_tick_bitmap_range = {100:(-3466, 3465), 500: (-347, 346), 3_000: (-58, 57), 10_000: (-18, 17)}
_tick_bitmap_range = {
100: (-3466, 3465),
500: (-347, 346),
3_000: (-58, 57),
10_000: (-18, 17),
}
10 changes: 9 additions & 1 deletion uniswap/uniswap.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
encode_sqrt_ratioX96,
is_same_address,
nearest_tick,
realised_fee_percentage,
)
from .decorators import supports, check_approval
from .constants import (
Expand Down Expand Up @@ -1926,7 +1927,14 @@ def estimate_price_impact(
cost_amount / (amount_in / (10 ** self.get_token(token_in).decimals))
) / 10 ** self.get_token(token_out).decimals

return float((price_small - price_amount) / price_small)
# calculate and subtract the realised fees from the price impact. See:
# https://github.com/uniswap-python/uniswap-python/issues/310
# The fee calculation will need to be updated when adding support for the AutoRouter.
price_impact_with_fees = float((price_small - price_amount) / price_small)
fee_realised_percentage = realised_fee_percentage(fee, amount_in)
price_impact_real = price_impact_with_fees - fee_realised_percentage

return price_impact_real

# ------ Exchange ------------------------------------------------------------------
@supports([1, 2])
Expand Down
16 changes: 16 additions & 0 deletions uniswap/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,19 @@ def nearest_tick(tick: int, fee: int) -> int:
def chunks(arr: Sequence[Any], n: int) -> Generator:
for i in range(0, len(arr), n):
yield arr[i : i + n]


def fee_to_fraction(fee: int):
return fee / 1000000


def realised_fee_percentage(fee: int, amount_in: int) -> float:
"""
Calculate realised fee expressed as a percentage of the amount_in.
The realised fee is rounded up as fractional units cannot be used -
this correlates to how the fees are rounded by Uniswap.
"""

fee_percentage = fee_to_fraction(fee)
fee_realised = math.ceil(amount_in * fee_percentage)
return fee_realised / amount_in
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy