uniswap_v3.utils

Documentation for eth_defi.uniswap_v3.utils Python module.

Uniswap v3 helper functions.

Functions

decode_path(full_path_encoded)

Decodes the path.

encode_path(path, fees[, exact_output])

Encode the routing path to be suitable to use with Quoter and SwapRouter.

encode_sqrt_ratio_x96(*, amount0, amount1)

Returns the sqrt ratio as a Q64.96 corresponding to a given ratio of amount1 and amount0

get_default_tick_range(fee)

Returns min and max tick for a given fee, this is used by default if the pool owner doesn't want to apply concentrated liquidity initially.

get_max_tick(fee)

Returns max tick for given fee.

get_min_tick(fee)

Returns min tick for given fee.

get_nearest_usable_tick(tick, fee)

get_token0_amount_in_range(liquidity, sp, sb)

Returns token0 (base token) amount in a liquidity range

get_token1_amount_in_range(liquidity, sp, sa)

Returns token1 (quote token) amount in a liquidity range

run_graphql_query(query, api_key, chain, *)

Run query on Uniswap v3 subgraph

tick_to_price(tick)

Returns price corresponding to a tick

tick_to_sqrt_price(tick)

Returns square root price corresponding to a tick

encode_sqrt_ratio_x96(*, amount0, amount1)

Returns the sqrt ratio as a Q64.96 corresponding to a given ratio of amount1 and amount0

Parameters
  • amount0 (int) – the denominator amount, i.e amount of token0

  • amount1 (int) – the numerator amount, i.e. amount of token1

Returns

the sqrt ratio

Return type

int

Lifted from StakeWise Oracle (AGPL license).

encode_path(path, fees, exact_output=False)

Encode the routing path to be suitable to use with Quoter and SwapRouter.

For example if we would like to route the swap from token1 -> token3 through 2 pools:
  • pool1: token1/token2

  • pool2: token2/token3

then encoded path would have this format: token1 - pool1’s fee - token2 - pool2’s - token3, in which each token address length is 20 bytes and fee length is 3 bytes

Read more.

Parameters
  • path (list[eth_typing.evm.HexAddress]) – List of token addresses how to route the trade

  • fees (list) – List of trading fees of the pools in the route. BPS * 100.

  • exact_output (bool) – Whether the encoded path be used for exactOutput quote or swap

Return type

bytes

decode_path(full_path_encoded)

Decodes the path. A bit tricky. Thanks to https://degencode.substack.com/p/project-uniswapv3-mempool-watcher

Parameters

full_path_encoded (bytes) – Encoded path as returned from router.decode_function_input (bytes)

Returns

Fully decoded path array including addresses and fees

Return type

list

get_min_tick(fee)

Returns min tick for given fee.

Adapted from https://github.com/Uniswap/v3-periphery/blob/v1.0.0/test/shared/ticks.ts

Parameters

fee (int) –

Return type

int

get_max_tick(fee)

Returns max tick for given fee.

Adapted from https://github.com/Uniswap/v3-periphery/blob/v1.0.0/test/shared/ticks.ts

Parameters

fee (int) –

Return type

int

get_default_tick_range(fee)

Returns min and max tick for a given fee, this is used by default if the pool owner doesn’t want to apply concentrated liquidity initially.

Parameters

fee (int) –

Return type

Tuple[int, int]

tick_to_price(tick)

Returns price corresponding to a tick

tick_to_sqrt_price(tick)

Returns square root price corresponding to a tick

get_token0_amount_in_range(liquidity, sp, sb)

Returns token0 (base token) amount in a liquidity range

This is derived formula based on: https://atiselsts.github.io/pdfs/uniswap-v3-liquidity-math.pdf

Parameters
  • liquidity – current virtual liquidity

  • sp – square root current price

  • sb – square root upper price

get_token1_amount_in_range(liquidity, sp, sa)

Returns token1 (quote token) amount in a liquidity range

This is derived formula based on: https://atiselsts.github.io/pdfs/uniswap-v3-liquidity-math.pdf

Parameters
  • liquidity – current virtual liquidity

  • sp – square root current price

  • sb – square root lower price

run_graphql_query(query, api_key, chain, *, variables={})

Run query on Uniswap v3 subgraph

Parameters
  • query (str) –

  • api_key (str) –

  • chain (int) –

  • variables (dict) –

Return type

dict