feat: price impact excludes fees by Voyz · Pull Request #325 · uniswap-python/uniswap-python · GitHub
Skip to content
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
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