degenbot.uniswap.v2_pool_calc¶

Calculation mixin for Uniswap V2-style constant-product pools.

Provides read-only calculation methods that operate on state held by V2PoolState (or a compatible state mixin). All methods are non-mutating and delegate to standalone calculation functions where possible.

Pools with different calculation paths (e.g., Aerodrome’s stable mode, Camelot’s k invariant) define their own calc mixin instead of using this one.

See: contract_reference/uniswap/V2/UniswapV2Factory.sol (UniswapV2Pair swap/mint/burn)

Module Contents¶

class degenbot.uniswap.v2_pool_calc.UniswapV2PoolCalc¶

Calculation methods matching the Uniswap V2 contract.

All methods operate on state held by the concrete class via V2PoolState. Subclasses override as needed for contract-specific differences.

Class variables with Uniswap V2 defaults:

FEE: Directional fee rate (default 0.3%)

reserves_token0: int¶
reserves_token1: int¶
state: degenbot.uniswap.v2_types.UniswapV2PoolState¶
tokens: tuple[degenbot.erc20.Erc20Token, degenbot.erc20.Erc20Token]¶
FEE: fractions.Fraction¶
calculate_tokens_in_from_tokens_out(token_out_quantity: int, token_out: degenbot.erc20.Erc20Token, override_state: degenbot.uniswap.v2_types.UniswapV2PoolState | None = None) → int¶

Calculate the required token INPUT for a target OUTPUT at current pool reserves.

Returns:

The required input token amount.

Raises:
calculate_tokens_out_from_tokens_in(token_in: degenbot.erc20.Erc20Token, token_in_quantity: int, override_state: degenbot.uniswap.v2_types.UniswapV2PoolState | None = None) → int¶

Calculate the expected token OUTPUT for a target INPUT at current pool reserves.

Returns:

The expected output token amount.

Raises:
get_absolute_price(token: degenbot.erc20.Erc20Token, override_state: degenbot.uniswap.v2_types.UniswapV2PoolState | None = None) → fractions.Fraction¶

Get the absolute price for the given token, expressed in units of the other.

Returns:

The absolute price as a Fraction.

get_absolute_exchange_rate(token: degenbot.erc20.Erc20Token, override_state: degenbot.uniswap.v2_types.UniswapV2PoolState | None = None) → fractions.Fraction¶

Get the absolute exchange rate for the given token.

Returns:

The absolute exchange rate as a Fraction.

Raises:

DegenbotValueError – If the token is not held by this pool.

get_nominal_price(token: degenbot.erc20.Erc20Token, override_state: degenbot.uniswap.v2_types.UniswapV2PoolState | None = None) → fractions.Fraction¶

Get the nominal price for the given token, corrected for decimals.

Returns:

The nominal price as a Fraction.

get_nominal_exchange_rate(token: degenbot.erc20.Erc20Token, override_state: degenbot.uniswap.v2_types.UniswapV2PoolState | None = None) → fractions.Fraction¶

Get the nominal exchange rate, corrected for decimal place values.

Returns:

The nominal exchange rate as a Fraction.